2 #include "uhttpd-cgi.h"
3 #include "uhttpd-utils.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[1] == '\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)
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;
137 struct uh_path_info *pi;
140 /* spawn pipes for me->child, child->me */
141 if( (pipe(rfd) < 0) || (pipe(wfd) < 0) )
143 uh_http_sendhf(cl, 500, "Internal Server Error",
144 "Failed to create pipe: %s", strerror(errno));
146 if( rfd[0] > 0 ) close(rfd[0]);
147 if( rfd[1] > 0 ) close(rfd[1]);
148 if( wfd[0] > 0 ) close(wfd[0]);
149 if( wfd[1] > 0 ) close(wfd[1]);
154 /* fork off child process */
159 uh_http_sendhf(cl, 500, "Internal Server Error",
160 "Failed to fork child: %s", strerror(errno));
169 /* patch stdout and stdin to pipes */
173 if( (pi = uh_path_lookup(cl, req->url)) != NULL )
175 /* check for regular, world-executable file */
176 if( (pi->stat.st_mode & S_IFREG) &&
177 (pi->stat.st_mode & S_IXOTH)
179 /* build environment */
182 /* common information */
183 setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
184 setenv("SERVER_SOFTWARE", "uHTTPd", 1);
185 setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
190 setenv("HTTPS", "on", 1);
194 setenv("SERVER_NAME", sa_straddr(&cl->servaddr), 1);
195 setenv("SERVER_ADDR", sa_straddr(&cl->servaddr), 1);
196 setenv("SERVER_PORT", sa_strport(&cl->servaddr), 1);
197 setenv("REMOTE_HOST", sa_straddr(&cl->peeraddr), 1);
198 setenv("REMOTE_ADDR", sa_straddr(&cl->peeraddr), 1);
199 setenv("REMOTE_PORT", sa_strport(&cl->peeraddr), 1);
201 /* path information */
202 setenv("SCRIPT_NAME", pi->name, 1);
203 setenv("SCRIPT_FILENAME", pi->phys, 1);
204 setenv("SCRIPT_WORKDIR", pi->wdir, 1); /* nonstandard */
205 setenv("DOCUMENT_ROOT", pi->root, 1);
206 setenv("QUERY_STRING", pi->query ? pi->query : "", 1);
209 setenv("PATH_INFO", pi->info, 1);
213 if( req->version > 1.0 )
214 setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
216 setenv("SERVER_PROTOCOL", "HTTP/1.0", 1);
219 switch( req->method )
221 case UH_HTTP_MSG_GET:
222 setenv("REQUEST_METHOD", "GET", 1);
225 case UH_HTTP_MSG_HEAD:
226 setenv("REQUEST_METHOD", "HEAD", 1);
229 case UH_HTTP_MSG_POST:
230 setenv("REQUEST_METHOD", "POST", 1);
235 setenv("REQUEST_URI", req->url, 1);
237 /* request message headers */
238 foreach_header(i, req->headers)
240 if( ! strcasecmp(req->headers[i], "Accept") )
241 setenv("HTTP_ACCEPT", req->headers[i+1], 1);
243 else if( ! strcasecmp(req->headers[i], "Accept-Charset") )
244 setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1);
246 else if( ! strcasecmp(req->headers[i], "Accept-Encoding") )
247 setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1);
249 else if( ! strcasecmp(req->headers[i], "Accept-Language") )
250 setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1);
252 else if( ! strcasecmp(req->headers[i], "Authorization") )
253 setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1);
255 else if( ! strcasecmp(req->headers[i], "Connection") )
256 setenv("HTTP_CONNECTION", req->headers[i+1], 1);
258 else if( ! strcasecmp(req->headers[i], "Cookie") )
259 setenv("HTTP_COOKIE", req->headers[i+1], 1);
261 else if( ! strcasecmp(req->headers[i], "Host") )
262 setenv("HTTP_HOST", req->headers[i+1], 1);
264 else if( ! strcasecmp(req->headers[i], "Referer") )
265 setenv("HTTP_REFERER", req->headers[i+1], 1);
267 else if( ! strcasecmp(req->headers[i], "User-Agent") )
268 setenv("HTTP_USER_AGENT", req->headers[i+1], 1);
270 else if( ! strcasecmp(req->headers[i], "Content-Type") )
271 setenv("CONTENT_TYPE", req->headers[i+1], 1);
273 else if( ! strcasecmp(req->headers[i], "Content-Length") )
274 setenv("CONTENT_LENGTH", req->headers[i+1], 1);
278 /* execute child code ... */
279 if( chdir(pi->wdir) )
282 execl(pi->phys, pi->phys, NULL);
284 /* in case it fails ... */
286 "Status: 500 Internal Server Error\r\n\r\n"
287 "Unable to launch the requested CGI program:\n"
289 pi->phys, strerror(errno)
297 "Status: 403 Forbidden\r\n\r\n"
298 "Access to this resource is forbidden\n"
307 "Status: 404 Not Found\r\n\r\n"
308 "Unable to launch the requested CGI program:\n"
309 " No such file or directory\n"
319 /* parent; handle I/O relaying */
321 /* close unneeded pipe ends */
326 fd_max = max(rfd[0], wfd[1]) + 1;
328 /* find content length */
329 if( req->method == UH_HTTP_MSG_POST )
331 foreach_header(i, req->headers)
333 if( ! strcasecmp(req->headers[i], "Content-Length") )
335 content_length = atoi(req->headers[i+1]);
342 memset(hdr, 0, sizeof(hdr));
344 /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */
350 FD_SET(rfd[0], &reader);
351 FD_SET(wfd[1], &writer);
356 /* wait until we can read or write or both */
357 if( select(fd_max, &reader, (content_length > -1) ? &writer : NULL, NULL, &timeout) > 0 )
359 /* ready to write to cgi program */
360 if( FD_ISSET(wfd[1], &writer) )
362 /* there is unread post data waiting */
363 if( content_length > 0 )
365 /* read it from socket ... */
366 if( (buflen = uh_tcp_recv(cl, buf, min(content_length, sizeof(buf)))) > 0 )
368 /* ... and write it to child's stdin */
369 if( write(wfd[1], buf, buflen) < 0 )
372 content_length -= buflen;
375 /* unexpected eof! */
378 if( write(wfd[1], "", 0) < 0 )
385 /* there is no more post data, close pipe to child's stdin */
393 /* ready to read from cgi program */
394 if( FD_ISSET(rfd[0], &reader) )
396 /* read data from child ... */
397 if( (buflen = read(rfd[0], buf, sizeof(buf))) > 0 )
399 /* we have not pushed out headers yet, parse input */
402 /* head buffer not full and no end yet */
403 if( hdrlen < sizeof(hdr) )
405 bufoff = min(buflen, sizeof(hdr) - hdrlen);
406 memcpy(&hdr[hdrlen], buf, bufoff);
415 /* try to parse header ... */
416 if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL )
419 uh_http_sendf(cl, NULL, "HTTP/%.1f %03d %s\r\n",
420 req->version, res->statuscode, res->statusmsg);
422 /* add Content-Type if no Location or Content-Type */
423 if( !uh_cgi_header_lookup(res, "Location") &&
424 !uh_cgi_header_lookup(res, "Content-Type")
426 uh_http_send(cl, NULL,
427 "Content-Type: text/plain\r\n", -1);
430 /* if request was HTTP 1.1 we'll respond chunked */
431 if( (req->version > 1.0) &&
432 !uh_cgi_header_lookup(res, "Transfer-Encoding")
434 uh_http_send(cl, NULL,
435 "Transfer-Encoding: chunked\r\n", -1);
438 /* write headers from CGI program */
439 foreach_header(i, res->headers)
441 uh_http_sendf(cl, NULL, "%s: %s\r\n",
442 res->headers[i], res->headers[i+1]);
445 /* terminate header */
446 uh_http_send(cl, NULL, "\r\n", -1);
448 /* push out remaining head buffer */
449 if( hdroff < hdrlen )
450 uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff);
453 /* ... failed and head buffer exceeded */
454 else if( hdrlen >= sizeof(hdr) )
456 uh_cgi_error_500(cl, req,
457 "The CGI program generated an invalid response:\n\n");
459 uh_http_send(cl, req, hdr, hdrlen);
462 /* ... failed but free buffer space, try again */
468 /* push out remaining read buffer */
469 if( bufoff < buflen )
470 uh_http_send(cl, req, &buf[bufoff], buflen - bufoff);
477 /* headers complete, pass through buffer to socket */
478 uh_http_send(cl, req, buf, buflen);
481 /* looks like eof from child */
484 /* send final chunk if we're in chunked transfer mode */
485 uh_http_send(cl, req, "", 0);
491 /* no activity for 3 seconds... looks dead */
494 uh_http_sendhf(cl, 504, "Gateway Timeout",
495 "The CGI script took too long to produce a response");