httpd.c 6.4 KB

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