1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

new trunk

git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@78 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
This commit is contained in:
cameronrich
2007-03-14 12:03:51 +00:00
parent 974cf12924
commit 73dfbb7568
177 changed files with 46362 additions and 0 deletions

138
httpd/Config.in Normal file
View File

@ -0,0 +1,138 @@
#
# For a description of the syntax of this configuration file,
# see scripts/config/Kconfig-language.txt
#
menu "Axhttpd Configuration"
depends on CONFIG_AXHTTPD
config CONFIG_HTTP_STATIC_BUILD
bool "Static Build"
default n
help
Select y if you want axhttpd to be a static build (i.e. don't use the
axtls shared library or dll).
config CONFIG_HTTP_PORT
int "HTTP port"
default 80
help
The port number of the normal HTTP server.
You must be a root user in order to use the default port.
config CONFIG_HTTP_HTTPS_PORT
int "HTTPS port"
default 443
help
The port number of the HTTPS server.
You must be a root user in order to use the default port.
config CONFIG_HTTP_SESSION_CACHE_SIZE
int "SSL session cache size"
default 5
help
The size of the SSL session cache.
This is not actually related to the number of concurrent users, but
for optimum performance they should be the same (with a penalty
in memory usage).
config CONFIG_HTTP_WEBROOT
string "Web root location"
default "../www" if !CONFIG_PLATFORM_WIN32
default "..\\www" if CONFIG_PLATFORM_WIN32
help
The location of the web root in relation to axhttpd. This is
the directory where index.html lives.
config CONFIG_HTTP_TIMEOUT
int "Timeout"
default 300
help
Set the timeout of a connection in seconds.
config CONFIG_HTTP_HAS_CGI
bool "Enable CGI"
default n
help
Enable the CGI capability.
config CONFIG_HTTP_CGI_EXTENSIONS
string "CGI File Extension(s)"
default ".php,.sh"
depends on CONFIG_HTTP_HAS_CGI
help
Tell axhhtpd what file extension(s) are used for CGI.
This is a comma separated list.
config CONFIG_HTTP_DIRECTORIES
bool "Enable Directory Listing"
default n
help
Enable directory listing.
config CONFIG_HTTP_HAS_AUTHORIZATION
bool "Enable authorization"
default n
help
Pages/directories can have passwords associated with them.
config CONFIG_HTTP_USE_CHROOT
bool "Use chroot()"
default n
depends on !CONFIG_PLATFORM_WIN32
help
Use chroot() to switch directories with a certain degree of
protection. However access to /bin and /lib have to replaced with
duplicate binaries.
This feature is normally disabled.
config CONFIG_HTTP_CHANGE_UID
bool "Change UID"
default n
depends on !CONFIG_PLATFORM_WIN32
help
Call setgid()/setuid() to disable access to protected files.
This feature is normally disabled.
config CONFIG_HTTP_HAS_IPV6
bool "Enable IPv6"
default n
depends on !CONFIG_PLATFORM_WIN32
help
Use IPv6 instead of IPv4.
Does not work under Win32
config CONFIG_HTTP_ALL_MIME_TYPES
bool "Use all mime types"
help
Use the full list of supported mime types.
Use this option if a "generic" webserver is used. However if it is
using only web pages (html, jpg, gif, png, css) then select this
option.
config CONFIG_HTTP_VERBOSE
bool "Verbose Mode"
default y if CONFIG_SSL_FULL_MODE
default n if !CONFIG_SSL_FULL_MODE
help
Enable extra statements used when using axhttpd.
config CONFIG_HTTP_IS_DAEMON
bool "Run as a daemon"
default n
depends on !CONFIG_PLATFORM_WIN32
help
Run axhttpd as a background process.
Does not work under Win32
endmenu

104
httpd/Makefile Normal file
View File

