summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2012-09-05 18:03:37 +0000
committerpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2012-09-05 18:03:37 +0000
commitcbb3953ab80ebbdd106e50b3354f285d7a84ee20 (patch)
treece1a329f597da59fb575f6d476befe38affd2780
parentf330280f76501ca2174b59913aeeb4ab5ca87802 (diff)
Add URL/CGI function mapping option to uIP web server
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@5096 7fd9a85b-ad96-42d3-883c-3090e2eb8679
-rwxr-xr-xapps/ChangeLog.txt2
-rw-r--r--apps/netutils/webserver/Kconfig27
-rw-r--r--apps/netutils/webserver/httpd.c25
-rw-r--r--apps/netutils/webserver/httpd_cgi.c10
4 files changed, 54 insertions, 10 deletions
diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt
index 0713a2eb5b..6d5f605c7c 100755
--- a/apps/ChangeLog.txt
+++ b/apps/ChangeLog.txt
@@ -308,3 +308,5 @@
* apps/netutils/webserver: Several inenhancements from Kate including the
ability to elide scripting and SERVER headers and the ability to map
files into memory before transferring them.
+ * apps/netutils/webserver: Add ability to map a URL to CGI function.
+ Contributed by Kate.
diff --git a/apps/netutils/webserver/Kconfig b/apps/netutils/webserver/Kconfig
index ef7171a8ef..c3ebe755d5 100644
--- a/apps/netutils/webserver/Kconfig
+++ b/apps/netutils/webserver/Kconfig
@@ -19,6 +19,33 @@ config NETUTILS_HTTPD_SCRIPT_DISABLE
---help---
This option, if selected, will elide the %! scripting
+config NETUTILS_HTTPD_CGIPATH
+ bool "URL/CGI function mapping"
+ default n
+ ---help---
+ This option enables mappings from URLs to call CGI functions. The
+ effect is that the existing httpd_cgi_register() interface can be
+ used thus:
+
+ const static struct httpd_cgi_call a[] = {
+ { NULL, "/abc", cgi_abc },
+ { NULL, "/xyz", cgi_xyz }
+ };
+
+ for (i = 0; i < sizeof a / sizeof *a; i++) {
+ httpd_cgi_register(&a[i]);
+ }
+
+ Where (under CONFIG_NETUTILS_HTTPD_CGIPATH) the "/xyz" is a URL path,
+ rather than a %! xyz style call in the existing manner.
+
+ This is useful when CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE is defined.
+
+ In other words, this provides a way to get your CGI functions called
+ without needing the scripting language. I'm using this to provide a
+ REST style interface over HTTP, where my CGI handlers just return a
+ HTTP status code with a content length of 0.
+
config NETUTILS_HTTPD_SERVERHEADER_DISABLE
bool "Disabled the SERVER header"
default n
diff --git a/apps/netutils/webserver/httpd.c b/apps/netutils/webserver/httpd.c
index 0e255416ac..2fd0191573 100644
--- a/apps/netutils/webserver/httpd.c
+++ b/apps/netutils/webserver/httpd.c
@@ -214,7 +214,13 @@ static int handle_script(struct httpd_state *pstate)
}
else
{
- httpd_cgi(pstate->ht_scriptptr)(pstate, pstate->ht_scriptptr);
+ httpd_cgifunction f;
+
+ f = httpd_cgi(pstate->ht_scriptptr);
+ if (f != NULL)
+ {
+ f(pstate, pstate->ht_scriptptr);
+ }
}
next_scriptstate(pstate);
@@ -392,6 +398,21 @@ static int httpd_sendfile(struct httpd_state *pstate)
nvdbg("[%d] sending file '%s'\n", pstate->ht_sockfd, pstate->ht_filename);
+#ifdef CONFIG_NETUTILS_HTTPD_CGIPATH
+ {
+ httpd_cgifunction f;
+
+ f = httpd_cgi(pstate->ht_filename);
+ if (f != NULL)
+ {
+ f(pstate, pstate->ht_filename);
+
+ ret = OK;
+ goto done;
+ }
+ }
+#endif
+
if (httpd_open(pstate->ht_filename, &pstate->ht_file) != OK)
{
ndbg("[%d] '%s' not found\n", pstate->ht_sockfd, pstate->ht_filename);
@@ -434,6 +455,8 @@ static int httpd_sendfile(struct httpd_state *pstate)
(void)httpd_close(&pstate->ht_file);
+done:
+
/* Send anything remaining in the buffer */
if (ret == OK && pstate->ht_sndlen > 0)
diff --git a/apps/netutils/webserver/httpd_cgi.c b/apps/netutils/webserver/httpd_cgi.c
index 62df72e4a2..fa11f12ec9 100644
--- a/apps/netutils/webserver/httpd_cgi.c
+++ b/apps/netutils/webserver/httpd_cgi.c
@@ -58,14 +58,6 @@ struct httpd_cgi_call *cgi_calls = NULL;
* Public Functions
****************************************************************************/
-/****************************************************************************
- * Name: nullfunction
- ****************************************************************************/
-
-static void nullfunction(struct httpd_state *pstate, char *ptr)
-{
-}
-
void httpd_cgi_register(struct httpd_cgi_call *cgi_call)
{
if (cgi_calls == NULL)
@@ -92,5 +84,5 @@ httpd_cgifunction httpd_cgi(char *name)
cgi_call = cgi_call->next;
}
- return nullfunction;
+ return NULL;
}