summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-06-29 09:30:09 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-06-29 09:30:09 -0600
commit1deaf2a9634576bf0e0f35a24911c3f22215fbd2 (patch)
treecfc63769fd41a0e4417e2a723d79e4ba8fb6f077 /apps
parent02bad8e430f112e7f0bac29f52861c7048cb2bbe (diff)
downloadnuttx-1deaf2a9634576bf0e0f35a24911c3f22215fbd2.tar.gz
nuttx-1deaf2a9634576bf0e0f35a24911c3f22215fbd2.tar.bz2
nuttx-1deaf2a9634576bf0e0f35a24911c3f22215fbd2.zip
Fixes for networking and tiny webserver from Max
Diffstat (limited to 'apps')
-rw-r--r--apps/include/netutils/httpd.h52
-rw-r--r--apps/netutils/webserver/Kconfig9
-rw-r--r--apps/netutils/webserver/httpd.c44
3 files changed, 68 insertions, 37 deletions
diff --git a/apps/include/netutils/httpd.h b/apps/include/netutils/httpd.h
index 56e5da38a..e18542df1 100644
--- a/apps/include/netutils/httpd.h
+++ b/apps/include/netutils/httpd.h
@@ -1,7 +1,7 @@
/****************************************************************************
* apps/include/netutils/httpd.h
*
- * Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2007, 2009, 2011-2012, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based on uIP which also has a BSD style license:
@@ -44,22 +44,17 @@
* Included Files
****************************************************************************/
+#include <nuttx/net/tcp.h>
#include <nuttx/net/uip.h>
#include <stdint.h>
#include <stdbool.h>
+#include <limits.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
-#ifdef __cplusplus
-# define EXTERN extern "C"
-extern "C" {
-#else
-# define EXTERN extern
-#endif
-
/* As threads are created to handle each request, a stack must be allocated
* for the thread. Use a default if the user provided no stacksize.
*/
@@ -89,12 +84,19 @@ extern "C" {
/* This is the maximum size of a file path */
-#if defined(CONFIG_NETUTILS_HTTPD_MMAP) || defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
-#define HTTPD_MAX_FILENAME PATH_MAX
-#else
-#define HTTPD_MAX_FILENAME 20
+#ifndef CONFIG_NETUTILS_HTTPD_MAXPATH
+# define CONFIG_NETUTILS_HTTPD_MAXPATH PATH_MAX
#endif
+#define HTTPD_MAX_FILENAME CONFIG_HTTPD_MAXPATH
+
+/* Other tunable values. If you need to change these values, please create
+ * new configurations in apps/netutils/webserver/Kconfig
+ */
+
+#defien HTTPD_MAX_CONTENTLEN 32
+#defien HTTPD_MAX_HEADERLEN 180
+
/****************************************************************************
* Public types
****************************************************************************/
@@ -160,7 +162,7 @@ struct httpd_cgi_call
* then added to the list of HTTPD CGI functions with the httpd_cgi_register()
* function.
- * Input Paramters:
+ * Input Parameters:
*
* name The C variable name of the function
* str The string name of the function, used in the script file
@@ -172,16 +174,28 @@ static void function(struct httpd_state *, char *); \
static struct httpd_cgi_call name = {NULL, str, function}
/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+# define EXTERN extern "C"
+extern "C"
+{
+#else
+# define EXTERN extern
+#endif
+
+/****************************************************************************
* Public Function Prototypes
****************************************************************************/
-EXTERN void httpd_init(void);
-EXTERN int httpd_listen(void);
-EXTERN void httpd_cgi_register(struct httpd_cgi_call *cgi_call);
-EXTERN uint16_t httpd_fs_count(char *name);
+void httpd_init(void);
+int httpd_listen(void);
+void httpd_cgi_register(struct httpd_cgi_call *cgi_call);
+uint16_t httpd_fs_count(char *name);
-EXTERN const struct httpd_fsdata_file g_httpdfs_root[];
-EXTERN const int g_httpd_numfiles;
+const struct httpd_fsdata_file g_httpdfs_root[];
+const int g_httpd_numfiles;
#undef EXTERN
#ifdef __cplusplus
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)