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 )
17 *off = (int)(bufptr - buf) + 4;
19 memset(&res, 0, sizeof(res));
26 for( pos = 0; pos < len; pos++ )
28 if( !hdrname && (buf[pos] == ':') )
32 if( (pos < len) && (buf[pos] == ' ') )
42 else if( (buf[pos] == '\r') || (buf[pos] == '\n') )
49 if( (pos < len) && (buf[pos] == '\n') )
54 if( (hdrcount + 1) < array_size(res.headers) )
56 if( ! strcasecmp(hdrname, "Status") )
58 res.statuscode = atoi(bufptr);
60 if( res.statuscode < 100 )
63 if( ((bufptr = strchr(bufptr, ' ')) != NULL) && (&bufptr[1] != 0) )
64 res.statusmsg = &bufptr[1];
68 res.headers[hdrcount++] = hdrname;
69 res.headers[hdrcount++] = bufptr;
89 static char * uh_cgi_header_lookup(struct http_response *res, const char *hdrname)
93 foreach_header(i, res->headers)
95 if( ! strcasecmp(res->headers[i], hdrname) )
96 return res->headers[i+1];
102 static void uh_cgi_error_500(struct client *cl, struct http_request *req, const char *message)
104 uh_http_sendf(cl, NULL,
105 "HTTP/%.1f 500 Internal Server Error\r\n"
106 "Content-Type: text/plain\r\n%s\r\n",
109 ? "Transfer-Encoding: chunked\r\n" : ""
112 uh_http_send(cl, req, message, -1);
116 void uh_cgi_request(struct client *cl, struct http_request *req)
118 int i, hdroff, bufoff;
122 int content_length = 0;
125 int rfd[2] = { 0, 0 };
126 int wfd[2] = { 0, 0 };
128 char buf[UH_LIMIT_MSGHEAD];
129 char hdr[UH_LIMIT_MSGHEAD];
134 struct timeval timeout;
135 struct http_response *res;
136 struct uh_path_info *pi;
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 if( (pi = uh_path_lookup(cl, req->url)) != NULL )
174 /* check for regular, world-executable file */
175 if( (pi->stat.st_mode & S_IFREG) &&
176 (pi->stat.st_mode & S_IXOTH)
178 /* build environment */
181 /* common information */
182 setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
183 setenv("SERVER_SOFTWARE", "uHTTPd", 1);
184 setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
189 setenv("HTTPS", "on", 1);
193 setenv("SERVER_NAME", sa_straddr(&cl->servaddr), 1);
194 setenv("SERVER_ADDR", sa_straddr(&cl->servaddr), 1);
195 setenv("SERVER_PORT", sa_strport(&cl->servaddr), 1);
196 setenv("REMOTE_HOST", sa_straddr(&cl->peeraddr), 1);
197 setenv("REMOTE_ADDR", sa_straddr(&cl->peeraddr), 1);
198 setenv("REMOTE_PORT", sa_strport(&cl->peeraddr), 1);
200 /* path information */
201 setenv("SCRIPT_NAME", pi->name, 1);
202 setenv("SCRIPT_FILENAME", pi->phys, 1);
203 setenv("SCRIPT_WORKDIR", pi->wdir, 1); /* nonstandard */
204 setenv("DOCUMENT_ROOT", pi->root, 1);
205 setenv("QUERY_STRING", pi->query ? pi->query : "", 1);
208 setenv("PATH_INFO", pi->info, 1);
212 if( req->version > 1.0 )
213 setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
215 setenv("SERVER_PROTOCOL", "HTTP/1.0", 1);
218 switch( req->method )
220 case UH_HTTP_MSG_GET:
221 setenv("REQUEST_METHOD", "GET", 1);
224 case UH_HTTP_MSG_HEAD:
225 setenv("REQUEST_METHOD", "HEAD", 1);
228 case UH_HTTP_MSG_POST:
229 setenv("REQUEST_METHOD", "POST", 1);
234 setenv("REQUEST_URI", req->url, 1);
236 /* request message headers */
237 foreach_header(i, req->headers)
239 if( ! strcasecmp(req->headers[i], "Accept") )
240 setenv("HTTP_ACCEPT", req->headers[i+1], 1);
242 else if( ! strcasecmp(req->headers[i], "Accept-Charset") )
243 setenv("HTTP_ACCEPT_CHARSET", req->headers[i+1], 1);
245 else if( ! strcasecmp(req->headers[i], "Accept-Encoding") )
246 setenv("HTTP_ACCEPT_ENCODING", req->headers[i+1], 1);
248 else if( ! strcasecmp(req->headers[i], "Accept-Language") )
249 setenv("HTTP_ACCEPT_LANGUAGE", req->headers[i+1], 1);
251 else if( ! strcasecmp(req->headers[i], "Authorization") )
252 setenv("HTTP_AUTHORIZATION", req->headers[i+1], 1);
254 else if( ! strcasecmp(req->headers[i], "Connection") )
255 setenv("HTTP_CONNECTION", req->headers[i+1], 1);
257 else if( ! strcasecmp(req->headers[i], "Cookie") )
258 setenv("HTTP_COOKIE", req->headers[i+1], 1);
260 else if( ! strcasecmp(req->headers[i], "Host") )
261 setenv("HTTP_HOST", req->headers[i+1], 1);
263 else if( ! strcasecmp(req->headers[i], "Referer") )
264 setenv("HTTP_REFERER", req->headers[i+1], 1);
266 else if( ! strcasecmp(req->headers[i], "User-Agent") )
267 setenv("HTTP_USER_AGENT", req->headers[i+1], 1);
269 else if( ! strcasecmp(req->headers[i], "Content-Type") )
270 setenv("CONTENT_TYPE", req->headers[i+1], 1);
272 else if( ! strcasecmp(req->headers[i], "Content-Length") )
273 setenv("CONTENT_LENGTH", req->headers[i+1], 1);
277 /* execute child code ... */
278 if( chdir(pi->wdir) )
281 execl(pi->phys, pi->phys, NULL);
283 /* in case it fails ... */
285 "Status: 500 Internal Server Error\r\n\r\n"
286 "Unable to launch the requested CGI program:\n"
288 pi->phys, strerror(errno)
296 "Status: 403 Forbidden\r\n\r\n"
297 "Access to this resource is forbidden\n"
306 "Status: 404 Not Found\r\n\r\n"
307 "Unable to launch the requested CGI program:\n"
308 " No such file or directory\n"
318 /* parent; handle I/O relaying */
320 /* close unneeded pipe ends */
325 fd_max = max(rfd[0], wfd[1]) + 1;
327 /* find content length */
328 if( req->method == UH_HTTP_MSG_POST )
330 foreach_header(i, req->headers)
332 if( ! strcasecmp(req->headers[i], "Content-Length") )
334 content_length = atoi(req->headers[i+1]);
341 memset(hdr, 0, sizeof(hdr));
343 /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */
349 FD_SET(rfd[0], &reader);
350 FD_SET(wfd[1], &writer);
355 /* wait until we can read or write or both */
356 if( select(fd_max, &reader, (content_length > -1) ? &writer : NULL, NULL, &timeout) > 0 )
358 /* ready to write to cgi program */
359 if( FD_ISSET(wfd[1], &writer) )
361 /* there is unread post data waiting */
362 if( content_length > 0 )
364 /* read it from socket ... */
365 if( (buflen = uh_tcp_recv(cl, buf, min(content_length, sizeof(buf)))) > 0 )
367 /* ... and write it to child's stdin */
368 if( write(wfd[1], buf, buflen) < 0 )
371 content_length -= buflen;
374 /* unexpected eof! */
377 if( write(wfd[1], "", 0) < 0 )
384 /* there is no more post data, close pipe to child's stdin */
392 /* ready to read from cgi program */
393 if( FD_ISSET(rfd[0], &reader) )
395 /* read data from child ... */
396 if( (buflen = read(rfd[0], buf, sizeof(buf))) > 0 )
398 /* we have not pushed out headers yet, parse input */
401 /* head buffer not full and no end yet */
402 if( hdrlen < sizeof(hdr) )
404 bufoff = min(buflen, sizeof(hdr) - hdrlen);
405 memcpy(&hdr[hdrlen], buf, bufoff);
414 /* try to parse header ... */
415 if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL )
418 uh_http_sendf(cl, NULL, "HTTP/%.1f %03d %s\r\n",
419 req->version, res->statuscode, res->statusmsg);
421 /* add Content-Type if no Location or Content-Type */
422 if( !uh_cgi_header_lookup(res, "Location") &&
423 !uh_cgi_header_lookup(res, "Content-Type")
425 uh_http_send(cl, NULL,
426 "Content-Type: text/plain\r\n", -1);
429 /* if request was HTTP 1.1 we'll respond chunked */
430 if( (req->version > 1.0) &&
431 !uh_cgi_header_lookup(res, "Transfer-Encoding")
433 uh_http_send(cl, NULL,
434 "Transfer-Encoding: chunked\r\n", -1);
437 /* write headers from CGI program */
438 foreach_header(i, res->headers)
440 uh_http_sendf(cl, NULL, "%s: %s\r\n",
441 res->headers[i], res->headers[i+1]);
444 /* terminate header */
445 uh_http_send(cl, NULL, "\r\n", -1);
447 /* push out remaining head buffer */
448 if( hdroff < hdrlen )
449 uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff);
452 /* ... failed and head buffer exceeded */
453 else if( hdrlen >= sizeof(hdr) )
455 uh_cgi_error_500(cl, req,
456 "The CGI program generated an invalid response:\n\n");
458 uh_http_send(cl, req, hdr, hdrlen);
461 /* ... failed but free buffer space, try again */
467 /* push out remaining read buffer */
468 if( bufoff < buflen )
469 uh_http_send(cl, req, &buf[bufoff], buflen - bufoff);
476 /* headers complete, pass through buffer to socket */
477 uh_http_send(cl, req, buf, buflen);
480 /* looks like eof from child */
483 /* send final chunk if we're in chunked transfer mode */
484 uh_http_send(cl, req, "", 0);
490 /* no activity for 3 seconds... looks dead */
493 uh_http_sendhf(cl, 504, "Gateway Timeout",
494 "The CGI script took too long to produce a response");