common.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef KBF_HTTP_H
  2. #define KBF_HTTP_H
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <esp_http_server.h>
  7. using std::string;
  8. using std::vector;
  9. using std::map;
  10. /**
  11. * @brief HTTP functions
  12. */
  13. namespace kbf::http {
  14. /** @brief Tag used for logging. */
  15. static constexpr const char *TAG = "kbf::http";
  16. /**
  17. * @brief Supported HTTP methods.
  18. *
  19. * Mostly used for syntax consistency; values match that of httpd_method_t.
  20. */
  21. enum Method {
  22. GET = 1,
  23. POST = 3,
  24. OPTIONS = 6,
  25. };
  26. /**
  27. * @brief Base class for HTTP Request and Response objects.
  28. */
  29. class HTTPObject {
  30. public:
  31. /**
  32. * @brief HTTP headers
  33. *
  34. * @warning Parsing headers from the incoming response is not supported in kbf::http::Client (yet).
  35. *
  36. * @note Only the headers listed in acceptedHeaders will be parsed automatically for incoming
  37. * requests. For parsing custom headers, use readHeader().
  38. */
  39. map<string, string> headers;
  40. /** @brief body */
  41. string body;
  42. /**
  43. * @brief POST data
  44. */
  45. map<string, string> postData;
  46. protected:
  47. HTTPObject() = default;
  48. explicit HTTPObject(string body);
  49. static void parseToMap(map<string, string> &target, const string &buffer);
  50. };
  51. /**
  52. * @brief Handles a HTTP request.
  53. */
  54. class Request : public HTTPObject {
  55. public:
  56. /**
  57. * @brief Creates instance from a httpd_req_t object.
  58. *
  59. * @param httpdRequest
  60. */
  61. explicit Request(httpd_req_t *httpdRequest);
  62. /**
  63. * @brief Vector of headers automatically parsed for incoming requests.
  64. */
  65. static inline const vector<string> acceptedHeaders = { // NOLINT(cert-err58-cpp)
  66. "Host",
  67. "User-Agent",
  68. "Content-Type",
  69. };
  70. /** @brief number of retry attempts in case of a socket read timeout */
  71. static inline const int MAX_READ_RETRY = 3;
  72. /**
  73. * @brief Parse a header from the httpd_req_t object. The key-value pair will also be added to the #headers map.
  74. *
  75. * @warning Can only be used on incoming requests.
  76. *
  77. * @param header
  78. * @return header string
  79. */
  80. string readHeader(const string &header);
  81. /** @brief HTTP method */
  82. Method method;
  83. /** @brief request URI */
  84. string uri;
  85. /** @brief query parameters */
  86. map<string, string> query{};
  87. private:
  88. httpd_req_t *httpdRequest;
  89. void readQuery();
  90. void readBody();
  91. };
  92. /**
  93. * @brief Stores a HTTP response.
  94. */
  95. class Response : public HTTPObject {
  96. public:
  97. /**
  98. * @brief Default constructor.
  99. *
  100. * @param body response body; default is ""
  101. * @param status HTTP status code; default is 200
  102. * @param contentType value of Content-Type header; default is "text/html"
  103. */
  104. explicit Response(string body = "", int status = 200, string contentType = "text/html");
  105. /** @brief HTTP status code */
  106. int status; // TODO enumerate me
  107. /** @brief HTTP status text */
  108. string statusText = "OK";
  109. /** @brief value of Content-Type header */
  110. string contentType;
  111. };
  112. }
  113. #endif //KBF_HTTP_H