kernel: fq_codel: dont reinit flow state
[openwrt/staging/stintel.git] / package / uhttpd / src / uhttpd-cgi.c
index e527922624e1e6c6996c7f61db9fc7af306bfdb8..69af90db4575145c1922dccb124b961a46695113 100644 (file)
@@ -133,7 +133,7 @@ static void uh_cgi_shutdown(struct uh_cgi_state *state)
 
 static bool uh_cgi_socket_cb(struct client *cl)
 {
-       int i, len, hdroff;
+       int i, len, blen, hdroff;
        char buf[UH_LIMIT_MSGHEAD];
 
        struct uh_cgi_state *state = (struct uh_cgi_state *)cl->priv;
@@ -184,21 +184,27 @@ static bool uh_cgi_socket_cb(struct client *cl)
        }
 
        /* try to read data from child */
-       while ((len = uh_raw_recv(cl->rpipe.fd, buf, sizeof(buf), -1)) > 0)
+       while ((len = uh_raw_recv(cl->rpipe.fd, buf, state->header_sent
+                                 ? sizeof(buf) : state->httpbuf.len, -1)) > 0)
        {
                /* we have not pushed out headers yet, parse input */
                if (!state->header_sent)
                {
                        /* try to parse header ... */
-                       memcpy(state->httpbuf, buf, len);
+                       memcpy(state->httpbuf.ptr, buf, len);
+                       state->httpbuf.len -= len;
+                       state->httpbuf.ptr += len;
 
-                       if (uh_cgi_header_parse(res, state->httpbuf, len, &hdroff))
+                       blen = state->httpbuf.ptr - state->httpbuf.buf;
+
+                       if (uh_cgi_header_parse(res, state->httpbuf.buf, blen, &hdroff))
                        {
                                /* write status */
                                ensure_out(uh_http_sendf(cl, NULL,
-                                       "HTTP/%.1f %03d %s\r\n"
+                                       "%s %03d %s\r\n"
                                        "Connection: close\r\n",
-                                       req->version, res->statuscode, res->statusmsg));
+                                       http_versions[req->version],
+                                       res->statuscode, res->statusmsg));
 
                                /* add Content-Type if no Location or Content-Type */
                                if (!uh_cgi_header_lookup(res, "Location") &&
@@ -209,7 +215,7 @@ static bool uh_cgi_socket_cb(struct client *cl)
                                }
 
                                /* if request was HTTP 1.1 we'll respond chunked */
-                               if ((req->version > 1.0) &&
+                               if ((req->version > UH_HTTP_VER_1_0) &&
                                        !uh_cgi_header_lookup(res, "Transfer-Encoding"))
                                {
                                        ensure_out(uh_http_send(cl, NULL,
@@ -229,18 +235,19 @@ static bool uh_cgi_socket_cb(struct client *cl)
                                state->header_sent = true;
 
                                /* push out remaining head buffer */
-                               if (hdroff < len)
+                               if (hdroff < blen)
                                {
                                        D("CGI: Child(%d) relaying %d rest bytes\n",
-                                         cl->proc.pid, len - hdroff);
+                                         cl->proc.pid, blen - hdroff);
 
                                        ensure_out(uh_http_send(cl, req,
-                                                                                       &buf[hdroff], len - hdroff));
+                                                               state->httpbuf.buf + hdroff,
+                                                               blen - hdroff));
                                }
                        }
 
                        /* ... failed and head buffer exceeded */
-                       else
+                       else if (!state->httpbuf.len)
                        {
                                /* I would do this ...
                                 *
@@ -254,10 +261,11 @@ static bool uh_cgi_socket_cb(struct client *cl)
                                 */
 
                                ensure_out(uh_http_sendf(cl, NULL,
-                                                                                "HTTP/%.1f 200 OK\r\n"
+                                                                                "%s 200 OK\r\n"
                                                                                 "Content-Type: text/plain\r\n"
                                                                                 "%s\r\n",
-                                                                                req->version, (req->version > 1.0)
+                                                                                http_versions[req->version],
+                                                                                (req->version > UH_HTTP_VER_1_0)
                                                                                 ? "Transfer-Encoding: chunked\r\n" : ""
                                ));
 
@@ -421,26 +429,10 @@ bool uh_cgi_request(struct client *cl, struct path_info *pi,
                        }
 
                        /* http version */
-                       if (req->version > 1.0)
-                               setenv("SERVER_PROTOCOL", "HTTP/1.1", 1);
-                       else
-                               setenv("SERVER_PROTOCOL", "HTTP/1.0", 1);
+                       setenv("SERVER_PROTOCOL", http_versions[req->version], 1);
 
                        /* request method */
-                       switch (req->method)
-                       {
-                               case UH_HTTP_MSG_GET:
-                                       setenv("REQUEST_METHOD", "GET", 1);
-                                       break;
-
-                               case UH_HTTP_MSG_HEAD:
-                                       setenv("REQUEST_METHOD", "HEAD", 1);
-                                       break;
-
-                               case UH_HTTP_MSG_POST:
-                                       setenv("REQUEST_METHOD", "POST", 1);
-                                       break;
-                       }
+                       setenv("REQUEST_METHOD", http_methods[req->method], 1);
 
                        /* request url */
                        setenv("REQUEST_URI", req->url, 1);
@@ -536,6 +528,9 @@ bool uh_cgi_request(struct client *cl, struct path_info *pi,
 
                D("CGI: Child(%d) created: rfd(%d) wfd(%d)\n", child, rfd[0], wfd[1]);
 
+               state->httpbuf.ptr = state->httpbuf.buf;
+               state->httpbuf.len = sizeof(state->httpbuf.buf);
+
                state->content_length = cl->httpbuf.len;
 
                /* find content length */