@ -0,0 +1,104 @@
#
# Copyright(C) 2007 Cameron Rich
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
all : web_server
include ../config/.config
include ../config/makefile.conf
ifndef CONFIG_PLATFORM_WIN32
ifdef CONFIG_PLATFORM_CYGWIN
TARGET=../$(STAGE)/axhttpd.exe
TARGET2=../$(STAGE)/htpasswd.exe
else
TARGET=../$(STAGE)/axhttpd
TARGET2=../$(STAGE)/htpasswd
endif
ifdef CONFIG_HTTP_STATIC_BUILD
LIBS=../$(STAGE)/libaxtls.a
else
LIBS=-L../$(STAGE) -laxtls
endif
CFLAGS += -I../ssl
else # win32 build
TARGET=../$(STAGE)/axhttpd.exe
TARGET2=../$(STAGE)/htpasswd.exe
ifdef CONFIG_HTTP_STATIC_BUILD
LIBS=../$(STAGE)/axtls.static.lib ..\\config\\axtls.res
else
LIBS=../$(STAGE)/axtls.lib ..\\config\\axtls.res
endif
endif
ifndef CONFIG_AXHTTPD
web_server:
else
web_server :: $(TARGET)
ifdef CONFIG_HTTP_HAS_AUTHORIZATION
web_server :: $(TARGET2)
endif
OBJ= \
axhttpd.o \
proc.o \
mime_types.o \
tdate_parse.o
include ../config/makefile.post
ifndef CONFIG_PLATFORM_WIN32
$(TARGET): $(OBJ) ../$(STAGE)/libaxtls.a
$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
ifndef CONFIG_DEBUG
ifndef CONFIG_PLATFORM_SOLARIS
strip --remove-section=.comment $(TARGET)
endif
endif
$(TARGET2): htpasswd.o ../$(STAGE)/libaxtls.a
$(LD) $(LDFLAGS) -o $@ htpasswd.o $(LIBS)
else # Win32
OBJ:=$(OBJ:.o=.obj)
%.obj : %.c
$(CC) $(CFLAGS) $<
htpasswd.obj : htpasswd.c
$(CC) $(CFLAGS) $<
$(TARGET): $(OBJ)
$(LD) $(LDFLAGS) $(LIBS) /out:$@ $(OBJ)
$(TARGET2): htpasswd.obj
$(LD) $(LDFLAGS) $(LIBS) /out:$@ $<
endif
endif # CONFIG_AXHTTPD
clean::
-@rm -f $(TARGET)*

133
httpd/axhttp.h Normal file
View File

@ -0,0 +1,133 @@
/*
* Copyright(C) 2007 Cameron Rich
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "os_port.h"
#include "ssl.h"
#define BACKLOG 15
#define VERSION "1.0.0"
#ifdef CONFIG_HTTP_HAS_IPV6
#define HAVE_IPV6
#endif
#define MAXREQUESTLENGTH 256
#define MAXCGIARGS 100
#define BLOCKSIZE 4096
#define INITIAL_CONNECTION_SLOTS 10
#define CONFIG_HTTP_DEFAULT_SSL_OPTIONS 0
#define STATE_WANT_TO_READ_HEAD 1
#define STATE_WANT_TO_SEND_HEAD 2
#define STATE_WANT_TO_READ_FILE 3
#define STATE_WANT_TO_SEND_FILE 4
#define STATE_DOING_DIR 5
enum
{
TYPE_GET,
TYPE_HEAD,
TYPE_POST
};
struct connstruct
{
struct connstruct *next;
int state;
int reqtype;
int networkdesc;
int filedesc;
SSL *ssl;
#if defined(CONFIG_HTTP_DIRECTORIES)
#ifdef WIN32
HANDLE dirp;
WIN32_FIND_DATA file_data;
#else
DIR *dirp;
#endif
#endif
time_t timeout;
char actualfile[MAXREQUESTLENGTH];
char filereq[MAXREQUESTLENGTH];
char dirname[MAXREQUESTLENGTH];
char virtualhostreq[MAXREQUESTLENGTH];
int numbytes;
char databuf[BLOCKSIZE];
uint8_t is_ssl;
uint8_t close_when_done;
time_t if_modified_since;
#if defined(CONFIG_HTTP_HAS_CGI)
char cgiargs[MAXREQUESTLENGTH];
char cgiscriptinfo[MAXREQUESTLENGTH];
char cgipathinfo[MAXREQUESTLENGTH];
#endif
#if defined(CONFIG_HTTP_HAS_AUTHORIZATION)
char authorization[MAXREQUESTLENGTH];
#endif
};
struct serverstruct
{
struct serverstruct *next;
int sd;
int is_ssl;
SSL_CTX *ssl_ctx;
};
#if defined(CONFIG_HTTP_HAS_CGI)
struct cgiextstruct
{
struct cgiextstruct *next;
char *ext;
};
#endif
/* global prototypes */
extern struct serverstruct *servers;
extern struct connstruct *usedconns;
extern struct connstruct *freeconns;
#if defined(CONFIG_HTTP_HAS_CGI)
extern struct cgiextstruct *cgiexts;
#endif
/* conn.c prototypes */
void removeconnection(struct connstruct *cn);
/* proc.c prototypes */
void procdodir(struct connstruct *cn);
void procreadhead(struct connstruct *cn);
void procsendhead(struct connstruct *cn);
void procreadfile(struct connstruct *cn);
void procsendfile(struct connstruct *cn);
/* misc.c prototypes */
char *my_strncpy(char *dest, const char *src, size_t n);
int isdir(const char *name);
/* mime_types.c prototypes */
void mime_init(void);
const char *getmimetype(const char *fn);
/* tdate prototypes */
void tdate_init(void);
time_t tdate_parse(const char* str);

