123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #ifndef KBF_HTTP_H
- #define KBF_HTTP_H
- #include <string>
- #include <vector>
- #include <map>
- #include <esp_http_server.h>
- using std::string;
- using std::vector;
- using std::map;
- /**
- * @brief HTTP functions
- */
- namespace kbf::http {
- /** @brief Tag used for logging. */
- static constexpr const char *TAG = "kbf::http";
- /**
- * @brief Supported HTTP methods.
- *
- * Mostly used for syntax consistency; values match that of httpd_method_t.
- */
- enum Method {
- GET = 1,
- POST = 3,
- OPTIONS = 6,
- };
- /**
- * @brief Base class for HTTP Request and Response objects.
- */
- class HTTPObject {
- public:
- /**
- * @brief HTTP headers
- *
- * @warning Parsing headers from the incoming response is not supported in kbf::http::Client (yet).
- *
- * @note Only the headers listed in acceptedHeaders will be parsed automatically for incoming
- * requests. For parsing custom headers, use readHeader().
- */
- map<string, string> headers;
- /** @brief body */
- string body;
- /**
- * @brief POST data
- */
- map<string, string> postData;
- protected:
- HTTPObject() = default;
- explicit HTTPObject(string body);
- static void parseToMap(map<string, string> &target, const string &buffer);
- };
- /**
- * @brief Handles a HTTP request.
- */
- class Request : public HTTPObject {
- public:
- /**
- * @brief Creates instance from a httpd_req_t object.
- *
- * @param httpdRequest
- */
- explicit Request(httpd_req_t *httpdRequest);
- /**
- * @brief Vector of headers automatically parsed for incoming requests.
- */
- static inline const vector<string> acceptedHeaders = { // NOLINT(cert-err58-cpp)
- "Host",
- "User-Agent",
- "Content-Type",
- };
- /** @brief number of retry attempts in case of a socket read timeout */
- static inline const int MAX_READ_RETRY = 3;
- /**
- * @brief Parse a header from the httpd_req_t object. The key-value pair will also be added to the #headers map.
- *
- * @warning Can only be used on incoming requests.
- *
- * @param header
- * @return header string
- */
- string readHeader(const string &header);
- /** @brief HTTP method */
- Method method;
- /** @brief request URI */
- string uri;
- /** @brief query parameters */
- map<string, string> query{};
- private:
- httpd_req_t *httpdRequest;
- void readQuery();
- void readBody();
- };
- /**
- * @brief Stores a HTTP response.
- */
- class Response : public HTTPObject {
- public:
- /**
- * @brief Default constructor.
- *
- * @param body response body; default is ""
- * @param status HTTP status code; default is 200
- * @param contentType value of Content-Type header; default is "text/html"
- */
- explicit Response(string body = "", int status = 200, string contentType = "text/html");
- /** @brief HTTP status code */
- int status; // TODO enumerate me
- /** @brief HTTP status text */
- string statusText = "OK";
- /** @brief value of Content-Type header */
- string contentType;
- };
- }
- #endif //KBF_HTTP_H
|