just some random thoguhts, doesn't work

This commit is contained in:
Blue 2023-04-17 18:07:29 -03:00
parent 6367dbffe8
commit e0a17115f9
Signed by: blue
GPG Key ID: 9B203B252A63EE38
5 changed files with 133 additions and 6 deletions

View File

@ -1,10 +1,12 @@
set(SOURCE_FILES set(SOURCE_FILES
main.cpp main.cpp
server.cpp server.cpp
response.cpp
) )
set(HEADER_FILES set(HEADER_FILES
server.h server.h
response.h
) )
target_sources(birdbadge PRIVATE ${SOURCE_FILES}) target_sources(birdbadge PRIVATE ${SOURCE_FILES})

42
src/response.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "response.h"
constexpr static const char* pathDelimiter("/");
Response::Response(const std::string_view& path):
status(Status::ok),
body()
{
std::vector<std::string_view> split = splitPath(path);
selfIndentification(split);
}
void Response::selfIndentification(const std::vector<std::string_view>& path) {
if (path.size() == 0) {
status = Status::ok;
}
}
void Response::writeHeader(std::ostream& out) const {
}
void Response::writeBody(std::ostream& out) const {
}
std::vector<std::string_view> Response::splitPath(std::string_view path) {
std::vector<std::string_view> output;
std::string_view::size_type first = 0;
while (first < path.size()) {
const std::string_view::size_type second = path.find_first_of(pathDelimiter, first);
if (first != second)
output.emplace_back(path.substr(first, second - first));
if (second == std::string_view::npos)
break;
first = second + 1;
}
return output;
}

28
src/response.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <string_view>
#include <vector>
class Response {
public:
enum Status {
ok,
notFound,
error
};
Response(const std::string_view& path);
void writeHeader(std::ostream& out) const;
void writeBody(std::ostream& out) const;
private:
void selfIndentification(const std::vector<std::string_view>& path);
static std::vector<std::string_view> splitPath(std::string_view path);
private:
Status status;
std::string body;
};

View File

@ -11,20 +11,28 @@ constexpr static const char* DOCUMENT_ROOT("DOCUMENT_ROOT");
constexpr static const char* SCRIPT_NAME("SCRIPT_NAME"); constexpr static const char* SCRIPT_NAME("SCRIPT_NAME");
constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME"); constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
constexpr static const char* SERVER_NAME("SERVER_NAME");
constexpr static const char* status405("Status: 405 Method Not Allowed"); constexpr static const char* status405("Status: 405 Method Not Allowed");
constexpr static const char* status500("Status: 500 Internal Error");
constexpr static const char* status200("Status: 200 OK");
constexpr static const char* contentTypeHtml("Content-type: text/html"); constexpr static const char* contentTypeHtml("Content-type: text/html");
constexpr static const char* headerEnd("\n\n"); constexpr static const char* headerEnd("\n\n");
constexpr static const char* newLine("\n");
constexpr static const char* svgHeader("image/svg+xml;charset=utf-8");
Server::Server(): Server::Server():
requestCount(0) requestCount(0),
serverName(std::nullopt)
{} {}
Server::~Server() { Server::~Server() {
} }
void Server::handleRequest(FCGX_Request& request) { void Server::handleRequest(FCGX_Request& request) {
++requestCount; accountRequest(request);
fcgi_streambuf cin_fcgi_streambuf(request.in); fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out); fcgi_streambuf cout_fcgi_streambuf(request.out);
@ -39,13 +47,12 @@ void Server::handleRequest(FCGX_Request& request) {
return; return;
} }
writeHeader(os);
try { try {
printEnv(os, request); std::string_view sPath = getPath(request);
Response response(sPath);
os << requestCount;
} catch (const std::exception e) { } catch (const std::exception e) {
os << contentTypeHtml;
os << headerEnd;
os << e.what(); os << e.what();
} }
} }
@ -57,3 +64,39 @@ void Server::printEnv(std::ostream& out, FCGX_Request& request) {
} }
} }
void Server::writeHeader(std::ostream& out) {
out << status200 << newLine;
out << contentTypeHtml;
out << headerEnd;
}
void Server::accountRequest(const FCGX_Request& request) {
++requestCount;
if (!serverName.has_value()) {
try {
serverName = FCGX_GetParam(SERVER_NAME, request.envp);
std::cout << "received server name " << serverName.value() << std::endl;
} catch (...) {
std::cout << "couldn't parse " << std::endl;
}
}
}
std::string_view Server::getPath(const FCGX_Request& request) {
std::string_view path;
if (serverName.has_value()) {
const std::string& sn = serverName.value();
std::string_view scriptFileName(FCGX_GetParam(SCRIPT_FILENAME, request.envp));
std::string::size_type snLocation = scriptFileName.find(sn);
if (snLocation != std::string::npos) {
if (snLocation + sn.size() < scriptFileName.size()) {
path = scriptFileName.substr(snLocation + sn.size() + 1);
}
}
}
return path;
}

View File

@ -2,10 +2,15 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <optional>
#include <string_view>
#include <vector>
#include <fcgiapp.h> #include <fcgiapp.h>
#include <fcgio.h> #include <fcgio.h>
#include "response.h"
class Server { class Server {
public: public:
Server(); Server();
@ -14,8 +19,15 @@ public:
void handleRequest(FCGX_Request& request); void handleRequest(FCGX_Request& request);
private: private:
void router(const std::vector<std::string_view>& path);
void printEnv(std::ostream& out, FCGX_Request& request); void printEnv(std::ostream& out, FCGX_Request& request);
void writeHeader(std::ostream& out);
void accountRequest(const FCGX_Request& request);
std::string_view getPath(const FCGX_Request& request);
private: private:
uint64_t requestCount; uint64_t requestCount;
std::optional<std::string> serverName;
}; };