2 #include "uhttpd-utils.h"
3 #include "uhttpd-cgi.h"
5 static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
12 static struct http_response res;
15 if( ((bufptr = strfind(buf, len, "\r\n\r\n", 4)) != NULL) ||
16 ((bufptr = strfind(buf, len, "\n\n", 2)) != NULL)
18 *off = (int)(bufptr - buf) + ((bufptr[0] == '\r') ? 4 : 2);
20 memset(&res, 0, sizeof(res));
27 for( pos = 0; pos < len; pos++ )
29 if( !hdrname && (buf[pos] == ':') )
33 if( (pos < len) && (buf[pos] == ' ') )
43 else if( (buf[pos] == '\r') || (buf[pos] == '\n') )
50 if( (pos < len) && (buf[pos] == '\n') )
55 if( (hdrcount + 1) < array_size(res.headers) )
57 if( ! strcasecmp(hdrname, "Status") )
59 res.statuscode = atoi(bufptr);
61 if( res.statuscode < 100 )
64 if( ((bufptr = strchr(bufptr, ' ')) != NULL) && (&bufptr[1] != 0) )
65 res.statusmsg = &bufptr[1];
69 res.headers[hdrcount++] = hdrname;
70 res.headers[hdrcount++] = bufptr;
90 static char * uh_cgi_header_lookup(struct http_response *res, const char *hdrname)
94 foreach_header(i, res->headers)
96 if( ! strcasecmp(res->headers[i], hdrname) )
97 return res->headers[i+1];
103 static void uh_cgi_error_500(struct client *cl, struct http_request *req, const char *message)
105 uh_http_sendf(cl, NULL,
106 "HTTP/%.1f 500 Internal Server Error\r\n"
107 "Content-Type: text/plain\r\n%s\r\n",
110 ? "Transfer-Encoding: chunked\r\n" : ""
113 uh_http_send(cl, req, message, -1);
117 void uh_cgi_request(struct client *cl, struct http_request *req, struct uh_path_info *pi)
119 int i, hdroff, bufoff;
123 int content_length = 0;
126 int rfd[2] = { 0, 0 };
127 int wfd[2] = { 0, 0 };
129 char buf[UH_LIMIT_MSGHEAD];
130 char hdr[UH_LIMIT_MSGHEAD];
135 struct timeval timeout;
136 struct http_response *res;
139 /* spawn pipes for me->child, child->me */
140 if( (pipe(rfd) < 0) || (pipe(wfd) < 0) )
142 uh_http_sendhf(cl, 500, "Internal Server Error",
143 "Failed to create pipe: %s", strerror(errno));
145 if( rfd[0] > 0 ) close(rfd[0]);
146 if( rfd[1] > 0 ) close(rfd[1]);
147 if( wfd[0] > 0 ) close(wfd[0]);
148 if( wfd[1] > 0 ) close(wfd[1]);
153 /* fork off child process */
158 uh_http_sendhf(cl, 500, "Internal Server Error",
159 "Failed to fork child: %s", strerror(errno));
168 /* patch stdout and stdin to pipes */
172 /* check for regular, world-executable file */
173 if( (pi->stat.st_mode & S_IFREG) &&
174 (pi->stat.st_mode & S_IXOTH)
176 /* build environment */
179 /* common information */
180 setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
181 setenv("SERVER_SOFTWARE", "uHTTPd", 1);
182 setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
187 setenv("HTTPS", "on", 1);
191 setenv("SERVER_NAME", sa_straddr(&cl->servaddr), 1);
192 setenv("SERVER_ADDR", sa_straddr(&cl->servaddr), 1);
193 setenv("SERVER_PORT", sa_strport(&cl->servaddr), 1);
194 setenv("REMOTE_HOST", sa_straddr(&cl->peeraddr), 1);
195 setenv("REMOTE_ADDR", sa_straddr(&cl->peeraddr), 1);
196 setenv("REMOTE_PORT", sa_strport(&cl->peeraddr), 1);
198 /* path information */
199 setenv("SCRIPT_NAME", pi->name, 1);
200 setenv("SCRIPT_FILENAME", pi->phys, 1);
201 setenv("DOCUMENT_ROOT", pi->root, 1);
202 setenv("QUERY_STRING", pi->query ? pi->query : "", 1);
205 setenv("PATH_INFO", pi->info, 1);
209 if( req->version > 1.0 )
210 setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
212 setenv("SERVER_PROTOCOL", "HTTP/1.0", 1);
215 switch( req->method )
217 case UH_HTTP_MSG_GET:
218 setenv("REQUEST_METHOD", "GET", 1);
221 case UH_HTTP_MSG_HEAD:
222 setenv("REQUEST_METHOD", "HEAD", 1);
225 case UH_HTTP_MSG_POST:
226 setenv("REQUEST_METHOD", "POST", 1);
231 setenv("REQUEST_URI", req->url, 1);
233 /* request message headers */
234 foreach_header(i, req->headers)
236 if( ! strcasecmp(req->headers[i], "Accept") )
237 setenv("HTTP_ACCEPT", req->headers[i+1], 1);
239 else if( ! strcasecmp(req->headers[i], "Accept-Charset") )
240 setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1);
242 else if( ! strcasecmp(req->headers[i], "Accept-Encoding") )
243 setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1);
245 else if( ! strcasecmp(req->headers[i], "Accept-Language") )
246 setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1);
248 else if( ! strcasecmp(req->headers[i], "Authorization") )
249 setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1);
251 else if( ! strcasecmp(req->headers[i], "Connection") )
252 setenv("HTTP_CONNECTION", req->headers[i+1], 1);
254 else if( ! strcasecmp(req->headers[i], "Cookie") )
255 setenv("HTTP_COOKIE", req->headers[i+1], 1);
257 else if( ! strcasecmp(req->headers[i], "Host") )
258 setenv("HTTP_HOST", req->headers[i+1], 1);
260 else if( ! strcasecmp(req->headers[i], "Referer") )
261 setenv("HTTP_REFERER", req->headers[i+1], 1);
263 else if( ! strcasecmp(req->headers[i], "User-Agent") )
264 setenv("HTTP_USER_AGENT", req->headers[i+1], 1);
266 else if( ! strcasecmp(req->headers[i], "Content-Type") )
267 setenv("CONTENT_TYPE", req->headers[i+1], 1);
269 else if( ! strcasecmp(req->headers[i], "Content-Length") )
270 setenv("CONTENT_LENGTH", req->headers[i+1], 1);
274 /* execute child code ... */
275 if( chdir(pi->root) )
278 execl(pi->phys, pi->phys, NULL);
280 /* in case it fails ... */
282 "Status: 500 Internal Server Error\r\n\r\n"
283 "Unable to launch the requested CGI program:\n"
285 pi->phys, strerror(errno)
293 "Status: 403 Forbidden\r\n\r\n"
294 "Access to this resource is forbidden\n"
304 /* parent; handle I/O relaying */
306 /* close unneeded pipe ends */
311 fd_max = max(rfd[0], wfd[1]) + 1;
313 /* find content length */
314 if( req->method == UH_HTTP_MSG_POST )
316 foreach_header(i, req->headers)
318 if( ! strcasecmp(req->headers[i], "Content-Length") )
320 content_length = atoi(req->headers[i+1]);
327 memset(hdr, 0, sizeof(hdr));
329 /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */
335 FD_SET(rfd[0], &reader);
336 FD_SET(wfd[1], &writer);
341 /* wait until we can read or write or both */
342 if( select(fd_max, &reader, (content_length > -1) ? &writer : NULL, NULL, &timeout) > 0 )
344 /* ready to write to cgi program */
345 if( FD_ISSET(wfd[1], &writer) )
347 /* there is unread post data waiting */
348 if( content_length > 0 )
350 /* read it from socket ... */
351 if( (buflen = uh_tcp_recv(cl, buf, min(content_length, sizeof(buf)))) > 0 )
353 /* ... and write it to child's stdin */
354 if( write(wfd[1], buf, buflen) < 0 )
357 content_length -= buflen;
360 /* unexpected eof! */
363 if( write(wfd[1], "", 0) < 0 )
370 /* there is no more post data, close pipe to child's stdin */
378 /* ready to read from cgi program */
379 if( FD_ISSET(rfd[0], &reader) )
381 /* read data from child ... */
382 if( (buflen = read(rfd[0], buf, sizeof(buf))) > 0 )
384 /* we have not pushed out headers yet, parse input */
387 /* head buffer not full and no end yet */
388 if( hdrlen < sizeof(hdr) )
390 bufoff = min(buflen, sizeof(hdr) - hdrlen);
391 memcpy(&hdr[hdrlen], buf, bufoff);
400 /* try to parse header ... */
401 if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL )
404 uh_http_sendf(cl, NULL, "HTTP/%.1f %03d %s\r\n",
405 req->version, res->statuscode, res->statusmsg);
407 /* add Content-Type if no Location or Content-Type */
408 if( !uh_cgi_header_lookup(res, "Location") &&
409 !uh_cgi_header_lookup(res, "Content-Type")
411 uh_http_send(cl, NULL,
412 "Content-Type: text/plain\r\n", -1);
415 /* if request was HTTP 1.1 we'll respond chunked */
416 if( (req->version > 1.0) &&
417 !uh_cgi_header_lookup(res, "Transfer-Encoding")
419 uh_http_send(cl, NULL,
420 "Transfer-Encoding: chunked\r\n", -1);
423 /* write headers from CGI program */
424 foreach_header(i, res->headers)
426 uh_http_sendf(cl, NULL, "%s: %s\r\n",
427 res->headers[i], res->headers[i+1]);
430 /* terminate header */
431 uh_http_send(cl, NULL, "\r\n", -1);
433 /* push out remaining head buffer */
434 if( hdroff < hdrlen )
435 uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff);
438 /* ... failed and head buffer exceeded */
439 else if( hdrlen >= sizeof(hdr) )
441 uh_cgi_error_500(cl, req,
442 "The CGI program generated an invalid response:\n\n");
444 uh_http_send(cl, req, hdr, hdrlen);
447 /* ... failed but free buffer space, try again */
453 /* push out remaining read buffer */
454 if( bufoff < buflen )
455 uh_http_send(cl, req, &buf[bufoff], buflen - bufoff);
462 /* headers complete, pass through buffer to socket */
463 uh_http_send(cl, req, buf, buflen);
466 /* looks like eof from child */
469 /* cgi script did not output useful stuff at all */
472 /* I would do this ...
474 * uh_cgi_error_500(cl, req,
475 * "The CGI program generated an "
476 * "invalid response:\n\n");
478 * ... but in order to stay as compatible as possible,
479 * treat whatever we got as text/plain response and
480 * build the required headers here.
483 uh_http_sendf(cl, NULL,
484 "HTTP/%.1f 200 OK\r\n"
485 "Content-Type: text/plain\r\n"
487 req->version, (req->version > 1.0)
488 ? "Transfer-Encoding: chunked\r\n" : ""
491 uh_http_send(cl, req, hdr, hdrlen);
494 /* send final chunk if we're in chunked transfer mode */
495 uh_http_send(cl, req, "", 0);
501 /* no activity for 3 seconds... looks dead */
504 uh_http_sendhf(cl, 504, "Gateway Timeout",
505 "The CGI script took too long to produce a response");