httpd.c 6.5 KB

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