summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-09-11 13:19:59 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-09-11 13:19:59 +0000
commit26ef7179b0f6c3be8c76eefdca033392209ee42d (patch)
tree06ea25a4404d94f98a2c42a56c34a9dfeb7a3aac /apps
parentff113b400adf80f5bf6f07233a2355e48b00676d (diff)
downloadpx4-nuttx-26ef7179b0f6c3be8c76eefdca033392209ee42d.tar.gz
px4-nuttx-26ef7179b0f6c3be8c76eefdca033392209ee42d.tar.bz2
px4-nuttx-26ef7179b0f6c3be8c76eefdca033392209ee42d.zip
AVR corrections from Richard Cochran; uIP webserver enhancements from Kate
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5126 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'apps')
-rwxr-xr-xapps/ChangeLog.txt4
-rw-r--r--apps/netutils/webserver/Kconfig37
-rw-r--r--apps/netutils/webserver/Makefile4
-rw-r--r--apps/netutils/webserver/httpd.c26
-rw-r--r--apps/netutils/webserver/httpd.h16
-rw-r--r--apps/netutils/webserver/httpd_sendfile.c128
6 files changed, 204 insertions, 11 deletions
diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt
index da56f5f3e..833a62cd1 100755
--- a/apps/ChangeLog.txt
+++ b/apps/ChangeLog.txt
@@ -316,4 +316,6 @@
(submiteed by Kate).
* apps/netutils/webserver/httpd_mmap.c: Fix errors when the mmap()
length is zero (submitted by Kate).
-
+ * apps/netutils/webserver/httpd_sendfile.c: Add and option,
+ CONFIG_NETUTILS_HTTPD_SENDFILE to transfer files using the NuttX
+ sendfile() interface.
diff --git a/apps/netutils/webserver/Kconfig b/apps/netutils/webserver/Kconfig
index c3ebe755d..fd17a6a97 100644
--- a/apps/netutils/webserver/Kconfig
+++ b/apps/netutils/webserver/Kconfig
@@ -15,7 +15,8 @@ if NETUTILS_WEBSERVER
config NETUTILS_HTTPD_SCRIPT_DISABLE
bool "Disable %! scripting"
- default n
+ default y if NETUTILS_HTTPD_SENDFILE
+ default n if !NETUTILS_HTTPD_SENDFILE
---help---
This option, if selected, will elide the %! scripting
@@ -52,10 +53,40 @@ config NETUTILS_HTTPD_SERVERHEADER_DISABLE
---help---
This option, if selected, will elide the Server\: header
+choice
+ prompt "File Transfer Method"
+ default NETUTILS_HTTPD_CLASSIC
+
+config NETUTILS_HTTPD_CLASSIC
+ bool "Pre-processed files"
+ ---help---
+ Traditionally, the uIP-based webserver only sends "files" that have
+ been prepared as a data structure using nutts/tools/mkfsdata.pl
+
config NETUTILS_HTTPD_MMAP
bool "File mmap-ing"
- default n
---help---
- Replaces standard uIP server file open operations with mmap-ing operations.
+ Traditionally, the uIP-based webserver only sends "files" that have
+ been prepared as a data structure using nutts/tools/mkfsdata.pl
+ However, extensions have been contributed. If this option is
+ selected, then files can be accessed from the NuttX file system
+ as well. This selection will map the files into memory (using mmap)
+ so that the logic is still basically compatible with the classic
+ approach.
+
+config NETUTILS_HTTPD_MMAP
+ bool "sendfile()"
+ select NETUTILS_HTTPD_SCRIPT_DISABLE
+ ---help---
+ Traditionally, the uIP-based webserver only sends "files" that have
+ been prepared as a data structure using nutts/tools/mkfsdata.pl
+ However, extensions have been contributed. If this option is
+ selected, then files can be accessed from the NuttX file system
+ as well. This selection will use the NuttX sendfile() interface
+ to send files. NOTE: if this option is selected, then scripting
+ must be disabled.
+
+endchoice
+
endif
diff --git a/apps/netutils/webserver/Makefile b/apps/netutils/webserver/Makefile
index 6d96c8fc5..4b1f2f9f3 100644
--- a/apps/netutils/webserver/Makefile
+++ b/apps/netutils/webserver/Makefile
@@ -44,7 +44,9 @@ CSRCS =
ifeq ($(CONFIG_NET_TCP),y)
CSRCS = httpd.c httpd_cgi.c
-ifeq ($(CONFIG_NETUTILS_HTTPD_MMAP),y)
+ifeq ($(CONFIG_NETUTILS_HTTPD_SENDFILE),y)
+CSRCS += httpd_sendfile.c
+else ifeq ($(CONFIG_NETUTILS_HTTPD_MMAP),y)
CSRCS += httpd_mmap.c
else
CSRCS += httpd_fs.c
diff --git a/apps/netutils/webserver/httpd.c b/apps/netutils/webserver/httpd.c
index 2fd019157..0c0ee0389 100644
--- a/apps/netutils/webserver/httpd.c
+++ b/apps/netutils/webserver/httpd.c
@@ -69,6 +69,14 @@
* Pre-processor Definitions
****************************************************************************/
+#if !defined(CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE) && defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
+# error "Script support and CONFIG_NETUTILS_HTTPD_SENDFILE are mutually exclusive"
+#endif
+
+#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE) && defined(CONFIG_NETUTILS_HTTPD_MMAP)
+# error "CONFIG_NETUTILS_HTTPD_SENDFILE and CONFIG_NETUTILS_HTTPD_MMAP are mutually exclusive"
+#endif
+
#define ISO_nl 0x0a
#define ISO_space 0x20
#define ISO_bang 0x21
@@ -131,7 +139,9 @@ static const char g_httpheader404[] =
static int httpd_open(const char *name, struct httpd_fs_file *file)
{
-#ifdef CONFIG_NETUTILS_HTTPD_MMAP
+#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
+ return httpd_sendfile_open(name, file);
+#elif defined(CONFIG_NETUTILS_HTTPD_MMAP)
return httpd_mmap_open(name, file);
#else
return httpd_fs_open(name, file);
@@ -140,7 +150,9 @@ static int httpd_open(const char *name, struct httpd_fs_file *file)
static int httpd_close(struct httpd_fs_file *file)
{
-#ifdef CONFIG_NETUTILS_HTTPD_MMAP
+#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
+ return httpd_sendfile_close(file);
+#elif defined(CONFIG_NETUTILS_HTTPD_MMAP)
return httpd_mmap_close(file);
#else
return OK;
@@ -424,7 +436,11 @@ static int httpd_sendfile(struct httpd_state *pstate)
if (send_headers(pstate, g_httpheader404, strlen(g_httpheader404)) == OK)
{
+#ifdef CONFIG_NETUTILS_HTTPD_SENDFILE
+ ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file);
+#else
ret = httpd_addchunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
+#endif
}
}
else
@@ -447,7 +463,11 @@ static int httpd_sendfile(struct httpd_state *pstate)
else
#endif
{
+#ifdef CONFIG_NETUTILS_HTTPD_SENDFILE
+ ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file);
+#else
ret = httpd_addchunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
+#endif
}
}
}
@@ -603,7 +623,7 @@ int httpd_listen(void)
void httpd_init(void)
{
-#ifndef CONFIG_NETUTILS_HTTPD_MMAP
+#if !defined(CONFIG_NETUTILS_HTTPD_MMAP) && !defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
httpd_fs_init();
#endif
}
diff --git a/apps/netutils/webserver/httpd.h b/apps/netutils/webserver/httpd.h
index 42a1e13f7..dbe3d0e20 100644
--- a/apps/netutils/webserver/httpd.h
+++ b/apps/netutils/webserver/httpd.h
@@ -58,12 +58,22 @@
/* 'file' must be allocated by caller and will be filled in by the function. */
-int httpd_fs_open(const char *name, struct httpd_fs_file *file);
-void httpd_fs_init(void);
+#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
+
+int httpd_sendfile_open(const char *name, struct httpd_fs_file *file);
+int httpd_sendfile_close(struct httpd_fs_file *file);
+int httpd_sendfile_send(int outfd, struct httpd_fs_file *file);
+
+#elif defined(CONFIG_NETUTILS_HTTPD_MMAP)
-#ifdef CONFIG_NETUTILS_HTTPD_MMAP
int httpd_mmap_open(const char *name, struct httpd_fs_file *file);
int httpd_mmap_close(struct httpd_fs_file *file);
+
+#else
+
+int httpd_fs_open(const char *name, struct httpd_fs_file *file);
+void httpd_fs_init(void);
+
#endif
#endif /* _NETUTILS_WEBSERVER_HTTPD_H */
diff --git a/apps/netutils/webserver/httpd_sendfile.c b/apps/netutils/webserver/httpd_sendfile.c
new file mode 100644
index 000000000..6a6ec24ab
--- /dev/null
+++ b/apps/netutils/webserver/httpd_sendfile.c
@@ -0,0 +1,128 @@
+/****************************************************************************
+ * netutils/webserver/httpd_mmap.c
+ *
+ * Copyright (C) 2012 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Header Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sendfile.h>
+
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <apps/netutils/httpd.h>
+
+#include "httpd.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int httpd_sendfile_open(const char *name, struct httpd_fs_file *file)
+{
+ char path[PATH_MAX];
+ struct stat st;
+
+ if (sizeof path < snprintf(path, sizeof path, "%s%s",
+ CONFIG_NETUTILS_HTTPD_PATH, name))
+ {
+ errno = ENAMETOOLONG;
+ return ERROR;
+ }
+
+ /* XXX: awaiting fstat to avoid a race */
+
+ if (-1 == stat(path, &st))
+ {
+ return ERROR;
+ }
+
+ if (st.st_size > INT_MAX || st.st_size > SIZE_MAX)
+ {
+ errno = EFBIG;
+ return ERROR;
+ }
+
+ file->len = (int) st.st_size;
+
+ file->fd = open(path, O_RDONLY);
+ if (file->fd == -1)
+ {
+ return ERROR;
+ }
+
+ return OK;
+}
+
+int httpd_sendfile_close(struct httpd_fs_file *file)
+{
+ if (-1 == close(file->fd))
+ {
+ return ERROR;
+ }
+
+ return OK;
+}
+
+int httpd_sendfile_send(int outfd, struct httpd_fs_file *file)
+{
+ if (-1 == sendfile(outfd, file->fd, 0, file->len))
+ {
+ return ERROR;
+ }
+
+ return OK;
+}
+