forked from boostorg/beast
Refactor http::header contents (API Change):
fix #124 The http::header data members "method", "url", and "reason" are changed from data members, to pairs of get and set functions which forward the call to the Fields type used to instantiate the template. Previously, these data members were implemented using std::string. With this change, the implementation of the Fields type used to instantiate the template is now in control of the representation of those values. This permits custom memory allocation strategies including uniform use of the Allocator type already provided to beast::http::basic_fields.
This commit is contained in:
+30
-24
@@ -70,7 +70,8 @@ public:
|
||||
};
|
||||
};
|
||||
|
||||
void testMessage()
|
||||
void
|
||||
testMessage()
|
||||
{
|
||||
static_assert(std::is_constructible<
|
||||
message<true, default_body, fields>>::value, "");
|
||||
@@ -130,42 +131,43 @@ public:
|
||||
// swap
|
||||
message<true, string_body, fields> m1;
|
||||
message<true, string_body, fields> m2;
|
||||
m1.url = "u";
|
||||
m1.target("u");
|
||||
m1.body = "1";
|
||||
m1.fields.insert("h", "v");
|
||||
m2.method = "G";
|
||||
m2.method("G");
|
||||
m2.body = "2";
|
||||
swap(m1, m2);
|
||||
BEAST_EXPECT(m1.method == "G");
|
||||
BEAST_EXPECT(m2.method.empty());
|
||||
BEAST_EXPECT(m1.url.empty());
|
||||
BEAST_EXPECT(m2.url == "u");
|
||||
BEAST_EXPECT(m1.method() == "G");
|
||||
BEAST_EXPECT(m2.method().empty());
|
||||
BEAST_EXPECT(m1.target().empty());
|
||||
BEAST_EXPECT(m2.target() == "u");
|
||||
BEAST_EXPECT(m1.body == "2");
|
||||
BEAST_EXPECT(m2.body == "1");
|
||||
BEAST_EXPECT(! m1.fields.exists("h"));
|
||||
BEAST_EXPECT(m2.fields.exists("h"));
|
||||
}
|
||||
|
||||
struct MoveHeaders
|
||||
struct MoveFields : fields
|
||||
{
|
||||
bool moved_to = false;
|
||||
bool moved_from = false;
|
||||
|
||||
MoveHeaders() = default;
|
||||
MoveFields() = default;
|
||||
|
||||
MoveHeaders(MoveHeaders&& other)
|
||||
MoveFields(MoveFields&& other)
|
||||
: moved_to(true)
|
||||
{
|
||||
other.moved_from = true;
|
||||
}
|
||||
|
||||
MoveHeaders& operator=(MoveHeaders&& other)
|
||||
MoveFields& operator=(MoveFields&& other)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
void testHeaders()
|
||||
void
|
||||
testHeaders()
|
||||
{
|
||||
{
|
||||
using req_type = request_header;
|
||||
@@ -182,22 +184,23 @@ public:
|
||||
}
|
||||
|
||||
{
|
||||
MoveHeaders h;
|
||||
header<true, MoveHeaders> r{std::move(h)};
|
||||
MoveFields h;
|
||||
header<true, MoveFields> r{std::move(h)};
|
||||
BEAST_EXPECT(h.moved_from);
|
||||
BEAST_EXPECT(r.fields.moved_to);
|
||||
request<string_body, MoveHeaders> m{std::move(r)};
|
||||
request<string_body, MoveFields> m{std::move(r)};
|
||||
BEAST_EXPECT(r.fields.moved_from);
|
||||
BEAST_EXPECT(m.fields.moved_to);
|
||||
}
|
||||
}
|
||||
|
||||
void testFreeFunctions()
|
||||
void
|
||||
testFreeFunctions()
|
||||
{
|
||||
{
|
||||
request<string_body> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("Upgrade", "test");
|
||||
BEAST_EXPECT(! is_upgrade(m));
|
||||
@@ -211,7 +214,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void testPrepare()
|
||||
void
|
||||
testPrepare()
|
||||
{
|
||||
request<string_body> m;
|
||||
m.version = 10;
|
||||
@@ -253,7 +257,8 @@ public:
|
||||
BEAST_EXPECT(! is_keep_alive(m));
|
||||
}
|
||||
|
||||
void testSwap()
|
||||
void
|
||||
testSwap()
|
||||
{
|
||||
message<false, string_body, fields> m1;
|
||||
message<false, string_body, fields> m2;
|
||||
@@ -262,14 +267,14 @@ public:
|
||||
m1.body = "1";
|
||||
m1.fields.insert("h", "v");
|
||||
m2.status = 404;
|
||||
m2.reason = "OK";
|
||||
m2.reason("OK");
|
||||
m2.body = "2";
|
||||
m2.version = 11;
|
||||
swap(m1, m2);
|
||||
BEAST_EXPECT(m1.status == 404);
|
||||
BEAST_EXPECT(m2.status == 200);
|
||||
BEAST_EXPECT(m1.reason == "OK");
|
||||
BEAST_EXPECT(m2.reason.empty());
|
||||
BEAST_EXPECT(m1.reason() == "OK");
|
||||
BEAST_EXPECT(m2.reason().empty());
|
||||
BEAST_EXPECT(m1.version == 11);
|
||||
BEAST_EXPECT(m2.version == 10);
|
||||
BEAST_EXPECT(m1.body == "2");
|
||||
@@ -291,7 +296,8 @@ public:
|
||||
}();
|
||||
}
|
||||
|
||||
void run() override
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testMessage();
|
||||
testHeaders();
|
||||
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
"msrp", "msrps", "mtqp", "mumble", "mupdate", "mvn", "news", "nfs", "ni",
|
||||
"nih", "nntp", "notes", "oid", "opaquelocktoken", "pack", "palm", "paparazzi",
|
||||
"pkcs11", "platform", "pop", "pres", "prospero", "proxy", "psyc", "query",
|
||||
"redis", "rediss", "reload", "res", "resource", "rmi", "rsync", "rtmfp",
|
||||
"redis", "rediss", "reload", "res", "target", "rmi", "rsync", "rtmfp",
|
||||
"rtmp", "rtsp", "rtsps", "rtspu", "secondlife", "service", "session", "sftp",
|
||||
"sgn", "shttp", "sieve", "sip", "sips", "skype", "smb", "sms", "smtp",
|
||||
"snews", "snmp", "soap.beep", "soap.beeps", "soldat", "spotify", "ssh",
|
||||
|
||||
@@ -130,8 +130,8 @@ public:
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.is_complete());
|
||||
BEAST_EXPECT(m.method == "GET");
|
||||
BEAST_EXPECT(m.url == "/");
|
||||
BEAST_EXPECT(m.method() == "GET");
|
||||
BEAST_EXPECT(m.target() == "/");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m.fields["User-Agent"] == "test");
|
||||
BEAST_EXPECT(m.body == "*");
|
||||
@@ -182,7 +182,7 @@ public:
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.is_complete());
|
||||
BEAST_EXPECT(m.status == 200);
|
||||
BEAST_EXPECT(m.reason == "OK");
|
||||
BEAST_EXPECT(m.reason() == "OK");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m.fields["Server"] == "test");
|
||||
BEAST_EXPECT(m.body == "*");
|
||||
|
||||
+39
-39
@@ -146,8 +146,8 @@ public:
|
||||
{
|
||||
header<true, fields> m;
|
||||
m.version = 11;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.fields.insert("User-Agent", "test");
|
||||
error_code ec;
|
||||
test::string_ostream ss{ios_};
|
||||
@@ -162,7 +162,7 @@ public:
|
||||
header<false, fields> m;
|
||||
m.version = 10;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.reason("OK");
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
error_code ec;
|
||||
@@ -184,7 +184,7 @@ public:
|
||||
message<false, string_body, fields> m;
|
||||
m.version = 10;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.reason("OK");
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
m.body = "*****";
|
||||
@@ -203,7 +203,7 @@ public:
|
||||
message<false, string_body, fields> m;
|
||||
m.version = 11;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.reason("OK");
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
m.body = "*****";
|
||||
@@ -236,8 +236,8 @@ public:
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
@@ -269,8 +269,8 @@ public:
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
@@ -304,8 +304,8 @@ public:
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
@@ -339,8 +339,8 @@ public:
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
@@ -369,8 +369,8 @@ public:
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
@@ -398,8 +398,8 @@ public:
|
||||
// auto content-length HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -415,8 +415,8 @@ public:
|
||||
// keep-alive HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -433,8 +433,8 @@ public:
|
||||
// upgrade HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -451,8 +451,8 @@ public:
|
||||
// no content-length HTTP/1.0
|
||||
{
|
||||
message<true, unsized_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -471,8 +471,8 @@ public:
|
||||
// auto content-length HTTP/1.1
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -488,8 +488,8 @@ public:
|
||||
// close HTTP/1.1
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -510,8 +510,8 @@ public:
|
||||
// upgrade HTTP/1.1
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
prepare(m, connection::upgrade);
|
||||
@@ -525,8 +525,8 @@ public:
|
||||
// no content-length HTTP/1.1
|
||||
{
|
||||
message<true, unsized_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -550,8 +550,8 @@ public:
|
||||
{
|
||||
// Conversion to std::string via operator<<
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -590,8 +590,8 @@ public:
|
||||
void testOstream()
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.method("GET");
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
@@ -631,9 +631,9 @@ public:
|
||||
test::string_ostream os{ios};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.method("GET");
|
||||
m.version = 11;
|
||||
m.url = "/";
|
||||
m.target("/");
|
||||
m.fields.insert("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(os, m, handler{});
|
||||
@@ -653,9 +653,9 @@ public:
|
||||
test::string_ostream is{ios};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.method("GET");
|
||||
m.version = 11;
|
||||
m.url = "/";
|
||||
m.target("/");
|
||||
m.fields.insert("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(is, m, handler{});
|
||||
|
||||
Reference in New Issue
Block a user