(d: TemplateData, source: TransformedSource)
| 2 | import { cacheCtrl, sw } from './cppCode'; |
| 3 | |
| 4 | const genEspIdfFileHandler = (d: TemplateData, source: TransformedSource): string => { |
| 5 | const path = `${d.basePath}/${source.filename}`; |
| 6 | const lines: string[] = [`static esp_err_t file_handler_${source.datanameUpperCase} (httpd_req_t *req)`, '{']; |
| 7 | const etagCheck = sw(d.etag, { |
| 8 | always: [ |
| 9 | ` size_t hdr_len = httpd_req_get_hdr_value_len(req, "If-None-Match");`, |
| 10 | ` if (hdr_len > 0) {`, |
| 11 | ` char* hdr_value = malloc(hdr_len + 1);`, |
| 12 | ` if (hdr_value == NULL) { httpd_resp_send_500(req); return ESP_FAIL; }`, |
| 13 | ` if (httpd_req_get_hdr_value_str(req, "If-None-Match", hdr_value, hdr_len + 1) == ESP_OK) {`, |
| 14 | ` if (strcmp(hdr_value, etag_${source.dataname}) == 0) {`, |
| 15 | ` free(hdr_value);`, |
| 16 | ` httpd_resp_set_status(req, "304 Not Modified");`, |
| 17 | ` ${d.definePrefix}_onFileServed("${path}", 304);`, |
| 18 | ` httpd_resp_send(req, NULL, 0);`, |
| 19 | ` return ESP_OK;`, |
| 20 | ` }`, |
| 21 | ` }`, |
| 22 | ` free(hdr_value);`, |
| 23 | ` }` |
| 24 | ].join('\n'), |
| 25 | compiler: [ |
| 26 | ` #ifdef ${d.definePrefix}_ENABLE_ETAG`, |
| 27 | ` size_t hdr_len = httpd_req_get_hdr_value_len(req, "If-None-Match");`, |
| 28 | ` if (hdr_len > 0) {`, |
| 29 | ` char* hdr_value = malloc(hdr_len + 1);`, |
| 30 | ` if (hdr_value == NULL) { httpd_resp_send_500(req); return ESP_FAIL; }`, |
| 31 | ` if (httpd_req_get_hdr_value_str(req, "If-None-Match", hdr_value, hdr_len + 1) == ESP_OK) {`, |
| 32 | ` if (strcmp(hdr_value, etag_${source.dataname}) == 0) {`, |
| 33 | ` free(hdr_value);`, |
| 34 | ` httpd_resp_set_status(req, "304 Not Modified");`, |
| 35 | ` ${d.definePrefix}_onFileServed("${path}", 304);`, |
| 36 | ` httpd_resp_send(req, NULL, 0);`, |
| 37 | ` return ESP_OK;`, |
| 38 | ` }`, |
| 39 | ` }`, |
| 40 | ` free(hdr_value);`, |
| 41 | ` }`, |
| 42 | ` #endif` |
| 43 | ].join('\n') |
| 44 | }); |
| 45 | if (etagCheck) lines.push(etagCheck); |
| 46 | lines.push(` httpd_resp_set_type(req, "${source.mime}");`); |
| 47 | const gzipEncoding = sw(d.gzip, { |
| 48 | always: source.isGzip ? ` httpd_resp_set_hdr(req, "Content-Encoding", "gzip");` : '', |
| 49 | compiler: source.isGzip |
| 50 | ? [ |
| 51 | ` #ifdef ${d.definePrefix}_ENABLE_GZIP`, |
| 52 | ` httpd_resp_set_hdr(req, "Content-Encoding", "gzip");`, |
| 53 | ` #endif` |
| 54 | ].join('\n') |
| 55 | : '' |
| 56 | }); |
| 57 | if (gzipEncoding) lines.push(gzipEncoding); |
| 58 | const cacheHeaders = sw(d.etag, { |
| 59 | always: [ |
| 60 | ` httpd_resp_set_hdr(req, "Cache-Control", "${cacheCtrl(source)}");`, |
| 61 | ` httpd_resp_set_hdr(req, "ETag", etag_${source.dataname});` |
no test coverage detected