Relocate everything into modules
This commit is contained in:
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "firesse"]
|
||||
path = firesse
|
||||
url = git@github.com:firestuff/firesse.git
|
||||
35
Makefile
35
Makefile
@@ -1,35 +0,0 @@
|
||||
FCGI_CXX ?= clang++
|
||||
FCGI_CXXFLAGS ?= -O3 -std=gnu++2a -Wall -Werror
|
||||
FCGI_LDLIBS ?= -lgflags -lglog -lpthread
|
||||
|
||||
all: example_simple example_clock
|
||||
|
||||
objects = sse.o sse_stream.o fastcgi.o fastcgi_conn.o fastcgi_request.o fastcgi_parse.o stream_buffer.o buffer.o
|
||||
|
||||
example_simple: example_simple.o $(objects)
|
||||
$(FCGI_CXX) $(FCGI_CXXFLAGS) -o $@ $+ $(FCGI_LDLIBS)
|
||||
|
||||
example_clock: example_clock.o $(objects)
|
||||
$(FCGI_CXX) $(FCGI_CXXFLAGS) -o $@ $+ $(FCGI_LDLIBS)
|
||||
|
||||
%.o: %.cc *.h Makefile
|
||||
$(FCGI_CXX) $(FCGI_CXXFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
rm --force example_simple example_clock fastcgi_conn_afl *.o
|
||||
|
||||
afl:
|
||||
$(MAKE) clean
|
||||
FCGI_CXX=afl-g++ $(MAKE) afl_int
|
||||
|
||||
afl_int: fastcgi_conn_afl
|
||||
|
||||
fastcgi_conn_afl: fastcgi_conn_afl.o $(objects)
|
||||
$(FCGI_CXX) $(FCGI_CXXFLAGS) -o $@ $+ $(FCGI_LDLIBS)
|
||||
|
||||
test: test_fastcgi_conn
|
||||
|
||||
test_fastcgi_conn: fastcgi_conn_afl
|
||||
@echo "Running $$(ls afl_state/testcases | wc -l) tests"
|
||||
for FILE in afl_state/testcases/*; do ./fastcgi_conn_afl < $$FILE; done
|
||||
@printf '\033[0;32mALL TESTS PASSED\033[0m\n'
|
||||
@@ -1,25 +0,0 @@
|
||||
#include <gflags/gflags.h>
|
||||
#include <glog/logging.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "sse.h"
|
||||
|
||||
DEFINE_int32(port, 9000, "TCP port to bind");
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
|
||||
SSEServer server(FLAGS_port, [](std::unique_ptr<SSEStream> stream) {
|
||||
while (true) {
|
||||
timeval tv;
|
||||
PCHECK(gettimeofday(&tv, nullptr) == 0);
|
||||
uint64_t time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
||||
if (!stream->WriteEvent(std::to_string(time_ms), 0, "time")) {
|
||||
break;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
});
|
||||
server.Serve();
|
||||
}
|
||||
1
firesse
Submodule
1
firesse
Submodule
Submodule firesse added at ee2667bb1a
26
sse.cc
26
sse.cc
@@ -1,26 +0,0 @@
|
||||
#include "sse.h"
|
||||
|
||||
SSEServer::SSEServer(int port, const std::function<void(std::unique_ptr<SSEStream>)>& callback)
|
||||
: callback_(callback),
|
||||
fastcgi_server_(port,
|
||||
[this](std::unique_ptr<FastCGIRequest> request) { OnRequest(std::move(request)); },
|
||||
1,
|
||||
{"HTTP_ACCEPT"}) {}
|
||||
|
||||
void SSEServer::Serve() {
|
||||
fastcgi_server_.Serve();
|
||||
}
|
||||
|
||||
void SSEServer::OnRequest(std::unique_ptr<FastCGIRequest> request) {
|
||||
if (request->GetParam("HTTP_ACCEPT") != "text/event-stream") {
|
||||
LOG(WARNING) << "bad HTTP_ACCEPT: " << request->GetParam("HTTP_ACCEPT");
|
||||
request->WriteHeader("Status", "400 Bad Request");
|
||||
request->WriteHeader("Content-Type", "text-plain");
|
||||
request->WriteBody("No \"Accept: text/event-stream\" header found in request. Please call this endpoint using EventSource.");
|
||||
request->End();
|
||||
return;
|
||||
}
|
||||
request->WriteHeader("Content-Type", "text/event-stream");
|
||||
request->WriteBody("");
|
||||
callback_(std::make_unique<SSEStream>(std::move(request)));
|
||||
}
|
||||
16
sse.h
16
sse.h
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastcgi.h"
|
||||
#include "sse_stream.h"
|
||||
|
||||
class SSEServer {
|
||||
public:
|
||||
SSEServer(int port, const std::function<void(std::unique_ptr<SSEStream>)>& callback);
|
||||
void Serve();
|
||||
|
||||
private:
|
||||
void OnRequest(std::unique_ptr<FastCGIRequest> request);
|
||||
|
||||
std::function<void(std::unique_ptr<SSEStream>)> callback_;
|
||||
FastCGIServer fastcgi_server_;
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "sse_stream.h"
|
||||
|
||||
#include "fastcgi_request.h"
|
||||
|
||||
SSEStream::SSEStream(std::unique_ptr<FastCGIRequest> request)
|
||||
: request_(std::move(request)) {}
|
||||
|
||||
bool SSEStream::WriteEvent(const std::string& data, uint64_t id, const std::string& type) {
|
||||
if (id) {
|
||||
request_->WriteBody("id: ");
|
||||
request_->WriteBody(std::to_string(id));
|
||||
request_->WriteBody("\n");
|
||||
}
|
||||
if (!type.empty()) {
|
||||
request_->WriteBody("event: ");
|
||||
request_->WriteBody(type);
|
||||
request_->WriteBody("\n");
|
||||
}
|
||||
request_->WriteBody("data: ");
|
||||
request_->WriteBody(data);
|
||||
request_->WriteBody("\n\n");
|
||||
|
||||
return request_->Flush();
|
||||
}
|
||||
|
||||
bool SSEStream::End() {
|
||||
return request_->End();
|
||||
}
|
||||
16
sse_stream.h
16
sse_stream.h
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
class FastCGIRequest;
|
||||
|
||||
class SSEStream {
|
||||
public:
|
||||
SSEStream(std::unique_ptr<FastCGIRequest> request);
|
||||
|
||||
[[nodiscard]] bool WriteEvent(const std::string& data, uint64_t id=0, const std::string& type="");
|
||||
bool End();
|
||||
|
||||
private:
|
||||
std::unique_ptr<FastCGIRequest> request_;
|
||||
};
|
||||
Reference in New Issue
Block a user