From 1deaf2a9634576bf0e0f35a24911c3f22215fbd2 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 29 Jun 2014 09:30:09 -0600 Subject: Fixes for networking and tiny webserver from Max --- apps/netutils/webserver/Kconfig | 9 +++++++++ apps/netutils/webserver/httpd.c | 44 ++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'apps/netutils/webserver') diff --git a/apps/netutils/webserver/Kconfig b/apps/netutils/webserver/Kconfig index d984427a3..59dcc5f30 100644 --- a/apps/netutils/webserver/Kconfig +++ b/apps/netutils/webserver/Kconfig @@ -33,6 +33,15 @@ config NETUTILS_HTTPD_SCRIPT_DISABLE ---help--- This option, if selected, will elide the %! scripting +config NETUTILS_HTTPD_MAXPATH + bool "Maximum size of a path" + default 64 + ---help--- + This is the maximum size of a PATH used in the web server. This setting + is the logically the same as the PATH_MAX setting that (and in fact, if + not defined, the MAX_PATH setting will be used). This setting allows + more conservative memory allocation. + config NETUTILS_HTTPD_CGIPATH bool "URL/CGI function mapping" default n diff --git a/apps/netutils/webserver/httpd.c b/apps/netutils/webserver/httpd.c index 26989611b..ab67842ee 100644 --- a/apps/netutils/webserver/httpd.c +++ b/apps/netutils/webserver/httpd.c @@ -336,8 +336,9 @@ static int send_headers(struct httpd_state *pstate, int status, int len) { const char *mime; const char *ptr; - char cl[32]; - char s[128]; + char contentlen[HTTPD_MAX_CONTENTLEN]; + char header[HTTPD_MAX_HEADERLEN]; + int hdrlen; int i; static const struct @@ -381,7 +382,8 @@ static int send_headers(struct httpd_state *pstate, int status, int len) if (len >= 0) { - (void) snprintf(cl, sizeof cl, "Content-Length: %d\r\n", len); + (void)snprintf(contentlen, HTTPD_MAX_CONTENTLEN, + "Content-Length: %d\r\n", len); } #ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE else @@ -395,26 +397,32 @@ static int send_headers(struct httpd_state *pstate, int status, int len) /* TODO: here we "SHOULD" include a Retry-After header */ } - i = snprintf(s, sizeof s, - "HTTP/1.0 %d %s\r\n" + /* Construct the header. + * + * REVISIT: Wouldn't asprintf be a better option than a large stack + * array? + */ + + hdrlen = snprintf(header, HTTPD_MAX_HEADERLEN, + "HTTP/1.0 %d %s\r\n" #ifndef CONFIG_NETUTILS_HTTPD_SERVERHEADER_DISABLE - "Server: uIP/NuttX http://nuttx.org/\r\n" -#endif - "Connection: %s\r\n" - "Content-type: %s\r\n" - "%s" - "\r\n", - status, - status >= 400 ? "Error" : "OK", + "Server: uIP/NuttX http://nuttx.org/\r\n" +#endif + "Connection: %s\r\n" + "Content-type: %s\r\n" + "%s" + "\r\n", + status, + status >= 400 ? "Error" : "OK", #ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE - pstate->ht_keepalive ? "keep-alive" : "close", + pstate->ht_keepalive ? "keep-alive" : "close", #else - "close", + "close", #endif - mime, - len >= 0 ? cl : ""); + mime, + len >= 0 ? contentlen : ""); - return send_chunk(pstate, s, i); + return send_chunk(pstate, header, hdrlen); } static int httpd_senderror(struct httpd_state *pstate, int status) -- cgit v1.2.3