Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/bin
/bin64
/_build*
/build_cmake
temp

# Emacs
Expand Down
38 changes: 38 additions & 0 deletions include/boost/http_proto/fields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,44 @@ class fields final
}
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
fields f;
f.set(field::host, "example.com");
std::stringstream ss;
ss << f;
assert( ss.str() == "Host: example.com\n" );
@endcode

@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.

@par Complexity
Linear in `f.buffer().size()`

@par Exception Safety
Basic guarantee.

@return A reference to the output stream, for chaining

@param os The output stream to write to.

@param f The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const fields& f);

} // http_proto
} // boost

Expand Down
36 changes: 36 additions & 0 deletions include/boost/http_proto/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,42 @@ class request
}
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
request req(method::get, "/");
std::stringstream ss;
ss << req;
@endcode

@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.

@par Complexity
Linear in `req.buffer().size()`

@par Exception Safety
Basic guarantee.

@return A reference to the output stream, for chaining

@param os The output stream to write to.

@param req The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const request& req);

} // http_proto
} // boost

Expand Down
38 changes: 38 additions & 0 deletions include/boost/http_proto/request_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <boost/http_proto/detail/config.hpp>
#include <boost/http_proto/message_base.hpp>

#include <iosfwd>

namespace boost {
namespace http_proto {

Expand Down Expand Up @@ -299,6 +301,42 @@ class request_base
http_proto::version v);
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
request_base req;
std::stringstream ss;
ss << req;
@endcode

@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.

@par Complexity
Linear in `req.buffer().size()`

@par Exception Safety
Basic guarantee.

@return A reference to the output stream, for chaining

@param os The output stream to write to.

@param req The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const request_base& req);

} // http_proto
} // boost

Expand Down
36 changes: 36 additions & 0 deletions include/boost/http_proto/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,42 @@ class response
}
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
response res(status::ok);
std::stringstream ss;
ss << res;
@endcode

@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.

@par Complexity
Linear in `res.buffer().size()`

@par Exception Safety
Basic guarantee.

@return A reference to the output stream, for chaining

@param os The output stream to write to.

@param res The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const response& res);

} // http_proto
} // boost

Expand Down
38 changes: 38 additions & 0 deletions include/boost/http_proto/response_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <boost/http_proto/message_base.hpp>
#include <boost/http_proto/status.hpp>

#include <iosfwd>

namespace boost {
namespace http_proto {

Expand Down Expand Up @@ -183,6 +185,42 @@ class response_base
http_proto::version v);
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
response_base res;
std::stringstream ss;
ss << res;
@endcode

@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.

@par Complexity
Linear in `res.buffer().size()`

@par Exception Safety
Basic guarantee.

@return A reference to the output stream, for chaining

@param os The output stream to write to.

@param res The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const response_base& res);

} // http_proto
} // boost

Expand Down
35 changes: 34 additions & 1 deletion src/fields_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/http_proto/error.hpp>
#include <boost/http_proto/field.hpp>
#include <boost/http_proto/fields_base.hpp>
#include <boost/http_proto/fields.hpp>
#include <boost/http_proto/header_limits.hpp>
#include <boost/http_proto/rfc/token_rule.hpp>

Expand Down Expand Up @@ -857,7 +858,38 @@ operator<<(
std::ostream& os,
const fields_base& f)
{
return os << f.buffer();
auto buf = f.buffer();
std::size_t i = 0;

while (i < buf.size()) {
if (i + 1 < buf.size() &&
buf[i] == '\r' &&
buf[i+1] == '\n') {
// Check if this is the trailing CRLF (at the end)
if (i + 2 == buf.size()) {
// This is the trailing CRLF, don't output it
break;
}
// Replace CRLF with LF
os << '\n';
i += 2;
} else {
os << buf[i];
i++;
}
}

return os;
}

//------------------------------------------------

std::ostream&
operator<<(
std::ostream& os,
const fields& f)
{
return operator<<(os, static_cast<const fields_base&>(f));
}

//------------------------------------------------
Expand Down Expand Up @@ -1496,3 +1528,4 @@ length(

} // http_proto
} // boost

23 changes: 23 additions & 0 deletions src/request_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
//

#include <boost/http_proto/request_base.hpp>
#include <boost/http_proto/request.hpp>
#include <boost/http_proto/fields_base.hpp>

#include <cstring>
#include <ostream>

namespace boost {
namespace http_proto {
Expand Down Expand Up @@ -122,5 +125,25 @@ set_start_line_impl(
h_.on_start_line();
}

//------------------------------------------------

std::ostream&
operator<<(
std::ostream& os,
const request_base& req)
{
return operator<<(os, static_cast<const fields_base&>(req));
}

//------------------------------------------------

std::ostream&
operator<<(
std::ostream& os,
const request& req)
{
return operator<<(os, static_cast<const request_base&>(req));
}

} // http_proto
} // boost
23 changes: 23 additions & 0 deletions src/response_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
//

#include <boost/http_proto/response_base.hpp>
#include <boost/http_proto/response.hpp>
#include <boost/http_proto/fields_base.hpp>

#include <cstring>
#include <ostream>

namespace boost {
namespace http_proto {
Expand Down Expand Up @@ -57,5 +60,25 @@ set_start_line_impl(
h_.on_start_line();
}

//------------------------------------------------

std::ostream&
operator<<(
std::ostream& os,
const response_base& res)
{
return operator<<(os, static_cast<const fields_base&>(res));
}

//------------------------------------------------

std::ostream&
operator<<(
std::ostream& os,
const response& res)
{
return operator<<(os, static_cast<const response_base&>(res));
}

} // http_proto
} // boost
Loading
Loading