Only Consume() after a full request is processed

This commit is contained in:
flamingcow
2019-05-09 19:19:26 -07:00
parent 1cba4ac1ae
commit 67f0fc48f9
7 changed files with 12 additions and 14 deletions

View File

@@ -9,11 +9,11 @@
namespace firecgi {
Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers)
Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers, int max_request_len)
: sock_(sock),
callback_(callback),
headers_(headers),
buf_(sock, max_record_len),
buf_(sock, max_request_len),
request_(this) {
char client_addr_str[INET6_ADDRSTRLEN];
PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str)));
@@ -122,6 +122,7 @@ int Connection::Read() {
// Magic signal for completed request (mirrors the HTTP/1.1 protocol)
requests_++;
callback_(&request_);
buf_.Consume(); // discard data and invalidate pointers
} else {
std::string_view in(buf_.Read(header->ContentLength()), header->ContentLength());
request_.AddIn(in);
@@ -141,7 +142,6 @@ int Connection::Read() {
buf_.Commit(); // we've acted on the bytes read so far
}
buf_.Consume();
return -1;
}

View File

@@ -13,7 +13,7 @@ namespace firecgi {
class Connection {
public:
Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers);
Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers, int max_request_len);
~Connection();
[[nodiscard]] int Read();

Submodule firebuf updated: 49766e29f4...4d9486e281

View File

@@ -44,8 +44,4 @@ struct ParamHeader {
uint8_t value_length;
};
constexpr auto max_content_len = 65535;
constexpr auto max_padding_len = 255;
constexpr auto max_record_len = sizeof(Header) + max_content_len + max_padding_len;
} // namespace firecgi

View File

@@ -18,7 +18,7 @@ template<class T> void AppendVec(const T& obj, std::vector<iovec>* vec) {
Request::Request(Connection* conn)
: conn_(conn),
out_buf_(max_record_len) {}
out_buf_(64*1024) {}
void Request::NewRequest(uint16_t request_id) {
request_id_ = request_id;

View File

@@ -11,11 +11,12 @@
namespace firecgi {
Server::Server(int port, const std::function<void(Request*)>& callback, int threads, const std::unordered_set<std::string_view>& headers)
Server::Server(int port, const std::function<void(Request*)>& callback, int threads, const std::unordered_set<std::string_view>& headers, int max_request_len)
: port_(port),
callback_(callback),
threads_(threads),
headers_(headers) {
headers_(headers),
max_request_len_(max_request_len) {
LOG(INFO) << "listening on [::1]:" << port_;
signal(SIGPIPE, SIG_IGN);
@@ -81,7 +82,7 @@ void Server::NewConn(int listen_sock, int epoll_fd) {
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
{
auto *conn = new Connection(client_sock, client_addr, callback_, headers_);
auto *conn = new Connection(client_sock, client_addr, callback_, headers_, max_request_len_);
struct epoll_event ev{
.events = EPOLLIN,
.data = {

View File

@@ -10,7 +10,7 @@ namespace firecgi {
class Server {
public:
Server(int port, const std::function<void(Request*)>& callback, int threads=1, const std::unordered_set<std::string_view>& headers={});
Server(int port, const std::function<void(Request*)>& callback, int threads=1, const std::unordered_set<std::string_view>& headers={}, int max_request_len=(16*1024));
void Serve();
private:
@@ -22,6 +22,7 @@ class Server {
const std::function<void(Request*)> callback_;
const int threads_;
const std::unordered_set<std::string_view> headers_;
const int max_request_len_;
};
} // firecgi