summaryrefslogtreecommitdiff
path: root/package/uhttpd/src/uhttpd-cgi.c
diff options
context:
space:
mode:
authorjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-05-03 17:19:22 +0000
committerjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2012-05-03 17:19:22 +0000
commitd0f3681e1e384dd7f11bc8ce8cd43c4fdff897d9 (patch)
tree529563447601c506de1f59ca01c4230d4e4e28f1 /package/uhttpd/src/uhttpd-cgi.c
parent37f687e09940b8aea678b166750b54cd90d34e55 (diff)
[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
Diffstat (limited to 'package/uhttpd/src/uhttpd-cgi.c')
-rw-r--r--package/uhttpd/src/uhttpd-cgi.c190
1 files changed, 95 insertions, 95 deletions
diff --git a/package/uhttpd/src/uhttpd-cgi.c b/package/uhttpd/src/uhttpd-cgi.c
index 16bfda7dba..f852125698 100644
--- a/package/uhttpd/src/uhttpd-cgi.c
+++ b/package/uhttpd/src/uhttpd-cgi.c
@@ -30,9 +30,9 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
static struct http_response res;
- if( ((bufptr = strfind(buf, len, "\r\n\r\n", 4)) != NULL) ||
- ((bufptr = strfind(buf, len, "\n\n", 2)) != NULL)
- ) {
+ if (((bufptr = strfind(buf, len, "\r\n\r\n", 4)) != NULL) ||
+ ((bufptr = strfind(buf, len, "\n\n", 2)) != NULL))
+ {
*off = (int)(bufptr - buf) + ((bufptr[0] == '\r') ? 4 : 2);
memset(&res, 0, sizeof(res));
@@ -42,45 +42,48 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
bufptr = &buf[0];
- for( pos = 0; pos < *off; pos++ )
+ for (pos = 0; pos < *off; pos++)
{
- if( !hdrname && (buf[pos] == ':') )
+ if (!hdrname && (buf[pos] == ':'))
{
buf[pos++] = 0;
- if( (pos < len) && (buf[pos] == ' ') )
+ if ((pos < len) && (buf[pos] == ' '))
pos++;
- if( pos < len )
+ if (pos < len)
{
hdrname = bufptr;
bufptr = &buf[pos];
}
}
- else if( (buf[pos] == '\r') || (buf[pos] == '\n') )
+ else if ((buf[pos] == '\r') || (buf[pos] == '\n'))
{
- if( ! hdrname )
+ if (! hdrname)
break;
buf[pos++] = 0;
- if( (pos < len) && (buf[pos] == '\n') )
+ if ((pos < len) && (buf[pos] == '\n'))
pos++;
- if( pos <= len )
+ if (pos <= len)
{
- if( (hdrcount + 1) < array_size(res.headers) )
+ if ((hdrcount + 1) < array_size(res.headers))
{
- if( ! strcasecmp(hdrname, "Status") )
+ if (!strcasecmp(hdrname, "Status"))
{
res.statuscode = atoi(bufptr);
- if( res.statuscode < 100 )
+ 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
{
@@ -105,29 +108,30 @@ 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;
foreach_header(i, res->headers)
{
- if( ! strcasecmp(res->headers[i], hdrname) )
+ if (!strcasecmp(res->headers[i], hdrname))
return res->headers[i+1];
}
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
- ) {
+ 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)
+ {
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;
@@ -165,26 +168,26 @@ void uh_cgi_request(
/* spawn pipes for me->child, child->me */
- if( (pipe(rfd) < 0) || (pipe(wfd) < 0) )
+ if ((pipe(rfd) < 0) || (pipe(wfd) < 0))
{
uh_http_sendhf(cl, 500, "Internal Server Error",
- "Failed to create pipe: %s", strerror(errno));
+ "Failed to create pipe: %s", strerror(errno));
- if( rfd[0] > 0 ) close(rfd[0]);
- if( rfd[1] > 0 ) close(rfd[1]);
- if( wfd[0] > 0 ) close(wfd[0]);
- if( wfd[1] > 0 ) close(wfd[1]);
+ if (rfd[0] > 0) close(rfd[0]);
+ if (rfd[1] > 0) close(rfd[1]);
+ if (wfd[0] > 0) close(wfd[0]);
+ if (wfd[1] > 0) close(wfd[1]);
return;
}
/* fork off child process */
- switch( (child = fork()) )
+ switch ((child = fork()))
{
/* oops */
case -1:
uh_http_sendhf(cl, 500, "Internal Server Error",
- "Failed to fork child: %s", strerror(errno));
+ "Failed to fork child: %s", strerror(errno));
return;
/* exec child */
@@ -212,9 +215,9 @@ void uh_cgi_request(
fd_cloexec(wfd[0]);
/* check for regular, world-executable file _or_ interpreter */
- if( ((pi->stat.st_mode & S_IFREG) &&
- (pi->stat.st_mode & S_IXOTH)) || (ip != NULL)
- ) {
+ if (((pi->stat.st_mode & S_IFREG) &&
+ (pi->stat.st_mode & S_IXOTH)) || (ip != NULL))
+ {
/* build environment */
clearenv();
@@ -225,7 +228,7 @@ void uh_cgi_request(
#ifdef HAVE_TLS
/* https? */
- if( cl->tls )
+ if (cl->tls)
setenv("HTTPS", "on", 1);
#endif
@@ -243,11 +246,11 @@ void uh_cgi_request(
setenv("DOCUMENT_ROOT", pi->root, 1);
setenv("QUERY_STRING", pi->query ? pi->query : "", 1);
- if( pi->info )
+ if (pi->info)
setenv("PATH_INFO", pi->info, 1);
/* REDIRECT_STATUS, php-cgi wants it */
- switch( req->redirect_status )
+ switch (req->redirect_status)
{
case 404:
setenv("REDIRECT_STATUS", "404", 1);
@@ -259,13 +262,13 @@ void uh_cgi_request(
}
/* http version */
- if( req->version > 1.0 )
+ if (req->version > 1.0)
setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
else
setenv("SERVER_PROTOCOL", "HTTP/1.0", 1);
/* request method */
- switch( req->method )
+ switch (req->method)
{
case UH_HTTP_MSG_GET:
setenv("REQUEST_METHOD", "GET", 1);
@@ -284,75 +287,71 @@ void uh_cgi_request(
setenv("REQUEST_URI", req->url, 1);
/* remote user */
- if( req->realm )
+ if (req->realm)
setenv("REMOTE_USER", req->realm->user, 1);
/* request message headers */
foreach_header(i, req->headers)
{
- if( ! strcasecmp(req->headers[i], "Accept") )
+ if (!strcasecmp(req->headers[i], "Accept"))
setenv("HTTP_ACCEPT", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Accept-Charset") )
+ else if (!strcasecmp(req->headers[i], "Accept-Charset"))
setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Accept-Encoding") )
+ else if (!strcasecmp(req->headers[i], "Accept-Encoding"))
setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Accept-Language") )
+ else if (!strcasecmp(req->headers[i], "Accept-Language"))
setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Authorization") )
+ else if (!strcasecmp(req->headers[i], "Authorization"))
setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Connection") )
+ else if (!strcasecmp(req->headers[i], "Connection"))
setenv("HTTP_CONNECTION", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Cookie") )
+ else if (!strcasecmp(req->headers[i], "Cookie"))
setenv("HTTP_COOKIE", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Host") )
+ else if (!strcasecmp(req->headers[i], "Host"))
setenv("HTTP_HOST", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Referer") )
+ else if (!strcasecmp(req->headers[i], "Referer"))
setenv("HTTP_REFERER", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "User-Agent") )
+ else if (!strcasecmp(req->headers[i], "User-Agent"))
setenv("HTTP_USER_AGENT", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Content-Type") )
+ else if (!strcasecmp(req->headers[i], "Content-Type"))
setenv("CONTENT_TYPE", req->headers[i+1], 1);
- else if( ! strcasecmp(req->headers[i], "Content-Length") )
+ else if (!strcasecmp(req->headers[i], "Content-Length"))
setenv("CONTENT_LENGTH", req->headers[i+1], 1);
}
/* execute child code ... */
- if( chdir(pi->root) )
+ if (chdir(pi->root))
perror("chdir()");
- if( ip != NULL )
+ if (ip != NULL)
execl(ip->path, ip->path, pi->phys, NULL);
else
execl(pi->phys, pi->phys, NULL);
/* in case it fails ... */
- 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)
- );
+ 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));
}
/* 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]);
@@ -371,11 +370,11 @@ void uh_cgi_request(
fd_max = max(rfd[0], wfd[1]) + 1;
/* find content length */
- if( req->method == UH_HTTP_MSG_POST )
+ if (req->method == UH_HTTP_MSG_POST)
{
foreach_header(i, req->headers)
{
- if( ! strcasecmp(req->headers[i], "Content-Length") )
+ if (!strcasecmp(req->headers[i], "Content-Length"))
{
content_length = atoi(req->headers[i+1]);
break;
@@ -387,7 +386,7 @@ void uh_cgi_request(
memset(hdr, 0, sizeof(hdr));
/* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */
- while( 1 )
+ while (1)
{
FD_ZERO(&reader);
FD_ZERO(&writer);
@@ -399,31 +398,33 @@ 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 )
+ if (rv == 0)
{
ensure_out(kill(child, 0));
}
/* wait until we can read or write or both */
- else if( rv > 0 )
+ else if (rv > 0)
{
/* ready to write to cgi program */
- if( FD_ISSET(wfd[1], &writer) )
+ if (FD_ISSET(wfd[1], &writer))
{
/* there is unread post data waiting */
- if( content_length > 0 )
+ if (content_length > 0)
{
/* read it from socket ... */
ensure_out(buflen = uh_tcp_recv(cl, buf,
min(content_length, sizeof(buf))));
- if( buflen > 0 )
+ if (buflen > 0)
{
/* ... and write it to child's stdin */
- if( write(wfd[1], buf, buflen) < 0 )
+ if (write(wfd[1], buf, buflen) < 0)
perror("write()");
content_length -= buflen;
@@ -432,7 +433,7 @@ void uh_cgi_request(
/* unexpected eof! */
else
{
- if( write(wfd[1], "", 0) < 0 )
+ if (write(wfd[1], "", 0) < 0)
perror("write()");
content_length = 0;
@@ -440,7 +441,7 @@ void uh_cgi_request(
}
/* there is no more post data, close pipe to child's stdin */
- else if( content_length > -1 )
+ else if (content_length > -1)
{
close(wfd[1]);
content_length = -1;
@@ -448,16 +449,16 @@ void uh_cgi_request(
}
/* ready to read from cgi program */
- if( FD_ISSET(rfd[0], &reader) )
+ if (FD_ISSET(rfd[0], &reader))
{
/* read data from child ... */
- if( (buflen = read(rfd[0], buf, sizeof(buf))) > 0 )
+ if ((buflen = read(rfd[0], buf, sizeof(buf))) > 0)
{
/* we have not pushed out headers yet, parse input */
- if( ! header_sent )
+ if (!header_sent)
{
/* head buffer not full and no end yet */
- if( hdrlen < sizeof(hdr) )
+ if (hdrlen < sizeof(hdr))
{
bufoff = min(buflen, sizeof(hdr) - hdrlen);
memcpy(&hdr[hdrlen], buf, bufoff);
@@ -470,7 +471,7 @@ void uh_cgi_request(
/* try to parse header ... */
- if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL )
+ if ((res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL)
{
/* write status */
ensure_out(uh_http_sendf(cl, NULL,
@@ -506,12 +507,12 @@ void uh_cgi_request(
ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
/* push out remaining head buffer */
- if( hdroff < hdrlen )
+ if (hdroff < hdrlen)
ensure_out(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff));
}
/* ... failed and head buffer exceeded */
- else if( hdrlen >= sizeof(hdr) )
+ else if (hdrlen >= sizeof(hdr))
{
ensure_out(uh_cgi_error_500(cl, req,
"The CGI program generated an invalid response:\n\n"));
@@ -526,7 +527,7 @@ void uh_cgi_request(
}
/* push out remaining read buffer */
- if( bufoff < buflen )
+ if (bufoff < buflen)
ensure_out(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff));
header_sent = 1;
@@ -542,7 +543,7 @@ void uh_cgi_request(
else
{
/* cgi script did not output useful stuff at all */
- if( ! header_sent )
+ if (!header_sent)
{
/* I would do this ...
*
@@ -576,7 +577,7 @@ void uh_cgi_request(
/* timeout exceeded or interrupted by SIGCHLD */
else
{
- if( (errno != EINTR) && ! header_sent )
+ if ((errno != EINTR) && ! header_sent)
{
ensure_out(uh_http_sendhf(cl, 504, "Gateway Timeout",
"The CGI script took too long to produce "
@@ -594,7 +595,7 @@ void uh_cgi_request(
close(rfd[0]);
close(wfd[1]);
- if( !kill(child, 0) )
+ if (!kill(child, 0))
{
kill(child, SIGTERM);
waitpid(child, NULL, 0);
@@ -603,4 +604,3 @@ void uh_cgi_request(
break;
}
}
-