+/*
+ * uhttpd - Tiny non-forking httpd - CGI handler
+ *
+ * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
#include "uhttpd.h"
#include "uhttpd-utils.h"
#include "uhttpd-cgi.h"
return NULL;
}
-static void 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)
{
- uh_http_sendf(cl, NULL,
+ 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);
+ }
- uh_http_send(cl, req, message, -1);
+ return -1;
}
-void uh_cgi_request(struct client *cl, struct http_request *req, struct uh_path_info *pi)
+void uh_cgi_request(struct client *cl, struct http_request *req, struct path_info *pi)
{
int i, hdroff, bufoff;
int hdrlen = 0;
/* request url */
setenv("REQUEST_URI", req->url, 1);
+ /* remote user */
+ if( req->realm )
+ setenv("REMOTE_USER", req->realm->user, 1);
+
/* request message headers */
foreach_header(i, req->headers)
{
memset(hdr, 0, sizeof(hdr));
+#define ensure(x) \
+ do { if( x < 0 ) goto out; } while(0)
+
/* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */
while( 1 )
{
FD_SET(rfd[0], &reader);
FD_SET(wfd[1], &writer);
- timeout.tv_sec = 3;
+ timeout.tv_sec = 15;
timeout.tv_usec = 0;
/* wait until we can read or write or both */
}
/* there is no more post data, close pipe to child's stdin */
- else
+ else if( content_length > -1 )
{
close(wfd[1]);
content_length = -1;
if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL )
{
/* write status */
- uh_http_sendf(cl, NULL, "HTTP/%.1f %03d %s\r\n",
- req->version, res->statuscode, res->statusmsg);
+ ensure(uh_http_sendf(cl, NULL,
+ "HTTP/%.1f %03d %s\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")
) {
- uh_http_send(cl, NULL,
- "Content-Type: text/plain\r\n", -1);
+ 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")
) {
- uh_http_send(cl, NULL,
- "Transfer-Encoding: chunked\r\n", -1);
+ ensure(uh_http_send(cl, NULL,
+ "Transfer-Encoding: chunked\r\n", -1));
}
/* write headers from CGI program */
foreach_header(i, res->headers)
{
- uh_http_sendf(cl, NULL, "%s: %s\r\n",
- res->headers[i], res->headers[i+1]);
+ ensure(uh_http_sendf(cl, NULL, "%s: %s\r\n",
+ res->headers[i], res->headers[i+1]));
}
/* terminate header */
- uh_http_send(cl, NULL, "\r\n", -1);
+ ensure(uh_http_send(cl, NULL, "\r\n", -1));
/* push out remaining head buffer */
if( hdroff < hdrlen )
- uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff);
+ ensure(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff));
}
/* ... failed and head buffer exceeded */
else if( hdrlen >= sizeof(hdr) )
{
- uh_cgi_error_500(cl, req,
- "The CGI program generated an invalid response:\n\n");
+ ensure(uh_cgi_error_500(cl, req,
+ "The CGI program generated an invalid response:\n\n"));
- uh_http_send(cl, req, hdr, hdrlen);
+ ensure(uh_http_send(cl, req, hdr, hdrlen));
}
/* ... failed but free buffer space, try again */
/* push out remaining read buffer */
if( bufoff < buflen )
- uh_http_send(cl, req, &buf[bufoff], buflen - bufoff);
+ ensure(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff));
header_sent = 1;
continue;
/* headers complete, pass through buffer to socket */
- uh_http_send(cl, req, buf, buflen);
+ ensure(uh_http_send(cl, req, buf, buflen));
}
/* looks like eof from child */
/* cgi script did not output useful stuff at all */
if( ! header_sent )
{
- uh_cgi_error_500(cl, req,
- "The CGI program generated an invalid response:\n\n");
-
- uh_http_send(cl, req, hdr, hdrlen);
+ /* 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));
}
/* send final chunk if we're in chunked transfer mode */
- uh_http_send(cl, req, "", 0);
+ ensure(uh_http_send(cl, req, "", 0));
break;
}
}
}
- /* no activity for 3 seconds... looks dead */
+ /* no activity for 15 seconds... looks dead */
else
{
- uh_http_sendhf(cl, 504, "Gateway Timeout",
- "The CGI script took too long to produce a response");
+ ensure(uh_http_sendhf(cl, 504, "Gateway Timeout",
+ "The CGI script took too long to produce a response"));
break;
}
}
+ out:
close(rfd[0]);
close(wfd[1]);