last_patch139.gz from Vladimir N. Oleynik:

>I also don't mean to disagree about leaving 30x status codes until after
>1.0.  In fact, although redirecting http://host/dir to http://host/dir/
>with a 301 is common practice (e.g. Apache, IIS), AFAIK it isn't
>actually required (or mentioned) by the HTTP specs.

Ok.
Attached patch have 302 and 408 implemented features.


--w
vodz
This commit is contained in:
Eric Andersen 2004-10-08 08:03:29 +00:00
parent 9e954abc4f
commit 07f2fea62c

View File

@ -2,7 +2,7 @@
* httpd implementation for busybox * httpd implementation for busybox
* *
* Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org> * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
* Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> * Copyright (C) 2003,2004 Vladimir Oleynik <dzo@simtreas.ru>
* *
* simplify patch stolen from libbb without using strdup * simplify patch stolen from libbb without using strdup
* *
@ -116,7 +116,7 @@
#include "busybox.h" #include "busybox.h"
static const char httpdVersion[] = "busybox httpd/1.34 2-Oct-2003"; static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
static const char default_path_httpd_conf[] = "/etc"; static const char default_path_httpd_conf[] = "/etc";
static const char httpd_conf[] = "httpd.conf"; static const char httpd_conf[] = "httpd.conf";
static const char home[] = "./"; static const char home[] = "./";
@ -127,6 +127,8 @@ static const char home[] = "./";
# define cont_l_fmt "%ld" # define cont_l_fmt "%ld"
#endif #endif
#define TIMEOUT 60
// Note: busybox xfuncs are not used because we want the server to keep running // Note: busybox xfuncs are not used because we want the server to keep running
// if something bad happens due to a malformed user request. // if something bad happens due to a malformed user request.
// As a result, all memory allocation after daemonize // As a result, all memory allocation after daemonize
@ -218,6 +220,8 @@ typedef struct
char *remoteuser; char *remoteuser;
#endif #endif
const char *query;
#ifdef CONFIG_FEATURE_HTTPD_CGI #ifdef CONFIG_FEATURE_HTTPD_CGI
char *referer; char *referer;
#endif #endif
@ -230,8 +234,11 @@ typedef struct
#endif #endif
unsigned port; /* server initial port and for unsigned port; /* server initial port and for
set env REMOTE_PORT */ set env REMOTE_PORT */
union HTTPD_FOUND {
const char *found_mime_type;
const char *found_moved_temporarily;
} httpd_found;
const char *found_mime_type;
off_t ContentLength; /* -1 - unknown */ off_t ContentLength; /* -1 - unknown */
time_t last_mod; time_t last_mod;
@ -253,6 +260,8 @@ typedef struct
#define a_c_r 0 #define a_c_r 0
#define a_c_w 1 #define a_c_w 1
#endif #endif
volatile int alarm_signaled;
} HttpdConfig; } HttpdConfig;
static HttpdConfig *config; static HttpdConfig *config;
@ -284,11 +293,13 @@ static const char* const suffixTable [] = {
typedef enum typedef enum
{ {
HTTP_OK = 200, HTTP_OK = 200,
HTTP_MOVED_TEMPORARILY = 302,
HTTP_BAD_REQUEST = 400, /* malformed syntax */
HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */ HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */
HTTP_NOT_FOUND = 404, HTTP_NOT_FOUND = 404,
HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
HTTP_BAD_REQUEST = 400, /* malformed syntax */
HTTP_FORBIDDEN = 403, HTTP_FORBIDDEN = 403,
HTTP_REQUEST_TIMEOUT = 408,
HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */
HTTP_INTERNAL_SERVER_ERROR = 500, HTTP_INTERNAL_SERVER_ERROR = 500,
#if 0 /* future use */ #if 0 /* future use */
HTTP_CONTINUE = 100, HTTP_CONTINUE = 100,
@ -299,7 +310,6 @@ typedef enum
HTTP_NO_CONTENT = 204, HTTP_NO_CONTENT = 204,
HTTP_MULTIPLE_CHOICES = 300, HTTP_MULTIPLE_CHOICES = 300,
HTTP_MOVED_PERMANENTLY = 301, HTTP_MOVED_PERMANENTLY = 301,
HTTP_MOVED_TEMPORARILY = 302,
HTTP_NOT_MODIFIED = 304, HTTP_NOT_MODIFIED = 304,
HTTP_PAYMENT_REQUIRED = 402, HTTP_PAYMENT_REQUIRED = 402,
HTTP_BAD_GATEWAY = 502, HTTP_BAD_GATEWAY = 502,
@ -317,6 +327,9 @@ typedef struct
static const HttpEnumString httpResponseNames[] = { static const HttpEnumString httpResponseNames[] = {
{ HTTP_OK, "OK" }, { HTTP_OK, "OK" },
{ HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
{ HTTP_REQUEST_TIMEOUT, "Request Timeout",
"No request appeared within a reasonable time period." },
{ HTTP_NOT_IMPLEMENTED, "Not Implemented", { HTTP_NOT_IMPLEMENTED, "Not Implemented",
"The requested method is not recognized by this server." }, "The requested method is not recognized by this server." },
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
@ -334,7 +347,6 @@ static const HttpEnumString httpResponseNames[] = {
{ HTTP_NO_CONTENT, "No Content" }, { HTTP_NO_CONTENT, "No Content" },
{ HTTP_MULTIPLE_CHOICES, "Multiple Choices" }, { HTTP_MULTIPLE_CHOICES, "Multiple Choices" },
{ HTTP_MOVED_PERMANENTLY, "Moved Permanently" }, { HTTP_MOVED_PERMANENTLY, "Moved Permanently" },
{ HTTP_MOVED_TEMPORARILY, "Moved Temporarily" },
{ HTTP_NOT_MODIFIED, "Not Modified" }, { HTTP_NOT_MODIFIED, "Not Modified" },
{ HTTP_BAD_GATEWAY, "Bad Gateway", "" }, { HTTP_BAD_GATEWAY, "Bad Gateway", "" },
{ HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" }, { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" },
@ -943,6 +955,7 @@ static int sendHeaders(HttpResponseNum responseNum)
char *buf = config->buf; char *buf = config->buf;
const char *responseString = ""; const char *responseString = "";
const char *infoString = 0; const char *infoString = 0;
const char *mime_type;
unsigned int i; unsigned int i;
time_t timer = time(0); time_t timer = time(0);
char timeStr[80]; char timeStr[80];
@ -956,16 +969,16 @@ static int sendHeaders(HttpResponseNum responseNum)
break; break;
} }
} }
if (responseNum != HTTP_OK) { /* error message is HTML */
config->found_mime_type = "text/html"; // error message is HTML mime_type = responseNum == HTTP_OK ?
} config->httpd_found.found_mime_type : "text/html";
/* emit the current date */ /* emit the current date */
strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer)); strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
len = sprintf(buf, len = sprintf(buf,
"HTTP/1.0 %d %s\nContent-type: %s\r\n" "HTTP/1.0 %d %s\nContent-type: %s\r\n"
"Date: %s\r\nConnection: close\r\n", "Date: %s\r\nConnection: close\r\n",
responseNum, responseString, config->found_mime_type, timeStr); responseNum, responseString, mime_type, timeStr);
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
if (responseNum == HTTP_UNAUTHORIZED) { if (responseNum == HTTP_UNAUTHORIZED) {
@ -973,6 +986,13 @@ static int sendHeaders(HttpResponseNum responseNum)
config->realm); config->realm);
} }
#endif #endif
if(responseNum == HTTP_MOVED_TEMPORARILY) {
len += sprintf(buf+len, "Location: %s/%s%s\r\n",
config->httpd_found.found_moved_temporarily,
(config->query ? "?" : ""),
(config->query ? config->query : ""));
}
if (config->ContentLength != -1) { /* file */ if (config->ContentLength != -1) { /* file */
strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod)); strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n", len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
@ -1035,7 +1055,6 @@ static int getLine(void)
* *
* $Parameters: * $Parameters:
* (const char *) url . . . . . . The requested URL (with leading /). * (const char *) url . . . . . . The requested URL (with leading /).
* (const char *urlArgs). . . . . Any URL arguments.
* (int bodyLen) . . . . . . . . Length of the post body. * (int bodyLen) . . . . . . . . Length of the post body.
* (const char *cookie) . . . . . For set HTTP_COOKIE. * (const char *cookie) . . . . . For set HTTP_COOKIE.
* (const char *content_type) . . For set CONTENT_TYPE. * (const char *content_type) . . For set CONTENT_TYPE.
@ -1047,8 +1066,7 @@ static int getLine(void)
* *
****************************************************************************/ ****************************************************************************/
static int sendCgi(const char *url, static int sendCgi(const char *url,
const char *request, const char *urlArgs, const char *request, int bodyLen, const char *cookie,
int bodyLen, const char *cookie,
const char *content_type) const char *content_type)
{ {
int fromCgi[2]; /* pipe for reading data from CGI */ int fromCgi[2]; /* pipe for reading data from CGI */
@ -1118,10 +1136,10 @@ static int sendCgi(const char *url,
addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */ addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
addEnv("PATH", "", getenv("PATH")); addEnv("PATH", "", getenv("PATH"));
addEnv("REQUEST", "METHOD", request); addEnv("REQUEST", "METHOD", request);
if(urlArgs) { if(config->query) {
char *uri = alloca(strlen(purl) + 2 + strlen(urlArgs)); char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
if(uri) if(uri)
sprintf(uri, "%s?%s", purl, urlArgs); sprintf(uri, "%s?%s", purl, config->query);
addEnv("REQUEST", "URI", uri); addEnv("REQUEST", "URI", uri);
} else { } else {
addEnv("REQUEST", "URI", purl); addEnv("REQUEST", "URI", purl);
@ -1130,7 +1148,7 @@ static int sendCgi(const char *url,
*script = '\0'; /* reduce /PATH_INFO */ *script = '\0'; /* reduce /PATH_INFO */
/* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */ /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
addEnv("SCRIPT_NAME", "", purl); addEnv("SCRIPT_NAME", "", purl);
addEnv("QUERY_STRING", "", urlArgs); addEnv("QUERY_STRING", "", config->query);
addEnv("SERVER", "SOFTWARE", httpdVersion); addEnv("SERVER", "SOFTWARE", httpdVersion);
addEnv("SERVER", "PROTOCOL", "HTTP/1.0"); addEnv("SERVER", "PROTOCOL", "HTTP/1.0");
addEnv("GATEWAY_INTERFACE", "", "CGI/1.1"); addEnv("GATEWAY_INTERFACE", "", "CGI/1.1");
@ -1324,14 +1342,14 @@ static int sendFile(const char *url)
break; break;
} }
/* also, if not found, set default as "application/octet-stream"; */ /* also, if not found, set default as "application/octet-stream"; */
config->found_mime_type = *(table+1); config->httpd_found.found_mime_type = *(table+1);
#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
if (suffix) { if (suffix) {
Htaccess * cur; Htaccess * cur;
for (cur = config->mime_a; cur; cur = cur->next) { for (cur = config->mime_a; cur; cur = cur->next) {
if(strcmp(cur->before_colon, suffix) == 0) { if(strcmp(cur->before_colon, suffix) == 0) {
config->found_mime_type = cur->after_colon; config->httpd_found.found_mime_type = cur->after_colon;
break; break;
} }
} }
@ -1341,7 +1359,7 @@ static int sendFile(const char *url)
#ifdef DEBUG #ifdef DEBUG
if (config->debugHttpd) if (config->debugHttpd)
fprintf(stderr, "Sending file '%s' Content-type: %s\n", fprintf(stderr, "Sending file '%s' Content-type: %s\n",
url, config->found_mime_type); url, config->httpd_found.found_mime_type);
#endif #endif
f = open(url, O_RDONLY); f = open(url, O_RDONLY);
@ -1485,6 +1503,20 @@ set_remoteuser_var:
#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */ #endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
/****************************************************************************
*
> $Function: handleIncoming()
*
* $Description: Handle an incoming http request.
*
****************************************************************************/
static void
handle_sigalrm( int sig )
{
sendHeaders(HTTP_REQUEST_TIMEOUT);
config->alarm_signaled = sig;
}
/**************************************************************************** /****************************************************************************
* *
@ -1499,7 +1531,6 @@ static void handleIncoming(void)
char *url; char *url;
char *purl; char *purl;
int blank = -1; int blank = -1;
char *urlArgs;
char *test; char *test;
struct stat sb; struct stat sb;
int ip_allowed; int ip_allowed;
@ -1514,14 +1545,21 @@ static void handleIncoming(void)
struct timeval tv; struct timeval tv;
int retval; int retval;
#endif #endif
struct sigaction sa;
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
int credentials = -1; /* if not requred this is Ok */ int credentials = -1; /* if not requred this is Ok */
#endif #endif
sa.sa_handler = handle_sigalrm;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; /* no SA_RESTART */
sigaction(SIGALRM, &sa, NULL);
do { do {
int count; int count;
(void) alarm( TIMEOUT );
if (getLine() <= 0) if (getLine() <= 0)
break; /* closed */ break; /* closed */
@ -1561,9 +1599,11 @@ BAD_REQUEST:
} }
strcpy(url, buf); strcpy(url, buf);
/* extract url args if present */ /* extract url args if present */
urlArgs = strchr(url, '?'); test = strchr(url, '?');
if (urlArgs) if (test) {
*urlArgs++ = 0; *test++ = 0;
config->query = test;
}
/* algorithm stolen from libbb bb_simplify_path(), /* algorithm stolen from libbb bb_simplify_path(),
but don`t strdup and reducing trailing slash and protect out root */ but don`t strdup and reducing trailing slash and protect out root */
@ -1593,17 +1633,16 @@ BAD_REQUEST:
*++purl = 0; /* so keep last character */ *++purl = 0; /* so keep last character */
test = purl; /* end ptr */ test = purl; /* end ptr */
/* If URL is directory, adding '/' */
/* If URL is directory, adding '/' */ /* If URL is directory, adding '/' */
if(test[-1] != '/') { if(test[-1] != '/') {
if ( is_directory(url + 1, 1, &sb) ) { if ( is_directory(url + 1, 1, &sb) ) {
*test++ = '/'; config->httpd_found.found_moved_temporarily = url;
*test = 0;
purl = test; /* end ptr */
} }
} }
#ifdef DEBUG #ifdef DEBUG
if (config->debugHttpd) if (config->debugHttpd)
fprintf(stderr, "url='%s', args=%s\n", url, urlArgs); fprintf(stderr, "url='%s', args=%s\n", url, config->query);
#endif #endif
test = url; test = url;
@ -1620,7 +1659,7 @@ BAD_REQUEST:
} }
// read until blank line for HTTP version specified, else parse immediate // read until blank line for HTTP version specified, else parse immediate
while (blank >= 0 && (count = getLine()) > 0) { while (blank >= 0 && alarm(TIMEOUT) >= 0 && (count = getLine()) > 0) {
#ifdef DEBUG #ifdef DEBUG
if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf); if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
@ -1665,6 +1704,9 @@ BAD_REQUEST:
} /* while extra header reading */ } /* while extra header reading */
(void) alarm( 0 );
if(config->alarm_signaled)
break;
if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) { if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
/* protect listing [/path]/httpd_conf or IP deny */ /* protect listing [/path]/httpd_conf or IP deny */
@ -1682,6 +1724,16 @@ FORBIDDEN: /* protect listing /cgi-bin */
} }
#endif #endif
if(config->httpd_found.found_moved_temporarily) {
sendHeaders(HTTP_MOVED_TEMPORARILY);
#ifdef DEBUG
/* clear unforked memory flag */
if(config->debugHttpd)
config->httpd_found.found_moved_temporarily = NULL;
#endif
break;
}
test = url + 1; /* skip first '/' */ test = url + 1; /* skip first '/' */
#ifdef CONFIG_FEATURE_HTTPD_CGI #ifdef CONFIG_FEATURE_HTTPD_CGI
@ -1692,7 +1744,7 @@ FORBIDDEN: /* protect listing /cgi-bin */
if (strncmp(test, "cgi-bin", 7) == 0) { if (strncmp(test, "cgi-bin", 7) == 0) {
if(test[7] == '/' && test[8] == 0) if(test[7] == '/' && test[8] == 0)
goto FORBIDDEN; // protect listing cgi-bin/ goto FORBIDDEN; // protect listing cgi-bin/
sendCgi(url, prequest, urlArgs, length, cookie, content_type); sendCgi(url, prequest, length, cookie, content_type);
} else { } else {
if (prequest != request_GET) if (prequest != request_GET)
sendHeaders(HTTP_NOT_IMPLEMENTED); sendHeaders(HTTP_NOT_IMPLEMENTED);