2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-03-30 19:17:21 +02:00
|
|
|
* apps/netutils/webserver/httpd.c
|
2011-03-19 22:04:13 +01:00
|
|
|
* httpd Web server
|
|
|
|
*
|
2012-02-18 19:13:30 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* This is a leverage of similar logic from uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2004, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The uIP web server is a very simplistic implementation of an HTTP
|
|
|
|
* server. It can serve web pages and files from a read-only ROM
|
|
|
|
* filesystem, and provides a very small scripting language.
|
|
|
|
*
|
|
|
|
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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 Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-02-27 16:03:28 +01:00
|
|
|
#include <strings.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-05-31 13:49:09 +02:00
|
|
|
#include <assert.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SINGLECONNECT
|
2012-09-17 21:44:53 +02:00
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/httpd.h"
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#include "httpd.h"
|
|
|
|
#include "httpd_cgi.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#if !defined(CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE) && \
|
|
|
|
defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
|
2012-09-11 15:19:59 +02:00
|
|
|
# error "Script support and CONFIG_NETUTILS_HTTPD_SENDFILE are mutually exclusive"
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE) && \
|
|
|
|
defined(CONFIG_NETUTILS_HTTPD_MMAP)
|
2012-09-11 15:19:59 +02:00
|
|
|
# error "CONFIG_NETUTILS_HTTPD_SENDFILE and CONFIG_NETUTILS_HTTPD_MMAP are mutually exclusive"
|
|
|
|
#endif
|
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
#define ISO_NL 0x0a
|
|
|
|
#define ISO_SPACE 0x20
|
|
|
|
#define ISO_BANG 0x21
|
|
|
|
#define ISO_PERCENT 0x25
|
|
|
|
#define ISO_PERIOD 0x2e
|
|
|
|
#define ISO_SLASH 0x2f
|
|
|
|
#define ISO_COLON 0x3a
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_PATH
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_PATH "/mnt"
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 14:34:43 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_ERRPATH
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_ERRPATH ""
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* The correct way to disable receive timeout errors is by setting the
|
|
|
|
* timeout to zero.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_TIMEOUT
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_TIMEOUT 0
|
|
|
|
#endif
|
|
|
|
|
2012-09-22 22:36:36 +02:00
|
|
|
/* If timeouts are not enabled, then keep-alive is disabled. This is to
|
|
|
|
* prevent a rogue HTTP client from blocking the httpd indefinitely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE)
|
|
|
|
# if CONFIG_NETUTILS_HTTPD_TIMEOUT == 0
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CLASSIC
|
2012-09-18 20:45:39 +02:00
|
|
|
# ifndef CONFIG_NETUTILS_HTTPD_INDEX
|
|
|
|
# ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_INDEX "index.shtml"
|
|
|
|
# else
|
|
|
|
# define CONFIG_NETUTILS_HTTPD_INDEX "index.html"
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
static int httpd_open(const char *name, struct httpd_fs_file *file)
|
|
|
|
{
|
2014-07-11 20:30:23 +02:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_CLASSIC)
|
|
|
|
return httpd_fs_open(name, file);
|
2012-09-11 15:19:59 +02:00
|
|
|
#elif defined(CONFIG_NETUTILS_HTTPD_MMAP)
|
2012-09-04 18:59:24 +02:00
|
|
|
return httpd_mmap_open(name, file);
|
2014-07-11 20:30:23 +02:00
|
|
|
#elif defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
|
|
|
|
return httpd_sendfile_open(name, file);
|
2012-09-04 18:59:24 +02:00
|
|
|
#else
|
2014-07-11 20:30:23 +02:00
|
|
|
# error "No file handling method"
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-18 20:45:39 +02:00
|
|
|
static int httpd_openindex(struct httpd_state *pstate)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t z;
|
|
|
|
|
|
|
|
z = strlen(pstate->ht_filename);
|
|
|
|
if (z > 0 && pstate->ht_filename[z - 1] == '/')
|
|
|
|
{
|
|
|
|
pstate->ht_filename[--z] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = httpd_open(pstate->ht_filename, &pstate->ht_file);
|
2014-07-11 20:30:23 +02:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE) || \
|
|
|
|
defined(CONFIG_NETUTILS_HTTPD_MMAP)
|
2012-09-18 20:45:39 +02:00
|
|
|
# if defined(CONFIG_NETUTILS_HTTPD_INDEX)
|
|
|
|
if (ret == ERROR && errno == EISDIR)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
snprintf(pstate->ht_filename + z,
|
|
|
|
sizeof pstate->ht_filename - z, "/%s",
|
|
|
|
CONFIG_NETUTILS_HTTPD_INDEX);
|
2012-09-18 20:45:39 +02:00
|
|
|
|
|
|
|
ret = httpd_open(pstate->ht_filename, &pstate->ht_file);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
static int httpd_close(struct httpd_fs_file *file)
|
|
|
|
{
|
2014-07-11 20:30:23 +02:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_CLASSIC)
|
|
|
|
return OK;
|
2012-09-11 15:19:59 +02:00
|
|
|
#elif defined(CONFIG_NETUTILS_HTTPD_MMAP)
|
2012-09-04 18:59:24 +02:00
|
|
|
return httpd_mmap_close(file);
|
2014-07-11 20:30:23 +02:00
|
|
|
#elif defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
|
|
|
|
return httpd_sendfile_close(file);
|
2012-09-04 18:59:24 +02:00
|
|
|
#else
|
2014-07-11 20:30:23 +02:00
|
|
|
# error "No file handling method"
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DUMPBUFFER
|
2018-11-23 17:13:34 +01:00
|
|
|
static void httpd_dumpbuffer(FAR const char *msg, FAR const char *buffer,
|
|
|
|
unsigned int nbytes)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-11-08 22:15:14 +01:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_NET have to
|
|
|
|
* be defined or the following does nothing.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
2012-09-22 14:23:35 +02:00
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
ninfodumpbuffer(msg, (FAR const uint8_t *)buffer, nbytes);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:39:53 +02:00
|
|
|
# define httpd_dumpbuffer(msg,buffer,nbytes)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DUMPPSTATE
|
|
|
|
static void httpd_dumppstate(struct httpd_state *pstate, const char *msg)
|
|
|
|
{
|
2016-06-11 22:13:39 +02:00
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_INFO) && defined(CONFIG_DEBUG_NET)
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("[%d] pstate(%p): [%s]\n", pstate->ht_sockfd, pstate, msg);
|
|
|
|
ninfo(" filename: [%s]\n", pstate->ht_filename);
|
|
|
|
ninfo(" htfile len: %d\n", pstate->ht_file.len);
|
|
|
|
ninfo(" sockfd: %d\n", pstate->ht_sockfd);
|
2012-09-22 14:23:35 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo(" scriptptr: %p\n", pstate->ht_scriptptr);
|
|
|
|
ninfo(" scriptlen: %d\n", pstate->ht_scriptlen);
|
2012-09-22 14:23:35 +02:00
|
|
|
#endif
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo(" sndlen: %d\n", pstate->ht_sndlen);
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2023-05-20 00:39:53 +02:00
|
|
|
# define httpd_dumppstate(pstate, msg)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
2011-03-19 22:04:13 +01:00
|
|
|
static void next_scriptstate(struct httpd_state *pstate)
|
|
|
|
{
|
|
|
|
char *p;
|
2020-11-08 22:15:14 +01:00
|
|
|
p = strchr(pstate->ht_scriptptr, ISO_NL) + 1;
|
2011-03-19 22:04:13 +01:00
|
|
|
pstate->ht_scriptlen -= (unsigned short)(p - pstate->ht_scriptptr);
|
|
|
|
pstate->ht_scriptptr = p;
|
|
|
|
}
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
2011-03-19 22:04:13 +01:00
|
|
|
static int handle_script(struct httpd_state *pstate)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *ptr;
|
2018-11-23 17:13:34 +01:00
|
|
|
int status;
|
|
|
|
bool chunked_http_tx = 0;
|
|
|
|
|
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
chunked_http_tx = pstate->ht_chunked;
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
while (pstate->ht_file.len > 0)
|
|
|
|
{
|
|
|
|
/* Check if we should start executing a script */
|
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
if (*pstate->ht_file.data == ISO_PERCENT &&
|
|
|
|
*(pstate->ht_file.data + 1) == ISO_BANG)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
pstate->ht_scriptptr = pstate->ht_file.data + 3;
|
|
|
|
pstate->ht_scriptlen = pstate->ht_file.len - 3;
|
2020-11-08 22:15:14 +01:00
|
|
|
if (*(pstate->ht_scriptptr - 1) == ISO_COLON)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2018-11-23 17:13:34 +01:00
|
|
|
if (httpd_open(pstate->ht_scriptptr + 1,
|
|
|
|
&pstate->ht_file) != OK)
|
2012-09-04 18:59:24 +02:00
|
|
|
{
|
2019-07-05 05:21:26 +02:00
|
|
|
return ERROR;
|
2012-09-04 18:59:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
status = httpd_send_datachunk(pstate->ht_sockfd,
|
|
|
|
pstate->ht_file.data,
|
|
|
|
pstate->ht_file.len,
|
|
|
|
chunked_http_tx);
|
|
|
|
DEBUGASSERT(status >= 0);
|
2018-11-26 01:43:09 +01:00
|
|
|
UNUSED(status);
|
2012-09-04 18:59:24 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
httpd_close(&pstate->ht_file);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-05 20:03:37 +02:00
|
|
|
httpd_cgifunction f;
|
|
|
|
|
|
|
|
f = httpd_cgi(pstate->ht_scriptptr);
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
|
|
|
f(pstate, pstate->ht_scriptptr);
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
next_scriptstate(pstate);
|
|
|
|
|
|
|
|
/* The script is over, so we reset the pointers and continue
|
|
|
|
* sending the rest of the file
|
|
|
|
*/
|
|
|
|
|
|
|
|
pstate->ht_file.data = pstate->ht_scriptptr;
|
|
|
|
pstate->ht_file.len = pstate->ht_scriptlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* See if we find the start of script marker in the block of HTML
|
|
|
|
* to be sent
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pstate->ht_file.len > HTTPD_IOBUFFER_SIZE)
|
|
|
|
{
|
|
|
|
len = HTTPD_IOBUFFER_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = pstate->ht_file.len;
|
|
|
|
}
|
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
if (*pstate->ht_file.data == ISO_PERCENT)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-11-08 22:15:14 +01:00
|
|
|
ptr = strchr(pstate->ht_file.data + 1, ISO_PERCENT);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-08 22:15:14 +01:00
|
|
|
ptr = strchr(pstate->ht_file.data, ISO_PERCENT);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr != NULL && ptr != pstate->ht_file.data)
|
|
|
|
{
|
|
|
|
len = (int)(ptr - pstate->ht_file.data);
|
|
|
|
if (len >= HTTPD_IOBUFFER_SIZE)
|
|
|
|
{
|
|
|
|
len = HTTPD_IOBUFFER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
status = httpd_send_datachunk(pstate->ht_sockfd,
|
|
|
|
pstate->ht_file.data,
|
|
|
|
len, chunked_http_tx);
|
|
|
|
DEBUGASSERT(status >= 0);
|
2018-11-26 01:43:09 +01:00
|
|
|
UNUSED(status);
|
2018-11-23 17:13:34 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
pstate->ht_file.data += len;
|
|
|
|
pstate->ht_file.len -= len;
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 15:58:43 +02:00
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
/* Chunked encoding terminator */
|
|
|
|
|
|
|
|
status = httpd_send_datachunk(pstate->ht_sockfd, 0, 0, chunked_http_tx);
|
|
|
|
DEBUGASSERT(status >= 0);
|
2018-11-26 01:43:09 +01:00
|
|
|
UNUSED(status);
|
2018-11-23 17:13:34 +01:00
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-18 14:34:43 +02:00
|
|
|
static int send_chunk(struct httpd_state *pstate, const char *buf, int len)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2012-09-18 14:34:43 +02:00
|
|
|
httpd_dumpbuffer("Outgoing chunk", buf, len);
|
|
|
|
ret = send(pstate->ht_sockfd, buf, len, 0);
|
|
|
|
if (ret < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-18 14:34:43 +02:00
|
|
|
return ERROR;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2012-09-18 14:34:43 +02:00
|
|
|
buf += ret;
|
|
|
|
len -= ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
while (len > 0);
|
2012-09-04 18:59:24 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-09-18 14:34:43 +02:00
|
|
|
static int httpd_senderror(struct httpd_state *pstate, int status)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2012-09-19 01:31:35 +02:00
|
|
|
char msg[10 + 1];
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("[%d] sending error '%d'\n", pstate->ht_sockfd, status);
|
2012-09-18 14:34:43 +02:00
|
|
|
|
|
|
|
if (status < 400 || status >= 600)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-18 14:34:43 +02:00
|
|
|
status = 500;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2012-09-22 22:36:36 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
|
|
|
if (status != 404)
|
|
|
|
{
|
|
|
|
pstate->ht_keepalive = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
snprintf(pstate->ht_filename, sizeof pstate->ht_filename,
|
|
|
|
"%s/%d.html", CONFIG_NETUTILS_HTTPD_ERRPATH, status);
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2012-09-19 01:31:35 +02:00
|
|
|
ret = httpd_openindex(pstate);
|
|
|
|
|
2021-03-30 19:17:21 +02:00
|
|
|
if (httpd_send_headers(pstate, status,
|
2018-11-23 17:13:34 +01:00
|
|
|
ret == OK ? pstate->ht_file.len : sizeof msg - 1) != OK)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-18 14:34:43 +02:00
|
|
|
return ERROR;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2012-09-19 01:31:35 +02:00
|
|
|
if (ret != OK)
|
2012-09-17 20:18:44 +02:00
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
snprintf(msg, sizeof msg, "Error %d\n", status);
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2012-09-19 01:31:35 +02:00
|
|
|
ret = send_chunk(pstate, msg, sizeof msg - 1);
|
2012-09-17 20:18:44 +02:00
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
else
|
|
|
|
{
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CLASSIC
|
|
|
|
ret = send_chunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
|
|
|
|
#else
|
2012-09-18 14:34:43 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_SENDFILE
|
|
|
|
ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file);
|
2014-07-11 20:30:23 +02:00
|
|
|
#endif
|
2012-09-18 14:34:43 +02:00
|
|
|
#endif
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
httpd_close(&pstate->ht_file);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int httpd_sendfile(struct httpd_state *pstate)
|
|
|
|
{
|
2012-09-04 18:59:24 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
2011-03-19 22:04:13 +01:00
|
|
|
char *ptr;
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
pstate->ht_sndlen = 0;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("[%d] sending file '%s'\n", pstate->ht_sockfd, pstate->ht_filename);
|
2012-09-04 18:59:24 +02:00
|
|
|
|
2012-09-05 20:03:37 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CGIPATH
|
2020-11-08 22:15:14 +01:00
|
|
|
{
|
|
|
|
httpd_cgifunction f;
|
2012-09-05 20:03:37 +02:00
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
f = httpd_cgi(pstate->ht_filename);
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
2012-09-22 22:36:36 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
2020-11-08 22:15:14 +01:00
|
|
|
pstate->ht_keepalive = false;
|
2012-09-22 22:36:36 +02:00
|
|
|
#endif
|
2020-11-08 22:15:14 +01:00
|
|
|
f(pstate, pstate->ht_filename);
|
2012-09-05 20:03:37 +02:00
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
2012-09-05 20:03:37 +02:00
|
|
|
#endif
|
|
|
|
|
2012-09-18 20:45:39 +02:00
|
|
|
if (httpd_openindex(pstate) != OK)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nwarn("WARNING: [%d] '%s' not found\n",
|
|
|
|
pstate->ht_sockfd, pstate->ht_filename);
|
2012-09-18 14:34:43 +02:00
|
|
|
return httpd_senderror(pstate, 404);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2012-09-04 18:59:24 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
2020-11-08 22:15:14 +01:00
|
|
|
ptr = strchr(pstate->ht_filename, ISO_PERIOD);
|
2012-09-19 01:31:35 +02:00
|
|
|
if (ptr != NULL &&
|
|
|
|
strncmp(ptr, ".shtml", strlen(".shtml")) == 0)
|
|
|
|
{
|
2012-09-22 22:36:36 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
|
|
|
pstate->ht_keepalive = false;
|
|
|
|
#endif
|
2021-03-30 19:17:21 +02:00
|
|
|
if (httpd_send_headers(pstate, 200, -1) != OK)
|
2012-09-18 14:34:43 +02:00
|
|
|
{
|
2019-07-05 05:21:26 +02:00
|
|
|
goto done;
|
2012-09-18 14:34:43 +02:00
|
|
|
}
|
2012-09-19 01:31:35 +02:00
|
|
|
|
|
|
|
ret = handle_script(pstate);
|
|
|
|
goto done;
|
|
|
|
}
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
2012-09-19 01:31:35 +02:00
|
|
|
|
2019-07-05 04:58:53 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST
|
2021-03-30 19:17:21 +02:00
|
|
|
if (httpd_send_headers(pstate, 200, -1) != OK)
|
2019-07-05 04:58:53 +02:00
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#else
|
2021-03-30 19:17:21 +02:00
|
|
|
if (httpd_send_headers(pstate, pstate->ht_file.len == 0 ? 204 : 200,
|
|
|
|
pstate->ht_file.len) != OK)
|
2012-09-19 01:31:35 +02:00
|
|
|
{
|
|
|
|
goto done;
|
|
|
|
}
|
2019-07-05 04:58:53 +02:00
|
|
|
#endif
|
2012-09-19 01:31:35 +02:00
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CLASSIC
|
2019-07-05 04:58:53 +02:00
|
|
|
ret = send_chunk(pstate, pstate->ht_file.data, pstate->ht_file.len);
|
2014-07-11 20:30:23 +02:00
|
|
|
#else
|
2012-09-11 15:19:59 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_SENDFILE
|
2019-07-05 04:58:53 +02:00
|
|
|
ret = httpd_sendfile_send(pstate->ht_sockfd, &pstate->ht_file);
|
2014-07-11 20:30:23 +02:00
|
|
|
#endif
|
2012-09-11 15:19:59 +02:00
|
|
|
#endif
|
2012-09-19 01:31:35 +02:00
|
|
|
|
|
|
|
done:
|
2020-01-02 13:09:50 +01:00
|
|
|
httpd_close(&pstate->ht_file);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
static inline int httpd_parse(struct httpd_state *pstate)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-22 14:23:35 +02:00
|
|
|
char *o;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
enum
|
2020-11-08 22:15:14 +01:00
|
|
|
{
|
|
|
|
STATE_METHOD,
|
|
|
|
STATE_HEADER,
|
|
|
|
STATE_BODY
|
|
|
|
} state;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
state = STATE_METHOD;
|
|
|
|
o = pstate->ht_buffer;
|
|
|
|
|
|
|
|
do
|
2012-09-18 01:02:58 +02:00
|
|
|
{
|
2012-09-22 14:23:35 +02:00
|
|
|
char *start;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if (o == pstate->ht_buffer + sizeof pstate->ht_buffer)
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nerr("ERROR: ht_buffer overflow\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 413;
|
|
|
|
}
|
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
{
|
|
|
|
ssize_t r;
|
2012-09-22 14:23:35 +02:00
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
r = recv(pstate->ht_sockfd, o,
|
|
|
|
sizeof pstate->ht_buffer - (o - pstate->ht_buffer), 0);
|
|
|
|
if (r == 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: [%d] connection lost\n", pstate->ht_sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-09-22 14:23:35 +02:00
|
|
|
|
|
|
|
#if CONFIG_NETUTILS_HTTPD_TIMEOUT > 0
|
2020-11-08 22:15:14 +01:00
|
|
|
if (r == -1 && errno == EWOULDBLOCK)
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: recv timeout\n");
|
2020-11-08 22:15:14 +01:00
|
|
|
return 408;
|
|
|
|
}
|
2012-09-18 01:02:58 +02:00
|
|
|
#endif
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
nerr("ERROR: [%d] recv failed: %d\n",
|
|
|
|
pstate->ht_sockfd, errno);
|
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
o += r;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
/* Here o marks the end of the total block currently awaiting
|
|
|
|
* processing. There may be multiple lines in a block; next we deal
|
|
|
|
* with each in turn.
|
2012-09-22 14:23:35 +02:00
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
for (start = pstate->ht_buffer;
|
|
|
|
(end = memchr(start, '\r', o - start)), end != NULL;
|
|
|
|
start = end)
|
|
|
|
{
|
|
|
|
*end = '\0';
|
|
|
|
end++;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
/* Here start and end are a single line within the current block */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
httpd_dumpbuffer("Incoming HTTP line", start, end - start);
|
|
|
|
|
|
|
|
if (*end != '\n')
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: expected CRLF\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
end++;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
case STATE_METHOD:
|
|
|
|
if (0 != strncmp(start, "GET ", 4))
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: method not supported\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 501;
|
|
|
|
}
|
|
|
|
|
|
|
|
start += 4;
|
|
|
|
v = start + strcspn(start, " ");
|
|
|
|
|
|
|
|
if (0 != strcmp(v, " HTTP/1.0") && 0 != strcmp(v, " HTTP/1.1"))
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: HTTP version not supported\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 505;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: url decoding */
|
|
|
|
|
|
|
|
if (v - start >= sizeof pstate->ht_filename)
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nerr("ERROR: ht_filename overflow\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 414;
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = '\0';
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(pstate->ht_filename, start, sizeof(pstate->ht_filename));
|
2012-09-22 14:23:35 +02:00
|
|
|
state = STATE_HEADER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_HEADER:
|
|
|
|
if (*start == '\0')
|
|
|
|
{
|
|
|
|
state = STATE_BODY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = start + strcspn(start, ":");
|
|
|
|
if (*v != '\0')
|
|
|
|
{
|
|
|
|
*v = '\0', v++;
|
|
|
|
v += strspn(v, ": ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*start == '\0' || *v == '\0')
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: header parse error\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
ninfo("[%d] Request header %s: %s\n",
|
|
|
|
pstate->ht_sockfd, start, v);
|
2012-09-22 14:23:35 +02:00
|
|
|
|
|
|
|
if (0 == strcasecmp(start, "Content-Length") && 0 != atoi(v))
|
|
|
|
{
|
2020-11-26 03:52:51 +01:00
|
|
|
nwarn("WARNING: non-zero request length\n");
|
2012-09-22 14:23:35 +02:00
|
|
|
return 413;
|
|
|
|
}
|
2012-09-22 22:36:36 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
2018-11-23 17:13:34 +01:00
|
|
|
else if (0 == strcasecmp(start, "Connection") &&
|
|
|
|
0 == strcasecmp(v, "keep-alive"))
|
2012-09-22 22:36:36 +02:00
|
|
|
{
|
|
|
|
pstate->ht_keepalive = true;
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-22 14:23:35 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_BODY:
|
2020-11-08 22:15:14 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
/* Not implemented */
|
2018-11-23 17:13:34 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shuffle down for the next block */
|
|
|
|
|
|
|
|
memmove(pstate->ht_buffer, start, o - start);
|
2012-09-22 22:36:36 +02:00
|
|
|
o -= (start - pstate->ht_buffer);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-22 14:23:35 +02:00
|
|
|
while (state != STATE_BODY);
|
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CLASSIC
|
2012-10-06 16:50:37 +02:00
|
|
|
if (0 == strcmp(pstate->ht_filename, "/"))
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2022-03-02 19:56:05 +01:00
|
|
|
strlcpy(pstate->ht_filename, "/" CONFIG_NETUTILS_HTTPD_INDEX,
|
|
|
|
sizeof(pstate->ht_filename));
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-18 20:45:39 +02:00
|
|
|
#endif
|
2012-09-18 14:34:43 +02:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("[%d] Filename: %s\n", pstate->ht_sockfd, pstate->ht_filename);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-22 14:23:35 +02:00
|
|
|
return 200;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_handler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Each time a new connection to port 80 is made, a new thread is created
|
|
|
|
* that begins at this entry point. There should be exactly one argument
|
|
|
|
* and it should be the socket descriptor (+1).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *httpd_handler(void *arg)
|
|
|
|
{
|
2018-11-23 17:13:34 +01:00
|
|
|
struct httpd_state *pstate =
|
|
|
|
(struct httpd_state *)malloc(sizeof(struct httpd_state));
|
2020-11-08 22:15:14 +01:00
|
|
|
int sockfd = (intptr_t)arg;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("[%d] Started\n", sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Verify that the state structure was successfully allocated */
|
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
2012-09-22 14:23:35 +02:00
|
|
|
int status;
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Re-initialize the thread state structure */
|
|
|
|
|
|
|
|
memset(pstate, 0, sizeof(struct httpd_state));
|
|
|
|
pstate->ht_sockfd = sockfd;
|
|
|
|
|
2012-09-22 22:36:36 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
|
|
|
do
|
2012-09-22 14:23:35 +02:00
|
|
|
{
|
2012-09-22 22:36:36 +02:00
|
|
|
pstate->ht_keepalive = false;
|
|
|
|
#endif
|
|
|
|
/* Then handle the next httpd command */
|
|
|
|
|
|
|
|
status = httpd_parse(pstate);
|
|
|
|
if (status >= 400)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
httpd_senderror(pstate, status);
|
2012-09-22 22:36:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
httpd_sendfile(pstate);
|
2012-09-22 22:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
2012-09-22 14:23:35 +02:00
|
|
|
}
|
2012-09-22 22:36:36 +02:00
|
|
|
while (pstate->ht_keepalive);
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* End of command processing -- Clean up and exit */
|
|
|
|
|
|
|
|
free(pstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit the task */
|
|
|
|
|
2018-12-27 15:46:13 +01:00
|
|
|
ninfo("[%d] Exiting\n", sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(sockfd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_SINGLECONNECT
|
2018-11-23 17:13:34 +01:00
|
|
|
static void single_server(uint16_t portno, pthread_startroutine_t handler,
|
|
|
|
int stacksize)
|
2012-09-17 21:44:53 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in myaddr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int listensd;
|
|
|
|
int acceptsd;
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2012-09-17 21:44:53 +02:00
|
|
|
struct linger ling;
|
|
|
|
#endif
|
2012-09-18 01:02:58 +02:00
|
|
|
#if CONFIG_NETUTILS_HTTPD_TIMEOUT > 0
|
|
|
|
struct timeval tv;
|
|
|
|
#endif
|
2012-09-17 21:44:53 +02:00
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
listensd = netlib_listenon(portno);
|
2012-09-17 21:44:53 +02:00
|
|
|
if (listensd < 0)
|
|
|
|
{
|
2012-09-18 01:02:58 +02:00
|
|
|
return;
|
2012-09-17 21:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Begin serving connections */
|
|
|
|
|
2018-11-23 17:13:34 +01:00
|
|
|
for (; ; )
|
2012-09-17 21:44:53 +02:00
|
|
|
{
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2018-11-23 17:13:34 +01:00
|
|
|
acceptsd = accept(listensd, (FAR struct sockaddr *)&myaddr, &addrlen);
|
2012-09-17 21:44:53 +02:00
|
|
|
|
|
|
|
if (acceptsd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: accept failure: %d\n", errno);
|
2014-07-28 15:23:04 +02:00
|
|
|
break;
|
2012-09-17 21:44:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Connection accepted -- serving sd=%d\n", acceptsd);
|
2012-09-17 21:44:53 +02:00
|
|
|
|
2020-11-08 22:15:14 +01:00
|
|
|
/* Configure to "linger" until all data is sent
|
|
|
|
* when the socket is closed
|
|
|
|
*/
|
2012-09-17 21:44:53 +02:00
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2012-09-17 21:44:53 +02:00
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 30; /* timeout is seconds */
|
2018-11-23 17:13:34 +01:00
|
|
|
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling,
|
|
|
|
sizeof(struct linger)) < 0)
|
2012-09-17 21:44:53 +02:00
|
|
|
{
|
|
|
|
close(acceptsd);
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SO_LINGER failure: %d\n", errno);
|
2018-11-23 17:13:34 +01:00
|
|
|
break;
|
2012-09-17 21:44:53 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
#if CONFIG_NETUTILS_HTTPD_TIMEOUT > 0
|
|
|
|
/* Set up a receive timeout */
|
|
|
|
|
|
|
|
tv.tv_sec = CONFIG_NETUTILS_HTTPD_TIMEOUT;
|
|
|
|
tv.tv_usec = 0;
|
2018-11-23 17:13:34 +01:00
|
|
|
if (setsockopt(acceptsd, SOL_SOCKET, SO_RCVTIMEO, &tv,
|
|
|
|
sizeof(struct timeval)) < 0)
|
2012-09-18 01:02:58 +02:00
|
|
|
{
|
|
|
|
close(acceptsd);
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SO_RCVTIMEO failure: %d\n", errno);
|
2018-11-23 17:13:34 +01:00
|
|
|
break;
|
2012-09-18 01:02:58 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-17 21:44:53 +02:00
|
|
|
/* Handle the request. This blocks until complete. */
|
|
|
|
|
2023-04-11 12:25:49 +02:00
|
|
|
handler((FAR void *)acceptsd);
|
2012-09-17 21:44:53 +02:00
|
|
|
}
|
2014-07-28 15:23:04 +02:00
|
|
|
|
|
|
|
/* Close the sockets */
|
|
|
|
|
|
|
|
close(acceptsd);
|
|
|
|
close(listensd);
|
2012-09-17 21:44:53 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2019-07-27 16:49:34 +02:00
|
|
|
|
2021-03-30 19:17:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_init
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void httpd_init(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_CLASSIC
|
|
|
|
httpd_fs_init();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_listen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int httpd_listen(void)
|
|
|
|
{
|
|
|
|
/* Execute httpd_handler on each connection to port 80 */
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_SINGLECONNECT
|
2012-09-17 21:44:53 +02:00
|
|
|
single_server(HTONS(80), httpd_handler, CONFIG_NETUTILS_HTTPDSTACKSIZE);
|
|
|
|
#else
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_server(HTONS(80), httpd_handler, CONFIG_NETUTILS_HTTPDSTACKSIZE);
|
2012-09-17 21:44:53 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-17 21:44:53 +02:00
|
|
|
/* the server accept loop only returns on errors */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-03-30 19:17:21 +02:00
|
|
|
* Name: httpd_send_datachunk
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-30 19:17:21 +02:00
|
|
|
int httpd_send_datachunk(int sockfd, void *data, int len, bool chunked)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2021-03-30 19:17:21 +02:00
|
|
|
int ret = 0;
|
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
char chunked_info[HTTPD_MAX_CHUNKEDLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
/* Chunk prolog */
|
|
|
|
|
|
|
|
if (chunked)
|
|
|
|
{
|
|
|
|
int chunked_info_len = snprintf(chunked_info, HTTPD_MAX_CHUNKEDLEN,
|
|
|
|
"%X\r\n", len);
|
|
|
|
ret = send(sockfd, chunked_info, chunked_info_len, 0);
|
|
|
|
DEBUGASSERT(ret == chunked_info_len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
/* Lower layer does not tolerate buf = NULL even if len = 0
|
|
|
|
* so just pass a dummy pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
data = &len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = send(sockfd, data, len, 0);
|
|
|
|
DEBUGASSERT(ret == len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
/* Chunk epilog */
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
if (chunked)
|
|
|
|
{
|
|
|
|
ret = send(sockfd, "\r\n", 2, 0);
|
|
|
|
DEBUGASSERT(ret == 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: httpd_send_headers
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int httpd_send_headers(struct httpd_state *pstate, int status, int len)
|
|
|
|
{
|
|
|
|
const char *mime;
|
|
|
|
const char *ptr;
|
|
|
|
char contentlen[HTTPD_MAX_CONTENTLEN] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
char header[HTTPD_MAX_HEADERLEN];
|
|
|
|
int hdrlen;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const char *ext;
|
|
|
|
const char *mime;
|
|
|
|
}
|
|
|
|
|
|
|
|
a[] =
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
|
|
|
|
{
|
|
|
|
"shtml", "text/html"
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
"html", "text/html"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"css", "text/css"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"txt", "text/plain"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"json", "application/json"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"js", "text/javascript"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"png", "image/png"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"gif", "image/gif"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"jpeg", "image/jpeg"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"jpg", "image/jpeg"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"mp3", "audio/mpeg"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
ptr = strrchr(pstate->ht_filename, ISO_PERIOD);
|
|
|
|
if (ptr == NULL)
|
|
|
|
{
|
|
|
|
mime = "application/octet-stream";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mime = "text/plain";
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof a / sizeof *a; i++)
|
|
|
|
{
|
|
|
|
if (strncmp(a[i].ext, ptr + 1, strlen(a[i].ext)) == 0)
|
|
|
|
{
|
|
|
|
mime = a[i].mime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST
|
|
|
|
if (false == httpd_is_file(pstate->ht_filename))
|
|
|
|
{
|
|
|
|
/* we assume that it's a directory */
|
|
|
|
|
|
|
|
mime = "text/html";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (len >= 0)
|
|
|
|
{
|
|
|
|
snprintf(contentlen, HTTPD_MAX_CONTENTLEN,
|
|
|
|
"Content-Length: %d\r\n", len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_NETUTILS_HTTPD_KEEPALIVE_DISABLE
|
|
|
|
/* Length unknown ahead of time */
|
|
|
|
|
|
|
|
pstate->ht_keepalive = false;
|
2012-09-04 18:59:24 +02:00
|
|
|
#endif
|
2021-03-30 19:17:21 +02:00
|
|
|
#if defined(CONFIG_NETUTILS_HTTPD_ENABLE_CHUNKED_ENCODING)
|
|
|
|
/* Turn on chunked encoding */
|
|
|
|
|
|
|
|
snprintf(contentlen, HTTPD_MAX_CONTENTLEN,
|
|
|
|
"Transfer-Encoding: chunked\r\n");
|
|
|
|
pstate->ht_chunked = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == 413)
|
|
|
|
{
|
|
|
|
/* TODO: here we "SHOULD" include a Retry-After header */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct the header.
|
|
|
|
*
|
|
|
|
* REVISIT: Wouldn't asprintf be a better option than a large stack
|
|
|
|
* array?
|
|
|
|
*/
|
|
|
|
|
|
|
|
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,
|
|
|
|
contentlen
|
|
|
|
);
|
|
|
|
|
|
|
|
return send_chunk(pstate, header, hdrlen);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|