[package] firewall: don't apply default udp/68 rule to ip6tables
[openwrt.git] / package / uhttpd / src / uhttpd-cgi.c
index 5565197..855a72f 100644 (file)
@@ -68,7 +68,7 @@ static struct http_response * uh_cgi_header_parse(char *buf, int len, int *off)
                                if( (pos < len) && (buf[pos] == '\n') )
                                        pos++;
 
-                               if( pos < len )
+                               if( pos <= len )
                                {
                                        if( (hdrcount + 1) < array_size(res.headers) )
                                        {
@@ -360,6 +360,9 @@ void uh_cgi_request(struct client *cl, struct http_request *req, struct path_inf
 
                        memset(hdr, 0, sizeof(hdr));
 
+                       timeout.tv_sec = cl->server->conf->script_timeout;
+                       timeout.tv_usec = 0;
+
 #define ensure(x) \
        do { if( x < 0 ) goto out; } while(0)
 
@@ -372,12 +375,11 @@ void uh_cgi_request(struct client *cl, struct http_request *req, struct path_inf
                                FD_SET(rfd[0], &reader);
                                FD_SET(wfd[1], &writer);
 
-                               timeout.tv_sec = 15;
-                               timeout.tv_usec = 0;
-
                                /* wait until we can read or write or both */
-                               if( select(fd_max, &reader, (content_length > -1) ? &writer : NULL, NULL, &timeout) > 0 )
-                               {
+                               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) )
                                        {
@@ -538,11 +540,18 @@ void uh_cgi_request(struct client *cl, struct http_request *req, struct path_inf
                                        }
                                }
 
-                               /* no activity for 15 seconds... looks dead */
+                               /* timeout exceeded or interrupted by SIGCHLD */
                                else
                                {
-                                       ensure(uh_http_sendhf(cl, 504, "Gateway Timeout",
-                                               "The CGI script took too long to produce a response"));
+                                       if( (errno != EINTR) && ! header_sent )
+                                       {
+                                               ensure(uh_http_sendhf(cl, 504, "Gateway Timeout",
+                                                       "The CGI script took too long to produce "
+                                                       "a response"));
+                                       }
+
+                                       /* send final chunk if we're in chunked transfer mode */
+                                       ensure(uh_http_send(cl, req, "", 0));
 
                                        break;
                                }
@@ -553,7 +562,10 @@ void uh_cgi_request(struct client *cl, struct http_request *req, struct path_inf
                        close(wfd[1]);
 
                        if( !kill(child, 0) )
+                       {
                                kill(child, SIGTERM);
+                               waitpid(child, NULL, 0);
+                       }
 
                        break;
        }
This page took 0.025026 seconds and 4 git commands to generate.