From 26ef7179b0f6c3be8c76eefdca033392209ee42d Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 11 Sep 2012 13:19:59 +0000 Subject: 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 --- apps/netutils/webserver/Kconfig | 37 ++++++++- apps/netutils/webserver/Makefile | 4 +- apps/netutils/webserver/httpd.c | 26 ++++++- apps/netutils/webserver/httpd.h | 16 +++- apps/netutils/webserver/httpd_sendfile.c | 128 +++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 apps/netutils/webserver/httpd_sendfile.c (limited to 'apps/netutils') 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 + * + * 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 + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#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; +} + -- cgit v1.2.3