2019-04-28 00:17:32 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
2019-05-04 19:00:16 -07:00
|
|
|
#include <netinet/tcp.h>
|
2019-04-29 00:11:07 +00:00
|
|
|
#include <signal.h>
|
2019-05-04 07:21:52 +00:00
|
|
|
#include <sys/epoll.h>
|
2019-04-28 00:17:32 +00:00
|
|
|
#include <sys/socket.h>
|
2019-05-04 12:16:31 -07:00
|
|
|
#include <thread>
|
2019-04-28 00:17:32 +00:00
|
|
|
|
|
|
|
|
#include "fastcgi.h"
|
|
|
|
|
#include "fastcgi_conn.h"
|
|
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
FastCGIServer::FastCGIServer(int port, const std::function<void(std::unique_ptr<FastCGIRequest>)>& callback, int threads, const std::unordered_set<std::string_view>& headers)
|
|
|
|
|
: port_(port),
|
|
|
|
|
callback_(callback),
|
|
|
|
|
threads_(threads),
|
2019-04-30 02:07:48 +00:00
|
|
|
headers_(headers) {
|
2019-05-04 12:16:31 -07:00
|
|
|
LOG(INFO) << "listening on [::1]:" << port_;
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-04-29 00:11:07 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2019-05-04 12:16:31 -07:00
|
|
|
}
|
2019-04-29 00:11:07 +00:00
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
void FastCGIServer::Serve() {
|
|
|
|
|
std::vector<std::thread> threads;
|
|
|
|
|
for (int i = 0; i < threads_ - 1; ++i) {
|
|
|
|
|
threads.emplace_back([this]() { ServeInt(); });
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
2019-05-04 12:16:31 -07:00
|
|
|
ServeInt();
|
|
|
|
|
}
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
void FastCGIServer::ServeInt() {
|
|
|
|
|
auto epoll_fd = epoll_create1(0);
|
|
|
|
|
PCHECK(epoll_fd >= 0) << "epoll_create()";
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
auto listen_sock = NewListenSock();
|
2019-05-04 07:21:52 +00:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
struct epoll_event ev{
|
|
|
|
|
.events = EPOLLIN,
|
|
|
|
|
.data = {
|
|
|
|
|
.ptr = nullptr,
|
|
|
|
|
},
|
|
|
|
|
};
|
2019-05-04 12:16:31 -07:00
|
|
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_sock, &ev) == 0);
|
2019-05-04 07:21:52 +00:00
|
|
|
}
|
2019-04-28 00:17:32 +00:00
|
|
|
|
|
|
|
|
while (true) {
|
2019-05-04 07:39:12 +00:00
|
|
|
constexpr auto max_events = 256;
|
|
|
|
|
struct epoll_event events[max_events];
|
2019-05-04 12:16:31 -07:00
|
|
|
auto num_fd = epoll_wait(epoll_fd, events, max_events, -1);
|
2019-05-04 07:21:52 +00:00
|
|
|
if (num_fd == -1 && errno == EINTR) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
PCHECK(num_fd > 0) << "epoll_wait()";
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-05-04 07:21:52 +00:00
|
|
|
for (auto i = 0; i < num_fd; ++i) {
|
|
|
|
|
if (events[i].data.ptr == nullptr) {
|
2019-05-04 12:16:31 -07:00
|
|
|
NewConn(listen_sock, epoll_fd);
|
2019-05-04 07:21:52 +00:00
|
|
|
} else {
|
|
|
|
|
auto conn = static_cast<FastCGIConn*>(events[i].data.ptr);
|
2019-05-04 07:57:55 +00:00
|
|
|
auto fd = conn->Read();
|
|
|
|
|
if (fd != -1) {
|
2019-05-04 12:16:31 -07:00
|
|
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == 0);
|
2019-05-04 07:21:52 +00:00
|
|
|
delete conn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
void FastCGIServer::NewConn(int listen_sock, int epoll_fd) {
|
2019-05-04 07:21:52 +00:00
|
|
|
sockaddr_in6 client_addr;
|
|
|
|
|
socklen_t client_addr_len = sizeof(client_addr);
|
|
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
auto client_sock = accept(listen_sock, (sockaddr*) &client_addr, &client_addr_len);
|
2019-05-04 07:21:52 +00:00
|
|
|
PCHECK(client_sock >= 0) << "accept()";
|
|
|
|
|
CHECK_EQ(client_addr.sin6_family, AF_INET6);
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-05-04 19:00:16 -07:00
|
|
|
int flags = 1;
|
|
|
|
|
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
|
|
|
|
|
|
2019-05-04 07:21:52 +00:00
|
|
|
{
|
2019-04-30 02:07:48 +00:00
|
|
|
auto *conn = new FastCGIConn(client_sock, client_addr, callback_, headers_);
|
2019-05-04 07:21:52 +00:00
|
|
|
struct epoll_event ev{
|
|
|
|
|
.events = EPOLLIN,
|
|
|
|
|
.data = {
|
|
|
|
|
.ptr = conn,
|
|
|
|
|
},
|
|
|
|
|
};
|
2019-05-04 12:16:31 -07:00
|
|
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_sock, &ev) == 0);
|
2019-04-28 00:17:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
int FastCGIServer::NewListenSock() {
|
|
|
|
|
auto sock = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
PCHECK(sock >= 0) << "socket()";
|
2019-04-28 00:17:32 +00:00
|
|
|
|
2019-05-04 12:16:31 -07:00
|
|
|
{
|
|
|
|
|
int optval = 1;
|
|
|
|
|
PCHECK(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
sockaddr_in6 bind_addr = {
|
|
|
|
|
.sin6_family = AF_INET6,
|
|
|
|
|
.sin6_port = htons(port_),
|
|
|
|
|
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
|
|
|
|
|
};
|
|
|
|
|
PCHECK(bind(sock, (sockaddr*) &bind_addr, sizeof(bind_addr)) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PCHECK(listen(sock, 128) == 0);
|
|
|
|
|
return sock;
|
|
|
|
|
}
|