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.
This commit is contained in:
Giuseppe Modugno 2017-11-27 09:18:41 +01:00 committed by goldsimon
parent 7edbd95b50
commit ab281cb750

View File

@ -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;