diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index da56f5f3e0..833a62cd11 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 c3ebe755d5..fd17a6a97e 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 6d96c8fc5f..4b1f2f9f3c 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 2fd0191573..0c0ee0389d 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 42a1e13f76..dbe3d0e206 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. */ +#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) + +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); -#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); #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 0000000000..6a6ec24ab3 --- /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; +} + diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 26e3621291..f1780c3234 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -3307,4 +3307,6 @@ (Noted by Ronen Vainish). * fs/mmap/fs_rammap.c: Fix logic error and errno check (contributed by Kate). + * arch/avr/src: Fixes from AVR32 build errors that have crept in + over the time; incorporated Kconfig for AVR3 (Richard Cochran). diff --git a/nuttx/arch/avr/src/Makefile b/nuttx/arch/avr/src/Makefile index 7034ef8f40..b72a6c8a8d 100644 --- a/nuttx/arch/avr/src/Makefile +++ b/nuttx/arch/avr/src/Makefile @@ -40,8 +40,7 @@ ARCH_SRCDIR = $(TOPDIR)/arch/$(CONFIG_ARCH)/src ifeq ($(CONFIG_ARCH_AVR32),y) ARCH_SUBDIR = avr32 -endif -ifeq ($(CONFIG_ARCH_AVR),y) +else ifeq ($(CONFIG_ARCH_AVR),y) ARCH_SUBDIR = avr endif diff --git a/nuttx/arch/avr/src/avr32/avr32_internal.h b/nuttx/arch/avr/src/avr32/avr32_internal.h index 56065edceb..332b0918e6 100644 --- a/nuttx/arch/avr/src/avr32/avr32_internal.h +++ b/nuttx/arch/avr/src/avr32/avr32_internal.h @@ -69,7 +69,7 @@ * structure. If is non-NULL only during interrupt processing. */ -extern volatile uint32_ *current_regs; +extern volatile uint32_t *current_regs; /* This is the beginning of heap as provided from up_head.S. This is the first * address in DRAM after the loaded program+bss+idle stack. The end of the diff --git a/nuttx/arch/avr/src/avr32/up_copystate.c b/nuttx/arch/avr/src/avr32/up_copystate.c index e3e4da0544..67640f8cce 100644 --- a/nuttx/arch/avr/src/avr32/up_copystate.c +++ b/nuttx/arch/avr/src/avr32/up_copystate.c @@ -41,6 +41,8 @@ #include +#include + #include "up_internal.h" /****************************************************************************