#ifndef BELL_BASE_HTTP_SERV #define BELL_BASE_HTTP_SERV #include #include #include #include #include namespace bell { class ResponseReader { public: ResponseReader(){}; virtual ~ResponseReader() = default; virtual size_t getTotalSize() = 0; virtual size_t read(char *buffer, size_t size) = 0; }; class FileResponseReader : public ResponseReader { public: FILE *file; size_t fileSize; FileResponseReader(std::string fileName) { file = fopen(fileName.c_str(), "r"); fseek(file, 0, SEEK_END); // seek to end of file fileSize = ftell(file); // get current file pointer fseek(file, 0, SEEK_SET); // seek back to beginning of file }; ~FileResponseReader() { fclose(file); }; size_t read(char *buffer, size_t size) { return fread(buffer, 1, size, file); } size_t getTotalSize() { return fileSize; } }; enum class RequestType { GET, POST }; struct HTTPRequest { std::map urlParams; std::map queryParams; std::string body; int handlerId; int connection; }; struct HTTPResponse { int connectionFd; int status; bool useGzip = false; std::string body; std::string contentType; std::unique_ptr responseReader; }; typedef std::function httpHandler; struct HTTPRoute { RequestType requestType; httpHandler handler; }; struct HTTPConnection { std::vector buffer; std::string currentLine = ""; int contentLength = 0; bool isReadingBody = false; std::string httpMethod; bool toBeClosed = false; bool isEventConnection = false; }; class BaseHTTPServer { public: BaseHTTPServer() {}; virtual ~BaseHTTPServer() = default; /** * Should contain server's bind port */ int serverPort; /** * Called when handler is being registered on the http server * * @param requestType GET or POST * @param endpoint registering under * httpHandler lambda to be called when given endpoint gets executed */ virtual void registerHandler(RequestType requestType, const std::string & endpoint, httpHandler) = 0; /** * Writes given response to a fd */ virtual void respond(const HTTPResponse &) = 0; }; } // namespace bell #endif