Compare commits
5 Commits
ec99454756
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe61f0dd20 | ||
|
|
f9988beb88 | ||
|
|
690c9ab8bd | ||
|
|
a39ef8e25d | ||
|
|
0ba446bacb |
151
.clang-format
Normal file
151
.clang-format
Normal file
@@ -0,0 +1,151 @@
|
||||
---
|
||||
Language: Cpp
|
||||
# BasedOnStyle: Google
|
||||
AccessModifierOffset: -1
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: true
|
||||
AlignTrailingComments: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
BraceWrapping:
|
||||
AfterClass: false
|
||||
AfterControlStatement: false
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeBraces: Attach
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: true
|
||||
DisableFormat: false
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories:
|
||||
- Regex: '^<ext/.*\.h>'
|
||||
Priority: 2
|
||||
- Regex: '^<.*\.h>'
|
||||
Priority: 1
|
||||
- Regex: '^<.*'
|
||||
Priority: 2
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IndentCaseLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: false
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Never
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 1
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Left
|
||||
RawStringFormats:
|
||||
- Language: Cpp
|
||||
Delimiters:
|
||||
- cc
|
||||
- CC
|
||||
- cpp
|
||||
- Cpp
|
||||
- CPP
|
||||
- 'c++'
|
||||
- 'C++'
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
- Language: TextProto
|
||||
Delimiters:
|
||||
- pb
|
||||
- PB
|
||||
- proto
|
||||
- PROTO
|
||||
EnclosingFunctions:
|
||||
- EqualsProto
|
||||
- EquivToProto
|
||||
- PARSE_PARTIAL_TEXT_PROTO
|
||||
- PARSE_TEST_PROTO
|
||||
- PARSE_TEXT_PROTO
|
||||
- ParseTextOrDie
|
||||
- ParseTextProtoOrDie
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Auto
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TabWidth: 8
|
||||
UseTab: Never
|
||||
...
|
||||
|
||||
2
Makefile
2
Makefile
@@ -20,7 +20,7 @@ firecgi.o: $(objects) firebuf/firebuf.o fireusage/fireusage.o
|
||||
gold -z relro -z now -r --output=$@ $+
|
||||
|
||||
firecgi.so: $(objects) firebuf/firebuf.o fireusage/fireusage.o
|
||||
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS)
|
||||
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDLIBS)
|
||||
|
||||
example_simple: example_simple.o firecgi.o
|
||||
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -pie -o $@ $+ $(FIRE_LDLIBS)
|
||||
|
||||
@@ -9,15 +9,19 @@
|
||||
|
||||
namespace firecgi {
|
||||
|
||||
Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, int max_request_len)
|
||||
Connection::Connection(int sock, const sockaddr_in6 &client_addr,
|
||||
const std::function<void(Request *)> &callback,
|
||||
int max_request_len)
|
||||
: sock_(sock),
|
||||
callback_(callback),
|
||||
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)));
|
||||
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);
|
||||
LOG(INFO) << "new connection: [" << client_addr_str
|
||||
<< "]:" << ntohs(client_addr.sin6_port);
|
||||
}
|
||||
|
||||
Connection::~Connection() {
|
||||
@@ -56,10 +60,10 @@ int Connection::Read() {
|
||||
}
|
||||
|
||||
switch (header->type) {
|
||||
case 1:
|
||||
{
|
||||
case 1: {
|
||||
if (header->ContentLength() != sizeof(BeginRequest)) {
|
||||
LOG(ERROR) << "FCGI_BeginRequestBody is the wrong length: " << header->ContentLength();
|
||||
LOG(ERROR) << "FCGI_BeginRequestBody is the wrong length: "
|
||||
<< header->ContentLength();
|
||||
return sock_;
|
||||
}
|
||||
|
||||
@@ -71,17 +75,17 @@ int Connection::Read() {
|
||||
}
|
||||
|
||||
request_.NewRequest(header->RequestId());
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case 4:
|
||||
{
|
||||
case 4: {
|
||||
if (header->RequestId() != request_.RequestId()) {
|
||||
LOG(ERROR) << "out of order FCGI_PARAMS record, or client is multiplexing requests (which we don't support)";
|
||||
LOG(ERROR) << "out of order FCGI_PARAMS record, or client is "
|
||||
"multiplexing requests (which we don't support)";
|
||||
return sock_;
|
||||
}
|
||||
|
||||
firebuf::ConstBuffer param_buf(buf_.Read(header->ContentLength()), header->ContentLength());
|
||||
firebuf::ConstBuffer param_buf(buf_.Read(header->ContentLength()),
|
||||
header->ContentLength());
|
||||
while (param_buf.ReadMaxLen() > 0) {
|
||||
const auto *param_header = param_buf.ReadObj<ParamHeader>();
|
||||
if (!param_header) {
|
||||
@@ -105,13 +109,12 @@ int Connection::Read() {
|
||||
|
||||
request_.AddParam(key, value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case 5:
|
||||
{
|
||||
case 5: {
|
||||
if (header->RequestId() != request_.RequestId()) {
|
||||
LOG(ERROR) << "out of order FCGI_STDIN record, or client is multiplexing requests (which we don't support)";
|
||||
LOG(ERROR) << "out of order FCGI_STDIN record, or client is "
|
||||
"multiplexing requests (which we don't support)";
|
||||
return sock_;
|
||||
}
|
||||
|
||||
@@ -122,12 +125,13 @@ int Connection::Read() {
|
||||
buf_.Consume(); // discard data and invalidate pointers
|
||||
} else {
|
||||
if (!request_.GetBody().empty()) {
|
||||
LOG(ERROR) << "received multiple stdin records. have you set \"fastcgi_request_buffering on\"?";
|
||||
LOG(ERROR) << "received multiple stdin records. have you set "
|
||||
"\"fastcgi_request_buffering on\"?";
|
||||
}
|
||||
request_.SetBody({buf_.Read(header->ContentLength()), header->ContentLength()});
|
||||
request_.SetBody(
|
||||
{buf_.Read(header->ContentLength()), header->ContentLength()});
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
default:
|
||||
LOG(ERROR) << "unknown record type: " << header->type;
|
||||
@@ -144,8 +148,6 @@ int Connection::Read() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t Connection::Requests() const {
|
||||
return requests_;
|
||||
}
|
||||
uint64_t Connection::Requests() const { return requests_; }
|
||||
|
||||
} // namespace firecgi
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <sys/uio.h>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
@@ -13,7 +13,9 @@ namespace firecgi {
|
||||
|
||||
class Connection {
|
||||
public:
|
||||
Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, int max_request_len);
|
||||
Connection(int sock, const sockaddr_in6& client_addr,
|
||||
const std::function<void(Request*)>& callback,
|
||||
int max_request_len);
|
||||
~Connection();
|
||||
|
||||
[[nodiscard]] int Read();
|
||||
@@ -28,7 +30,6 @@ class Connection {
|
||||
Request request_;
|
||||
|
||||
uint64_t requests_ = 0;
|
||||
|
||||
};
|
||||
|
||||
} // namespace firecgi
|
||||
|
||||
@@ -7,7 +7,9 @@ int main(int argc, char* argv[]) {
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
|
||||
{
|
||||
firecgi::Connection conn(STDIN_FILENO, {}, [](firecgi::Request* req) { req->End(); }, {}, 16*1024);
|
||||
firecgi::Connection conn(
|
||||
STDIN_FILENO, {}, [](firecgi::Request* req) { req->End(); }, {},
|
||||
16 * 1024);
|
||||
static_cast<void>(conn.Read());
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,14 @@ int main(int argc, char *argv[]) {
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
|
||||
firecgi::Server server(FLAGS_port, [](firecgi::Request* request) {
|
||||
firecgi::Server server(
|
||||
FLAGS_port,
|
||||
[](firecgi::Request* request) {
|
||||
request->WriteHeader("Content-Type", "text/plain");
|
||||
request->WriteBody("Hello world");
|
||||
request->End();
|
||||
}, FLAGS_threads);
|
||||
},
|
||||
FLAGS_threads);
|
||||
server.RegisterSignalHandlers();
|
||||
server.Serve();
|
||||
|
||||
|
||||
2
firebuf
2
firebuf
Submodule firebuf updated: 2102147a2f...109a9be193
Submodule fireusage updated: 7972d253ab...37be853a8b
5
parse.h
5
parse.h
@@ -9,6 +9,7 @@ struct Header {
|
||||
|
||||
uint8_t version = 1;
|
||||
uint8_t type;
|
||||
|
||||
private:
|
||||
uint16_t request_id_; // network byte order
|
||||
uint16_t content_length_; // network byte order
|
||||
@@ -20,7 +21,9 @@ struct Header {
|
||||
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); }
|
||||
void SetContentLength(uint16_t content_length) {
|
||||
content_length_ = htons(content_length);
|
||||
}
|
||||
};
|
||||
|
||||
struct BeginRequest {
|
||||
|
||||
25
request.cc
25
request.cc
@@ -7,7 +7,8 @@
|
||||
namespace firecgi {
|
||||
namespace {
|
||||
|
||||
template<class T> void AppendVec(const T& obj, std::vector<iovec>* vec) {
|
||||
template <class T>
|
||||
void AppendVec(const T& obj, std::vector<iovec>* vec) {
|
||||
vec->push_back(iovec{
|
||||
.iov_base = (void*)(&obj),
|
||||
.iov_len = sizeof(obj),
|
||||
@@ -16,9 +17,7 @@ template<class T> void AppendVec(const T& obj, std::vector<iovec>* vec) {
|
||||
|
||||
} // namespace
|
||||
|
||||
Request::Request(Connection* conn)
|
||||
: conn_(conn),
|
||||
out_buf_(64*1024) {}
|
||||
Request::Request(Connection* conn) : conn_(conn), out_buf_(64 * 1024) {}
|
||||
|
||||
Request::~Request() {
|
||||
if (on_close_) {
|
||||
@@ -39,17 +38,14 @@ void Request::NewRequest(uint16_t request_id) {
|
||||
body_written_ = false;
|
||||
}
|
||||
|
||||
uint16_t Request::RequestId() const {
|
||||
return request_id_;
|
||||
}
|
||||
uint16_t Request::RequestId() const { return request_id_; }
|
||||
|
||||
void Request::AddParam(const std::string_view& key, const std::string_view& value) {
|
||||
void Request::AddParam(const std::string_view& key,
|
||||
const std::string_view& value) {
|
||||
params_.try_emplace(key, value);
|
||||
}
|
||||
|
||||
void Request::SetBody(const std::string_view& body) {
|
||||
body_ = body;
|
||||
}
|
||||
void Request::SetBody(const std::string_view& body) { body_ = body; }
|
||||
|
||||
const std::string_view& Request::GetParam(const std::string_view& key) const {
|
||||
auto iter = params_.find(key);
|
||||
@@ -60,15 +56,14 @@ const std::string_view& Request::GetParam(const std::string_view& key) const {
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
const std::string_view& Request::GetBody() const {
|
||||
return body_;
|
||||
}
|
||||
const std::string_view& Request::GetBody() const { return body_; }
|
||||
|
||||
void Request::OnClose(const std::function<void()>& on_close) {
|
||||
on_close_ = on_close;
|
||||
}
|
||||
|
||||
void Request::WriteHeader(const std::string_view& name, const std::string_view& value) {
|
||||
void Request::WriteHeader(const std::string_view& name,
|
||||
const std::string_view& value) {
|
||||
std::lock_guard<std::recursive_mutex> l(output_mu_);
|
||||
|
||||
CHECK(!body_written_);
|
||||
|
||||
23
server.cc
23
server.cc
@@ -1,5 +1,4 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <iomanip>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <signal.h>
|
||||
@@ -7,16 +6,18 @@
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/socket.h>
|
||||
#include <iomanip>
|
||||
#include <thread>
|
||||
|
||||
#include "fireusage/usage.h"
|
||||
|
||||
#include "server.h"
|
||||
#include "connection.h"
|
||||
#include "server.h"
|
||||
|
||||
namespace firecgi {
|
||||
|
||||
Server::Server(int port, const std::function<void(Request*)>& callback, int threads, int max_request_len)
|
||||
Server::Server(int port, const std::function<void(Request*)>& callback,
|
||||
int threads, int max_request_len)
|
||||
: port_(port),
|
||||
callback_(callback),
|
||||
threads_(threads),
|
||||
@@ -28,6 +29,8 @@ Server::Server(int port, const std::function<void(Request*)>& callback, int thre
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
|
||||
Server::~Server() { PCHECK(close(close_fd_) == 0); }
|
||||
|
||||
void Server::Serve() {
|
||||
std::vector<std::thread> threads;
|
||||
for (int i = 0; i < threads_ - 1; ++i) {
|
||||
@@ -112,7 +115,7 @@ void Server::ServeInt() {
|
||||
usage_tracker.Stop();
|
||||
PCHECK(close(listen_sock) == 0);
|
||||
PCHECK(close(epoll_fd) == 0);
|
||||
usage_tracker.Log();
|
||||
usage_tracker.Log("requests");
|
||||
return;
|
||||
} else {
|
||||
auto conn = static_cast<Connection*>(events[i].data.ptr);
|
||||
@@ -132,14 +135,17 @@ Connection* Server::NewConn(int listen_sock, int epoll_fd) {
|
||||
sockaddr_in6 client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
|
||||
auto client_sock = accept(listen_sock, (sockaddr*) &client_addr, &client_addr_len);
|
||||
auto client_sock =
|
||||
accept(listen_sock, (sockaddr*)&client_addr, &client_addr_len);
|
||||
PCHECK(client_sock >= 0) << "accept()";
|
||||
CHECK_EQ(client_addr.sin6_family, AF_INET6);
|
||||
|
||||
int flags = 1;
|
||||
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
|
||||
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) ==
|
||||
0);
|
||||
|
||||
auto *conn = new Connection(client_sock, client_addr, callback_, max_request_len_);
|
||||
auto* conn =
|
||||
new Connection(client_sock, client_addr, callback_, max_request_len_);
|
||||
{
|
||||
struct epoll_event ev {
|
||||
.events = EPOLLIN,
|
||||
@@ -159,7 +165,8 @@ int Server::NewListenSock() {
|
||||
|
||||
{
|
||||
int optval = 1;
|
||||
PCHECK(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)) == 0);
|
||||
PCHECK(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval,
|
||||
sizeof(optval)) == 0);
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
7
server.h
7
server.h
@@ -10,7 +10,10 @@ namespace firecgi {
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server(int port, const std::function<void(Request*)>& callback, int threads=1, int max_request_len=(16*1024));
|
||||
Server(int port, const std::function<void(Request*)>& callback,
|
||||
int threads = 1, int max_request_len = (16 * 1024));
|
||||
~Server();
|
||||
|
||||
void Serve();
|
||||
void Shutdown();
|
||||
void RegisterSignalHandlers();
|
||||
@@ -28,4 +31,4 @@ class Server {
|
||||
int close_fd_;
|
||||
};
|
||||
|
||||
} // firecgi
|
||||
} // namespace firecgi
|
||||
|
||||
Reference in New Issue
Block a user