From d486533dfcd1b07e2e1734a9a6cb730ca7d04a91 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 28 Apr 2019 17:55:39 +0000 Subject: [PATCH] Split out fastcgi_parse --- fastcgi_conn.cpp | 61 +++++++----------------------------------------- fastcgi_parse.h | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 fastcgi_parse.h diff --git a/fastcgi_conn.cpp b/fastcgi_conn.cpp index a9569ee..3e9484c 100644 --- a/fastcgi_conn.cpp +++ b/fastcgi_conn.cpp @@ -4,58 +4,13 @@ #include "fastcgi_conn.h" +#include "fastcgi_parse.h" #include "fastcgi_request.h" -namespace { - -struct fcgi_header { - uint8_t version; - uint8_t type; - private: - uint16_t request_id_; // network byte order - uint16_t content_length_; // network byte order - public: - uint8_t padding_length = 0; - uint8_t reserved = 0; - - uint16_t RequestId() const { return ntohs(request_id_); } - uint16_t ContentLength() const { return ntohs(content_length_); } - - void SetRequestId(uint16_t request_id) { request_id_ = htons(request_id); } - void SetContentLength(uint16_t content_length) { content_length_ = htons(content_length); } -}; - -struct fcgi_begin_request { - private: - uint16_t role_; // network byte order - public: - uint8_t flags; - uint8_t reserved[5]; - - uint16_t Role() const { return ntohs(role_); } -}; - -struct fcgi_end_request { - uint32_t app_status = htonl(0); // network byte order - uint8_t protocol_status; - uint8_t reserved[3] = {}; -}; - -struct fcgi_param_header { - uint8_t key_length; - uint8_t value_length; -}; - -constexpr auto fcgi_max_content_len = 65535; -constexpr auto fcgi_max_padding_len = 255; -constexpr auto fcgi_max_record_len = sizeof(fcgi_header) + fcgi_max_content_len + fcgi_max_padding_len; - -} // namespace - FastCGIConn::FastCGIConn(int sock, const sockaddr_in6& client_addr, const std::function)>& callback) : sock_(sock), callback_(callback), - buf_(sock, fcgi_max_record_len) { + buf_(sock, fastcgi_max_record_len) { char client_addr_str[INET6_ADDRSTRLEN]; PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str))); @@ -71,7 +26,7 @@ void FastCGIConn::WriteBlock(uint8_t type, uint16_t request_id, const std::vecto std::vector out_vecs; out_vecs.reserve(vecs.size() + 1); - fcgi_header header; + FastCGIHeader header; header.version = 1; header.type = type; header.SetRequestId(request_id); @@ -97,7 +52,7 @@ void FastCGIConn::WriteOutput(uint16_t request_id, const std::vector& vec } void FastCGIConn::WriteEnd(uint16_t request_id) { - fcgi_end_request end; + FastCGIEndRequest end; std::vector vecs; vecs.push_back(iovec{ @@ -109,7 +64,7 @@ void FastCGIConn::WriteEnd(uint16_t request_id) { void FastCGIConn::Serve() { while (true) { - const auto *header = buf_.ReadObj(); + const auto *header = buf_.ReadObj(); if (!header) { LOG(INFO) << "readobj failed"; break; @@ -123,8 +78,8 @@ void FastCGIConn::Serve() { switch (header->type) { case 1: { - CHECK_EQ(header->ContentLength(), sizeof(fcgi_begin_request)); - const auto *begin_request = CHECK_NOTNULL(buf_.ReadObj()); + CHECK_EQ(header->ContentLength(), sizeof(FastCGIBeginRequest)); + const auto *begin_request = CHECK_NOTNULL(buf_.ReadObj()); CHECK_EQ(begin_request->Role(), 1); request_.reset(new FastCGIRequest(header->RequestId(), this)); @@ -135,7 +90,7 @@ void FastCGIConn::Serve() { { ConstBuffer param_buf(buf_.Read(header->ContentLength()), header->ContentLength()); while (param_buf.ReadMaxLen() > 0) { - const auto *param_header = param_buf.ReadObj(); + const auto *param_header = param_buf.ReadObj(); std::string_view key(param_buf.Read(param_header->key_length), param_header->key_length); std::string_view value(param_buf.Read(param_header->value_length), param_header->value_length); request_->AddParam(key, value); diff --git a/fastcgi_parse.h b/fastcgi_parse.h new file mode 100644 index 0000000..43c7b92 --- /dev/null +++ b/fastcgi_parse.h @@ -0,0 +1,43 @@ +#pragma once + +struct FastCGIHeader { + uint8_t version; + uint8_t type; + private: + uint16_t request_id_; // network byte order + uint16_t content_length_; // network byte order + public: + uint8_t padding_length = 0; + uint8_t reserved = 0; + + uint16_t RequestId() const { return ntohs(request_id_); } + uint16_t ContentLength() const { return ntohs(content_length_); } + + void SetRequestId(uint16_t request_id) { request_id_ = htons(request_id); } + void SetContentLength(uint16_t content_length) { content_length_ = htons(content_length); } +}; + +struct FastCGIBeginRequest { + private: + uint16_t role_; // network byte order + public: + uint8_t flags; + uint8_t reserved[5]; + + uint16_t Role() const { return ntohs(role_); } +}; + +struct FastCGIEndRequest { + uint32_t app_status = htonl(0); // network byte order + uint8_t protocol_status; + uint8_t reserved[3] = {}; +}; + +struct FastCGIParamHeader { + uint8_t key_length; + uint8_t value_length; +}; + +constexpr auto fastcgi_max_content_len = 65535; +constexpr auto fastcgi_max_padding_len = 255; +constexpr auto fastcgi_max_record_len = sizeof(FastCGIHeader) + fastcgi_max_content_len + fastcgi_max_padding_len;