esp32 firmware for a toaster reflow oven WIP!!!!!

token_subs.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. //#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  2. #include <esp_log.h>
  3. #include <esp_http_server.h>
  4. #include <rom/queue.h>
  5. #include <lwip/ip4_addr.h>
  6. #include <sys/param.h>
  7. #include <common_utils/utils.h>
  8. #include <fileserver/token_subs.h>
  9. #include "fileserver/embedded_files.h"
  10. #include "fileserver/token_subs.h"
  11. #define ESP_TRY(x) \
  12. do { \
  13. esp_err_t try_er = (x); \
  14. if (try_er != ESP_OK) return try_er; \
  15. } while(0)
  16. static const char* TAG = "token_subs";
  17. // TODO implement buffering to avoid sending many tiny chunks when escaping
  18. /* encode for HTML. returns 0 or 1 - 1 = success */
  19. static esp_err_t send_html_chunk(httpd_req_t *r, const char *data, ssize_t len)
  20. {
  21. assert(r);
  22. assert(data);
  23. int start = 0, end = 0;
  24. char c;
  25. if (len < 0) len = (int) strlen(data);
  26. if (len==0) return ESP_OK;
  27. for (end = 0; end < len; end++) {
  28. c = data[end];
  29. if (c == 0) {
  30. // we found EOS
  31. break; // not return - the last chunk is printed after the loop
  32. }
  33. if (c == '"' || c == '\'' || c == '<' || c == '>') {
  34. if (start < end) ESP_TRY(httpd_resp_send_chunk(r, data + start, end - start));
  35. start = end + 1;
  36. }
  37. if (c == '"') ESP_TRY(httpd_resp_send_chunk(r, "&#34;", 5));
  38. else if (c == '\'') ESP_TRY(httpd_resp_send_chunk(r, "&#39;", 5));
  39. else if (c == '<') ESP_TRY(httpd_resp_send_chunk(r, "&lt;", 4));
  40. else if (c == '>') ESP_TRY(httpd_resp_send_chunk(r, "&gt;", 4));
  41. }
  42. if (start < end) ESP_TRY(httpd_resp_send_chunk(r, data + start, end - start));
  43. return ESP_OK;
  44. }
  45. /* encode for JS. returns 0 or 1 - 1 = success */
  46. static esp_err_t send_js_chunk(httpd_req_t *r, const char *data, ssize_t len)
  47. {
  48. assert(r);
  49. assert(data);
  50. int start = 0, end = 0;
  51. char c;
  52. if (len < 0) len = (int) strlen(data);
  53. if (len==0) return ESP_OK;
  54. for (end = 0; end < len; end++) {
  55. c = data[end];
  56. if (c == 0) {
  57. // we found EOS
  58. break; // not return - the last chunk is printed after the loop
  59. }
  60. if (c == '"' || c == '\\' || c == '/' || c == '\'' || c == '<' || c == '>' || c == '\n' || c == '\r') {
  61. if (start < end) ESP_TRY(httpd_resp_send_chunk(r, data + start, end - start));
  62. start = end + 1;
  63. }
  64. if (c == '"') ESP_TRY(httpd_resp_send_chunk(r, "\\\"", 2));
  65. else if (c == '\'') ESP_TRY(httpd_resp_send_chunk(r, "\\'", 2));
  66. else if (c == '\\') ESP_TRY(httpd_resp_send_chunk(r, "\\\\", 2));
  67. else if (c == '/') ESP_TRY(httpd_resp_send_chunk(r, "\\/", 2));
  68. else if (c == '<') ESP_TRY(httpd_resp_send_chunk(r, "\\u003C", 6));
  69. else if (c == '>') ESP_TRY(httpd_resp_send_chunk(r, "\\u003E", 6));
  70. else if (c == '\n') ESP_TRY(httpd_resp_send_chunk(r, "\\n", 2));
  71. else if (c == '\r') ESP_TRY(httpd_resp_send_chunk(r, "\\r", 2));
  72. }
  73. if (start < end) ESP_TRY(httpd_resp_send_chunk(r, data + start, end - start));
  74. return ESP_OK;
  75. }
  76. esp_err_t httpd_resp_send_chunk_escaped(httpd_req_t *r, const char *buf, ssize_t len, tpl_escape_t escape)
  77. {
  78. switch (escape) {
  79. default: // this enum should be exhaustive, but in case something went wrong, just print it verbatim
  80. case TPL_ESCAPE_NONE:
  81. return httpd_resp_send_chunk(r, buf, len);
  82. case TPL_ESCAPE_HTML:
  83. return send_html_chunk(r, buf, len);
  84. case TPL_ESCAPE_JS:
  85. return send_js_chunk(r, buf, len);
  86. }
  87. }
  88. esp_err_t httpd_send_static_file(httpd_req_t *r, int file_index, tpl_escape_t escape, uint32_t opts)
  89. {
  90. assert(file_index < EMBEDDED_FILE_LOOKUP_LEN);
  91. const struct embedded_file_info *file = &EMBEDDED_FILE_LOOKUP[file_index];
  92. return httpd_send_static_file_struct(r, file, escape, opts);
  93. }
  94. esp_err_t httpd_send_static_file_struct(httpd_req_t *r, const struct embedded_file_info *file, tpl_escape_t escape, uint32_t opts)
  95. {
  96. if (0 == (opts & HTOPT_NO_HEADERS)) {
  97. ESP_TRY(httpd_resp_set_type(r, file->mime));
  98. ESP_TRY(httpd_resp_set_hdr(r, "Cache-Control", "max-age=86400, public, must-revalidate"));
  99. }
  100. ESP_TRY(httpd_resp_send_chunk_escaped(r, (const char *) file->start, (size_t)(file->end - file->start), escape));
  101. if (0 == (opts & HTOPT_NO_CLOSE)) {
  102. ESP_TRY(httpd_resp_send_chunk(r, NULL, 0));
  103. }
  104. return ESP_OK;
  105. }
  106. esp_err_t httpd_send_template_file(httpd_req_t *r,
  107. int file_index,
  108. template_subst_t replacer,
  109. void *context,
  110. uint32_t opts)
  111. {
  112. assert(file_index < EMBEDDED_FILE_LOOKUP_LEN);
  113. const struct embedded_file_info *file = &EMBEDDED_FILE_LOOKUP[file_index];
  114. return httpd_send_template_file_struct(r,file,replacer,context,opts);
  115. }
  116. esp_err_t httpd_send_template_file_struct(httpd_req_t *r,
  117. const struct embedded_file_info *file,
  118. template_subst_t replacer,
  119. void *context,
  120. uint32_t opts)
  121. {
  122. if (0 == (opts & HTOPT_NO_HEADERS)) {
  123. ESP_TRY(httpd_resp_set_type(r, file->mime));
  124. ESP_TRY(httpd_resp_set_hdr(r, "Cache-Control", "no-cache, no-store, must-revalidate"));
  125. }
  126. return httpd_send_template(r, (const char *) file->start, (size_t)(file->end - file->start), replacer, context, opts);
  127. }
  128. esp_err_t tpl_kv_replacer(httpd_req_t *r, void *context, const char *token, tpl_escape_t escape)
  129. {
  130. assert(context);
  131. assert(token);
  132. struct tpl_kv_entry *entry;
  133. struct tpl_kv_list *head = context;
  134. SLIST_FOREACH(entry, head, link) {
  135. if (0==strcmp(entry->key, token)) {
  136. if (entry->subst_heap) {
  137. if (entry->subst_heap[0]) {
  138. return httpd_resp_send_chunk_escaped(r, entry->subst_heap, -1, escape);
  139. }
  140. } else {
  141. if (entry->subst[0]) {
  142. return httpd_resp_send_chunk_escaped(r, entry->subst, -1, escape);
  143. }
  144. return ESP_OK;
  145. }
  146. return ESP_OK;
  147. }
  148. }
  149. return ESP_ERR_NOT_FOUND;
  150. }
  151. struct stacked_replacer_context {
  152. template_subst_t replacer0;
  153. void *context0;
  154. template_subst_t replacer1;
  155. void *context1;
  156. };
  157. esp_err_t stacked_replacer(httpd_req_t *r, void *context, const char *token, tpl_escape_t escape)
  158. {
  159. assert(context);
  160. assert(token);
  161. struct stacked_replacer_context *combo = context;
  162. if (ESP_OK == combo->replacer0(r, combo->context0, token, escape)) {
  163. return ESP_OK;
  164. }
  165. if (ESP_OK == combo->replacer1(r, combo->context1, token, escape)) {
  166. return ESP_OK;
  167. }
  168. return ESP_ERR_NOT_FOUND;
  169. }
  170. esp_err_t httpd_send_template(httpd_req_t *r,
  171. const char *template, ssize_t template_len,
  172. template_subst_t replacer,
  173. void *context,
  174. uint32_t opts)
  175. {
  176. if (template_len < 0) template_len = strlen(template);
  177. // replacer and context may be NULL
  178. assert(template);
  179. assert(r);
  180. // data end
  181. const char * const end = template + template_len;
  182. // start of to-be-processed data
  183. const char * pos = template;
  184. // start position for finding opening braces, updated after a failed match to avoid infinite loop on the same bad token
  185. const char * searchpos = pos;
  186. // tokens must be copied to a buffer to allow adding the terminating null byte
  187. char token_buf[MAX_TOKEN_LEN];
  188. while (pos < end) {
  189. const char * openbr = strchr(searchpos, '{');
  190. if (openbr == NULL) {
  191. // no more templates
  192. ESP_TRY(httpd_resp_send_chunk(r, pos, (size_t) (end - pos)));
  193. break;
  194. }
  195. // this brace could start a valid template. check if it seems valid...
  196. const char * closebr = strchr(openbr, '}');
  197. if (closebr == NULL) {
  198. // there are no further closing braces, so it can't be a template
  199. // we also know there can't be any more substitutions, because they would lack a closing } too
  200. ESP_TRY(httpd_resp_send_chunk(r, pos, (size_t) (end - pos)));
  201. break;
  202. }
  203. // see if the braces content looks like a token
  204. const char *t = openbr + 1;
  205. bool token_valid = true;
  206. struct tpl_kv_list substitutions_head = tpl_kv_init();
  207. struct tpl_kv_entry *new_subst_pair = NULL;
  208. // a token can be either a name for replacement by the replacer func, or an include with static kv replacements
  209. bool is_include = false;
  210. bool token_is_optional = false;
  211. const char *token_end = NULL; // points one char after the end of the token
  212. // parsing the token
  213. {
  214. if (*t == '@') {
  215. ESP_LOGD(TAG, "Parsing an Include token");
  216. is_include = true;
  217. t++;
  218. }
  219. enum {
  220. P_NAME, P_KEY, P_VALUE
  221. } state = P_NAME;
  222. const char *kv_start = NULL;
  223. while (t != closebr || state == P_VALUE) {
  224. char c = *t;
  225. if (state == P_NAME) {
  226. if (!((c >= 'a' && c <= 'z') ||
  227. (c >= 'A' && c <= 'Z') ||
  228. (c >= '0' && c <= '9') ||
  229. c == '.' || c == '_' || c == '-' || c == ':')) {
  230. if (!is_include && c == '?') {
  231. token_end = t;
  232. token_is_optional = true;
  233. } else {
  234. if (is_include && c == '|') {
  235. token_end = t;
  236. state = P_KEY;
  237. kv_start = t + 1;
  238. // pipe separates the include's filename and literal substitutions
  239. // we know there is a closing } somewhere, and {@....| doesn't occur normally, so let's assume it's correct
  240. }
  241. else {
  242. token_valid = false;
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. else if (state == P_KEY) {
  249. if (!((c >= 'a' && c <= 'z') ||
  250. (c >= 'A' && c <= 'Z') ||
  251. (c >= '0' && c <= '9') ||
  252. c == '.' || c == '_' || c == '-')) {
  253. if (c == '=') {
  254. new_subst_pair = calloc(1, sizeof(struct tpl_kv_entry));
  255. const size_t klen = MIN(TPL_KV_KEY_LEN, t - kv_start);
  256. strncpy(new_subst_pair->key, kv_start, klen);
  257. new_subst_pair->key[klen] = 0;
  258. kv_start = t + 1;
  259. state = P_VALUE;
  260. // pipe separates the include's filename and literal substitutions
  261. // we know there is a closing } somewhere, and {@....| doesn't occur normally, so let's assume it's correct
  262. }
  263. }
  264. }
  265. else if (state == P_VALUE) {
  266. if (c == '|' || c == '}') {
  267. const size_t vlen = MIN(TPL_KV_SUBST_LEN, t - kv_start);
  268. strncpy(new_subst_pair->subst, kv_start, vlen);
  269. new_subst_pair->subst[vlen] = 0;
  270. // attach the kv pair to the list
  271. SLIST_INSERT_HEAD(&substitutions_head, new_subst_pair, link);
  272. ESP_LOGD(TAG, "Adding subs kv %s -> %s", new_subst_pair->key, new_subst_pair->subst);
  273. new_subst_pair = NULL;
  274. kv_start = t + 1; // go past the pipe
  275. state = P_KEY;
  276. if (t == closebr) {
  277. break; // found the ending brace, so let's quit the kv parse loop
  278. }
  279. }
  280. }
  281. t++;
  282. }
  283. // clean up after a messed up subs kv pairs syntax
  284. if (new_subst_pair != NULL) {
  285. free(new_subst_pair);
  286. }
  287. }
  288. if (!token_valid) {
  289. // false match, include it in the block to send before the next token
  290. searchpos = openbr + 1;
  291. ESP_LOGD(TAG, "Skip invalid token near %10s", openbr);
  292. continue;
  293. }
  294. // now we know it looks like a substitution token
  295. // flush data before the token
  296. if (pos != openbr) ESP_TRY(httpd_resp_send_chunk(r, pos, (size_t) (openbr - pos)));
  297. const char *token_start = openbr;
  298. tpl_escape_t escape = TPL_ESCAPE_NONE;
  299. // extract and terminate the token
  300. size_t token_len = MIN(MAX_TOKEN_LEN-1, closebr - openbr - 1);
  301. if (token_end) {
  302. token_len = MIN(token_len, token_end - openbr - 1);
  303. }
  304. if (is_include) {
  305. token_start += 1; // skip the @
  306. token_len -= 1;
  307. } else {
  308. if (0 == strncmp("h:", openbr + 1, 2)) {
  309. escape = TPL_ESCAPE_HTML;
  310. token_start += 2;
  311. token_len -= 2;
  312. }
  313. else if (0 == strncmp("j:", openbr + 1, 2)) {
  314. escape = TPL_ESCAPE_JS;
  315. token_start += 2;
  316. token_len -= 2;
  317. }
  318. }
  319. strncpy(token_buf, token_start+1, token_len);
  320. token_buf[token_len] = 0;
  321. ESP_LOGD(TAG, "Token: %s", token_buf);
  322. esp_err_t rv;
  323. if (is_include) {
  324. ESP_LOGD(TAG, "Trying to include a sub-file");
  325. const struct embedded_file_info *file = NULL;
  326. rv = www_get_static_file(token_buf, FILE_ACCESS_PROTECTED, &file);
  327. if (rv != ESP_OK) {
  328. ESP_LOGE(TAG, "Failed to statically include \"%s\" in a template - %s", token_buf, esp_err_to_name(rv));
  329. // this will cause the token to be emitted verbatim
  330. } else {
  331. ESP_LOGD(TAG, "Descending...");
  332. // combine the two replacers
  333. struct stacked_replacer_context combo = {
  334. .replacer0 = replacer,
  335. .context0 = context,
  336. .replacer1 = tpl_kv_replacer,
  337. .context1 = &substitutions_head
  338. };
  339. rv = httpd_send_template_file_struct(r, file, stacked_replacer, &combo, HTOPT_INCLUDE);
  340. ESP_LOGD(TAG, "...back in parent");
  341. }
  342. // tear down the list
  343. tpl_kv_free(&substitutions_head);
  344. if (rv != ESP_OK) {
  345. // just send it verbatim...
  346. ESP_TRY(httpd_resp_send_chunk(r, openbr, (size_t) (closebr - openbr + 1)));
  347. }
  348. } else {
  349. if (replacer) {
  350. ESP_LOGD(TAG, "Running replacer for \"%s\" with escape %d", token_buf, escape);
  351. rv = replacer(r, context, token_buf, escape);
  352. if (rv != ESP_OK) {
  353. if (rv == ESP_ERR_NOT_FOUND) {
  354. ESP_LOGD(TAG, "Token rejected");
  355. // optional token becomes empty string if not replaced
  356. if (!token_is_optional) {
  357. ESP_LOGD(TAG, "Not optional, keeping verbatim");
  358. // replacer rejected the token, keep it verbatim
  359. ESP_TRY(httpd_resp_send_chunk(r, openbr, (size_t) (closebr - openbr + 1)));
  360. }
  361. }
  362. else {
  363. ESP_LOGE(TAG, "Unexpected error from replacer func: 0x%02x - %s", rv, esp_err_to_name(rv));
  364. return rv;
  365. }
  366. }
  367. } else {
  368. ESP_LOGD(TAG, "Not replacer");
  369. // no replacer, only includes - used for 'static' files
  370. if (!token_is_optional) {
  371. ESP_LOGD(TAG, "Token not optional, keeping verbatim");
  372. ESP_TRY(httpd_resp_send_chunk(r, openbr, (size_t) (closebr - openbr + 1)));
  373. }
  374. }
  375. }
  376. searchpos = pos = closebr + 1;
  377. }
  378. if (0 == (opts & HTOPT_NO_CLOSE)) {
  379. ESP_TRY(httpd_resp_send_chunk(r, NULL, 0));
  380. }
  381. return ESP_OK;
  382. }
  383. esp_err_t tpl_kv_add_int(struct tpl_kv_list *head, const char *key, int32_t num)
  384. {
  385. char buf[12];
  386. itoa(num, buf, 10);
  387. return tpl_kv_add(head, key, buf);
  388. }
  389. esp_err_t tpl_kv_add_long(struct tpl_kv_list *head, const char *key, int64_t num)
  390. {
  391. char buf[21];
  392. sprintf(buf, "%"PRIi64, num);
  393. return tpl_kv_add(head, key, buf);
  394. }
  395. esp_err_t tpl_kv_add_ipv4str(struct tpl_kv_list *head, const char *key, uint32_t ip4h)
  396. {
  397. char buf[IP4ADDR_STRLEN_MAX];
  398. ip4_addr_t addr;
  399. addr.addr = lwip_htonl(ip4h);
  400. ip4addr_ntoa_r(&addr, buf, IP4ADDR_STRLEN_MAX);
  401. return tpl_kv_add(head, key, buf);
  402. }
  403. esp_err_t tpl_kv_add(struct tpl_kv_list *head, const char *key, const char *subst)
  404. {
  405. ESP_LOGD(TAG, "kv add subs %s := %s", key, subst);
  406. struct tpl_kv_entry *entry = calloc(1, sizeof(struct tpl_kv_entry));
  407. if (entry == NULL) return ESP_ERR_NO_MEM;
  408. assert(strlen(key) < TPL_KV_KEY_LEN);
  409. assert(strlen(subst) < TPL_KV_SUBST_LEN);
  410. strncpy(entry->key, key, TPL_KV_KEY_LEN);
  411. entry->key[TPL_KV_KEY_LEN - 1] = 0;
  412. strncpy(entry->subst, subst, TPL_KV_SUBST_LEN - 1);
  413. entry->subst[TPL_KV_KEY_LEN - 1] = 0;
  414. SLIST_INSERT_HEAD(head, entry, link);
  415. return ESP_OK;
  416. }
  417. esp_err_t tpl_kv_add_heapstr(struct tpl_kv_list *head, const char *key, char *subst)
  418. {
  419. ESP_LOGD(TAG, "kv add subs %s := (heap str)", key);
  420. struct tpl_kv_entry *entry = calloc(1, sizeof(struct tpl_kv_entry));
  421. if (entry == NULL) return ESP_ERR_NO_MEM;
  422. assert(strlen(key) < TPL_KV_KEY_LEN);
  423. strncpy(entry->key, key, TPL_KV_KEY_LEN);
  424. entry->key[TPL_KV_KEY_LEN - 1] = 0;
  425. entry->subst_heap = subst;
  426. SLIST_INSERT_HEAD(head, entry, link);
  427. return ESP_OK;
  428. }
  429. esp_err_t tpl_kv_sprintf(struct tpl_kv_list *head, const char *key, const char *format, ...)
  430. {
  431. ESP_LOGD(TAG, "kv printf %s := %s", key, format);
  432. struct tpl_kv_entry *entry = calloc(1, sizeof(struct tpl_kv_entry));
  433. if (entry == NULL) return ESP_ERR_NO_MEM;
  434. assert(strlen(key) < TPL_KV_KEY_LEN);
  435. strncpy(entry->key, key, TPL_KV_KEY_LEN);
  436. entry->key[TPL_KV_KEY_LEN - 1] = 0;
  437. va_list list;
  438. va_start(list, format);
  439. vsnprintf(entry->subst, TPL_KV_SUBST_LEN, format, list);
  440. va_end(list);
  441. entry->subst[TPL_KV_KEY_LEN - 1] = 0;
  442. SLIST_INSERT_HEAD(head, entry, link);
  443. return ESP_OK;
  444. }
  445. void tpl_kv_free(struct tpl_kv_list *head)
  446. {
  447. struct tpl_kv_entry *item, *next;
  448. SLIST_FOREACH_SAFE(item, head, link, next) {
  449. if (item->subst_heap) {
  450. free(item->subst_heap);
  451. item->subst_heap = NULL;
  452. }
  453. free(item);
  454. }
  455. }
  456. esp_err_t tpl_kv_send_as_ascii_map(httpd_req_t *req, struct tpl_kv_list *head)
  457. {
  458. httpd_resp_set_type(req, "text/plain; charset=utf-8");
  459. #define BUF_CAP 512
  460. char *buf_head = malloc(BUF_CAP);
  461. if (!buf_head) {
  462. ESP_LOGE(TAG, "Malloc err");
  463. return ESP_FAIL;
  464. }
  465. char *buf = buf_head;
  466. size_t cap = BUF_CAP;
  467. struct tpl_kv_entry *entry;
  468. // GCC nested function
  469. esp_err_t send_part() {
  470. esp_err_t suc = httpd_resp_send_chunk(req, buf_head, BUF_CAP-cap);
  471. buf = buf_head; // buf is assigned to buf head
  472. cap = BUF_CAP;
  473. if (suc != ESP_OK) {
  474. ESP_LOGE(TAG, "Error sending buffer");
  475. free(buf_head);
  476. httpd_resp_send_chunk(req, NULL, 0);
  477. }
  478. return suc;
  479. }
  480. SLIST_FOREACH(entry, head, link) {
  481. while(NULL == (buf = append(buf, entry->key, &cap))) ESP_TRY(send_part());
  482. while(NULL == (buf = append(buf, "\x1f", &cap))) ESP_TRY(send_part());
  483. if (entry->subst_heap) {
  484. if (strlen(entry->subst_heap) >= BUF_CAP) {
  485. // send what we have
  486. ESP_TRY(send_part());
  487. esp_err_t suc = httpd_resp_send_chunk(req, entry->subst_heap, -1);
  488. if (suc != ESP_OK) {
  489. ESP_LOGE(TAG, "Error sending buffer");
  490. free(buf_head);
  491. httpd_resp_send_chunk(req, NULL, 0);
  492. }
  493. } else {
  494. while (NULL == (buf = append(buf, entry->subst_heap, &cap))) ESP_TRY(send_part());
  495. }
  496. } else {
  497. while(NULL == (buf = append(buf, entry->subst, &cap))) ESP_TRY(send_part());
  498. }
  499. if (entry->link.sle_next) {
  500. while(NULL == (buf = append(buf, "\x1e", &cap))) ESP_TRY(send_part());
  501. }
  502. }
  503. // send leftovers
  504. if (buf != buf_head) {
  505. esp_err_t suc = httpd_resp_send_chunk(req, buf_head, BUF_CAP-cap);
  506. if (suc != ESP_OK) {
  507. ESP_LOGE(TAG, "Error sending buffer");
  508. }
  509. }
  510. // Commit
  511. httpd_resp_send_chunk(req, NULL, 0);
  512. free(buf_head);
  513. return ESP_OK;
  514. }