mirror of
https://github.com/matrix-construct/construct
synced 2024-06-02 18:18:56 +02:00
ircd::http: Consolidate line terminator static strings.
This commit is contained in:
parent
ee23c98eda
commit
60fda46bdf
|
@ -67,6 +67,8 @@ struct ircd::http::line
|
|||
struct request;
|
||||
struct response;
|
||||
|
||||
static const string_view terminator; // "\r\n"
|
||||
|
||||
using string_view::string_view;
|
||||
line(parse::capstan &);
|
||||
};
|
||||
|
@ -186,6 +188,8 @@ struct ircd::http::headers
|
|||
using closure = std::function<void (const header &)>;
|
||||
using closure_bool = std::function<bool (const header &)>;
|
||||
|
||||
static const string_view terminator; // "\r\n\r\n"
|
||||
|
||||
bool for_each(const closure_bool &) const;
|
||||
string_view operator[](const string_view &key) const;
|
||||
string_view at(const string_view &key) const;
|
||||
|
|
18
ircd/http.cc
18
ircd/http.cc
|
@ -552,6 +552,12 @@ ircd::http::has(const headers &headers,
|
|||
// headers::headers
|
||||
//
|
||||
|
||||
decltype(ircd::http::headers::terminator)
|
||||
ircd::http::headers::terminator
|
||||
{
|
||||
"\r\n\r\n"
|
||||
};
|
||||
|
||||
ircd::http::headers::headers(parse::capstan &pc,
|
||||
const closure &c)
|
||||
:headers
|
||||
|
@ -714,6 +720,16 @@ const
|
|||
};
|
||||
}
|
||||
|
||||
//
|
||||
// http::line
|
||||
//
|
||||
|
||||
decltype(ircd::http::line::terminator)
|
||||
ircd::http::line::terminator
|
||||
{
|
||||
"\r\n"
|
||||
};
|
||||
|
||||
ircd::http::line::line(parse::capstan &pc)
|
||||
:string_view{[&pc]
|
||||
{
|
||||
|
@ -978,7 +994,7 @@ ircd::http::writeline(window_buffer &write,
|
|||
{
|
||||
const auto newline{[](const mutable_buffer &out)
|
||||
{
|
||||
return copy(out, "\r\n"_sv);
|
||||
return copy(out, line::terminator);
|
||||
}};
|
||||
|
||||
write(closure);
|
||||
|
|
|
@ -2809,10 +2809,9 @@ ircd::server::tag::read_head(const const_buffer &buffer,
|
|||
auto &req{*request};
|
||||
|
||||
// informal search for head terminator
|
||||
static const string_view terminator{"\r\n\r\n"};
|
||||
const auto pos
|
||||
{
|
||||
string_view{buffer}.find(terminator)
|
||||
string_view{buffer}.find(http::headers::terminator)
|
||||
};
|
||||
|
||||
// No terminator found; account for what was received in this buffer
|
||||
|
@ -2828,7 +2827,7 @@ ircd::server::tag::read_head(const const_buffer &buffer,
|
|||
// including the terminator which is considered part of the dome.
|
||||
const size_t addl_head_bytes
|
||||
{
|
||||
pos + size(terminator)
|
||||
pos + size(http::headers::terminator)
|
||||
};
|
||||
|
||||
// The received buffer may go past the end of the head.
|
||||
|
@ -3076,10 +3075,9 @@ ircd::server::tag::read_chunk_head(const const_buffer &buffer,
|
|||
const auto &content{req.in.content};
|
||||
|
||||
// informal search for head terminator
|
||||
static const string_view terminator{"\r\n"};
|
||||
const auto pos
|
||||
{
|
||||
string_view{buffer}.find(terminator)
|
||||
string_view{buffer}.find(http::line::terminator)
|
||||
};
|
||||
|
||||
if(pos == string_view::npos)
|
||||
|
@ -3091,7 +3089,7 @@ ircd::server::tag::read_chunk_head(const const_buffer &buffer,
|
|||
// This indicates how much head was just received from this buffer only.
|
||||
const size_t addl_head_bytes
|
||||
{
|
||||
pos + size(terminator)
|
||||
pos + size(http::line::terminator)
|
||||
};
|
||||
|
||||
// The received buffer may go past the end of the head.
|
||||
|
@ -3128,7 +3126,7 @@ ircd::server::tag::read_chunk_head(const const_buffer &buffer,
|
|||
|
||||
// Play the tape through the formal grammar.
|
||||
const http::response::chunk chunk{pc};
|
||||
state.chunk_length = chunk.size + size(terminator);
|
||||
state.chunk_length = chunk.size + size(http::line::terminator);
|
||||
|
||||
// Now we check how much chunk was received beyond the head
|
||||
const auto &chunk_read
|
||||
|
@ -3238,18 +3236,17 @@ ircd::server::chunk_content_completed(tag &tag,
|
|||
assert(tag.request);
|
||||
auto &req{*tag.request};
|
||||
auto &state{tag.state};
|
||||
static const string_view terminator{"\r\n"};
|
||||
|
||||
// Remove the terminator from the total length state.
|
||||
assert(state.content_length >= size(terminator));
|
||||
state.content_length -= size(terminator);
|
||||
state.content_read -= size(terminator);
|
||||
assert(state.content_length >= size(http::line::terminator));
|
||||
state.content_length -= size(http::line::terminator);
|
||||
state.content_read -= size(http::line::terminator);
|
||||
|
||||
// Remove the terminator from the chunk length state.
|
||||
assert(state.chunk_length >= 2);
|
||||
assert(state.chunk_read == state.chunk_length);
|
||||
state.chunk_length -= size(terminator);
|
||||
state.chunk_read -= size(terminator);
|
||||
state.chunk_length -= size(http::line::terminator);
|
||||
state.chunk_read -= size(http::line::terminator);
|
||||
|
||||
// State sanity tests
|
||||
assert(state.content_length >= state.content_read);
|
||||
|
@ -3288,10 +3285,9 @@ ircd::server::tag::read_chunk_dynamic_head(const const_buffer &buffer,
|
|||
auto &req{*request};
|
||||
|
||||
// informal search for head terminator
|
||||
static const string_view terminator{"\r\n"};
|
||||
const auto pos
|
||||
{
|
||||
string_view{buffer}.find(terminator)
|
||||
string_view{buffer}.find(http::line::terminator)
|
||||
};
|
||||
|
||||
if(pos == string_view::npos)
|
||||
|
@ -3304,7 +3300,7 @@ ircd::server::tag::read_chunk_dynamic_head(const const_buffer &buffer,
|
|||
// This indicates how much head was just received from this buffer only.
|
||||
const size_t addl_head_bytes
|
||||
{
|
||||
pos + size(terminator)
|
||||
pos + size(http::line::terminator)
|
||||
};
|
||||
|
||||
// The received buffer may go past the end of the head.
|
||||
|
@ -3338,7 +3334,7 @@ ircd::server::tag::read_chunk_dynamic_head(const const_buffer &buffer,
|
|||
// Play the tape through the formal grammar.
|
||||
const http::response::chunk chunk{pc};
|
||||
assert(state.chunk_length == size_t(-1));
|
||||
state.chunk_length = chunk.size + size(terminator);
|
||||
state.chunk_length = chunk.size + size(http::line::terminator);
|
||||
|
||||
// Increment the content_length to now include this chunk
|
||||
state.content_length += state.chunk_length;
|
||||
|
@ -3454,23 +3450,22 @@ ircd::server::chunk_dynamic_content_completed(tag &tag,
|
|||
auto &state{tag.state};
|
||||
assert(!req.in.chunks.empty());
|
||||
auto &chunk{req.in.chunks.back()};
|
||||
static const string_view terminator{"\r\n"};
|
||||
|
||||
// Remove the terminator from the total length state.
|
||||
assert(state.content_length >= size(terminator));
|
||||
assert(state.content_read >= size(terminator));
|
||||
state.content_length -= size(terminator);
|
||||
state.content_read -= size(terminator);
|
||||
assert(state.content_length >= size(http::line::terminator));
|
||||
assert(state.content_read >= size(http::line::terminator));
|
||||
state.content_length -= size(http::line::terminator);
|
||||
state.content_read -= size(http::line::terminator);
|
||||
|
||||
// Remove the terminator from the chunk length state.
|
||||
assert(state.chunk_length >= size(terminator));
|
||||
assert(state.chunk_read >= size(terminator));
|
||||
assert(state.chunk_length >= size(http::line::terminator));
|
||||
assert(state.chunk_read >= size(http::line::terminator));
|
||||
assert(state.chunk_read == state.chunk_length);
|
||||
state.chunk_length -= size(terminator);
|
||||
state.chunk_read -= size(terminator);
|
||||
state.chunk_length -= size(http::line::terminator);
|
||||
state.chunk_read -= size(http::line::terminator);
|
||||
|
||||
// Remove the terminator from the end of the chunk
|
||||
std::get<1>(chunk) -= size(terminator);
|
||||
std::get<1>(chunk) -= size(http::line::terminator);
|
||||
assert(size(chunk) == state.chunk_length);
|
||||
assert(std::get<0>(chunk) <= std::get<1>(chunk));
|
||||
|
||||
|
|
Loading…
Reference in a new issue