X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=package%2Fuhttpd%2Fsrc%2Fuhttpd-cgi.c;h=e527922624e1e6c6996c7f61db9fc7af306bfdb8;hb=84032b177b6535804ac879c6b52a1775cdd8edc3;hp=08612491624d0c8c1303152b6e7b4732153caa3c;hpb=e417063c23716f0dd35f98f1990bb8dad7b963f4;p=openwrt.git diff --git a/package/uhttpd/src/uhttpd-cgi.c b/package/uhttpd/src/uhttpd-cgi.c index 0861249162..e527922624 100644 --- a/package/uhttpd/src/uhttpd-cgi.c +++ b/package/uhttpd/src/uhttpd-cgi.c @@ -1,7 +1,7 @@ /* * uhttpd - Tiny single-threaded httpd - CGI handler * - * Copyright (C) 2010 Jo-Philipp Wich + * Copyright (C) 2010-2012 Jo-Philipp Wich * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,72 +20,79 @@ #include "uhttpd-utils.h" #include "uhttpd-cgi.h" -static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off) + +static bool +uh_cgi_header_parse(struct http_response *res, char *buf, int len, int *off) { char *bufptr = NULL; char *hdrname = NULL; int hdrcount = 0; int pos = 0; - 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)); + memset(res, 0, sizeof(*res)); - res.statuscode = 200; - res.statusmsg = "OK"; + res->statuscode = 200; + res->statusmsg = "OK"; bufptr = &buf[0]; - for( pos = 0; pos < len; 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')) { - buf[pos++] = 0; - - if( ! hdrname ) + if (! hdrname) break; - if( (pos < len) && (buf[pos] == '\n') ) + buf[pos++] = 0; + + 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); + res->statuscode = atoi(bufptr); - if( res.statuscode < 100 ) - res.statuscode = 200; + if (res->statuscode < 100) + res->statuscode = 200; - if( ((bufptr = strchr(bufptr, ' ')) != NULL) && (&bufptr[1] != 0) ) - res.statusmsg = &bufptr[1]; + if (((bufptr = strchr(bufptr, ' ')) != NULL) && + (&bufptr[1] != 0)) + { + res->statusmsg = &bufptr[1]; + } + + D("CGI: HTTP/1.x %03d %s\n", + res->statuscode, res->statusmsg); } else { - res.headers[hdrcount++] = hdrname; - res.headers[hdrcount++] = bufptr; + D("CGI: HTTP: %s: %s\n", hdrname, bufptr); + + res->headers[hdrcount++] = hdrname; + res->headers[hdrcount++] = bufptr; } bufptr = &buf[pos]; @@ -93,492 +100,462 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off) } else { - return NULL; + return false; } } } } - return &res; + return true; } - return NULL; + return false; } -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 void uh_cgi_shutdown(struct uh_cgi_state *state) { - 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); - } - - return -1; + free(state); } - -void uh_cgi_request(struct client *cl, struct http_request *req, struct path_info *pi) +static bool uh_cgi_socket_cb(struct client *cl) { - int i, hdroff, bufoff; - int hdrlen = 0; - int buflen = 0; - int fd_max = 0; - int content_length = 0; - int header_sent = 0; + int i, len, hdroff; + char buf[UH_LIMIT_MSGHEAD]; - int rfd[2] = { 0, 0 }; - int wfd[2] = { 0, 0 }; + struct uh_cgi_state *state = (struct uh_cgi_state *)cl->priv; + struct http_response *res = &cl->response; + struct http_request *req = &cl->request; - char buf[UH_LIMIT_MSGHEAD]; - char hdr[UH_LIMIT_MSGHEAD]; + /* there is unread post data waiting */ + while (state->content_length > 0) + { + /* remaining data in http head buffer ... */ + if (cl->httpbuf.len > 0) + { + len = min(state->content_length, cl->httpbuf.len); - pid_t child; + D("CGI: Child(%d) feed %d HTTP buffer bytes\n", cl->proc.pid, len); - fd_set reader; - fd_set writer; + memcpy(buf, cl->httpbuf.ptr, len); - struct sigaction sa; - struct timeval timeout; - struct http_response *res; + cl->httpbuf.len -= len; + cl->httpbuf.ptr +=len; + } + /* read it from socket ... */ + else + { + len = uh_tcp_recv(cl, buf, + min(state->content_length, sizeof(buf))); - /* spawn pipes for me->child, child->me */ - if( (pipe(rfd) < 0) || (pipe(wfd) < 0) ) - { - uh_http_sendhf(cl, 500, "Internal Server Error", - "Failed to create pipe: %s", strerror(errno)); + if ((len < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) + break; - 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]); + D("CGI: Child(%d) feed %d/%d TCP socket bytes\n", + cl->proc.pid, len, min(state->content_length, sizeof(buf))); + } - return; + if (len) + state->content_length -= len; + else + state->content_length = 0; + + /* ... write to CGI process */ + len = uh_raw_send(cl->wpipe.fd, buf, len, + cl->server->conf->script_timeout); + + /* explicit EOF notification for the child */ + if (state->content_length <= 0) + uh_ufd_remove(&cl->wpipe); } - /* fork off child process */ - switch( (child = fork()) ) + /* try to read data from child */ + while ((len = uh_raw_recv(cl->rpipe.fd, buf, sizeof(buf), -1)) > 0) { - /* oops */ - case -1: - uh_http_sendhf(cl, 500, "Internal Server Error", - "Failed to fork child: %s", strerror(errno)); - return; - - /* exec child */ - case 0: - /* restore SIGTERM */ - sa.sa_flags = 0; - sa.sa_handler = SIG_DFL; - sigemptyset(&sa.sa_mask); - sigaction(SIGTERM, &sa, NULL); - - /* close loose pipe ends */ - close(rfd[0]); - close(wfd[1]); - - /* patch stdout and stdin to pipes */ - dup2(rfd[1], 1); - dup2(wfd[0], 0); - - /* check for regular, world-executable file */ - if( (pi->stat.st_mode & S_IFREG) && - (pi->stat.st_mode & S_IXOTH) - ) { - /* build environment */ - clearenv(); - - /* common information */ - setenv("GATEWAY_INTERFACE", "CGI/1.1", 1); - setenv("SERVER_SOFTWARE", "uHTTPd", 1); - setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); + /* we have not pushed out headers yet, parse input */ + if (!state->header_sent) + { + /* try to parse header ... */ + memcpy(state->httpbuf, buf, len); -#ifdef HAVE_TLS - /* https? */ - if( cl->tls ) - setenv("HTTPS", "on", 1); -#endif + if (uh_cgi_header_parse(res, state->httpbuf, len, &hdroff)) + { + /* write status */ + ensure_out(uh_http_sendf(cl, NULL, + "HTTP/%.1f %03d %s\r\n" + "Connection: close\r\n", + req->version, res->statuscode, res->statusmsg)); + + /* add Content-Type if no Location or Content-Type */ + if (!uh_cgi_header_lookup(res, "Location") && + !uh_cgi_header_lookup(res, "Content-Type")) + { + ensure_out(uh_http_send(cl, NULL, + "Content-Type: text/plain\r\n", -1)); + } - /* addresses */ - setenv("SERVER_NAME", sa_straddr(&cl->servaddr), 1); - setenv("SERVER_ADDR", sa_straddr(&cl->servaddr), 1); - setenv("SERVER_PORT", sa_strport(&cl->servaddr), 1); - setenv("REMOTE_HOST", sa_straddr(&cl->peeraddr), 1); - setenv("REMOTE_ADDR", sa_straddr(&cl->peeraddr), 1); - setenv("REMOTE_PORT", sa_strport(&cl->peeraddr), 1); - - /* path information */ - setenv("SCRIPT_NAME", pi->name, 1); - setenv("SCRIPT_FILENAME", pi->phys, 1); - setenv("DOCUMENT_ROOT", pi->root, 1); - setenv("QUERY_STRING", pi->query ? pi->query : "", 1); - - if( pi->info ) - setenv("PATH_INFO", pi->info, 1); - - /* REDIRECT_STATUS, php-cgi wants it */ - switch( req->redirect_status ) + /* if request was HTTP 1.1 we'll respond chunked */ + if ((req->version > 1.0) && + !uh_cgi_header_lookup(res, "Transfer-Encoding")) { - case 404: - setenv("REDIRECT_STATUS", "404", 1); - break; + ensure_out(uh_http_send(cl, NULL, + "Transfer-Encoding: chunked\r\n", -1)); + } - default: - setenv("REDIRECT_STATUS", "200", 1); - break; + /* write headers from CGI program */ + foreach_header(i, res->headers) + { + ensure_out(uh_http_sendf(cl, NULL, "%s: %s\r\n", + res->headers[i], res->headers[i+1])); } - /* http version */ - if( req->version > 1.0 ) - setenv("SERVER_PROTOCOL", "HTTP/1.1", 1); - else - setenv("SERVER_PROTOCOL", "HTTP/1.0", 1); + /* terminate header */ + ensure_out(uh_http_send(cl, NULL, "\r\n", -1)); - /* request method */ - switch( req->method ) - { - case UH_HTTP_MSG_GET: - setenv("REQUEST_METHOD", "GET", 1); - break; + state->header_sent = true; - case UH_HTTP_MSG_HEAD: - setenv("REQUEST_METHOD", "HEAD", 1); - break; + /* push out remaining head buffer */ + if (hdroff < len) + { + D("CGI: Child(%d) relaying %d rest bytes\n", + cl->proc.pid, len - hdroff); - case UH_HTTP_MSG_POST: - setenv("REQUEST_METHOD", "POST", 1); - break; + ensure_out(uh_http_send(cl, req, + &buf[hdroff], len - hdroff)); } + } - /* request url */ - setenv("REQUEST_URI", req->url, 1); + /* ... failed and head buffer exceeded */ + else + { + /* I would do this ... + * + * uh_cgi_error_500(cl, req, + * "The CGI program generated an " + * "invalid response:\n\n"); + * + * ... but in order to stay as compatible as possible, + * treat whatever we got as text/plain response and + * build the required headers here. + */ + + ensure_out(uh_http_sendf(cl, NULL, + "HTTP/%.1f 200 OK\r\n" + "Content-Type: text/plain\r\n" + "%s\r\n", + req->version, (req->version > 1.0) + ? "Transfer-Encoding: chunked\r\n" : "" + )); + + state->header_sent = true; + + D("CGI: Child(%d) relaying %d invalid bytes\n", + cl->proc.pid, len); + + ensure_out(uh_http_send(cl, req, buf, len)); + } + } + else + { + /* headers complete, pass through buffer to socket */ + D("CGI: Child(%d) relaying %d normal bytes\n", cl->proc.pid, len); + ensure_out(uh_http_send(cl, req, buf, len)); + } + } - /* remote user */ - if( req->realm ) - setenv("REMOTE_USER", req->realm->user, 1); + /* got EOF or read error from child */ + if ((len == 0) || + ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (len == -1))) + { + D("CGI: Child(%d) presumed dead [%s]\n", cl->proc.pid, strerror(errno)); - /* request message headers */ - foreach_header(i, req->headers) - { - if( ! strcasecmp(req->headers[i], "Accept") ) - setenv("HTTP_ACCEPT", req->headers[i+1], 1); + goto out; + } - else if( ! strcasecmp(req->headers[i], "Accept-Charset") ) - setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1); + return true; - else if( ! strcasecmp(req->headers[i], "Accept-Encoding") ) - setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1); +out: + if (!state->header_sent) + { + if (cl->timeout.pending) + uh_http_sendhf(cl, 502, "Bad Gateway", + "The CGI process did not produce any response\n"); + else + uh_http_sendhf(cl, 504, "Gateway Timeout", + "The CGI process took too long to produce a " + "response\n"); + } + else + { + uh_http_send(cl, req, "", 0); + } - else if( ! strcasecmp(req->headers[i], "Accept-Language") ) - setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1); + uh_cgi_shutdown(state); + return false; +} - else if( ! strcasecmp(req->headers[i], "Authorization") ) - setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1); +bool uh_cgi_request(struct client *cl, struct path_info *pi, + struct interpreter *ip) +{ + int i; - else if( ! strcasecmp(req->headers[i], "Connection") ) - setenv("HTTP_CONNECTION", req->headers[i+1], 1); + int rfd[2] = { 0, 0 }; + int wfd[2] = { 0, 0 }; - else if( ! strcasecmp(req->headers[i], "Cookie") ) - setenv("HTTP_COOKIE", req->headers[i+1], 1); + pid_t child; - else if( ! strcasecmp(req->headers[i], "Host") ) - setenv("HTTP_HOST", req->headers[i+1], 1); + struct uh_cgi_state *state; + struct http_request *req = &cl->request; - else if( ! strcasecmp(req->headers[i], "Referer") ) - setenv("HTTP_REFERER", req->headers[i+1], 1); + /* allocate state */ + if (!(state = malloc(sizeof(*state)))) + { + uh_http_sendhf(cl, 500, "Internal Server Error", "Out of memory"); + return false; + } - else if( ! strcasecmp(req->headers[i], "User-Agent") ) - setenv("HTTP_USER_AGENT", req->headers[i+1], 1); + /* spawn pipes for me->child, child->me */ + if ((pipe(rfd) < 0) || (pipe(wfd) < 0)) + { + 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]); - else if( ! strcasecmp(req->headers[i], "Content-Type") ) - setenv("CONTENT_TYPE", req->headers[i+1], 1); + uh_http_sendhf(cl, 500, "Internal Server Error", + "Failed to create pipe: %s\n", strerror(errno)); - else if( ! strcasecmp(req->headers[i], "Content-Length") ) - setenv("CONTENT_LENGTH", req->headers[i+1], 1); - } + return false; + } + /* fork off child process */ + switch ((child = fork())) + { + /* oops */ + case -1: + uh_http_sendhf(cl, 500, "Internal Server Error", + "Failed to fork child: %s\n", strerror(errno)); - /* execute child code ... */ - if( chdir(pi->root) ) - perror("chdir()"); + return false; - execl(pi->phys, pi->phys, NULL); + /* exec child */ + case 0: +#ifdef DEBUG + sleep(atoi(getenv("UHTTPD_SLEEP_ON_FORK") ?: "0")); +#endif - /* 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", - pi->phys, strerror(errno) - ); + /* do not leak parent epoll descriptor */ + uloop_done(); + + /* close loose pipe ends */ + close(rfd[0]); + close(wfd[1]); + + /* patch stdout and stdin to pipes */ + dup2(rfd[1], 1); + dup2(wfd[0], 0); + + /* avoid leaking our pipe into child-child processes */ + fd_cloexec(rfd[1]); + 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)) + { + /* build environment */ + clearenv(); + + /* common information */ + setenv("GATEWAY_INTERFACE", "CGI/1.1", 1); + setenv("SERVER_SOFTWARE", "uHTTPd", 1); + setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); + +#ifdef HAVE_TLS + /* https? */ + if (cl->tls) + setenv("HTTPS", "on", 1); +#endif + + /* addresses */ + setenv("SERVER_NAME", sa_straddr(&cl->servaddr), 1); + setenv("SERVER_ADDR", sa_straddr(&cl->servaddr), 1); + setenv("SERVER_PORT", sa_strport(&cl->servaddr), 1); + setenv("REMOTE_HOST", sa_straddr(&cl->peeraddr), 1); + setenv("REMOTE_ADDR", sa_straddr(&cl->peeraddr), 1); + setenv("REMOTE_PORT", sa_strport(&cl->peeraddr), 1); + + /* path information */ + setenv("SCRIPT_NAME", pi->name, 1); + setenv("SCRIPT_FILENAME", pi->phys, 1); + setenv("DOCUMENT_ROOT", pi->root, 1); + setenv("QUERY_STRING", pi->query ? pi->query : "", 1); + + if (pi->info) + setenv("PATH_INFO", pi->info, 1); + + /* REDIRECT_STATUS, php-cgi wants it */ + switch (req->redirect_status) + { + case 404: + setenv("REDIRECT_STATUS", "404", 1); + break; + + default: + setenv("REDIRECT_STATUS", "200", 1); + break; } - /* 403 */ + /* http version */ + 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) { - printf( - "Status: 403 Forbidden\r\n\r\n" - "Access to this resource is forbidden\n" - ); - } + case UH_HTTP_MSG_GET: + setenv("REQUEST_METHOD", "GET", 1); + break; - close(wfd[0]); - close(rfd[1]); - exit(0); + case UH_HTTP_MSG_HEAD: + setenv("REQUEST_METHOD", "HEAD", 1); + break; - break; + case UH_HTTP_MSG_POST: + setenv("REQUEST_METHOD", "POST", 1); + break; + } - /* parent; handle I/O relaying */ - default: - /* close unneeded pipe ends */ - close(rfd[1]); - close(wfd[0]); + /* request url */ + setenv("REQUEST_URI", req->url, 1); - /* max watch fd */ - fd_max = max(rfd[0], wfd[1]) + 1; + /* remote user */ + if (req->realm) + setenv("REMOTE_USER", req->realm->user, 1); - /* find content length */ - if( req->method == UH_HTTP_MSG_POST ) + /* request message headers */ + foreach_header(i, req->headers) { - foreach_header(i, req->headers) - { - if( ! strcasecmp(req->headers[i], "Content-Length") ) - { - content_length = atoi(req->headers[i+1]); - break; - } - } - } + if (!strcasecmp(req->headers[i], "Accept")) + setenv("HTTP_ACCEPT", req->headers[i+1], 1); + else if (!strcasecmp(req->headers[i], "Accept-Charset")) + setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1); - memset(hdr, 0, sizeof(hdr)); + else if (!strcasecmp(req->headers[i], "Accept-Encoding")) + setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1); - timeout.tv_sec = cl->server->conf->script_timeout; - timeout.tv_usec = 0; + else if (!strcasecmp(req->headers[i], "Accept-Language")) + setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1); -#define ensure(x) \ - do { if( x < 0 ) goto out; } while(0) + else if (!strcasecmp(req->headers[i], "Authorization")) + setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1); - /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */ - while( 1 ) - { - FD_ZERO(&reader); - FD_ZERO(&writer); - - FD_SET(rfd[0], &reader); - FD_SET(wfd[1], &writer); - - /* wait until we can read or write or both */ - if( select_intr(fd_max, &reader, - (content_length > -1) ? &writer : NULL, NULL, - (header_sent < 1) ? &timeout : NULL) > 0 - ) { - /* ready to write to cgi program */ - if( FD_ISSET(wfd[1], &writer) ) - { - /* there is unread post data waiting */ - if( content_length > 0 ) - { - /* read it from socket ... */ - if( (buflen = uh_tcp_recv(cl, buf, min(content_length, sizeof(buf)))) > 0 ) - { - /* ... and write it to child's stdin */ - if( write(wfd[1], buf, buflen) < 0 ) - perror("write()"); + else if (!strcasecmp(req->headers[i], "Connection")) + setenv("HTTP_CONNECTION", req->headers[i+1], 1); - content_length -= buflen; - } + else if (!strcasecmp(req->headers[i], "Cookie")) + setenv("HTTP_COOKIE", req->headers[i+1], 1); - /* unexpected eof! */ - else - { - if( write(wfd[1], "", 0) < 0 ) - perror("write()"); + else if (!strcasecmp(req->headers[i], "Host")) + setenv("HTTP_HOST", req->headers[i+1], 1); - content_length = 0; - } - } + else if (!strcasecmp(req->headers[i], "Referer")) + setenv("HTTP_REFERER", req->headers[i+1], 1); - /* there is no more post data, close pipe to child's stdin */ - else if( content_length > -1 ) - { - close(wfd[1]); - content_length = -1; - } - } + else if (!strcasecmp(req->headers[i], "User-Agent")) + setenv("HTTP_USER_AGENT", req->headers[i+1], 1); - /* ready to read from cgi program */ - if( FD_ISSET(rfd[0], &reader) ) - { - /* read data from child ... */ - if( (buflen = read(rfd[0], buf, sizeof(buf))) > 0 ) - { - /* we have not pushed out headers yet, parse input */ - if( ! header_sent ) - { - /* head buffer not full and no end yet */ - if( hdrlen < sizeof(hdr) ) - { - bufoff = min(buflen, sizeof(hdr) - hdrlen); - memcpy(&hdr[hdrlen], buf, bufoff); - hdrlen += bufoff; - } - else - { - bufoff = 0; - } - - - /* try to parse header ... */ - if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL ) - { - /* write status */ - ensure(uh_http_sendf(cl, NULL, - "HTTP/%.1f %03d %s\r\n" - "Connection: close\r\n", - req->version, res->statuscode, - res->statusmsg)); - - /* add Content-Type if no Location or Content-Type */ - if( !uh_cgi_header_lookup(res, "Location") && - !uh_cgi_header_lookup(res, "Content-Type") - ) { - ensure(uh_http_send(cl, NULL, - "Content-Type: text/plain\r\n", -1)); - } - - /* if request was HTTP 1.1 we'll respond chunked */ - if( (req->version > 1.0) && - !uh_cgi_header_lookup(res, "Transfer-Encoding") - ) { - ensure(uh_http_send(cl, NULL, - "Transfer-Encoding: chunked\r\n", -1)); - } - - /* write headers from CGI program */ - foreach_header(i, res->headers) - { - ensure(uh_http_sendf(cl, NULL, "%s: %s\r\n", - res->headers[i], res->headers[i+1])); - } - - /* terminate header */ - ensure(uh_http_send(cl, NULL, "\r\n", -1)); - - /* push out remaining head buffer */ - if( hdroff < hdrlen ) - ensure(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff)); - } - - /* ... failed and head buffer exceeded */ - else if( hdrlen >= sizeof(hdr) ) - { - ensure(uh_cgi_error_500(cl, req, - "The CGI program generated an invalid response:\n\n")); - - ensure(uh_http_send(cl, req, hdr, hdrlen)); - } - - /* ... failed but free buffer space, try again */ - else - { - continue; - } - - /* push out remaining read buffer */ - if( bufoff < buflen ) - ensure(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff)); - - header_sent = 1; - continue; - } + else if (!strcasecmp(req->headers[i], "Content-Type")) + setenv("CONTENT_TYPE", req->headers[i+1], 1); + else if (!strcasecmp(req->headers[i], "Content-Length")) + setenv("CONTENT_LENGTH", req->headers[i+1], 1); + } - /* headers complete, pass through buffer to socket */ - ensure(uh_http_send(cl, req, buf, buflen)); - } - /* looks like eof from child */ - else - { - /* cgi script did not output useful stuff at all */ - if( ! header_sent ) - { - /* I would do this ... - * - * uh_cgi_error_500(cl, req, - * "The CGI program generated an " - * "invalid response:\n\n"); - * - * ... but in order to stay as compatible as possible, - * treat whatever we got as text/plain response and - * build the required headers here. - */ - - ensure(uh_http_sendf(cl, NULL, - "HTTP/%.1f 200 OK\r\n" - "Content-Type: text/plain\r\n" - "%s\r\n", - req->version, (req->version > 1.0) - ? "Transfer-Encoding: chunked\r\n" : "" - )); - - ensure(uh_http_send(cl, req, hdr, hdrlen)); - } + /* execute child code ... */ + if (chdir(pi->root)) + perror("chdir()"); - /* send final chunk if we're in chunked transfer mode */ - ensure(uh_http_send(cl, req, "", 0)); - break; - } - } - } + if (ip != NULL) + execl(ip->path, ip->path, pi->phys, NULL); + else + execl(pi->phys, pi->phys, NULL); - /* timeout exceeded or interrupted by SIGCHLD */ - else - { - if( (errno != EINTR) && ! header_sent ) - { - ensure(uh_http_sendhf(cl, 504, "Gateway Timeout", - "The CGI script took too long to produce " - "a response")); - } + /* 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)); + } - /* send final chunk if we're in chunked transfer mode */ - ensure(uh_http_send(cl, req, "", 0)); + /* 403 */ + else + { + printf("Status: 403 Forbidden\r\n\r\n" + "Access to this resource is forbidden\n"); + } - break; - } - } + close(wfd[0]); + close(rfd[1]); + exit(0); + + break; + + /* parent; handle I/O relaying */ + default: + memset(state, 0, sizeof(*state)); + + cl->rpipe.fd = rfd[0]; + cl->wpipe.fd = wfd[1]; + cl->proc.pid = child; + + /* make pipe non-blocking */ + fd_nonblock(cl->rpipe.fd); + fd_nonblock(cl->wpipe.fd); + + /* close unneeded pipe ends */ + close(rfd[1]); + close(wfd[0]); - out: - close(rfd[0]); - close(wfd[1]); + D("CGI: Child(%d) created: rfd(%d) wfd(%d)\n", child, rfd[0], wfd[1]); - if( !kill(child, 0) ) + state->content_length = cl->httpbuf.len; + + /* find content length */ + if (req->method == UH_HTTP_MSG_POST) + { + foreach_header(i, req->headers) { - kill(child, SIGTERM); - waitpid(child, NULL, 0); + if (!strcasecmp(req->headers[i], "Content-Length")) + { + state->content_length = atoi(req->headers[i+1]); + break; + } } + } + + cl->cb = uh_cgi_socket_cb; + cl->priv = state; - break; + break; } -} + return true; +}