1
0
mirror of git://projects.qi-hardware.com/openwrt-xburst.git synced 2024-11-27 17:55:55 +02:00

[package] uhttpd: display errors in init script, code formatting changes, bump package version

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@31572 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
jow 2012-05-03 17:19:22 +00:00
parent 37f687e099
commit d0f3681e1e
7 changed files with 535 additions and 496 deletions

View File

@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=uhttpd
PKG_RELEASE:=31
PKG_RELEASE:=32
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
PKG_CONFIG_DEPENDS := \

View File

@ -108,6 +108,11 @@ start_instance()
SERVICE_PID_FILE=/var/run/uhttpd_${cfg}.pid
service_start $UHTTPD_BIN -f $UHTTPD_ARGS
# Check if daemon is running, if not then
# re-execute in foreground to display error.
sleep 1 && service_check $UHTTPD_BIN || \
$UHTTPD_BIN -f $UHTTPD_ARGS
}
stop_instance()

View File

@ -31,8 +31,8 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
if (((bufptr = strfind(buf, len, "\r\n\r\n", 4)) != NULL) ||
((bufptr = strfind(buf, len, "\n\n", 2)) != NULL)
) {
((bufptr = strfind(buf, len, "\n\n", 2)) != NULL))
{
*off = (int)(bufptr - buf) + ((bufptr[0] == '\r') ? 4 : 2);
memset(&res, 0, sizeof(res));
@ -79,9 +79,12 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
if (res.statuscode < 100)
res.statuscode = 200;
if( ((bufptr = strchr(bufptr, ' ')) != NULL) && (&bufptr[1] != 0) )
if (((bufptr = strchr(bufptr, ' ')) != NULL) &&
(&bufptr[1] != 0))
{
res.statusmsg = &bufptr[1];
}
}
else
{
res.headers[hdrcount++] = hdrname;
@ -105,7 +108,8 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
return NULL;
}
static char * uh_cgi_header_lookup(struct http_response *res, const char *hdrname)
static char * uh_cgi_header_lookup(struct http_response *res,
const char *hdrname)
{
int i;
@ -118,16 +122,16 @@ static char * uh_cgi_header_lookup(struct http_response *res, const char *hdrnam
return NULL;
}
static int uh_cgi_error_500(struct client *cl, struct http_request *req, const char *message)
static int uh_cgi_error_500(struct client *cl, struct http_request *req,
const char *message)
{
if (uh_http_sendf(cl, NULL,
"HTTP/%.1f 500 Internal Server Error\r\n"
"Content-Type: text/plain\r\n%s\r\n",
req->version,
(req->version > 1.0)
? "Transfer-Encoding: chunked\r\n" : ""
) >= 0
) {
? "Transfer-Encoding: chunked\r\n" : "") >= 0)
{
return uh_http_send(cl, req, message, -1);
}
@ -135,10 +139,9 @@ static int uh_cgi_error_500(struct client *cl, struct http_request *req, const c
}
void uh_cgi_request(
struct client *cl, struct http_request *req,
struct path_info *pi, struct interpreter *ip
) {
void uh_cgi_request(struct client *cl, struct http_request *req,
struct path_info *pi, struct interpreter *ip)
{
int i, hdroff, bufoff, rv;
int hdrlen = 0;
int buflen = 0;
@ -213,8 +216,8 @@ void uh_cgi_request(
/* check for regular, world-executable file _or_ interpreter */
if (((pi->stat.st_mode & S_IFREG) &&
(pi->stat.st_mode & S_IXOTH)) || (ip != NULL)
) {
(pi->stat.st_mode & S_IXOTH)) || (ip != NULL))
{
/* build environment */
clearenv();
@ -338,21 +341,17 @@ void uh_cgi_request(
execl(pi->phys, pi->phys, NULL);
/* in case it fails ... */
printf(
"Status: 500 Internal Server Error\r\n\r\n"
printf("Status: 500 Internal Server Error\r\n\r\n"
"Unable to launch the requested CGI program:\n"
" %s: %s\n",
ip ? ip->path : pi->phys, strerror(errno)
);
ip ? ip->path : pi->phys, strerror(errno));
}
/* 403 */
else
{
printf(
"Status: 403 Forbidden\r\n\r\n"
"Access to this resource is forbidden\n"
);
printf("Status: 403 Forbidden\r\n\r\n"
"Access to this resource is forbidden\n");
}
close(wfd[0]);
@ -399,7 +398,9 @@ void uh_cgi_request(
timeout.tv_usec = 0;
ensure_out(rv = select_intr(fd_max, &reader,
(content_length > -1) ? &writer : NULL, NULL, &timeout));
(content_length > -1)
? &writer : NULL,
NULL, &timeout));
/* timeout */
if (rv == 0)
@ -603,4 +604,3 @@ void uh_cgi_request(
break;
}
}

View File

@ -56,8 +56,7 @@ static const char * uh_file_mktag(struct stat *s)
snprintf(tag, sizeof(tag), "\"%x-%x-%x\"",
(unsigned int) s->st_ino,
(unsigned int) s->st_size,
(unsigned int) s->st_mtime
);
(unsigned int) s->st_mtime);
return tag;
}
@ -98,26 +97,30 @@ static char * uh_file_header_lookup(struct http_request *req, const char *name)
}
static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s)
static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req,
struct stat *s)
{
ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
if (s)
{
ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)));
ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n", uh_file_unix2date(s->st_mtime)));
ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n",
uh_file_unix2date(s->st_mtime)));
}
return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
}
static int uh_file_response_200(struct client *cl, struct http_request *req, struct stat *s)
static int uh_file_response_200(struct client *cl, struct http_request *req,
struct stat *s)
{
ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version));
return uh_file_response_ok_hdrs(cl, req, s);
}
static int uh_file_response_304(struct client *cl, struct http_request *req, struct stat *s)
static int uh_file_response_304(struct client *cl, struct http_request *req,
struct stat *s)
{
ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version));
return uh_file_response_ok_hdrs(cl, req, s);
@ -130,7 +133,8 @@ static int uh_file_response_412(struct client *cl, struct http_request *req)
"Connection: close\r\n", req->version);
}
static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s, int *ok)
static int uh_file_if_match(struct client *cl, struct http_request *req,
struct stat *s, int *ok)
{
const char *tag = uh_file_mktag(s);
char *hdr = uh_file_header_lookup(req, "If-Match");
@ -164,7 +168,9 @@ static int uh_file_if_match(struct client *cl, struct http_request *req, struct
return *ok;
}
static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok)
static int uh_file_if_modified_since(struct client *cl,
struct http_request *req, struct stat *s,
int *ok)
{
char *hdr = uh_file_header_lookup(req, "If-Modified-Since");
*ok = 1;
@ -181,7 +187,8 @@ static int uh_file_if_modified_since(struct client *cl, struct http_request *req
return *ok;
}
static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s, int *ok)
static int uh_file_if_none_match(struct client *cl, struct http_request *req,
struct stat *s, int *ok)
{
const char *tag = uh_file_mktag(s);
char *hdr = uh_file_header_lookup(req, "If-None-Match");
@ -206,9 +213,13 @@ static int uh_file_if_none_match(struct client *cl, struct http_request *req, st
if ((req->method == UH_HTTP_MSG_GET) ||
(req->method == UH_HTTP_MSG_HEAD))
{
ensure_ret(uh_file_response_304(cl, req, s));
}
else
{
ensure_ret(uh_file_response_412(cl, req));
}
break;
}
@ -218,7 +229,8 @@ static int uh_file_if_none_match(struct client *cl, struct http_request *req, st
return *ok;
}
static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s, int *ok)
static int uh_file_if_range(struct client *cl, struct http_request *req,
struct stat *s, int *ok)
{
char *hdr = uh_file_header_lookup(req, "If-Range");
*ok = 1;
@ -232,7 +244,9 @@ static int uh_file_if_range(struct client *cl, struct http_request *req, struct
return *ok;
}
static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok)
static int uh_file_if_unmodified_since(struct client *cl,
struct http_request *req, struct stat *s,
int *ok)
{
char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since");
*ok = 1;
@ -255,7 +269,8 @@ static int uh_file_scandir_filter_dir(const struct dirent *e)
return strcmp(e->d_name, ".") ? 1 : 0;
}
static void uh_file_dirlist(struct client *cl, struct http_request *req, struct path_info *pi)
static void uh_file_dirlist(struct client *cl, struct http_request *req,
struct path_info *pi)
{
int i;
int count = 0;
@ -267,10 +282,10 @@ static void uh_file_dirlist(struct client *cl, struct http_request *req, struct
ensure_out(uh_http_sendf(cl, req,
"<html><head><title>Index of %s</title></head>"
"<body><h1>Index of %s</h1><hr /><ol>",
pi->name, pi->name
));
pi->name, pi->name));
if( (count = scandir(pi->phys, &files, uh_file_scandir_filter_dir, alphasort)) > 0 )
if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
alphasort)) > 0)
{
memset(filename, 0, sizeof(filename));
memcpy(filename, pi->phys, sizeof(filename));
@ -283,15 +298,18 @@ static void uh_file_dirlist(struct client *cl, struct http_request *req, struct
sizeof(filename) - strlen(files[i]->d_name));
if (!stat(filename, &s) &&
(s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH)
)
(s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
{
ensure_out(uh_http_sendf(cl, req,
"<li><strong><a href='%s%s'>%s</a>/</strong><br />"
"<small>modified: %s<br />directory - %.02f kbyte"
"<br /><br /></small></li>",
pi->name, files[i]->d_name, files[i]->d_name,
uh_file_unix2date(s.st_mtime), s.st_size / 1024.0
));
"<li><strong><a href='%s%s'>%s</a>/"
"</strong><br /><small>modified: %s"
"<br />directory - %.02f kbyte<br />"
"<br /></small></li>",
pi->name, files[i]->d_name,
files[i]->d_name,
uh_file_unix2date(s.st_mtime),
s.st_size / 1024.0));
}
*pathptr = 0;
}
@ -303,16 +321,19 @@ static void uh_file_dirlist(struct client *cl, struct http_request *req, struct
sizeof(filename) - strlen(files[i]->d_name));
if (!stat(filename, &s) &&
!(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH)
)
!(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))
{
ensure_out(uh_http_sendf(cl, req,
"<li><strong><a href='%s%s'>%s</a></strong><br />"
"<small>modified: %s<br />%s - %.02f kbyte<br />"
"<li><strong><a href='%s%s'>%s</a>"
"</strong><br /><small>modified: %s"
"<br />%s - %.02f kbyte<br />"
"<br /></small></li>",
pi->name, files[i]->d_name, files[i]->d_name,
pi->name, files[i]->d_name,
files[i]->d_name,
uh_file_unix2date(s.st_mtime),
uh_file_mime_lookup(filename), s.st_size / 1024.0
));
uh_file_mime_lookup(filename),
s.st_size / 1024.0));
}
*pathptr = 0;
}
@ -409,4 +430,3 @@ out:
if (fd > -1)
close(fd);
}

