2019-04-28 00:17:32 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
2019-05-04 16:59:25 +00:00
|
|
|
#include <netinet/tcp.h>
|
2019-04-28 06:47:30 +00:00
|
|
|
#include <sys/uio.h>
|
2019-04-28 00:17:32 +00:00
|
|
|
|
|
|
|
|
#include "fastcgi_conn.h"
|
|
|
|
|
|
2019-04-28 17:55:39 +00:00
|
|
|
#include "fastcgi_parse.h"
|
2019-04-28 06:47:30 +00:00
|
|
|
#include "fastcgi_request.h"
|
|
|
|
|
|
2019-04-30 02:07:48 +00:00
|
|
|
FastCGIConn::FastCGIConn(int sock, const sockaddr_in6& client_addr, const std::function<void(std::unique_ptr<FastCGIRequest>)>& callback, const std::unordered_set<std::string_view>& headers)
|
2019-04-28 00:17:32 +00:00
|
|
|
: sock_(sock),
|
2019-04-28 06:47:30 +00:00
|
|
|
callback_(callback),
|
2019-04-30 02:07:48 +00:00
|
|
|
headers_(headers),
|
2019-04-28 17:55:39 +00:00
|
|
|
buf_(sock, fastcgi_max_record_len) {
|
2019-04-28 00:17:32 +00:00
|
|
|
char client_addr_str[INET6_ADDRSTRLEN];
|
|
|
|
|
PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str)));
|
|
|
|
|
|
|
|
|
|
LOG(INFO) << "new connection: [" << client_addr_str << "]:" << ntohs(client_addr.sin6_port);
|
2019-05-04 16:59:25 +00:00
|
|
|
|
|
|
|
|
int flags = 1;
|
|
|
|
|
PCHECK(setsockopt(sock_, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FastCGIConn::~FastCGIConn() {
|
|
|
|
|
PCHECK(close(sock_) == 0);
|
2019-04-28 20:40:00 +00:00
|
|
|
LOG(INFO) << "connection closed (handled " << requests_ << " requests)";
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 00:11:07 +00:00
|
|
|
bool FastCGIConn::Write(const std::vector<iovec>& vecs) {
|
2019-04-28 18:19:32 +00:00
|
|
|
size_t total_size = 0;
|
|
|
|
|
for (const auto& vec : vecs) {
|
|
|
|
|
total_size += vec.iov_len;
|
2019-04-28 06:47:30 +00:00
|
|
|
}
|
2019-04-29 00:11:07 +00:00
|
|
|
return writev(sock_, vecs.data(), vecs.size()) == total_size;
|
2019-04-28 06:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-04 07:57:55 +00:00
|
|
|
int FastCGIConn::Read() {
|
2019-05-04 07:21:52 +00:00
|
|
|
if (!buf_.Refill()) {
|
2019-05-04 07:57:55 +00:00
|
|
|
return sock_;
|
2019-05-04 07:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-28 00:17:32 +00:00
|
|
|
while (true) {
|
2019-05-04 07:21:52 +00:00
|
|
|
buf_.ResetRead();
|
|
|
|
|
|
2019-04-28 17:55:39 +00:00
|
|
|
const auto *header = buf_.ReadObj<FastCGIHeader>();
|
2019-04-28 00:17:32 +00:00
|
|
|
if (!header) {
|
2019-04-28 17:50:44 +00:00
|
|
|
break;
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHECK_EQ(header->version, 1);
|
2019-05-04 07:21:52 +00:00
|
|
|
if (buf_.ReadMaxLen() < header->ContentLength()) {
|
|
|
|
|
break;
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (header->type) {
|
|
|
|
|
case 1:
|
|
|
|
|
{
|
2019-04-28 17:55:39 +00:00
|
|
|
CHECK_EQ(header->ContentLength(), sizeof(FastCGIBeginRequest));
|
|
|
|
|
const auto *begin_request = CHECK_NOTNULL(buf_.ReadObj<FastCGIBeginRequest>());
|
2019-04-28 06:47:30 +00:00
|
|
|
CHECK_EQ(begin_request->Role(), 1);
|
|
|
|
|
|
|
|
|
|
request_.reset(new FastCGIRequest(header->RequestId(), this));
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
{
|
2019-04-28 20:40:00 +00:00
|
|
|
CHECK_EQ(header->RequestId(), request_->RequestId());
|
2019-04-28 06:47:30 +00:00
|
|
|
ConstBuffer param_buf(buf_.Read(header->ContentLength()), header->ContentLength());
|
|
|
|
|
while (param_buf.ReadMaxLen() > 0) {
|
2019-04-28 17:55:39 +00:00
|
|
|
const auto *param_header = param_buf.ReadObj<FastCGIParamHeader>();
|
2019-04-28 06:47:30 +00:00
|
|
|
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);
|
2019-04-30 02:07:48 +00:00
|
|
|
if (headers_.find(key) != headers_.end()) {
|
|
|
|
|
request_->AddParam(key, value);
|
|
|
|
|
}
|
2019-04-28 06:47:30 +00:00
|
|
|
}
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
break;
|
2019-04-28 06:47:30 +00:00
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
|
{
|
2019-04-28 20:40:00 +00:00
|
|
|
CHECK_EQ(header->RequestId(), request_->RequestId());
|
2019-04-28 06:47:30 +00:00
|
|
|
if (header->ContentLength() == 0) {
|
|
|
|
|
// Magic signal for completed request (mirrors the HTTP/1.1 protocol)
|
2019-04-28 20:40:00 +00:00
|
|
|
requests_++;
|
2019-04-28 06:47:30 +00:00
|
|
|
callback_(std::move(request_));
|
|
|
|
|
} else {
|
|
|
|
|
std::string_view in(buf_.Read(header->ContentLength()), header->ContentLength());
|
|
|
|
|
request_->AddIn(in);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 17:50:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
CHECK(false) << "unknown record type: " << header->type;
|
|
|
|
|
break;
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-28 17:50:44 +00:00
|
|
|
if (!buf_.Discard(header->padding_length)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-28 00:17:32 +00:00
|
|
|
|
|
|
|
|
buf_.Commit(); // we've acted on the bytes read so far
|
|
|
|
|
}
|
2019-04-28 17:50:44 +00:00
|
|
|
|
2019-05-04 07:21:52 +00:00
|
|
|
buf_.Consume();
|
2019-05-04 07:57:55 +00:00
|
|
|
return -1;
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|