BaseHTTPServer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef BELL_BASE_HTTP_SERV
  2. #define BELL_BASE_HTTP_SERV
  3. #include <cstdio>
  4. #include <string>
  5. #include <map>
  6. #include <memory>
  7. #include <functional>
  8. #include <vector>
  9. namespace bell {
  10. class ResponseReader {
  11. public:
  12. ResponseReader(){};
  13. virtual ~ResponseReader() = default;
  14. virtual size_t getTotalSize() = 0;
  15. virtual size_t read(char *buffer, size_t size) = 0;
  16. virtual void close() = 0;
  17. };
  18. class FileResponseReader : public ResponseReader {
  19. public:
  20. FILE *file;
  21. size_t fileSize;
  22. FileResponseReader(std::string fileName) {
  23. file = fopen(fileName.c_str(), "r");
  24. fseek(file, 0, SEEK_END); // seek to end of file
  25. fileSize = ftell(file); // get current file pointer
  26. fseek(file, 0, SEEK_SET); // seek back to beginning of file
  27. };
  28. ~FileResponseReader() { fclose(file); };
  29. size_t read(char *buffer, size_t size) {
  30. return fread(buffer, 1, size, file);
  31. }
  32. void close() {
  33. fclose(file);
  34. }
  35. size_t getTotalSize() { return fileSize; }
  36. };
  37. enum class RequestType { GET, POST };
  38. struct HTTPRequest {
  39. std::map<std::string, std::string> urlParams;
  40. std::map<std::string, std::string> queryParams;
  41. std::string body;
  42. std::string url;
  43. int handlerId;
  44. int connection;
  45. };
  46. struct HTTPResponse {
  47. int connectionFd;
  48. int status;
  49. bool useGzip = false;
  50. std::string body;
  51. std::string contentType;
  52. std::vector<std::string> extraHeaders = std::vector<std::string>();
  53. std::unique_ptr<ResponseReader> responseReader;
  54. };
  55. typedef std::function<void(HTTPRequest &)> httpHandler;
  56. struct HTTPRoute {
  57. RequestType requestType;
  58. httpHandler handler;
  59. };
  60. struct HTTPConnection {
  61. std::vector<uint8_t> buffer;
  62. std::string currentLine = "";
  63. int contentLength = 0;
  64. bool isReadingBody = false;
  65. std::string httpMethod;
  66. bool toBeClosed = false;
  67. bool isEventConnection = false;
  68. };
  69. class BaseHTTPServer {
  70. public:
  71. BaseHTTPServer() {};
  72. virtual ~BaseHTTPServer() = default;
  73. /**
  74. * Should contain server's bind port
  75. */
  76. int serverPort;
  77. /**
  78. * Called when handler is being registered on the http server
  79. *
  80. * @param requestType GET or POST
  81. * @param endpoint registering under
  82. * httpHandler lambda to be called when given endpoint gets executed
  83. */
  84. virtual void registerHandler(RequestType requestType, const std::string & endpoint,
  85. httpHandler) = 0;
  86. /**
  87. * Writes given response to a fd
  88. */
  89. virtual void respond(const HTTPResponse &) = 0;
  90. };
  91. } // namespace bell
  92. #endif