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(int port) {
  28. ESP_LOGI(TAG, "startSSL(%d)", port);
  29. if (running) {
  30. ESP_LOGE(TAG, "server already running");
  31. ABORT("fix me");
  32. }
  33. handle = nullptr;
  34. httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
  35. extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
  36. extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
  37. conf.cacert_pem = cacert_pem_start;
  38. conf.cacert_len = cacert_pem_end - cacert_pem_start;
  39. extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
  40. extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
  41. conf.prvtkey_pem = prvtkey_pem_start;
  42. conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
  43. CHECK(httpd_ssl_start(&handle, &conf));
  44. registerUriHandlers();
  45. running = true;
  46. return *this;
  47. }
  48. void http::Server::registerUriHandlers() {
  49. ESP_LOGI(TAG, "registering URI handlers");
  50. for (auto &route : routes) {
  51. ESP_LOGI(TAG, " method: %d; uri: %s", static_cast<int>(route.method), route.uri.c_str());
  52. httpd_uri_t uriHandler = {
  53. .uri = route.uri.c_str(),
  54. .method = static_cast<httpd_method_t>(route.method),
  55. .handler = handleHttpRequest,
  56. .user_ctx = &route
  57. };
  58. CHECK(httpd_register_uri_handler(handle, &uriHandler));
  59. }
  60. }
  61. void http::Server::stop() {
  62. CHECK(httpd_stop(handle));
  63. running = false;
  64. }
  65. // TODO handle all status codes; also maybe use macro or constexpr or something?
  66. static string getDefaultStatusText(int status) {
  67. if (status == 200) return "OK";
  68. if (status == 204) return "No Content";
  69. if (status == 400) return "Bad Request";
  70. if (status == 500) return "Internal Server Error";
  71. return "Unknown HTTP Status";
  72. }
  73. esp_err_t http::Server::handleHttpRequest(httpd_req_t *httpdRequest) {
  74. auto route = static_cast<Route *>(httpdRequest->user_ctx);
  75. ESP_LOGD(TAG, "handleHttpRequest; method: %d, path: \"%s\"", httpdRequest->method, route->uri.c_str());
  76. auto request = Request(httpdRequest);
  77. auto response = route->handler(request, route->data);
  78. CHECK(httpd_resp_set_hdr(httpdRequest, "Server", "kbf_http_server/0.1"));
  79. CHECK(httpd_resp_set_type(httpdRequest, response.contentType.c_str()));
  80. for (const auto &[name, value] : response.headers) {
  81. CHECK(httpd_resp_set_hdr(httpdRequest, name.c_str(), value.c_str()));
  82. }
  83. if (response.statusText.empty()) {
  84. response.statusText = getDefaultStatusText(response.status);
  85. }
  86. string status = std::to_string(response.status) + " " + response.statusText;
  87. ESP_LOGD(TAG, "response: %s", status.c_str());
  88. CHECK(httpd_resp_set_status(httpdRequest, status.c_str()));
  89. ESP_LOG_BUFFER_HEXDUMP(TAG, response.body.c_str(), response.body.size(), ESP_LOG_VERBOSE);
  90. auto err = httpd_resp_send(httpdRequest, response.body.c_str(), response.body.length());
  91. if (err == ESP_ERR_HTTPD_RESP_SEND) {
  92. ESP_LOGW(TAG, "send failed; maybe client disconnected?");
  93. return ESP_ERR_HTTPD_RESP_SEND;
  94. }
  95. CHECK(err);
  96. return ESP_OK;
  97. }
  98. http::Server::~Server() {
  99. if (running) stop();
  100. }