From 8bd670430ae70d36c19cde01c0c43c1bd0dbf7f9 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Thu, 16 Nov 2017 22:24:17 +0100 Subject: [PATCH] httpd: ssi: move checking file extensions against g_pcSSIExtensions array into its own function guarded by LWIP_HTTPD_SSI_BY_FILE_EXTENSION --- src/apps/httpd/httpd.c | 70 ++++++++++++++++++------------ src/include/lwip/apps/httpd_opts.h | 12 +++++ 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/apps/httpd/httpd.c b/src/apps/httpd/httpd.c index 36380ea4..b3ca15b4 100644 --- a/src/apps/httpd/httpd.c +++ b/src/apps/httpd/httpd.c @@ -2087,6 +2087,47 @@ badrequest: } } +#if LWIP_HTTPD_SSI && (LWIP_HTTPD_SSI_BY_FILE_EXTENSION == 1) +/* Check if SSI should be parsed for this file/URL + * (With LWIP_HTTPD_SSI_BY_FILE_EXTENSION == 2, this function can be + * overridden by an external implementation.) + * + * @return 1 for SSI, 0 for standard files + */ +static u8_t +http_uri_is_ssi(struct fs_file *file, const char *uri) +{ + size_t loop; + u8_t tag_check = 0; + if (file != NULL) { + /* See if we have been asked for an shtml file and, if so, + enable tag checking. */ + const char *ext = NULL, *sub; + char *param = (char *)strstr(uri, "?"); + if (param != NULL) { + /* separate uri from parameters for now, set back later */ + *param = 0; + } + sub = uri; + ext = uri; + for (sub = strstr(sub, "."); sub != NULL; sub = strstr(sub, ".")) { + ext = sub; + sub++; + } + for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) { + if (!lwip_stricmp(ext, g_pcSSIExtensions[loop])) { + tag_check = 1; + break; + } + } + if (param != NULL) { + *param = '?'; + } + } + return tag_check; +} +#endif /* LWIP_HTTPD_SSI */ + /** Try to find the file specified by uri and, if found, initialize hs * accordingly. * @@ -2193,33 +2234,8 @@ http_find_file(struct http_state *hs, const char *uri, int is_09) } else { file = http_get_404_file(hs, &uri); } -#if LWIP_HTTPD_SSI - if (file != NULL) { - /* See if we have been asked for an shtml file and, if so, - enable tag checking. */ - const char *ext = NULL, *sub; - char *param = (char *)strstr(uri, "?"); - if (param != NULL) { - /* separate uri from parameters for now, set back later */ - *param = 0; - } - sub = uri; - ext = uri; - for (sub = strstr(sub, "."); sub != NULL; sub = strstr(sub, ".")) { - ext = sub; - sub++; - } - tag_check = 0; - for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) { - if (!lwip_stricmp(ext, g_pcSSIExtensions[loop])) { - tag_check = 1; - break; - } - } - if (param != NULL) { - *param = '?'; - } - } +#if LWIP_HTTPD_SSI && LWIP_HTTPD_SSI_BY_FILE_EXTENSION + tag_check = http_uri_is_ssi(file, uri); #endif /* LWIP_HTTPD_SSI */ } if (file == NULL) { diff --git a/src/include/lwip/apps/httpd_opts.h b/src/include/lwip/apps/httpd_opts.h index 2d2191be..a107ee7e 100644 --- a/src/include/lwip/apps/httpd_opts.h +++ b/src/include/lwip/apps/httpd_opts.h @@ -109,6 +109,18 @@ #define LWIP_HTTPD_SSI_RAW 0 #endif +/** Set this to 0 to prevent parsing the file extension at runtime to decide + * if a file should be scanned for SSI tags or not. + * Default is 1 (file extensions are checked using the g_pcSSIExtensions array) + * Set to 2 to override this runtime test function. + * + * This is enabled by default, but if you only use a newer version of makefsdata + * supporting the "-ssi" option, this info is already present in + */ +#if !defined LWIP_HTTPD_SSI_BY_FILE_EXTENSION || defined __DOXYGEN__ +#define LWIP_HTTPD_SSI_BY_FILE_EXTENSION 1 +#endif + /** Set this to 1 to support HTTP POST */ #if !defined LWIP_HTTPD_SUPPORT_POST || defined __DOXYGEN__ #define LWIP_HTTPD_SUPPORT_POST 0