httpd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. #define MODULE "httpd"
  2. #include "common.h"
  3. #include "fw.h"
  4. #include "httpd.h"
  5. #include "config.h"
  6. #include <incbin.h>
  7. #include <unzipLIB.h>
  8. #define HTTPD_PRIORITY 4
  9. static httpd_handle_t httpd;
  10. static const char fallback_language[] = "en"; /* For unknown language */
  11. static const char redir_filename[] = "_redir"; /* For a directory "file" */
  12. #define MAX_LANG_LEN 16
  13. /* Looping version of httpd_send(); this is a hidden function in the server */
  14. static esp_err_t httpd_send_all(httpd_req_t *req, const void *buf, size_t len)
  15. {
  16. const char *p = buf;
  17. while (len) {
  18. int sent = httpd_send(req, p, len);
  19. if (sent <= 0)
  20. return ESP_ERR_HTTPD_RESP_SEND;
  21. p += sent;
  22. len -= sent;
  23. }
  24. return ESP_OK;
  25. }
  26. /* Create a file pointer from an http request */
  27. static ssize_t httpd_io_read(void *cookie, char *buf, size_t n)
  28. {
  29. int rv = httpd_req_recv(cookie, buf, n);
  30. return rv < 0 ? -1 : rv;
  31. }
  32. static ssize_t httpd_io_write(void *cookie, const char *buf, size_t n)
  33. {
  34. return httpd_resp_send_chunk(cookie, buf, n) ? 0 : n;
  35. }
  36. static int httpd_io_close_write(void *cookie)
  37. {
  38. return httpd_resp_send_chunk(cookie, NULL, 0) ? -1 : 0;
  39. }
  40. static FILE *httpd_fopen_read(httpd_req_t *req)
  41. {
  42. static const cookie_io_functions_t http_io_read_funcs = {
  43. .read = httpd_io_read,
  44. .write = NULL, /* Not writeable */
  45. .seek = NULL, /* Not seekable */
  46. .close = NULL,
  47. };
  48. return fopencookie((void *)req, "r", http_io_read_funcs);
  49. }
  50. static FILE *httpd_fopen_write(httpd_req_t *req)
  51. {
  52. static const cookie_io_functions_t http_io_write_funcs = {
  53. .read = httpd_io_read,
  54. .write = httpd_io_write,
  55. .seek = NULL, /* Not seekable */
  56. .close = httpd_io_close_write
  57. };
  58. return fopencookie((void *)req, "r+", http_io_write_funcs);
  59. }
  60. #define TIMEBUF_LEN 32
  61. static const char *http_date(const struct tm *when)
  62. {
  63. static char timebuf[32];
  64. strftime(timebuf, sizeof timebuf,
  65. "%a, %d %b %Y %H:%M:%S GMT", when);
  66. return timebuf;
  67. }
  68. static const char *http_now(void)
  69. {
  70. time_t t = time(NULL);
  71. return http_date(gmtime(&t));
  72. }
  73. static struct tm *set_weekday(struct tm *tm)
  74. {
  75. /*
  76. * This is a variation on Zeller's congruence with a table lookup
  77. * for the month. The table contains the number of days since March 1,
  78. * mod 7 (for Jan and Feb, from March 1 of the *previous year*.)
  79. *
  80. * Sample test cases:
  81. * Wed Mar 1 0000
  82. * Thu Jan 1 1970
  83. * Wed Apr 27 2022
  84. * Mon Feb 28 2000
  85. * Wed Mar 1 2000
  86. * Sun Feb 28 2100
  87. * Mon Mar 1 2100
  88. */
  89. static const uint8_t md[12] = { 5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2 };
  90. unsigned int c, y, m, d;
  91. y = tm->tm_year + 1900;
  92. m = tm->tm_mon;
  93. d = tm->tm_mday;
  94. if (m < 2)
  95. y--; /* Jan, Feb */
  96. c = y/100;
  97. /*
  98. * 2 represents the base date of Tue Feb 29 0000
  99. *
  100. * 0 = Sun, 6 = Sat
  101. */
  102. tm->tm_wday = (d + md[m] + y + (y >> 2) - c + (c >> 2) + 2) % 7;
  103. return tm;
  104. }
  105. static const char *http_dos_date(uint32_t dos_date)
  106. {
  107. struct tm tm;
  108. tm.tm_sec = (dos_date << 1) & 63;
  109. tm.tm_min = (dos_date >> 5) & 63;
  110. tm.tm_hour = (dos_date >> 11) & 31;
  111. tm.tm_mday = (dos_date >> 16) & 31;
  112. tm.tm_mon = ((dos_date >> 21) & 15) - 1;
  113. tm.tm_year = (dos_date >> 25) + 80;
  114. tm.tm_isdst = 0; /* Times are stored in GMT */
  115. return http_date(set_weekday(&tm));
  116. }
  117. static const char text_plain[] = "text/plain; charset=\"UTF-8\"";
  118. static void httpd_print_request(const httpd_req_t *req)
  119. {
  120. printf("[HTTP] %s %s\n", http_method_str(req->method), req->uri);
  121. }
  122. static char *httpd_req_get_hdr(httpd_req_t *req, const char *field, size_t *lenp)
  123. {
  124. size_t len = httpd_req_get_hdr_value_len(req, field);
  125. char *val = NULL;
  126. if (len) {
  127. val = malloc(len+1);
  128. if (val) {
  129. httpd_req_get_hdr_value_str(req, field, val, len+1);
  130. val[len] = '\0';
  131. }
  132. }
  133. if (lenp)
  134. *lenp = len;
  135. return val;
  136. }
  137. enum hsp_flags {
  138. HSP_CRLF = 1, /* Append CR LF to the body */
  139. HSP_CLOSE = 2, /* Add a Connection: close header */
  140. HSP_REFERER = 4, /* Use referer as body (for redirects) */
  141. HSP_UNCACHE = 8 /* Wipe caches, please... */
  142. };
  143. static esp_err_t httpd_send_plain(httpd_req_t *req,
  144. unsigned int rcode,
  145. const char *body, size_t blen,
  146. enum hsp_flags flags, unsigned int refresh)
  147. {
  148. char *header = NULL;
  149. esp_err_t err;
  150. int hlen;
  151. const char *now = http_now();
  152. MSG("http_send_plain %u \"%.*s\"\n", rcode, (int)blen, body);
  153. if (rcode > 499)
  154. flags |= HSP_CLOSE;
  155. const char *closer = flags & HSP_CLOSE ? "Connection: close\r\n" : "";
  156. bool redirect = rcode >= 300 && rcode <= 399;
  157. char *referer = NULL;
  158. char *refresher_buf = NULL;
  159. if (refresh || (flags & HSP_REFERER)) {
  160. size_t referer_len;
  161. referer = httpd_req_get_hdr(req, "Referer", &referer_len);
  162. /* "Effective" referer */
  163. const char * const ereferer = referer ? referer : "/";
  164. if (refresh) {
  165. asprintf(&refresher_buf, "Refresh: %u;url=%s\r\n",
  166. refresh, ereferer);
  167. }
  168. if (flags & HSP_REFERER) {
  169. body = ereferer;
  170. blen = referer ? referer_len : 1;
  171. }
  172. }
  173. const char * const refresher = refresher_buf ? refresher_buf : "";
  174. const char * const uncacher = (flags & HSP_UNCACHE)
  175. ? "Clear-Site-Data: \"cache\"\r\n" : "";
  176. if (redirect) {
  177. /* \0 -> \n so don't include it */
  178. size_t blenadj = sizeof("3xx Redirect \r");
  179. /* Always CR LF */
  180. flags |= HSP_CRLF;
  181. /* Drop any CR LF already in the redirect string */
  182. for (size_t bchk = 0; bchk < blen; bchk++) {
  183. if (body[bchk] == '\r' || body[bchk] == '\n') {
  184. blen = bchk;
  185. break;
  186. }
  187. }
  188. hlen = asprintf(&header,
  189. "HTTP/1.1 %u\r\n"
  190. "Content-Type: %s\r\n"
  191. "Content-Length: %zu\r\n"
  192. "Date %s\r\n"
  193. "Location: %.*s\r\n"
  194. "%s%s%s"
  195. "\r\n"
  196. "%3u Redirect ",
  197. rcode, text_plain, blen + blenadj,
  198. now, (int)blen, body,
  199. closer, refresher, uncacher,
  200. rcode);
  201. } else {
  202. size_t blenadj = (flags & HSP_CRLF) ? 2 : 0;
  203. hlen = asprintf(&header,
  204. "HTTP/1.1 %u\r\n"
  205. "Content-Type: %s\r\n"
  206. "Content-Length: %zu\r\n"
  207. "Cache-Control: no-cache\r\n"
  208. "Date: %s\r\n"
  209. "%s%s%s"
  210. "\r\n",
  211. rcode, text_plain, blen + blenadj, now,
  212. closer, refresher, uncacher);
  213. }
  214. if (refresher_buf)
  215. free(refresher_buf);
  216. if (referer)
  217. free(referer);
  218. if (!header)
  219. return ESP_ERR_NO_MEM;
  220. err = httpd_send_all(req, header, hlen);
  221. if (!err && blen) {
  222. err = httpd_send_all(req, body, blen);
  223. }
  224. if (!err && (flags & HSP_CRLF)) {
  225. err = httpd_send_all(req, "\r\n", 2);
  226. }
  227. if (header)
  228. free(header);
  229. return err;
  230. }
  231. #define SL(s) (s), (sizeof(s)-1)
  232. #define HTTP_ERR(r,e,s) httpd_send_plain((r), (e), SL(s), HSP_CRLF, 0)
  233. static esp_err_t httpd_err_enoent(httpd_req_t *req)
  234. {
  235. return HTTP_ERR(req, 404, "URI not found");
  236. }
  237. static esp_err_t httpd_send_ok(httpd_req_t *req)
  238. {
  239. return HTTP_ERR(req, 200, "OK");
  240. }
  241. static esp_err_t httpd_err_enomem(httpd_req_t *req)
  242. {
  243. return HTTP_ERR(req, 503, "Out of memory");
  244. }
  245. static esp_err_t httpd_update_done(httpd_req_t *req, const char *what, int err)
  246. {
  247. char *response = NULL;
  248. int len;
  249. unsigned int reboot_time = reboot_delayed();
  250. if (err) {
  251. len = asprintf(&response,
  252. "%s update failed: %s\r\n"
  253. "Rebooting in %u seconds\r\n",
  254. what, firmware_errstr(err), reboot_time);
  255. } else {
  256. len = asprintf(&response,
  257. "%s update complete\r\n"
  258. "Rebooting in %u seconds\r\n",
  259. what, reboot_time);
  260. }
  261. if (!response)
  262. len = 0;
  263. esp_err_t rv = httpd_send_plain(req, err ? 400 : 200, response, len,
  264. HSP_CLOSE|HSP_UNCACHE, reboot_time+5);
  265. if (response)
  266. free(response);
  267. return rv;
  268. }
  269. static esp_err_t httpd_firmware_update(httpd_req_t *req)
  270. {
  271. int rv;
  272. /* XXX: use httpd_fopen_read() here */
  273. rv = firmware_update_start((read_func_t)httpd_req_recv, (token_t)req, false);
  274. if (!rv)
  275. rv = firmware_update_wait(portMAX_DELAY);
  276. return httpd_update_done(req, "Firmware", rv);
  277. }
  278. static esp_err_t httpd_set_config(httpd_req_t *req, const char *query)
  279. {
  280. FILE *f;
  281. size_t qlen;
  282. int rv1 = 0;
  283. if (query) {
  284. rv1 = set_config_url_string(query);
  285. }
  286. int rv2 = 0;
  287. f = NULL;
  288. if (req->content_len) {
  289. f = httpd_fopen_read(req);
  290. if (!f)
  291. return HTTP_ERR(req, 500, "Unable to get request handle");
  292. }
  293. rv2 = read_config(f, true);
  294. if (f)
  295. fclose(f);
  296. return httpd_update_done(req, "Configuration", rv1 ? rv1 : rv2);
  297. }
  298. #define MIN_STATUS_REF 1 /* Minimum refresh time in s */
  299. static void httpd_get_status_extra(FILE *f, httpd_req_t *req)
  300. {
  301. static const char refresh_time_config[] = "http.status.refresh";
  302. char timebuf[64];
  303. size_t len;
  304. struct timeval tv;
  305. unsigned long statref;
  306. statref = Max(getenv_ul(refresh_time_config, 0), MIN_STATUS_REF);
  307. if (httpd_req_get_url_query_str(req, timebuf, sizeof timebuf) == ESP_OK &&
  308. *timebuf) {
  309. char *ep;
  310. unsigned long newstatref = strtoul(timebuf, &ep, 10);
  311. if (!*ep && newstatref >= MIN_STATUS_REF && newstatref != statref) {
  312. statref = newstatref;
  313. setenv_config(refresh_time_config, timebuf);
  314. read_config(NULL, true); /* Save changed config */
  315. }
  316. }
  317. fprintf(f, "%s=%lu\n", refresh_time_config, statref);
  318. gettimeofday(&tv,NULL);
  319. const struct tm *tm = localtime(&tv.tv_sec);
  320. len = strftime(timebuf, sizeof timebuf, "localtime=%Y-%m-%d %H:%M:%S.", tm);
  321. snprintf(timebuf+len, sizeof timebuf - len, "%06u",
  322. (unsigned long)tv.tv_usec);
  323. len += 3;
  324. len += strftime(timebuf+len, sizeof timebuf - len, " %z (%Z)\n", tm);
  325. fwrite(timebuf, 1, len, f);
  326. }
  327. static esp_err_t httpd_get_config_status(httpd_req_t *req, bool status)
  328. {
  329. FILE *f = httpd_fopen_write(req);
  330. if (!f)
  331. return HTTP_ERR(req, 500, "Unable to get request handle");
  332. httpd_resp_set_type(req, text_plain);
  333. httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
  334. if (status)
  335. httpd_get_status_extra(f, req);
  336. int rv = write_env(f, status);
  337. fclose(f);
  338. return rv ? ESP_FAIL : ESP_OK;
  339. }
  340. static esp_err_t httpd_set_lang(httpd_req_t *req, const char *query)
  341. {
  342. if (query) {
  343. int qlen = strlen(query);
  344. setenv_config("LANG", qlen && qlen <= MAX_LANG_LEN ? query : NULL);
  345. read_config(NULL, true); /* Save configuration */
  346. }
  347. /*
  348. * 303 = "See other": proper return code saying "this is not what
  349. * you asked for, this is *about* what you asked for"; see spec
  350. * but this is exactly what we want here.
  351. */
  352. return httpd_send_plain(req, 303, NULL, 0, HSP_REFERER, 0);
  353. }
  354. static esp_err_t httpd_get_lang(httpd_req_t *req)
  355. {
  356. const char *lang = getenv_def("LANG", "");
  357. return httpd_send_plain(req, 200, lang, strlen(lang), HSP_CRLF, 0);
  358. }
  359. static esp_err_t httpd_lang_redirect(httpd_req_t *req)
  360. {
  361. char lang_buf[sizeof req->uri + MAX_LANG_LEN];
  362. int len = snprintf(lang_buf, sizeof lang_buf, "/lang/%s%s",
  363. getenv_def("LANG", fallback_language),
  364. req->uri + (sizeof("/sys/lang")-1));
  365. return httpd_send_plain(req, 302, lang_buf, len, 0, 0);
  366. }
  367. #define STRING_MATCHES(str, len, what) \
  368. (((len) == sizeof(what)-1) && !memcmp((str), (what), sizeof(what)-1))
  369. #define STRING_MATCHES_PREFIX(str, len, what) \
  370. (((len) >= sizeof(what)-1) && !memcmp((str), (what), sizeof(what)-1))
  371. static esp_err_t httpd_sys_handler(httpd_req_t *req)
  372. {
  373. httpd_print_request(req);
  374. if (req->method == HTTP_POST &&
  375. !httpd_req_get_hdr_value_len(req, "Content-Length")) {
  376. return HTTP_ERR(req, 411, "Length required");
  377. }
  378. const char *query = strchrnul(req->uri, '?');
  379. if (query < req->uri+5 || memcmp(req->uri, "/sys/", 5))
  380. return httpd_err_enoent(req); /* This should never happen */
  381. const char *file = req->uri + 5;
  382. size_t filelen = query - file;
  383. query = *query == '?' ? query+1 : NULL;
  384. if (STRING_MATCHES_PREFIX(file, filelen, "lang"))
  385. return httpd_lang_redirect(req);
  386. if (STRING_MATCHES(file, filelen, "getstatus"))
  387. return httpd_get_config_status(req, true);
  388. if (STRING_MATCHES(file, filelen, "getconfig"))
  389. return httpd_get_config_status(req, false);
  390. if (req->method == HTTP_POST && STRING_MATCHES(file, filelen, "fwupdate"))
  391. return httpd_firmware_update(req);
  392. if (STRING_MATCHES(file, filelen, "setconfig"))
  393. return httpd_set_config(req, query);
  394. if (STRING_MATCHES(file, filelen, "getlang"))
  395. return httpd_get_lang(req);
  396. if (STRING_MATCHES(file, filelen, "setlang"))
  397. return httpd_set_lang(req, query);
  398. return httpd_err_enoent(req);
  399. }
  400. INCBIN_EXTERN(wwwzip);
  401. struct mime_type {
  402. const char *ext;
  403. uint16_t ext_len;
  404. uint16_t flags;
  405. const char *mime;
  406. };
  407. #define MT_CHARSET 1 /* Add charset to Content-Type */
  408. #define MT_REDIR 2 /* It is a redirect */
  409. static const struct mime_type mime_types[] = {
  410. { ".html", 5, MT_CHARSET, "text/html" },
  411. { ".xhtml", 6, MT_CHARSET, "text/html" },
  412. { ".css", 4, MT_CHARSET, "text/css" },
  413. { ".webp", 5, 0, "image/webp" },
  414. { ".jpg", 4, 0, "image/jpeg" },
  415. { ".png", 4, 0, "image/png" },
  416. { ".ico", 4, 0, "image/png" }, /* favicon.ico */
  417. { ".svg", 4, MT_CHARSET, "image/svg+xml" },
  418. { ".otf", 4, 0, "font/otf" },
  419. { ".ttf", 4, 0, "font/ttf" },
  420. { ".woff", 5, 0, "font/woff" },
  421. { ".woff2", 6, 0, "font/woff2" },
  422. { ".pdf", 4, 0, "application/pdf" },
  423. { ".js", 3, MT_CHARSET, "text/javascript" },
  424. { ".mjs", 4, MT_CHARSET, "text/javascript" },
  425. { ".json", 5, MT_CHARSET, "application/json" },
  426. { ".xml", 4, MT_CHARSET, "text/xml" },
  427. { ".bin", 4, 0, "application/octet-stream" },
  428. { ".fw", 3, 0, "application/octet-stream" },
  429. { ".capt", 5, 0, "application/captive+json" },
  430. { "_redir", 6, MT_REDIR, NULL },
  431. { NULL, 0, MT_CHARSET, "text/plain" } /* default */
  432. };
  433. static esp_err_t httpd_static_handler(httpd_req_t *req)
  434. {
  435. size_t buffer_size = UNZ_BUFSIZE;
  436. const char *uri, *enduri;
  437. bool is_dir;
  438. char *buffer = NULL;
  439. ZIPFILE *zip = NULL;
  440. unzFile unz = NULL;
  441. bool file_open = false;
  442. int err = 0;
  443. size_t len;
  444. httpd_print_request(req);
  445. uri = req->uri;
  446. while (*uri == '/')
  447. uri++; /* Skip leading slashes */
  448. const char *first_slash = NULL, *last_slash = NULL;
  449. for (enduri = uri; *enduri; enduri++) {
  450. if (*enduri == '/') {
  451. last_slash = enduri;
  452. if (!first_slash)
  453. first_slash = enduri;
  454. }
  455. }
  456. if (enduri == uri) {
  457. is_dir = true;
  458. } else if (last_slash == enduri-1) {
  459. is_dir = true;
  460. enduri--; /* Drop terminal slash */
  461. if (first_slash == last_slash)
  462. first_slash = NULL;
  463. } else {
  464. is_dir = false; /* Try the plain filename first */
  465. }
  466. const size_t filename_buffer_size =
  467. (enduri - uri) + 2 + sizeof redir_filename;
  468. if (buffer_size < filename_buffer_size)
  469. buffer_size = filename_buffer_size;
  470. buffer = malloc(buffer_size);
  471. zip = malloc(sizeof *zip);
  472. if (!buffer || !zip) {
  473. err = httpd_err_enomem(req);
  474. goto out;
  475. }
  476. char * const filebase = buffer + 1;
  477. char * const endbase = mempcpy(filebase, uri, enduri - uri);
  478. filebase[-1] = '/';
  479. unz = unzOpen(NULL, (void *)gwwwzipData, gwwwzipSize,
  480. zip, NULL, NULL, NULL, NULL);
  481. if (!unz) {
  482. MSG("[HTTP] unzOpen failed!\n");
  483. err = HTTP_ERR(req, 500, "Cannot open content archive");
  484. goto out;
  485. }
  486. char * const filename = filebase; /* Separate for future needs */
  487. char *endfile = endbase;
  488. unsigned int m;
  489. bool found = false;
  490. for (m = is_dir ? 2 : 0; m < 3; m++) {
  491. char *sx;
  492. switch (m) {
  493. default: /* filename = /url */
  494. endfile = endbase;
  495. break;
  496. case 1: /* filename = /url_redir */
  497. sx = endbase;
  498. endfile = mempcpy(sx, redir_filename, sizeof redir_filename) - 1;
  499. break;
  500. case 2: /* filename = /url/_redir */
  501. sx = endbase - (endbase[-1] == '/');
  502. *sx++ = '/';
  503. endfile = mempcpy(sx, redir_filename, sizeof redir_filename) - 1;
  504. break;
  505. }
  506. *endfile = '\0';
  507. if (!filename)
  508. continue;
  509. filename[-1] = '/';
  510. MSG("trying to open: %s... ", filename);
  511. if (unzLocateFile(unz, filename, 1) == UNZ_OK) {
  512. CMSG("found\n");
  513. found = true;
  514. break;
  515. } else {
  516. CMSG("not found\n");
  517. }
  518. }
  519. if (!found) {
  520. err = httpd_err_enoent(req);
  521. goto out;
  522. }
  523. size_t filelen = endfile - filename;
  524. const struct mime_type *mime_type = mime_types;
  525. /* The default entry with length 0 will always match */
  526. while (mime_type->ext_len) {
  527. len = mime_type->ext_len;
  528. if (len <= filelen && !memcmp(endfile - len, mime_type->ext, len))
  529. break;
  530. mime_type++;
  531. }
  532. MSG("found %s ext %s type %s\n",
  533. filename,
  534. mime_type->ext ? mime_type->ext : "(none)",
  535. mime_type->mime ? mime_type->mime : "(none)");
  536. unz_file_info fileinfo;
  537. memset(&fileinfo, 0, sizeof fileinfo);
  538. unzGetCurrentFileInfo(unz, &fileinfo, NULL, 0, NULL, 0, NULL, 0);
  539. MSG("len %u compressed %u\n",
  540. fileinfo.uncompressed_size,
  541. fileinfo.compressed_size);
  542. /*
  543. * Is it a redirect?
  544. */
  545. if (mime_type->flags & MT_REDIR) {
  546. if (fileinfo.uncompressed_size > buffer_size ||
  547. unzOpenCurrentFile(unz) != UNZ_OK) {
  548. err = HTTP_ERR(req, 500, "Cannot open file in archive");
  549. goto out;
  550. }
  551. file_open = true;
  552. len = fileinfo.uncompressed_size;
  553. if (unzReadCurrentFile(unz, buffer, len) != len) {
  554. err = ESP_ERR_HTTPD_RESULT_TRUNC;
  555. goto out;
  556. }
  557. MSG("redirect: %.*s\n", (int)len, buffer);
  558. err = httpd_send_plain(req, 302, buffer, len, 0, 0);
  559. goto out;
  560. }
  561. /*
  562. * Hopefully the combination of date and CRC
  563. * is strong enough to quality for a "strong" ETag
  564. */
  565. char etag[16+3+1];
  566. snprintf(etag, sizeof etag, "\"%08x:%08x\"",
  567. fileinfo.dosDate, fileinfo.crc);
  568. bool skip_body = req->method == HTTP_HEAD || !fileinfo.uncompressed_size;
  569. bool skip_meta = false;
  570. const char *response = "200 OK";
  571. if (httpd_req_get_hdr_value_str(req, "If-None-Match",
  572. buffer, buffer_size) == ESP_OK &&
  573. strstr(buffer, etag)) {
  574. skip_body = skip_meta = true;
  575. response = "304 Not Modified";
  576. }
  577. len = snprintf(buffer, buffer_size-2,
  578. "HTTP/1.1 %s\r\n"
  579. "Date: %s\r\n"
  580. "Cache-Control: max-age=10, immutable\r\n"
  581. "ETag: %s\r\n",
  582. response,
  583. http_now(),
  584. etag);
  585. if (len < buffer_size-2 && !skip_meta) {
  586. const char *mime_extra =
  587. mime_type->flags & MT_CHARSET ? "; charset=\"UTF-8\"" : "";
  588. len += snprintf(buffer + len, buffer_size-2 - len,
  589. "Content-Type: %s%s\r\n"
  590. "Content-Length: %u\r\n"
  591. "Allow: GET, HEAD\r\n"
  592. "Connection: close\r\n"
  593. "Last-Modified: %s\r\n",
  594. mime_type->mime, mime_extra,
  595. fileinfo.uncompressed_size,
  596. http_dos_date(fileinfo.dosDate));
  597. }
  598. if (len >= buffer_size-2) {
  599. err = HTTP_ERR(req, 500, "buffer_size too small");
  600. goto out;
  601. }
  602. buffer[len++] = '\r';
  603. buffer[len++] = '\n';
  604. err = httpd_send_all(req, buffer, len);
  605. if (skip_body || err) {
  606. /* No need to spend time uncompressing the file content */
  607. goto out;
  608. }
  609. if (unzOpenCurrentFile(unz) != UNZ_OK) {
  610. err = HTTP_ERR(req, 400, "Cannot open file in archive");
  611. goto out;
  612. }
  613. file_open = true;
  614. len = fileinfo.uncompressed_size;
  615. while (len) {
  616. size_t chunk = len;
  617. if (chunk > buffer_size)
  618. chunk = buffer_size;
  619. if (unzReadCurrentFile(unz, buffer, chunk) != chunk) {
  620. err = ESP_ERR_HTTPD_RESULT_TRUNC;
  621. goto out;
  622. }
  623. err = httpd_send_all(req, buffer, chunk);
  624. if (err)
  625. goto out;
  626. len -= chunk;
  627. }
  628. err = ESP_OK; /* All good! */
  629. out:
  630. if (file_open)
  631. unzCloseCurrentFile(unz);
  632. if (unz)
  633. unzClose(unz);
  634. if (zip)
  635. free(zip);
  636. if (buffer)
  637. free(buffer);
  638. return err;
  639. }
  640. /*
  641. * Match a URL against a path prefix. To keep httpd from refusing to
  642. * register subpaths, the root template does not include the leading
  643. * '/', but uri is required to have it. Do not include a trailing /
  644. * in prefix; it is implied.
  645. */
  646. static bool httpd_uri_match_prefix(const char *template, const char *uri,
  647. size_t len)
  648. {
  649. #if 0
  650. printf("[HTTP] matching URI \"%.*s\" against template \"%s\"\n",
  651. len, uri, template);
  652. #endif
  653. if (!len-- || *uri++ != '/')
  654. return false;
  655. /* Previous template character (leading '/' implied) */
  656. unsigned char tp = '/';
  657. while (1) {
  658. unsigned char t = *template++;
  659. unsigned char u;
  660. if (!len-- || !(u = *uri++)) {
  661. return !t;
  662. } else if (!t) {
  663. return tp == '/' || u == '/';
  664. } else if (t != u) {
  665. return false;
  666. }
  667. tp = t;
  668. }
  669. }
  670. /* Do not include leading or trailing /; most specific prefix first */
  671. static const httpd_uri_t uri_handlers[] = {
  672. {
  673. .uri = "sys",
  674. .method = HTTP_GET,
  675. .handler = httpd_sys_handler,
  676. .user_ctx = NULL
  677. },
  678. {
  679. .uri = "",
  680. .method = HTTP_GET,
  681. .handler = httpd_static_handler,
  682. .user_ctx = NULL
  683. },
  684. {
  685. .uri = "",
  686. .method = HTTP_HEAD,
  687. .handler = httpd_static_handler,
  688. .user_ctx = NULL
  689. },
  690. {
  691. .uri = "sys",
  692. .method = HTTP_POST,
  693. .handler = httpd_sys_handler,
  694. .user_ctx = NULL
  695. },
  696. };
  697. void my_httpd_stop(void)
  698. {
  699. if (httpd) {
  700. esp_unregister_shutdown_handler(my_httpd_stop);
  701. httpd_stop(httpd);
  702. httpd = NULL;
  703. }
  704. }
  705. void my_httpd_start(void)
  706. {
  707. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  708. httpd_handle_t server;
  709. if (httpd)
  710. return;
  711. config.task_priority = HTTPD_PRIORITY;
  712. config.max_open_sockets = 10;
  713. printf("[HTTP] Default stack size: %zu\n", config.stack_size);
  714. config.stack_size <<= 2;
  715. printf("[HTTP] Requesting stack size: %zu\n", config.stack_size);
  716. config.uri_match_fn = httpd_uri_match_prefix;
  717. if (httpd_start(&server, &config) != ESP_OK)
  718. return;
  719. esp_register_shutdown_handler(my_httpd_stop);
  720. httpd = server;
  721. for (size_t i = 0; i < ARRAY_SIZE(uri_handlers); i++) {
  722. const httpd_uri_t * const handler = &uri_handlers[i];
  723. if (httpd_register_uri_handler(httpd, handler))
  724. printf("[HTTP] failed to register URI handler: %s %s\n",
  725. http_method_str(handler->method), handler->uri);
  726. }
  727. printf("[HTTP] httpd started\n");
  728. }