From ab281cb7503cb85ef0f44f4f13213380b52cbce5 Mon Sep 17 00:00:00 2001 From: Giuseppe Modugno Date: Mon, 27 Nov 2017 09:18:41 +0100 Subject: [PATCH] httpd: move httpd_cgi_handler() call This patch moves in advance httpd_cgi_handler() call, before assigning variables like hs->file and hs->left. In this way, CGI handler could prepare the reply data and set the "file properties", like file->data and file->len. At the exit, hs->file and hs->left is correctly assigned. The handler prototype says it can't access file pointer, however it is simple to obtain it after setting LWIP_HTTPD_FILE_STATE that enables a "file state" pointer. It is sufficient to assign file->state to file itself in fs_open_custom(). For example, I have the request GET /login.cgi?user=admin&pwd=admin and I have to reply with some JSON data. The answer depends on parameters user and pwd passed in the query string. --- src/apps/httpd/httpd.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/apps/httpd/httpd.c b/src/apps/httpd/httpd.c index 88f76a17..276ec245 100644 --- a/src/apps/httpd/httpd.c +++ b/src/apps/httpd/httpd.c @@ -2292,6 +2292,27 @@ http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const cha LWIP_UNUSED_ARG(tag_check); #endif /* LWIP_HTTPD_SSI */ hs->handle = file; +#if LWIP_HTTPD_CGI_SSI + if (params != NULL) { + /* URI contains parameters, call generic CGI handler */ + int count; +#if LWIP_HTTPD_CGI + if (http_cgi_paramcount >= 0) { + count = http_cgi_paramcount; + } else +#endif + { + count = extract_uri_parameters(hs, params); + } + httpd_cgi_handler(uri, count, http_cgi_params, http_cgi_param_vals +#if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE + , hs->handle->state +#endif /* LWIP_HTTPD_FILE_STATE */ + ); + } +#else /* LWIP_HTTPD_CGI_SSI */ + LWIP_UNUSED_ARG(params); +#endif /* LWIP_HTTPD_CGI_SSI */ hs->file = file->data; LWIP_ASSERT("File length must be positive!", (file->len >= 0)); #if LWIP_HTTPD_CUSTOM_FILES @@ -2323,27 +2344,6 @@ http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const cha } } #endif /* LWIP_HTTPD_SUPPORT_V09*/ -#if LWIP_HTTPD_CGI_SSI - if (params != NULL) { - /* URI contains parameters, call generic CGI handler */ - int count; -#if LWIP_HTTPD_CGI - if (http_cgi_paramcount >= 0) { - count = http_cgi_paramcount; - } else -#endif - { - count = extract_uri_parameters(hs, params); - } - httpd_cgi_handler(uri, count, http_cgi_params, http_cgi_param_vals -#if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE - , hs->handle->state -#endif /* LWIP_HTTPD_FILE_STATE */ - ); - } -#else /* LWIP_HTTPD_CGI_SSI */ - LWIP_UNUSED_ARG(params); -#endif /* LWIP_HTTPD_CGI_SSI */ } else { hs->handle = NULL; hs->file = NULL;