Remove the nested read loops in FastCGIConn, hide complexity inside StreamBuffer
This commit is contained in:
@@ -55,7 +55,7 @@ constexpr auto fcgi_max_record_len = sizeof(fcgi_header) + fcgi_max_content_len
|
||||
FastCGIConn::FastCGIConn(int sock, const sockaddr_in6& client_addr, const std::function<void(std::unique_ptr<FastCGIRequest>)>& callback)
|
||||
: sock_(sock),
|
||||
callback_(callback),
|
||||
buf_(fcgi_max_record_len) {
|
||||
buf_(sock, fcgi_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)));
|
||||
|
||||
@@ -67,22 +67,6 @@ FastCGIConn::~FastCGIConn() {
|
||||
LOG(INFO) << "connection closed";
|
||||
}
|
||||
|
||||
void FastCGIConn::Serve() {
|
||||
while (true) {
|
||||
auto read_len = read(sock_, buf_.WritePtr(), buf_.WriteMaxLen());
|
||||
PCHECK(read_len >= 0);
|
||||
if (read_len == 0) {
|
||||
LOG(INFO) << "peer closed connection";
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
buf_.Wrote(read_len);
|
||||
|
||||
ParseBuf();
|
||||
buf_.Consume(); // free buffer tail space for next read
|
||||
}
|
||||
}
|
||||
|
||||
void FastCGIConn::WriteBlock(uint8_t type, uint16_t request_id, const std::vector<iovec>& vecs) {
|
||||
std::vector<iovec> out_vecs;
|
||||
out_vecs.reserve(vecs.size() + 1);
|
||||
@@ -123,17 +107,16 @@ void FastCGIConn::WriteEnd(uint16_t request_id) {
|
||||
WriteBlock(3, request_id, vecs);
|
||||
}
|
||||
|
||||
void FastCGIConn::ParseBuf() {
|
||||
buf_.ResetRead();
|
||||
|
||||
void FastCGIConn::Serve() {
|
||||
while (true) {
|
||||
const auto *header = buf_.ReadObj<fcgi_header>();
|
||||
if (!header) {
|
||||
return;
|
||||
LOG(INFO) << "readobj failed";
|
||||
break;
|
||||
}
|
||||
|
||||
CHECK_EQ(header->version, 1);
|
||||
if (buf_.ReadMaxLen() < header->ContentLength() + header->padding_length) {
|
||||
if (!buf_.BufferAtLeast(header->ContentLength())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -170,10 +153,19 @@ void FastCGIConn::ParseBuf() {
|
||||
request_->AddIn(in);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
CHECK(false) << "unknown record type: " << header->type;
|
||||
break;
|
||||
}
|
||||
|
||||
CHECK(buf_.Discard(header->padding_length));
|
||||
if (!buf_.Discard(header->padding_length)) {
|
||||
break;
|
||||
}
|
||||
|
||||
buf_.Commit(); // we've acted on the bytes read so far
|
||||
}
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user