httpd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #define MODULE "httpd"
  2. #include "common.h"
  3. #include "fw.h"
  4. #include "httpd.h"
  5. #include <incbin.h>
  6. #include <unzipLIB.h>
  7. static httpd_handle_t httpd;
  8. esp_err_t httpd_firmware_upgrade_handler(httpd_req_t *req)
  9. {
  10. char *response;
  11. esp_err_t err;
  12. int rv, len;
  13. printf("[POST] len = %zu uri = \"%s\"\n",
  14. req->content_len, req->uri);
  15. if (!req->content_len) {
  16. return httpd_resp_send_err(req, 411, "Length required");
  17. }
  18. rv = firmware_update((read_func_t)httpd_req_recv, (token_t)req);
  19. if (rv == FWUPDATE_ERR_IN_PROGRESS)
  20. return httpd_resp_send_err(req, 409, "Firmware update already in progress");
  21. else if (rv)
  22. return httpd_resp_send_err(req, 500, "Firmware update failed");
  23. len = asprintf(&response,
  24. "<!DOCTYPE html>\r\n"
  25. "<html>\r\n"
  26. "<head>\r\n"
  27. "<title>Firmware update completed</title>\r\n"
  28. "</head>\r\n"
  29. "<body>\r\n"
  30. "<h1>Firmware update completed</h1>\r\n"
  31. "<p>Rebooting in %u seconds</p>\r\n"
  32. "</body>\r\n"
  33. "</html>\r\n",
  34. reboot_delayed());
  35. /* 200 and text/html are the response defaults, no need to set */
  36. httpd_resp_set_hdr(req, "Connection", "close");
  37. err = httpd_resp_send(req, response, len);
  38. free(response);
  39. return err;
  40. }
  41. INCBIN(wwwzip, "data/www.zip");
  42. struct mime_type {
  43. const char *ext;
  44. size_t ext_len;
  45. const char *mime;
  46. };
  47. static const struct mime_type mime_types[] = {
  48. { ".html", 5, "text/html" },
  49. { ".xhtml", 6, "text/html" },
  50. { ".css", 4, "text/css" },
  51. { ".webp", 5, "image/webp" },
  52. { ".jpg", 4, "image/jpeg" },
  53. { ".png", 4, "image/png" },
  54. { ".ico", 4, "image/png" }, /* favicon.ico */
  55. { ".svg", 4, "image/svg+xml" },
  56. { ".pdf", 4, "application/pdf" },
  57. { ".js", 3, "text/javascript" },
  58. { ".mjs", 4, "text/javascript" },
  59. { ".xml", 4, "text/xml" },
  60. { ".bin", 4, "application/octet-stream" },
  61. { ".fw", 3, "application/octet-stream" },
  62. { NULL, 0, "text/plain; charset=UTF-8" } /* default */
  63. };
  64. static esp_err_t httpd_static_handler(httpd_req_t *req)
  65. {
  66. static const char index_filename[] = "index.html";
  67. const size_t buffer_size = UNZ_BUFSIZE;
  68. const char *uri, *enduri;
  69. bool add_index;
  70. char *buffer = NULL;
  71. ZIPFILE *zip = NULL;
  72. unzFile unz = NULL;
  73. bool file_open = false;
  74. int err = 0;
  75. size_t len;
  76. uri = req->uri;
  77. while (*uri == '/')
  78. uri++; /* Skip leading slashes */
  79. enduri = strchr(uri, '\0');
  80. if (enduri == uri) {
  81. add_index = true;
  82. } else if (enduri[-1] == '/') {
  83. add_index = true;
  84. enduri--; /* Drop terminal slash */
  85. } else {
  86. add_index = false; /* Try the plain filename first */
  87. }
  88. MSG("requesting: /%.*s\n", enduri - uri, uri);
  89. buffer = malloc(buffer_size);
  90. zip = malloc(sizeof *zip);
  91. if (!buffer || !zip) {
  92. httpd_resp_send_err(req, 503, "Out of memory");
  93. goto out;
  94. }
  95. if (enduri - uri + 1 + sizeof index_filename >= buffer_size) {
  96. err = httpd_resp_send_err(req, 414, "URI too long");
  97. goto out;
  98. }
  99. char *p = mempcpy(buffer, uri, enduri - uri);
  100. *p = '\0';
  101. unz = unzOpen(NULL, (void *)gwwwzipData, gwwwzipSize,
  102. zip, NULL, NULL, NULL, NULL);
  103. if (!unz) {
  104. MSG("[HTTP] unzOpen failed!\n");
  105. err = httpd_resp_send_err(req, 500, "Cannot open content archive");
  106. goto out;
  107. }
  108. while (1) {
  109. if (add_index) {
  110. if (p > buffer)
  111. *p++ = '/';
  112. p = mempcpy(p, index_filename, sizeof index_filename);
  113. }
  114. MSG("trying to open: %s\n", buffer);
  115. if (unzLocateFile(unz, buffer, 1) == UNZ_OK)
  116. break;
  117. if (add_index) {
  118. err = httpd_resp_send_404(req);
  119. goto out;
  120. }
  121. add_index = true; /* Try again with the index filename */
  122. }
  123. /* Note: p points to the end of the filename string */
  124. size_t filelen = p - buffer;
  125. const struct mime_type *mime_type = mime_types;
  126. /* The default entry with length 0 will always match */
  127. while (mime_type->ext_len) {
  128. len = mime_type->ext_len;
  129. if (len < filelen && !memcmp(p - len, mime_type->ext, len))
  130. break;
  131. mime_type++;
  132. }
  133. unz_file_info fileinfo;
  134. memset(&fileinfo, 0, sizeof fileinfo);
  135. unzGetCurrentFileInfo(unz, &fileinfo, NULL, 0, NULL, 0, NULL, 0);
  136. /*
  137. * This is kind of brain dead, but it seems like the only sane
  138. * way to not have to build the whole response in memory even
  139. * though the length is known a priori.
  140. */
  141. len = snprintf(buffer, buffer_size,
  142. "HTTP/1.1 200 OK\r\n"
  143. "Content-Type: %s\r\n"
  144. "Content-Length: %u\r\n"
  145. "Allow: GET, HEAD\r\n"
  146. "Etag: \"%08x:%08x\"\r\n"
  147. "Connection: close\r\n"
  148. "\r\n",
  149. mime_type->mime,
  150. fileinfo.uncompressed_size,
  151. /*
  152. * Hopefully the combination of date and CRC
  153. * is strong enough to quality for a "strong" ETag
  154. */
  155. fileinfo.dosDate, fileinfo.crc);
  156. if (len >= buffer_size) {
  157. err = httpd_resp_send_err(req, 500, "buffer_size too small");
  158. goto out;
  159. }
  160. err = httpd_send(req, buffer, len);
  161. if (err != len || req->method == HTTP_HEAD ||
  162. !fileinfo.uncompressed_size)
  163. goto out;
  164. if (unzOpenCurrentFile(unz) != UNZ_OK) {
  165. err = httpd_resp_send_err(req, 500, "Cannot open file in archive");
  166. goto out;
  167. }
  168. file_open = true;
  169. len = fileinfo.uncompressed_size;
  170. while (len) {
  171. size_t chunk = len;
  172. if (chunk > buffer_size)
  173. chunk = buffer_size;
  174. if (unzReadCurrentFile(unz, buffer, chunk) != chunk) {
  175. err = ESP_ERR_HTTPD_RESULT_TRUNC;
  176. goto out;
  177. }
  178. err = httpd_send(req, buffer, chunk);
  179. if (err != chunk)
  180. goto out;
  181. len -= chunk;
  182. }
  183. err = ESP_OK; /* All good! */
  184. out:
  185. if (file_open)
  186. unzCloseCurrentFile(unz);
  187. if (unz)
  188. unzClose(unz);
  189. if (zip)
  190. free(zip);
  191. if (buffer)
  192. free(buffer);
  193. return err;
  194. }
  195. static const httpd_uri_t uri_handlers[] = {
  196. {
  197. .uri = "/*",
  198. .method = HTTP_GET,
  199. .handler = httpd_static_handler,
  200. .user_ctx = NULL
  201. },
  202. {
  203. .uri = "/*",
  204. .method = HTTP_HEAD,
  205. .handler = httpd_static_handler,
  206. .user_ctx = NULL
  207. },
  208. {
  209. .uri = "/fwupdate/?",
  210. .method = HTTP_POST,
  211. .handler = httpd_firmware_upgrade_handler,
  212. .user_ctx = NULL
  213. }
  214. };
  215. void my_httpd_stop(void)
  216. {
  217. if (httpd) {
  218. esp_unregister_shutdown_handler(my_httpd_stop);
  219. httpd_stop(httpd);
  220. httpd = NULL;
  221. }
  222. }
  223. void my_httpd_start(void)
  224. {
  225. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  226. httpd_handle_t server;
  227. if (httpd)
  228. return;
  229. config.task_priority = 2;
  230. printf("[HTTP] Default stack size: %zu\n", config.stack_size);
  231. config.stack_size <<= 2;
  232. printf("[HTTP] Requesting stack size: %zu\n", config.stack_size);
  233. config.uri_match_fn = httpd_uri_match_wildcard;
  234. if (httpd_start(&server, &config) != ESP_OK)
  235. return;
  236. httpd = server;
  237. for (size_t i = 0; i < ARRAY_SIZE(uri_handlers); i++)
  238. httpd_register_uri_handler(httpd, &uri_handlers[i]);
  239. esp_register_shutdown_handler(my_httpd_stop);
  240. printf("[HTTP] httpd started\n");
  241. }