forked from boostorg/beast
Add message keep_alive, chunked, content_length members
This commit is contained in:
1 parent
aa473a6da4
commit
6a46e48424
15 files changed
+1025
-325
No files matched your search
+52
-28
@@ -190,52 +190,76 @@ public:
|
||||
struct reader;
|
||||
|
||||
protected:
|
||||
/** Set or clear the method string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void set_method_impl(string_view s);
|
||||
|
||||
/** Set or clear the target string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void set_target_impl(string_view s);
|
||||
|
||||
/** Set or clear the reason string.
|
||||
|
||||
@note Only called for responses.
|
||||
*/
|
||||
void set_reason_impl(string_view s);
|
||||
|
||||
/** Returns the request-method string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
string_view get_method_impl() const;
|
||||
string_view
|
||||
get_method_impl() const;
|
||||
|
||||
/** Returns the request-target string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
string_view get_target_impl() const;
|
||||
string_view
|
||||
get_target_impl() const;
|
||||
|
||||
/** Returns the response reason-phrase string.
|
||||
|
||||
@note Only called for responses.
|
||||
*/
|
||||
string_view get_reason_impl() const;
|
||||
string_view
|
||||
get_reason_impl() const;
|
||||
|
||||
/** Updates the payload metadata.
|
||||
|
||||
@param b `true` if chunked
|
||||
|
||||
@param n The content length if known, otherwise `boost::none`
|
||||
/** Returns the chunked Transfer-Encoding setting
|
||||
*/
|
||||
void prepare_payload_impl(bool b, boost::optional<std::uint64_t> n);
|
||||
bool
|
||||
get_chunked_impl() const;
|
||||
|
||||
/** Returns the keep-alive setting
|
||||
*/
|
||||
bool
|
||||
get_keep_alive_impl(unsigned version) const;
|
||||
|
||||
/** Set or clear the method string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void
|
||||
set_method_impl(string_view s);
|
||||
|
||||
/** Set or clear the target string.
|
||||
|
||||
@note Only called for requests.
|
||||
*/
|
||||
void
|
||||
set_target_impl(string_view s);
|
||||
|
||||
/** Set or clear the reason string.
|
||||
|
||||
@note Only called for responses.
|
||||
*/
|
||||
void
|
||||
set_reason_impl(string_view s);
|
||||
|
||||
/** Sets or clears the chunked Transfer-Encoding value
|
||||
*/
|
||||
void
|
||||
set_chunked_impl(bool value);
|
||||
|
||||
/** Sets or clears the Content-Length field
|
||||
*/
|
||||
void
|
||||
set_content_length_impl(boost::optional<std::uint64_t>);
|
||||
|
||||
/** Adjusts the Connection field
|
||||
*/
|
||||
void
|
||||
set_keep_alive_impl(unsigned version, bool keep_alive);
|
||||
};
|
||||
|
||||
static_assert(is_fields<Fields>::value, "");
|
||||
static_assert(is_fields<Fields>::value,
|
||||
"Fields requirements not met");
|
||||
|
||||
//]
|
||||
|
||||
|
||||
@@ -1124,7 +1124,7 @@ public:
|
||||
0x0A,0x49,0x66,0x3A,0x20,0x40,0xC1,0x50,0x5C,0xD6,0xC3,0x86,0xFC,0x8D,0x5C,0x7C,0x96,0x45,0x0D,0x0A,0x4D,0x4D,0x48,0x53,
|
||||
0x2D,0x45,0x78,0x65,0x6D,0x70,0x74,0x65,0x64,0x2D,0x41,0x64,0x64,0x72,0x65,0x73,0x73,0x3A,0x0D,0x0A,0x49,0x6E,0x6A,0x65,
|
||||
0x63,0x74,0x69,0x6F,0x6E,0x2D,0x49,0x6E,0x66,0x6F,0x3A,0x20,0x0D,0x0A,0x43,0x6F,0x6E,0x74,0x65,0x74,0x6E,0x2D,0x4C,0x65,
|
||||
0x6E,0x67,0x74,0x68,0x3A,0x20,0x30,0x0D,0x0A,0x0D,0x0A
|
||||
0x6E,0x67,0x74,0x68,0x3A,0x20,0x30,0x0D,0x0A,0x0D,0x0A
|
||||
};
|
||||
|
||||
error_code ec;
|
||||
|
||||
+321
-1
@@ -584,7 +584,323 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void run() override
|
||||
void
|
||||
testKeepAlive()
|
||||
{
|
||||
response<empty_body> res;
|
||||
auto const keep_alive =
|
||||
[&](bool v)
|
||||
{
|
||||
res.keep_alive(v);
|
||||
BEAST_EXPECT(
|
||||
(res.keep_alive() && v) ||
|
||||
(! res.keep_alive() && ! v));
|
||||
};
|
||||
|
||||
BOOST_STATIC_ASSERT(fields::max_static_buffer == 4096);
|
||||
std::string const big(4096 + 1, 'a');
|
||||
|
||||
// HTTP/1.0
|
||||
res.version = 10;
|
||||
res.erase(field::connection);
|
||||
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
auto const test10 =
|
||||
[&](std::string s)
|
||||
{
|
||||
res.set(field::connection, s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s + ", close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", keep-alive");
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive, " + s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s+ ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive, " + s);
|
||||
};
|
||||
|
||||
test10("foo");
|
||||
test10(big);
|
||||
|
||||
// HTTP/1.1
|
||||
res.version = 11;
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
auto const test11 =
|
||||
[&](std::string s)
|
||||
{
|
||||
res.set(field::connection, s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", close");
|
||||
|
||||
res.set(field::connection, "close, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close, " + s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", close");
|
||||
|
||||
res.set(field::connection, "close, " + s + ", keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close, " + s);
|
||||
};
|
||||
|
||||
test11("foo");
|
||||
test11(big);
|
||||
}
|
||||
|
||||
void
|
||||
testContentLength()
|
||||
{
|
||||
response<empty_body> res{status::ok, 11};
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
auto const check = [&](std::string s)
|
||||
{
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
};
|
||||
|
||||
check("foo");
|
||||
|
||||
BOOST_STATIC_ASSERT(fields::max_static_buffer == 4096);
|
||||
std::string const big(4096 + 1, 'a');
|
||||
|
||||
check(big);
|
||||
}
|
||||
|
||||
void
|
||||
testChunked()
|
||||
{
|
||||
response<empty_body> res{status::ok, 11};
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
auto const chunked =
|
||||
[&](bool v)
|
||||
{
|
||||
res.chunked(v);
|
||||
BEAST_EXPECT(
|
||||
(res.chunked() && v) ||
|
||||
(! res.chunked() && ! v));
|
||||
BEAST_EXPECT(res.count(
|
||||
field::content_length) == 0);
|
||||
};
|
||||
|
||||
res.erase(field::transfer_encoding);
|
||||
res.set(field::content_length, 32);
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked");
|
||||
|
||||
res.erase(field::transfer_encoding);
|
||||
res.set(field::content_length, 32);
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
|
||||
|
||||
res.set(field::transfer_encoding, "foo");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo, chunked");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "foo, chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "foo");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo, chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testMembers();
|
||||
testHeaders();
|
||||
@@ -592,6 +908,10 @@ public:
|
||||
testErase();
|
||||
testContainer();
|
||||
testPreparePayload();
|
||||
|
||||
testKeepAlive();
|
||||
testContentLength();
|
||||
testChunked();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -191,13 +191,17 @@ public:
|
||||
|
||||
test_fields() = delete;
|
||||
test_fields(token) {}
|
||||
void set_method_impl(string_view) {}
|
||||
void set_target_impl(string_view s) { target = s.to_string(); }
|
||||
void set_reason_impl(string_view) {}
|
||||
string_view get_method_impl() const { return {}; }
|
||||
string_view get_target_impl() const { return target; }
|
||||
string_view get_reason_impl() const { return {}; }
|
||||
void prepare_payload_impl(bool, boost::optional<std::uint64_t>) {}
|
||||
bool get_chunked_impl() const { return false; }
|
||||
bool get_keep_alive_impl(unsigned) const { return true; }
|
||||
void set_method_impl(string_view) {}
|
||||
void set_target_impl(string_view s) { target = s.to_string(); }
|
||||
void set_reason_impl(string_view) {}
|
||||
void set_chunked_impl(bool) {}
|
||||
void set_content_length_impl(boost::optional<std::uint64_t>) {}
|
||||
void set_keep_alive_impl(unsigned, bool) {}
|
||||
};
|
||||
|
||||
void
|
||||
|
||||
Reference in new issue
Block a user