567
httpd/axhttpd.c Normal file
View File

@ -0,0 +1,567 @@
/*
* Copyright(C) 2007 Cameron Rich
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "axhttp.h"
struct serverstruct *servers;
struct connstruct *usedconns;
struct connstruct *freeconns;
static void addtoservers(int sd);
static int openlistener(int port);
static void handlenewconnection(int listenfd, int is_ssl);
static void addconnection(int sd, char *ip, int is_ssl);
static void ax_chdir(void);
#if defined(CONFIG_HTTP_HAS_CGI)
struct cgiextstruct *cgiexts;
static void addcgiext(const char *tp);
#if !defined(WIN32)
static void reaper(int sigtype)
{
wait3(NULL, WNOHANG, NULL);
}
#endif
#endif
#ifdef CONFIG_HTTP_VERBOSE /* should really be in debug mode or something */
/* clean up memory for valgrind */
static void sigint_cleanup(int sig)
{
struct serverstruct *sp;
struct connstruct *tp;
while (servers != NULL)
{
if (servers->is_ssl)
ssl_ctx_free(servers->ssl_ctx);
sp = servers->next;
free(servers);
servers = sp;
}
while (freeconns != NULL)
{
tp = freeconns->next;
free(freeconns);
freeconns = tp;
}
while (usedconns != NULL)
{
tp = usedconns->next;
free(usedconns);
usedconns = tp;
}
#if defined(CONFIG_HTTP_HAS_CGI)
while (cgiexts)
{
struct cgiextstruct *cp = cgiexts->next;
if (cp == NULL) /* last entry */
free(cgiexts->ext);
free(cgiexts);
cgiexts = cp;
}
#endif
exit(0);
}
static void die(int sigtype)
{
exit(0);
}
#endif
int main(int argc, char *argv[])
{
fd_set rfds, wfds;
struct connstruct *tp, *to;
struct serverstruct *sp;
int rnum, wnum, active;
int i;
time_t currtime;
#ifdef WIN32
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
WSAStartup(wVersionRequested,&wsaData);
#else
signal(SIGPIPE, SIG_IGN);
#if defined(CONFIG_HTTP_HAS_CGI)
signal(SIGCHLD, reaper);
#endif
#ifdef CONFIG_HTTP_VERBOSE
signal(SIGQUIT, die);
#endif
#endif
#ifdef CONFIG_HTTP_VERBOSE
signal(SIGTERM, die);
signal(SIGINT, sigint_cleanup);
#endif
mime_init();
tdate_init();
for (i = 0; i < INITIAL_CONNECTION_SLOTS; i++)
{
tp = freeconns;
freeconns = (struct connstruct *)calloc(1, sizeof(struct connstruct));
freeconns->next = tp;
}
if ((active = openlistener(CONFIG_HTTP_PORT)) == -1)
{
#ifdef CONFIG_HTTP_VERBOSE
fprintf(stderr, "ERR: Couldn't bind to port %d\n",
CONFIG_HTTP_PORT);
#endif
exit(1);
}
addtoservers(active);
if ((active = openlistener(CONFIG_HTTP_HTTPS_PORT)) == -1)
{
#ifdef CONFIG_HTTP_VERBOSE
fprintf(stderr, "ERR: Couldn't bind to port %d\n",
CONFIG_HTTP_HTTPS_PORT);
#endif
exit(1);
}
addtoservers(active);
servers->ssl_ctx = ssl_ctx_new(CONFIG_HTTP_DEFAULT_SSL_OPTIONS,
CONFIG_HTTP_SESSION_CACHE_SIZE);
servers->is_ssl = 1;
#if defined(CONFIG_HTTP_HAS_CGI)
addcgiext(CONFIG_HTTP_CGI_EXTENSIONS);
#endif
#if defined(CONFIG_HTTP_VERBOSE)
printf("axhttpd (%s): listening on ports %d (http) and %d (https)\n",
ssl_version(), CONFIG_HTTP_PORT, CONFIG_HTTP_HTTPS_PORT);
TTY_FLUSH();
#endif
ax_chdir();
#ifndef WIN32
#ifdef CONFIG_HTTP_CHANGE_UID
setgid(32767);
setuid(32767);
#endif
#ifdef CONFIG_HTTP_IS_DAEMON
if (fork() > 0) /* parent will die */
exit(0);
setsid();
#endif
#endif
/* main loop */
while (1)
{
FD_ZERO(&rfds);
FD_ZERO(&wfds);
rnum = wnum = -1;
sp = servers;
while (sp != NULL) /* read each server port */
{
FD_SET(sp->sd, &rfds);
if (sp->sd > rnum)
rnum = sp->sd;
sp = sp->next;
}
/* Add the established sockets */
tp = usedconns;
currtime = time(NULL);
while (tp != NULL)
{
if (currtime > tp->timeout) /* timed out? Kill it. */
{
to = tp;
tp = tp->next;
removeconnection(to);
continue;
}
if (tp->state == STATE_WANT_TO_READ_HEAD)
{
FD_SET(tp->networkdesc, &rfds);
if (tp->networkdesc > rnum)
rnum = tp->networkdesc;
}
if (tp->state == STATE_WANT_TO_SEND_HEAD)
{
FD_SET(tp->networkdesc, &wfds);
if (tp->networkdesc > wnum)
wnum = tp->networkdesc;
}
if (tp->state == STATE_WANT_TO_READ_FILE)
{
FD_SET(tp->filedesc, &rfds);
if (tp->filedesc > rnum)
rnum = tp->filedesc;
}
if (tp->state == STATE_WANT_TO_SEND_FILE)
{
FD_SET(tp->networkdesc, &wfds);
if (tp->networkdesc > wnum)
wnum = tp->networkdesc;
}
#if defined(CONFIG_HTTP_DIRECTORIES)
if (tp->state == STATE_DOING_DIR)
{
FD_SET(tp->networkdesc, &wfds);
if (tp->networkdesc > wnum)
wnum = tp->networkdesc;
}
#endif
tp = tp->next;
}
active = select(wnum > rnum ? wnum+1 : rnum+1,
rnum != -1 ? &rfds : NULL,
wnum != -1 ? &wfds : NULL,
NULL, NULL);
/* New connection? */
sp = servers;
while (active > 0 && sp != NULL)
{
if (FD_ISSET(sp->sd, &rfds))
{
handlenewconnection(sp->sd, sp->is_ssl);
active--;
}
sp = sp->next;
}
/* Handle the established sockets */
tp = usedconns;
while (active > 0 && tp != NULL)
{
to = tp;
tp = tp->next;
if (to->state == STATE_WANT_TO_READ_HEAD &&
FD_ISSET(to->networkdesc, &rfds))
{
active--;
procreadhead(to);
}
if (to->state == STATE_WANT_TO_SEND_HEAD &&
FD_ISSET(to->networkdesc, &wfds))
{
active--;
procsendhead(to);
}
if (to->state == STATE_WANT_TO_READ_FILE &&
FD_ISSET(to->filedesc, &rfds))
{
active--;
procreadfile(to);
}
if (to->state == STATE_WANT_TO_SEND_FILE &&
FD_ISSET(to->networkdesc, &wfds))
{
active--;
procsendfile(to);
}
#if defined(CONFIG_HTTP_DIRECTORIES)
if (to->state == STATE_DOING_DIR &&
FD_ISSET(to->networkdesc, &wfds))
{
active--;
procdodir(to);
}
#endif
}
}
return 0;
}
#if defined(CONFIG_HTTP_HAS_CGI)
static void addcgiext(const char *cgi_exts)
{
char *cp = strdup(cgi_exts);
/* extenstions are comma separated */
do
{
struct cgiextstruct *ex = (struct cgiextstruct *)
malloc(sizeof(struct cgiextstruct));
ex->ext = cp;
ex->next = cgiexts;
cgiexts = ex;
if ((cp = strchr(cp, ',')) != NULL)
*cp++ = 0;
} while (cp != NULL);
}
#endif
static void addtoservers(int sd)
{
struct serverstruct *tp = (struct serverstruct *)
calloc(1, sizeof(struct serverstruct));
tp->next = servers;
tp->sd = sd;
servers = tp;
}
#ifdef HAVE_IPV6
static void handlenewconnection(int listenfd, int is_ssl)
{
struct sockaddr_in6 their_addr;
int tp = sizeof(their_addr);
char ipbuf[100];
int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp);
if (tp == sizeof(struct sockaddr_in6))
inet_ntop(AF_INET6, &their_addr.sin6_addr, ipbuf, sizeof(ipbuf));
else if (tp == sizeof(struct sockaddr_in))
inet_ntop(AF_INET, &(((struct sockaddr_in *)&their_addr)->sin_addr),
ipbuf, sizeof(ipbuf));
else
*ipbuf = '\0';
addconnection(connfd, ipbuf, is_ssl);
}
#else
static void handlenewconnection(int listenfd, int is_ssl)
{
struct sockaddr_in their_addr;
socklen_t tp = sizeof(struct sockaddr_in);
int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp);
addconnection(connfd, inet_ntoa(their_addr.sin_addr), is_ssl);
}
#endif
static int openlistener(int port)
{
int sd;
#ifdef WIN32
char tp = 1;
#else
int tp = 1;
#endif
#ifndef HAVE_IPV6
struct sockaddr_in my_addr;
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
return -1;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons((short)port);
my_addr.sin_addr.s_addr = INADDR_ANY;
#else
struct sockaddr_in6 my_addr;
if ((sd = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
return -1;
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin6_family = AF_INET6;
my_addr.sin6_port = htons(port);
my_addr.sin6_addr.s_addr = INADDR_ANY;
#endif
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &tp, sizeof(tp));
if (bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
close(sd);
return -1;
}
listen(sd, BACKLOG);
return sd;
}
/* Wrapper function for strncpy() that guarantees
a null-terminated string. This is to avoid any possible
issues due to strncpy()'s behaviour.
*/
char *my_strncpy(char *dest, const char *src, size_t n)
{
strncpy(dest, src, n);
dest[n-1] = '\0';
return dest;
}
int isdir(const char *tpbuf)
{
struct stat st;
char path[MAXREQUESTLENGTH];
strcpy(path, tpbuf);
#ifdef WIN32 /* win32 stat() can't handle trailing '\' */
if (path[strlen(path)-1] == '\\')
path[strlen(path)-1] = 0;
#endif
if (stat(path, &st) == -1)
return 0;
if ((st.st_mode & S_IFMT) == S_IFDIR)
return 1;
return 0;
}
static void addconnection(int sd, char *ip, int is_ssl)
{
struct connstruct *tp;
/* Get ourselves a connstruct */
if (freeconns == NULL)
tp = (struct connstruct *)calloc(1, sizeof(struct connstruct));
else
{
tp = freeconns;
freeconns = tp->next;
}
/* Attach it to the used list */
tp->next = usedconns;
usedconns = tp;
tp->networkdesc = sd;
if (is_ssl)
tp->ssl = ssl_server_new(servers->ssl_ctx, sd);
tp->is_ssl = is_ssl;
tp->filedesc = -1;
#if defined(CONFIG_HTTP_HAS_DIRECTORIES)
tp->dirp = NULL;
#endif
*(tp->actualfile) = '\0';
*(tp->filereq) = '\0';
#if defined(CONFIG_HTTP_HAS_CGI)
*(tp->cgiargs) = '\0';
#endif
tp->state = STATE_WANT_TO_READ_HEAD;
tp->reqtype = TYPE_GET;
tp->close_when_done = 0;
tp->timeout = time(NULL) + CONFIG_HTTP_TIMEOUT;
}
void removeconnection(struct connstruct *cn)
{
struct connstruct *tp;
int shouldret = 0;
tp = usedconns;
if (tp == NULL || cn == NULL)
shouldret = 1;
else if (tp == cn)
usedconns = tp->next;
else
{
while (tp != NULL)
{
if (tp->next == cn)
{
tp->next = (tp->next)->next;
shouldret = 0;
break;
}
tp = tp->next;
shouldret = 1;
}
}
if (shouldret)
return;
/* If we did, add it to the free list */
cn->next = freeconns;
freeconns = cn;
/* Close it all down */
if (cn->networkdesc != -1)
{
if (cn->is_ssl)
{
ssl_free(cn->ssl);
cn->ssl = NULL;
}
SOCKET_CLOSE(cn->networkdesc);
}
if (cn->filedesc != -1)
close(cn->filedesc);
#if defined(CONFIG_HTTP_HAS_DIRECTORIES)
if (cn->dirp != NULL)
#ifdef WIN32
FindClose(cn->dirp);
#else
closedir(cn->dirp);
#endif
#endif
}
/*
* Change directories one way or the other.
*/
static void ax_chdir(void)
{
static char *webroot = CONFIG_HTTP_WEBROOT;
#if defined(WIN32) || !defined(CONFIG_HTTP_USE_CHROOT)
if (chdir(webroot))
#else /* use chroot() instead */
if (chroot(webroot))
#endif
{
#ifdef CONFIG_HTTP_VERBOSE
fprintf(stderr, "'%s' is not a directory\n", webroot);
#endif
exit(1);
}
}

124
httpd/htpasswd.c Normal file
View File

@ -0,0 +1,124 @@
/*
* Copyright(C) 2007 Cameron Rich
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ssl.h"
int tfd;
void base64_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen)
{
static const char b64str[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
while (inlen && outlen)
{
*out++ = b64str[(in[0] >> 2) & 0x3f];
if (!--outlen)
break;
*out++ = b64str[((in[0] << 4)
+ (--inlen ? in[1] >> 4 : 0)) & 0x3f];
if (!--outlen)
break;
*out++ = (inlen
? b64str[((in[1] << 2)
+ (--inlen ? in[2] >> 6 : 0))
& 0x3f]
: '=');
if (!--outlen)
break;
*out++ = inlen ? b64str[in[2] & 0x3f] : '=';
if (!--outlen)
break;
if (inlen)
inlen--;
if (inlen)
in += 3;
}
if (outlen)
*out = '\0';
}
static void usage(void)
{
fprintf(stderr,"Usage: htpasswd username\n");
exit(1);
}
#ifdef WIN32
static char * getpass(const char *prompt)
{
static char buf[127];
FILE *fp = stdin;
printf(prompt); TTY_FLUSH();
#if 0
fp = fopen("/dev/tty", "w");
if (fp == NULL)
{
printf("null\n"); TTY_FLUSH();
fp = stdin;
}
#endif
fgets(buf, sizeof(buf), fp);
while (buf[strlen(buf)-1] < ' ')
buf[strlen(buf)-1] = '\0';
//if (fp != stdin)
// fclose(fp);
return buf;
}
#endif
int main(int argc, char *argv[])
{
char* pw;
uint8_t md5_salt[MD5_SIZE], md5_pass[MD5_SIZE];
char b64_salt[MD5_SIZE+10], b64_pass[MD5_SIZE+10];
MD5_CTX ctx;
if (argc != 2)
usage();
pw = strdup(getpass("New password:"));
if (strcmp(pw, getpass("Re-type new password:")) != 0)
{
fprintf(stderr, "They don't match, sorry.\n" );
exit(1);
}
RNG_initialize((uint8_t *)pw, sizeof(pw));
get_random(MD5_SIZE, md5_salt);
RNG_terminate();
base64_encode(md5_salt, MD5_SIZE, b64_salt, sizeof(b64_salt));
MD5Init(&ctx);
MD5Update(&ctx, md5_salt, MD5_SIZE);
MD5Update(&ctx, (uint8_t *)pw, strlen(pw));
MD5Final(&ctx, md5_pass);
base64_encode(md5_pass, MD5_SIZE, b64_pass, sizeof(b64_pass));
printf("Add the following to your '.htpasswd' file\n");
printf("%s:%s$%s\n", argv[1], b64_salt, b64_pass);
return 0;
}

194
httpd/mime_types.c Normal file
View File

@ -0,0 +1,194 @@
/*
* Copyright(C) 2007 Cameron Rich
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "axhttp.h"
static const char mime_default[] = "text/plain";
typedef struct
{
const char * const ext;
const char * const type;
} mime_table_t;
static mime_table_t mime_table[] =
{
/* Fundamental types */
{ ".html", "text/html" },
{ ".htm", "text/html" },
{ ".css", "text/css" },
/* Basic graphics */
{ ".jpg", "image/jpeg" },
{ ".gif", "image/gif" },
{ ".png", "image/png" },
#ifdef CONFIG_HTTP_ALL_MIME_TYPES
/* This list is a bit expensive to maintain normally, so it's an option. */
{ ".txt", "text/plain" },
{ ".rtx", "text/richtext" },
{ ".etx", "text/x-setext" },
{ ".tsv", "text/tab-separated-values" },
{ ".xml", "text/xml" },
{ ".dtd", "text/xml" },
{ ".jpe", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".jfif", "image/jpeg" },
{ ".tif", "image/tiff" },
{ ".tiff", "image/tiff" },
{ ".pbm", "image/x-portable-bitmap" },
{ ".pgm", "image/x-portable-graymap" },
{ ".ppm", "image/x-portable-pixmap" },
{ ".pnm", "image/x-portable-anymap" },
{ ".xbm", "image/x-xbitmap" },
{ ".xpm", "image/x-xpixmap" },
{ ".xwd", "image/x-xwindowdump" },
{ ".ief", "image/ief" },
{ ".au", "audio/basic" },
{ ".snd", "audio/basic" },
{ ".aif", "audio/x-aiff" },
{ ".aiff", "audio/x-aiff" },
{ ".aifc", "audio/x-aiff" },
{ ".ra", "audio/x-pn-realaudio" },
{ ".ram", "audio/x-pn-realaudio" },
{ ".rm", "audio/x-pn-realaudio" },
{ ".rpm", "audio/x-pn-realaudio-plugin" },
{ ".wav", "audio/wav" },
{ ".mid", "audio/midi" },
{ ".midi", "audio/midi" },
{ ".kar", "audio/midi" },
{ ".mpga", "audio/mpeg" },
{ ".mp2", "audio/mpeg" },
{ ".mp3", "audio/mpeg" },
{ ".mpeg", "video/mpeg" },
{ ".mpg", "video/mpeg" },
{ ".mpe", "video/mpeg" },
{ ".qt", "video/quicktime" },
{ ".mov", "video/quicktime" },
{ ".avi", "video/x-msvideo" },
{ ".movie", "video/x-sgi-movie" },
{ ".mv", "video/x-sgi-movie" },
{ ".vx", "video/x-rad-screenplay" },
{ ".a", "application/octet-stream" },
{ ".bin", "application/octet-stream" },
{ ".exe", "application/octet-stream" },
{ ".dump", "application/octet-stream" },
{ ".o", "application/octet-stream" },
{ ".class", "application/java" },
{ ".js", "application/x-javascript" },
{ ".ai", "application/postscript" },
{ ".eps", "application/postscript" },
{ ".ps", "application/postscript" },
{ ".dir", "application/x-director" },
{ ".dcr", "application/x-director" },
{ ".dxr", "application/x-director" },
{ ".fgd", "application/x-director" },
{ ".aam", "application/x-authorware-map" },
{ ".aas", "application/x-authorware-seg" },
{ ".aab", "application/x-authorware-bin" },
{ ".fh4", "image/x-freehand" },
{ ".fh7", "image/x-freehand" },
{ ".fh5", "image/x-freehand" },
{ ".fhc", "image/x-freehand" },
{ ".fh", "image/x-freehand" },
{ ".spl", "application/futuresplash" },
{ ".swf", "application/x-shockwave-flash" },
{ ".dvi", "application/x-dvi" },
{ ".gtar", "application/x-gtar" },
{ ".hdf", "application/x-hdf" },
{ ".hqx", "application/mac-binhex40" },
{ ".iv", "application/x-inventor" },
{ ".latex", "application/x-latex" },
{ ".man", "application/x-troff-man" },
{ ".me", "application/x-troff-me" },
{ ".mif", "application/x-mif" },
{ ".ms", "application/x-troff-ms" },
{ ".oda", "application/oda" },
{ ".pdf", "application/pdf" },
{ ".rtf", "application/rtf" },
{ ".bcpio", "application/x-bcpio" },
{ ".cpio", "application/x-cpio" },
{ ".sv4cpio", "application/x-sv4cpio" },
{ ".sv4crc", "application/x-sv4crc" },
{ ".sh", "application/x-shar" },
{ ".shar", "application/x-shar" },
{ ".sit", "application/x-stuffit" },
{ ".tar", "application/x-tar" },
{ ".tex", "application/x-tex" },
{ ".texi", "application/x-texinfo" },
{ ".texinfo", "application/x-texinfo" },
{ ".tr", "application/x-troff" },
{ ".roff", "application/x-troff" },
{ ".man", "application/x-troff-man" },
{ ".me", "application/x-troff-me" },
{ ".ms", "application/x-troff-ms" },
{ ".zip", "application/x-zip-compressed" },
{ ".tsp", "application/dsptype" },
{ ".wsrc", "application/x-wais-source" },
{ ".ustar", "application/x-ustar" },
{ ".cdf", "application/x-netcdf" },
{ ".nc", "application/x-netcdf" },
{ ".doc", "application/msword" },
{ ".ppt", "application/powerpoint" },
{ ".wrl", "model/vrml" },
{ ".vrml", "model/vrml" },
{ ".mime", "message/rfc822" },
{ ".pac", "application/x-ns-proxy-autoconfig" },
{ ".wml", "text/vnd.wap.wml" },
{ ".wmlc", "application/vnd.wap.wmlc" },
{ ".wmls", "text/vnd.wap.wmlscript" },
{ ".wmlsc", "application/vnd.wap.wmlscriptc" },
{ ".wbmp", "image/vnd.wap.wbmp" },
{ ".tgz", "application/x-gzip" },
{ ".tar.gz", "application/x-gzip" },
{ ".bz2", "application/x-bzip2" },
{ ".zip", "application/zip" }
#endif
};
static int mime_cmp(const mime_table_t *t1, const mime_table_t *t2)
{
return strcasecmp(t1->ext, t2->ext);
}
void mime_init(void)
{
qsort(mime_table, sizeof(mime_table)/sizeof(mime_table_t),
sizeof(mime_table_t),
(int (*)(const void *, const void *))mime_cmp);
}
const char *getmimetype(const char *name)
{
mime_table_t *mime_type;
if ((name = strrchr(name, '.')) == NULL)
return mime_default;
mime_type = bsearch(&name, mime_table,
sizeof(mime_table)/sizeof(mime_table_t),
sizeof(mime_table_t),
(int (*)(const void *, const void *))mime_cmp);
return mime_type == NULL ? mime_default : mime_type->type;
}