View File

@ -479,9 +479,10 @@ void uh_lua_request(struct client *cl, struct http_request *req, lua_State *L)
/* wait until we can read or write or both */
if (select_intr(fd_max, &reader,
(content_length > -1) ? &writer : NULL, NULL,
(data_sent < 1) ? &timeout : NULL) > 0
) {
(content_length > -1) ? &writer : NULL,
NULL,
(data_sent < 1) ? &timeout : NULL) > 0)
{
/* ready to write to Lua child */
if (FD_ISSET(wfd[1], &writer))
{

View File

@ -220,7 +220,8 @@ int uh_tcp_recv(struct client *cl, char *buf, int len)
}
int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
int uh_http_sendhf(struct client *cl, int code, const char *summary,
const char *fmt, ...)
{
va_list ap;
@ -271,9 +272,9 @@ int uh_http_sendc(struct client *cl, const char *data, int len)
return 0;
}
int uh_http_sendf(
struct client *cl, struct http_request *req, const char *fmt, ...
) {
int uh_http_sendf(struct client *cl, struct http_request *req,
const char *fmt, ...)
{
va_list ap;
char buffer[UH_LIMIT_MSGHEAD];
int len;
@ -290,9 +291,9 @@ int uh_http_sendf(
return 0;
}
int uh_http_send(
struct client *cl, struct http_request *req, const char *buf, int len
) {
int uh_http_send(struct client *cl, struct http_request *req,
const char *buf, int len)
{
if (len < 0)
len = strlen(buf);
@ -475,8 +476,8 @@ static char * canonpath(const char *path, char *path_resolved)
/* collapse /x/../ */
else if ((path_cpy[2] == '.') &&
((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
) {
((path_cpy[3] == '/') || (path_cpy[3] == '\0')))
{
while ((path_res > path_resolved) && (*--path_res != '/'))
;
@ -542,22 +543,26 @@ struct path_info * uh_path_lookup(struct client *cl, const char *url)
/* urldecode component w/o query */
if (pathptr > url)
if ( uh_urldecode(
&buffer[strlen(docroot)],
{
if (uh_urldecode(&buffer[strlen(docroot)],
sizeof(buffer) - strlen(docroot) - 1,
url, pathptr - url ) < 0)
{
return NULL; /* bad URL */
}
}
}
/* no query string, decode all of url */
else
{
if ( uh_urldecode(
&buffer[strlen(docroot)],
if (uh_urldecode(&buffer[strlen(docroot)],
sizeof(buffer) - strlen(docroot) - 1,
url, strlen(url) ) < 0)
{
return NULL; /* bad URL */
}
}
/* create canon path */
for (i = strlen(buffer), slash = (buffer[max(0, i-1)] == '/'); i >= 0; i--)
@ -568,8 +573,8 @@ struct path_info * uh_path_lookup(struct client *cl, const char *url)
memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
if (no_sym ? realpath(path_info, path_phys)
: canonpath(path_info, path_phys)
) {
: canonpath(path_info, path_phys))
{
memset(path_info, 0, sizeof(path_info));
memcpy(path_info, &buffer[i],
min(strlen(buffer) - i, sizeof(path_info) - 1));
@ -582,8 +587,8 @@ struct path_info * uh_path_lookup(struct client *cl, const char *url)
/* check whether found path is within docroot */
if (strncmp(path_phys, docroot, strlen(docroot)) ||
((path_phys[strlen(docroot)] != 0) &&
(path_phys[strlen(docroot)] != '/'))
) {
(path_phys[strlen(docroot)] != '/')))
{
return NULL;
}
@ -701,8 +706,8 @@ struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
/* try to resolve passwd entry */
if (((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
(pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
) {
(pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0))
{
memcpy(new->pass, pwd->pw_passwd,
min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
}
@ -729,9 +734,9 @@ struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
return NULL;
}
int uh_auth_check(
struct client *cl, struct http_request *req, struct path_info *pi
) {
int uh_auth_check(struct client *cl, struct http_request *req,
struct path_info *pi)
{
int i, plen, rlen, protected;
char buffer[UH_LIMIT_MSGHEAD];
char *user = NULL;
@ -763,8 +768,8 @@ int uh_auth_check(
{
if (!strcasecmp(req->headers[i], "Authorization") &&
(strlen(req->headers[i+1]) > 6) &&
!strncasecmp(req->headers[i+1], "Basic ", 6)
) {
!strncasecmp(req->headers[i+1], "Basic ", 6))
{
memset(buffer, 0, sizeof(buffer));
uh_b64decode(buffer, sizeof(buffer) - 1,
(unsigned char *) &req->headers[i+1][6],
@ -790,8 +795,8 @@ int uh_auth_check(
if ((plen >= rlen) &&
!strncasecmp(pi->name, realm->path, rlen) &&
!strcmp(user, realm->user)
) {
!strcmp(user, realm->user))
{
req->realm = realm;
break;
}
@ -933,8 +938,7 @@ struct interpreter * uh_interpreter_add(const char *extn, const char *path)
{
struct interpreter *new = NULL;
if( (new = (struct interpreter *)
malloc(sizeof(struct interpreter))) != NULL )
if ((new = (struct interpreter *)malloc(sizeof(struct interpreter))) != NULL)
{
memset(new, 0, sizeof(struct interpreter));

View File

@ -69,7 +69,9 @@ static void uh_config_parse(struct config *conf)
if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
!(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
!(eol = strchr(col2, '\n')) || (*eol++ = 0))
{
continue;
}
if (!uh_auth_add(line, col1, col2))
{
@ -83,7 +85,9 @@ static void uh_config_parse(struct config *conf)
{
if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
!(eol = strchr(col1, '\n')) || (*eol++ = 0))
{
continue;
}
conf->index_file = strdup(col1);
}
@ -91,7 +95,9 @@ static void uh_config_parse(struct config *conf)
{
if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
!(eol = strchr(col1, '\n')) || (*eol++ = 0))
{
continue;
}
conf->error_handler = strdup(col1);
}
@ -101,7 +107,9 @@ static void uh_config_parse(struct config *conf)
if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
!(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
!(eol = strchr(col2, '\n')) || (*eol++ = 0))
{
continue;
}
if (!uh_interpreter_add(col1, col2))
{
@ -118,10 +126,11 @@ static void uh_config_parse(struct config *conf)
}
}
static int uh_socket_bind(
fd_set *serv_fds, int *max_fd, const char *host, const char *port,
struct addrinfo *hints, int do_tls, struct config *conf
) {
static int uh_socket_bind(fd_set *serv_fds, int *max_fd,
const char *host, const char *port,
struct addrinfo *hints, int do_tls,
struct config *conf)
{
int sock = -1;
int yes = 1;
int status;
@ -225,7 +234,8 @@ static int uh_socket_bind(
return bound;
}
static struct http_request * uh_http_header_parse(struct client *cl, char *buffer, int buflen)
static struct http_request * uh_http_header_parse(struct client *cl,
char *buffer, int buflen)
{
char *method = &buffer[0];
char *path = NULL;
@ -338,8 +348,8 @@ static struct http_request * uh_http_header_parse(struct client *cl, char *buffe
/* have name but no value and found a colon, start of value */
else if (hdrname && !hdrdata &&
((i+1) < buflen) && (buffer[i] == ':')
) {
((i+1) < buflen) && (buffer[i] == ':'))
{
buffer[i] = 0;
hdrdata = &buffer[i+1];
@ -437,8 +447,8 @@ static int uh_path_match(const char *prefix, const char *url)
if ((strstr(url, prefix) == url) &&
((prefix[strlen(prefix)-1] == '/') ||
(strlen(url) == strlen(prefix)) ||
(url[strlen(prefix)] == '/'))
) {
(url[strlen(prefix)] == '/')))
{
return 1;
}
@ -446,9 +456,9 @@ static int uh_path_match(const char *prefix, const char *url)
}
#endif
static void uh_dispatch_request(
struct client *cl, struct http_request *req, struct path_info *pin
) {
static void uh_dispatch_request(struct client *cl, struct http_request *req,
struct path_info *pin)
{
#ifdef HAVE_CGI
struct interpreter *ipr = NULL;
@ -543,7 +553,8 @@ static void uh_mainloop(struct config *conf, fd_set serv_fds, int max_fd)
else
{
fprintf(stderr,
"uh_client_add(): Cannot allocate memory\n");
"uh_client_add(): "
"Cannot allocate memory\n");
close(new_fd);
}
@ -668,8 +679,8 @@ static inline int uh_inittls(struct config *conf)
!(conf->tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
!(conf->tls_close = dlsym(lib, "uh_tls_client_close")) ||
!(conf->tls_recv = dlsym(lib, "uh_tls_client_recv")) ||
!(conf->tls_send = dlsym(lib, "uh_tls_client_send"))
) {
!(conf->tls_send = dlsym(lib, "uh_tls_client_send")))
{
fprintf(stderr,
"Error: Failed to lookup required symbols "
"in TLS plugin: %s\n", dlerror()
@ -756,8 +767,8 @@ int main (int argc, char **argv)
while ((opt = getopt(argc, argv,
"fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:A:")) > 0
) {
"fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:A:")) > 0)
{
switch(opt)
{
/* [addr:]port */
@ -796,10 +807,9 @@ int main (int argc, char **argv)
#endif
/* bind sockets */
bound += uh_socket_bind(
&serv_fds, &max_fd, bind[0] ? bind : NULL, port,
&hints, (opt == 's'), &conf
);
bound += uh_socket_bind(&serv_fds, &max_fd,
bind[0] ? bind : NULL,
port, &hints, (opt == 's'), &conf);
memset(bind, 0, sizeof(bind));
break;
@ -1065,16 +1075,15 @@ int main (int argc, char **argv)
{
fprintf(stderr,
"Notice: Unable to load Lua plugin - disabling Lua support! "
"(Reason: %s)\n", dlerror()
);
"(Reason: %s)\n", dlerror());
}
else
{
/* resolve functions */
if (!(conf.lua_init = dlsym(lib, "uh_lua_init")) ||
!(conf.lua_close = dlsym(lib, "uh_lua_close")) ||
!(conf.lua_request = dlsym(lib, "uh_lua_request"))
) {
!(conf.lua_request = dlsym(lib, "uh_lua_request")))
{
fprintf(stderr,
"Error: Failed to lookup required symbols "
"in Lua plugin: %s\n", dlerror()