httpd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. { ".xml", 4, "text/xml" },
  58. { ".bin", 4, "application/octet-stream" },
  59. { ".fw", 3, "application/octet-stream" },
  60. { NULL, 0, "text/plain; charset=UTF-8" } /* default */
  61. };
  62. static esp_err_t httpd_static_handler(httpd_req_t *req)
  63. {
  64. static const char index_filename[] = "index.html";
  65. const size_t buffer_size = UNZ_BUFSIZE;
  66. const char *uri, *enduri;
  67. bool add_index;
  68. char *buffer = NULL;
  69. ZIPFILE *zip = NULL;
  70. unzFile unz = NULL;
  71. bool file_open = false;
  72. int err = 0;
  73. size_t len;
  74. uri = req->uri;
  75. while (*uri == '/')
  76. uri++; /* Skip leading slashes */
  77. enduri = strchr(uri, '\0');
  78. if (enduri == uri) {
  79. add_index = true;
  80. } else if (enduri[-1] == '/') {
  81. add_index = true;
  82. enduri--; /* Drop terminal slash */
  83. } else {
  84. add_index = false; /* Try the plain filename first */
  85. }
  86. MSG("requesting: /%.*s\n", enduri - uri, uri);
  87. buffer = malloc(buffer_size);
  88. zip = malloc(sizeof *zip);
  89. if (!buffer || !zip) {
  90. httpd_resp_send_err(req, 503, "Out of memory");
  91. goto out;
  92. }
  93. if (enduri - uri + 1 + sizeof index_filename >= buffer_size) {
  94. err = httpd_resp_send_err(req, 414, "URI too long");
  95. goto out;
  96. }
  97. char *p = mempcpy(buffer, uri, enduri - uri);
  98. *p = '\0';
  99. unz = unzOpen(NULL, (void *)gwwwzipData, gwwwzipSize,
  100. zip, NULL, NULL, NULL, NULL);
  101. if (!unz) {
  102. MSG("[HTTP] unzOpen failed!\n");
  103. err = httpd_resp_send_err(req, 500, "Cannot open content archive");
  104. goto out;
  105. }
  106. while (1) {
  107. if (add_index) {
  108. if (p > buffer)
  109. *p++ = '/';
  110. p = mempcpy(p, index_filename, sizeof index_filename);
  111. }
  112. MSG("trying to open: %s\n", buffer);
  113. if (unzLocateFile(unz, buffer, 1) == UNZ_OK)
  114. break;
  115. if (add_index) {
  116. err = httpd_resp_send_404(req);
  117. goto out;
  118. }
  119. add_index = true; /* Try again with the index filename */
  120. }
  121. /* Note: p points to the end of the filename string */
  122. size_t filelen = p - buffer;
  123. const struct mime_type *mime_type = mime_types;
  124. /* The default entry with length 0 will always match */
  125. while (mime_type->ext_len) {
  126. len = mime_type->ext_len;
  127. if (len < filelen && !memcmp(p - len, mime_type->ext, len))
  128. break;
  129. mime_type++;
  130. }
  131. unz_file_info fileinfo;
  132. memset(&fileinfo, 0, sizeof fileinfo);
  133. unzGetCurrentFileInfo(unz, &fileinfo, NULL, 0, NULL, 0, NULL, 0);
  134. /*
  135. * This is kind of brain dead, but it seems like the only sane
  136. * way to not have to build the whole response in memory even
  137. * though the length is known a priori.
  138. */
  139. len = snprintf(buffer, buffer_size,
  140. "HTTP/1.1 200 OK\r\n"
  141. "Content-Type: %s\r\n"
  142. "Content-Length: %u\r\n"
  143. "Allow: GET, HEAD\r\n"
  144. "Etag: \"%08x:%08x\"\r\n"
  145. "Connection: close\r\n"
  146. "\r\n",
  147. mime_type->mime,
  148. fileinfo.uncompressed_size,
  149. /*
  150. * Hopefully the combination of date and CRC
  151. * is strong enough to quality for a "strong" ETag
  152. */
  153. fileinfo.dosDate, fileinfo.crc);
  154. if (len >= buffer_size) {
  155. err = httpd_resp_send_err(req, 500, "buffer_size too small");
  156. goto out;
  157. }
  158. err = httpd_send(req, buffer, len);
  159. if (err != len || req->method == HTTP_HEAD ||
  160. !fileinfo.uncompressed_size)
  161. goto out;
  162. if (unzOpenCurrentFile(unz) != UNZ_OK) {
  163. err = httpd_resp_send_err(req, 500, "Cannot open file in archive");
  164. goto out;
  165. }
  166. file_open = true;
  167. len = fileinfo.uncompressed_size;
  168. while (len) {
  169. size_t chunk = len;
  170. if (chunk > buffer_size)
  171. chunk = buffer_size;
  172. if (unzReadCurrentFile(unz, buffer, chunk) != chunk) {
  173. err = ESP_ERR_HTTPD_RESULT_TRUNC;
  174. goto out;
  175. }
  176. err = httpd_send(req, buffer, chunk);
  177. if (err != chunk)
  178. goto out;
  179. len -= chunk;
  180. }
  181. err = ESP_OK; /* All good! */
  182. out:
  183. if (file_open)
  184. unzCloseCurrentFile(unz);
  185. if (unz)
  186. unzClose(unz);
  187. if (zip)
  188. free(zip);
  189. if (buffer)
  190. free(buffer);
  191. return err;
  192. }
  193. static const httpd_uri_t uri_handlers[] = {
  194. {
  195. .uri = "/*",
  196. .method = HTTP_GET,
  197. .handler = httpd_static_handler,
  198. .user_ctx = NULL
  199. },
  200. {
  201. .uri = "/*",
  202. .method = HTTP_HEAD,
  203. .handler = httpd_static_handler,
  204. .user_ctx = NULL
  205. },
  206. {
  207. .uri = "/fwupdate/?",
  208. .method = HTTP_POST,
  209. .handler = httpd_firmware_upgrade_handler,
  210. .user_ctx = NULL
  211. }
  212. };
  213. void my_httpd_stop(void)
  214. {
  215. if (httpd) {
  216. esp_unregister_shutdown_handler(my_httpd_stop);
  217. httpd_stop(httpd);
  218. httpd = NULL;
  219. }
  220. }
  221. void my_httpd_start(void)
  222. {
  223. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  224. httpd_handle_t server;
  225. if (httpd)
  226. return;
  227. config.task_priority = 2;
  228. printf("[HTTP] Default stack size: %zu\n", config.stack_size);
  229. config.stack_size <<= 2;
  230. printf("[HTTP] Requesting stack size: %zu\n", config.stack_size);
  231. config.uri_match_fn = httpd_uri_match_wildcard;
  232. if (httpd_start(&server, &config) != ESP_OK)
  233. return;
  234. httpd = server;
  235. for (size_t i = 0; i < ARRAY_SIZE(uri_handlers); i++)
  236. httpd_register_uri_handler(httpd, &uri_handlers[i]);
  237. esp_register_shutdown_handler(my_httpd_stop);
  238. printf("[HTTP] httpd started\n");
  239. }