Skip to content
Merged
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
76 changes: 40 additions & 36 deletions include/boost/http_proto/fields_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,46 @@ class fields_base
core::string_view value,
system::error_code& ec);

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

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
request req;
req.set(field::content_length, "42");
std::stringstream ss;
ss << req;
assert( ss.str() == "GET / HTTP/1.1\nContent-Length: 42\n" );
@endcode

@par Effects
@code
return os << f.buffer();
@endcode

@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.
*/
friend
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const fields_base& f);

private:
BOOST_HTTP_PROTO_DECL
void
Expand Down Expand Up @@ -1352,42 +1392,6 @@ class fields_base
std::size_t i) const noexcept;
};

/** Format the container to the output stream

This function serializes the container to
the specified output stream.

@par Example
@code
request req;
std::stringstream ss;
ss << req;
assert( ss.str() == "GET / HTTP/1.1\r\n\r\n" );
@endcode

@par Effects
@code
return os << f.buffer();
@endcode

@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_base& f);

} // http_proto
} // boost

Expand Down
3 changes: 3 additions & 0 deletions include/boost/http_proto/method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace http_proto {

Each item corresponds to a particular method string
used in HTTP request messages.

@note The values are guaranteed to be consecutive,
starting at 0.
*/
enum class method : char
{
Expand Down
20 changes: 10 additions & 10 deletions include/boost/http_proto/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ class serializer

Initializes the serializer with the HTTP
start-line and headers from `m`, and returns
a @ref stream object for reading the body
from an external source.
a @ref stream object for writing the body
data into the serializer's internal buffer.

Once the serializer is destroyed, @ref reset
is called, or @ref is_done returns true, the
only valid operation on the stream is destruction.

The stream allows inverted control flow: the
caller supplies body data via the serializer’s
caller supplies body data to the serializer’s
internal buffer while reading from an external
source.

Expand All @@ -351,17 +351,17 @@ class serializer

@par Example
@code
serializer::stream strm = serializer.start_stream(response);
serializer::stream st = serializer.start_stream(response);
do
{
if(strm.is_open())
if(st.is_open())
{
std::size_t n = source.read_some(strm.prepare());
std::size_t n = source.read_some(st.prepare());

if(ec == error::eof)
strm.close();
st.close();
else
strm.commit(n);
st.commit(n);
}

write_some(client, serializer);
Expand Down Expand Up @@ -392,8 +392,8 @@ class serializer
@param m The message to read the HTTP
start-line and headers from.

@return A @ref stream object for reading body
content into the serializer's buffer.
@return A @ref stream object for writing the body
data into the serializer's internal buffer.

@see
@ref stream,
Expand Down
10 changes: 7 additions & 3 deletions src/fields_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,14 +850,18 @@ find_all(
&h_, find(name).i_);
}

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

std::ostream&
operator<<(
std::ostream& os,
const fields_base& f)
{
return os << f.buffer();
if(f.h_.prefix != 0)
os << core::string_view(f.h_.cbuf, f.h_.prefix - 2) << '\n';

for(auto ref : f)
os << ref.name << ": " << ref.value << '\n';

return os;
}

//------------------------------------------------
Expand Down
12 changes: 8 additions & 4 deletions test/unit/fields_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,11 +1704,15 @@ struct fields_base_test
// operator<<
{
std::stringstream ss;
fields f;
f.set(field::content_length, "42");
ss << f;
response r;
r.set(field::content_length, "42");
r.set(field::connection, "Close");
ss << r;
BOOST_TEST_EQ(
ss.str(), "Content-Length: 42\r\n\r\n");
ss.str(),
"HTTP/1.1 200 OK\n"
"Content-Length: 42\n"
"Connection: Close\n");
}
}

Expand Down
Loading