Skip to content

Commit 2117759

Browse files
committed
refactor fields_base operator<<
Fixes #182
1 parent c998efd commit 2117759

3 files changed

Lines changed: 55 additions & 43 deletions

File tree

include/boost/http_proto/fields_base.hpp

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,46 @@ class fields_base
13031303
core::string_view value,
13041304
system::error_code& ec);
13051305

1306+
//--------------------------------------------
1307+
1308+
/** Format the container to the output stream
1309+
1310+
This function serializes the container to
1311+
the specified output stream.
1312+
1313+
@par Example
1314+
@code
1315+
request req;
1316+
req.set(field::content_length, "42");
1317+
std::stringstream ss;
1318+
ss << req;
1319+
assert( ss.str() == "GET / HTTP/1.1\nContent-Length: 42\n" );
1320+
@endcode
1321+
1322+
@par Effects
1323+
@code
1324+
return os << f.buffer();
1325+
@endcode
1326+
1327+
@par Complexity
1328+
Linear in `f.buffer().size()`
1329+
1330+
@par Exception Safety
1331+
Basic guarantee.
1332+
1333+
@return A reference to the output stream, for chaining
1334+
1335+
@param os The output stream to write to.
1336+
1337+
@param f The container to write.
1338+
*/
1339+
friend
1340+
BOOST_HTTP_PROTO_DECL
1341+
std::ostream&
1342+
operator<<(
1343+
std::ostream& os,
1344+
const fields_base& f);
1345+
13061346
private:
13071347
BOOST_HTTP_PROTO_DECL
13081348
void
@@ -1352,42 +1392,6 @@ class fields_base
13521392
std::size_t i) const noexcept;
13531393
};
13541394

1355-
/** Format the container to the output stream
1356-
1357-
This function serializes the container to
1358-
the specified output stream.
1359-
1360-
@par Example
1361-
@code
1362-
request req;
1363-
std::stringstream ss;
1364-
ss << req;
1365-
assert( ss.str() == "GET / HTTP/1.1\r\n\r\n" );
1366-
@endcode
1367-
1368-
@par Effects
1369-
@code
1370-
return os << f.buffer();
1371-
@endcode
1372-
1373-
@par Complexity
1374-
Linear in `f.buffer().size()`
1375-
1376-
@par Exception Safety
1377-
Basic guarantee.
1378-
1379-
@return A reference to the output stream, for chaining
1380-
1381-
@param os The output stream to write to.
1382-
1383-
@param f The container to write.
1384-
*/
1385-
BOOST_HTTP_PROTO_DECL
1386-
std::ostream&
1387-
operator<<(
1388-
std::ostream& os,
1389-
const fields_base& f);
1390-
13911395
} // http_proto
13921396
} // boost
13931397

src/fields_base.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,18 @@ find_all(
850850
&h_, find(name).i_);
851851
}
852852

853-
//------------------------------------------------
854-
855853
std::ostream&
856854
operator<<(
857855
std::ostream& os,
858856
const fields_base& f)
859857
{
860-
return os << f.buffer();
858+
if(f.h_.prefix != 0)
859+
os << core::string_view(f.h_.cbuf, f.h_.prefix - 2) << '\n';
860+
861+
for(auto ref : f)
862+
os << ref.name << ": " << ref.value << '\n';
863+
864+
return os;
861865
}
862866

863867
//------------------------------------------------

test/unit/fields_base.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,11 +1704,15 @@ struct fields_base_test
17041704
// operator<<
17051705
{
17061706
std::stringstream ss;
1707-
fields f;
1708-
f.set(field::content_length, "42");
1709-
ss << f;
1707+
response r;
1708+
r.set(field::content_length, "42");
1709+
r.set(field::connection, "Close");
1710+
ss << r;
17101711
BOOST_TEST_EQ(
1711-
ss.str(), "Content-Length: 42\r\n\r\n");
1712+
ss.str(),
1713+
"HTTP/1.1 200 OK\n"
1714+
"Content-Length: 42\n"
1715+
"Connection: Close\n");
17121716
}
17131717
}
17141718

0 commit comments

Comments
 (0)