server.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "kbf/http/server.h"
  2. #include <esp_log.h>
  3. #include <esp_https_server.h>
  4. #include <memory>
  5. #include "kbf/macros.h"
  6. using namespace kbf;
  7. http::Server &http::Server::route(http::Server::Route route) {
  8. ESP_LOGD(TAG, "adding route: %s", route.uri.c_str());
  9. routes.push_back(route);
  10. return *this;
  11. }
  12. http::Server &http::Server::start(int port) {
  13. ESP_LOGI(TAG, "start(%d)", port);
  14. if (running) {
  15. ESP_LOGE(TAG, "server already running");
  16. ABORT("fix me");
  17. }
  18. handle = nullptr;
  19. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  20. config.server_port = port;
  21. config.stack_size = 16384;
  22. CHECK(httpd_start(&handle, &config));
  23. registerUriHandlers();
  24. running = true;
  25. return *this;
  26. }
  27. http::Server &http::Server::startSSL(const unsigned char *cert_start, const unsigned char *cert_end,
  28. const unsigned char *key_start, const unsigned char *key_end,
  29. int port) {
  30. ESP_LOGI(TAG, "%s(%d)", __func__, port);
  31. if (running) {
  32. ESP_LOGE(TAG, "server already running");
  33. ABORT("fix me");
  34. }
  35. handle = nullptr;
  36. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  37. conf.cacert_pem = cert_start;
  38. conf.cacert_len = cert_end - cert_start;
  39. conf.prvtkey_pem = key_start;
  40. conf.prvtkey_len = key_end - key_start;
  41. CHECK(httpd_ssl_start(&handle, &conf));
  42. registerUriHandlers();
  43. running = true;
  44. return *this;
  45. }
  46. void http::Server::registerUriHandlers() {
  47. ESP_LOGD(TAG, "%s()", __func__);
  48. for (auto &route : routes) {
  49. ESP_LOGI(TAG, " method: %d; uri: %s", static_cast<int>(route.method), route.uri.c_str());
  50. httpd_uri_t uriHandler = {
  51. .uri = route.uri.c_str(),
  52. .method = static_cast<httpd_method_t>(route.method),
  53. .handler = handleHttpRequest,
  54. .user_ctx = &route
  55. };
  56. CHECK(httpd_register_uri_handler(handle, &uriHandler));
  57. }
  58. }
  59. void http::Server::stop() {
  60. ESP_LOGI(TAG, "%s()", __func__);
  61. CHECK(httpd_stop(handle));
  62. running = false;
  63. }
  64. // TODO handle all status codes; also maybe use macro or constexpr or something?
  65. static string getDefaultStatusText(int status) {
  66. if (status == 200) return "OK";
  67. if (status == 204) return "No Content";
  68. if (status == 400) return "Bad Request";
  69. if (status == 500) return "Internal Server Error";
  70. return "Unknown HTTP Status";
  71. }
  72. esp_err_t http::Server::handleHttpRequest(httpd_req_t *httpdRequest) {
  73. auto route = static_cast<Route *>(httpdRequest->user_ctx);
  74. ESP_LOGD(TAG, "%s; method: %d, path: \"%s\"", __func__, httpdRequest->method, route->uri.c_str());
  75. auto request = Request(httpdRequest);
  76. auto response = route->handler(request, route->data);
  77. CHECK(httpd_resp_set_hdr(httpdRequest, "Server", "kbf_http_server/0.1"));
  78. CHECK(httpd_resp_set_type(httpdRequest, response.contentType.c_str()));
  79. for (const auto &[name, value] : response.headers) {
  80. CHECK(httpd_resp_set_hdr(httpdRequest, name.c_str(), value.c_str()));
  81. }
  82. if (response.statusText.empty()) {
  83. response.statusText = getDefaultStatusText(response.status);
  84. }
  85. string status = std::to_string(response.status) + " " + response.statusText;
  86. ESP_LOGD(TAG, "response: %s", status.c_str());
  87. CHECK(httpd_resp_set_status(httpdRequest, status.c_str()));
  88. ESP_LOG_BUFFER_HEXDUMP(TAG, response.body.c_str(), response.body.size(), ESP_LOG_VERBOSE);
  89. auto err = httpd_resp_send(httpdRequest, response.body.c_str(), response.body.length());
  90. if (err == ESP_ERR_HTTPD_RESP_SEND) {
  91. ESP_LOGW(TAG, "send failed; maybe client disconnected?");
  92. return ESP_ERR_HTTPD_RESP_SEND;
  93. }
  94. CHECK(err);
  95. if (route->onResponseSent) route->onResponseSent(response, route->data);
  96. return ESP_OK;
  97. }
  98. http::Server::~Server() {
  99. if (running) stop();
  100. }