Fixes for networking and tiny webserver from Max

This commit is contained in:
Gregory Nutt 2014-06-29 09:30:09 -06:00
parent 460eb3333c
commit 80f8944eeb
3 changed files with 72 additions and 41 deletions

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/include/netutils/httpd.h
*
* Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2011-2012, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based on uIP which also has a BSD style license:
@ -44,22 +44,17 @@
* Included Files
****************************************************************************/
#include <nuttx/net/tcp.h>
#include <nuttx/net/uip.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef __cplusplus
# define EXTERN extern "C"
extern "C" {
#else
# define EXTERN extern
#endif
/* As threads are created to handle each request, a stack must be allocated
* for the thread. Use a default if the user provided no stacksize.
*/
@ -89,12 +84,19 @@ extern "C" {
/* This is the maximum size of a file path */
#if defined(CONFIG_NETUTILS_HTTPD_MMAP) || defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
#define HTTPD_MAX_FILENAME PATH_MAX
#else
#define HTTPD_MAX_FILENAME 20
#ifndef CONFIG_NETUTILS_HTTPD_MAXPATH
# define CONFIG_NETUTILS_HTTPD_MAXPATH PATH_MAX
#endif
#define HTTPD_MAX_FILENAME CONFIG_HTTPD_MAXPATH
/* Other tunable values. If you need to change these values, please create
* new configurations in apps/netutils/webserver/Kconfig
*/
#defien HTTPD_MAX_CONTENTLEN 32
#defien HTTPD_MAX_HEADERLEN 180
/****************************************************************************
* Public types
****************************************************************************/
@ -160,7 +162,7 @@ struct httpd_cgi_call
* then added to the list of HTTPD CGI functions with the httpd_cgi_register()
* function.
* Input Paramters:
* Input Parameters:
*
* name The C variable name of the function
* str The string name of the function, used in the script file
@ -171,17 +173,29 @@ struct httpd_cgi_call
static void function(struct httpd_state *, char *); \
static struct httpd_cgi_call name = {NULL, str, function}
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
# define EXTERN extern "C"
extern "C"
{
#else
# define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
EXTERN void httpd_init(void);
EXTERN int httpd_listen(void);
EXTERN void httpd_cgi_register(struct httpd_cgi_call *cgi_call);
EXTERN uint16_t httpd_fs_count(char *name);
void httpd_init(void);
int httpd_listen(void);
void httpd_cgi_register(struct httpd_cgi_call *cgi_call);
uint16_t httpd_fs_count(char *name);
EXTERN const struct httpd_fsdata_file g_httpdfs_root[];
EXTERN const int g_httpd_numfiles;
const struct httpd_fsdata_file g_httpdfs_root[];
const int g_httpd_numfiles;
#undef EXTERN
#ifdef __cplusplus

View File

@ -33,6 +33,15 @@ config NETUTILS_HTTPD_SCRIPT_DISABLE
---help---
This option, if selected, will elide the %! scripting
config NETUTILS_HTTPD_MAXPATH
bool "Maximum size of a path"
default 64
---help---
This is the maximum size of a PATH used in the web server. This setting
is the logically the same as the PATH_MAX setting that (and in fact, if
not defined, the MAX_PATH setting will be used). This setting allows
more conservative memory allocation.
config NETUTILS_HTTPD_CGIPATH
bool "URL/CGI function mapping"
default n

View File

@ -336,8 +336,9 @@ static int send_headers(struct httpd_state *pstate, int status, int len)
{
const char *mime;
const char *ptr;
char cl[32];
char s[128];
char contentlen[HTTPD_MAX_CONTENTLEN];
char header[HTTPD_MAX_HEADERLEN];
int hdrlen;
int i;
static const struct
@ -381,7 +382,8 @@ static int send_headers(struct httpd_state *pstate, int status, int len)
if (len >= 0)
{
(void) snprintf(cl, sizeof cl, "Content-Length: %d\r\n", len);
(void)snprintf(contentlen, HTTPD_MAX_CONTENTLEN,
"Content-Length: %d\r\n", len);
}
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
else
@ -395,26 +397,32 @@ static int send_headers(struct httpd_state *pstate, int status, int len)
/* TODO: here we "SHOULD" include a Retry-After header */
}
i = snprintf(s, sizeof s,
"HTTP/1.0 %d %s\r\n"
#ifndef CONFIG_NETUTILS_HTTPD_SERVERHEADER_DISABLE
"Server: uIP/NuttX http://nuttx.org/\r\n"
#endif
"Connection: %s\r\n"
"Content-type: %s\r\n"
"%s"
"\r\n",
status,
status >= 400 ? "Error" : "OK",
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
pstate->ht_keepalive ? "keep-alive" : "close",
#else
"close",
#endif
mime,
len >= 0 ? cl : "");
/* Construct the header.
*
* REVISIT: Wouldn't asprintf be a better option than a large stack
* array?
*/
return send_chunk(pstate, s, i);
hdrlen = snprintf(header, HTTPD_MAX_HEADERLEN,
"HTTP/1.0 %d %s\r\n"
#ifndef CONFIG_NETUTILS_HTTPD_SERVERHEADER_DISABLE
"Server: uIP/NuttX http://nuttx.org/\r\n"
#endif
"Connection: %s\r\n"
"Content-type: %s\r\n"
"%s"
"\r\n",
status,
status >= 400 ? "Error" : "OK",
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
pstate->ht_keepalive ? "keep-alive" : "close",
#else
"close",
#endif
mime,
len >= 0 ? contentlen : "");
return send_chunk(pstate, header, hdrlen);
}
static int httpd_senderror(struct httpd_state *pstate, int status)