1034
httpd/proc.c Normal file

File diff suppressed because it is too large Load Diff

107
httpd/tdate_parse.c Normal file
View File

@ -0,0 +1,107 @@
/*
* Copyright(C) 2007 Cameron Rich
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "axhttp.h"
struct day_mon_map
{
const char* s;
uint8_t l;
};
static struct day_mon_map wday_tab[] =
{
{ "Sun", 0 }, { "Mon", 1 }, { "Tue", 2 }, { "Wed", 3 },
{ "Thu", 4 }, { "Fri", 5 }, { "Sat", 6 },
};
static struct day_mon_map mon_tab[] =
{
{ "Jan", 0 }, { "Feb", 1 }, { "Mar", 2 }, { "Apr", 3 },
{ "May", 4 }, { "Jun", 5 }, { "Jul", 6 }, { "Aug", 7 },
{ "Sep", 8 }, { "Oct", 9 }, { "Nov", 10 }, { "Dec", 11 },
};
static int day_mon_map_compare(const char *v1, const char *v2)
{
return strcmp(((struct day_mon_map*)v1)->s, ((struct day_mon_map*)v2)->s);
}
void tdate_init(void)
{
qsort(wday_tab, sizeof(wday_tab)/sizeof(struct day_mon_map),
sizeof(struct day_mon_map),
(int (*)(const void *, const void *))day_mon_map_compare);
qsort(mon_tab, sizeof(mon_tab)/sizeof(struct day_mon_map),
sizeof(struct day_mon_map),
(int (*)(const void *, const void *))day_mon_map_compare);
}
static int8_t day_mon_map_search(const char* str,
const struct day_mon_map* tab, int n)
{
struct day_mon_map *search = bsearch(&str, tab, n,
sizeof(struct day_mon_map),
(int (*)(const void *, const void *))day_mon_map_compare);
return search ? search->l : -1;
}
time_t tdate_parse(const char* str)
{
struct tm tm;
char str_mon[4], str_wday[4];
int tm_sec, tm_min, tm_hour, tm_mday, tm_year;
/* Initialize. */
memset(&tm, 0, sizeof(struct tm));
/* wdy, DD mth YY HH:MM:SS GMT */
if ((sscanf(str, "%3[a-zA-Z], %d %3[a-zA-Z] %d %d:%d:%d GMT",
str_wday, &tm_mday, str_mon, &tm_year, &tm_hour, &tm_min,
&tm_sec) == 7) ||
/* wdy mth DD HH:MM:SS YY */
(sscanf(str, "%3[a-zA-Z] %3[a-zA-Z] %d %d:%d:%d %d",
str_wday, str_mon, &tm_mday, &tm_hour, &tm_min, &tm_sec,
&tm_year) == 7))
{
int8_t tm_wday = day_mon_map_search(str_wday, wday_tab,
sizeof(wday_tab)/sizeof(struct day_mon_map));
int8_t tm_mon = day_mon_map_search(str_mon, mon_tab,
sizeof(mon_tab)/sizeof(struct day_mon_map));
if (tm_wday < 0 || tm_mon < 0)
return -1;
tm.tm_wday = tm_wday;
tm.tm_mon = tm_mon;
tm.tm_mday = tm_mday;
tm.tm_hour = tm_hour;
tm.tm_min = tm_min;
tm.tm_sec = tm_sec;
tm.tm_year = tm_year - 1900;
return mktime(&tm);
}
return -1; /* error */
}