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
This commit is contained in:
parent
cbe214703b
commit
c87dcca676
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 */
|
||||
|
128
netutils/webserver/httpd_sendfile.c
Normal file
128
netutils/webserver/httpd_sendfile.c
Normal file
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user