use chunked data for POST, set HTTP version to 1.1
authorFelix Fietkau <nbd@openwrt.org>
Fri, 21 Mar 2014 12:48:59 +0000 (13:48 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 21 Mar 2014 12:48:59 +0000 (13:48 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
uclient-backend.h
uclient-http.c

index f72c11fb3ad2f5eee7b14c09b96b64bb32d2ab33..5f83995d5835932ea3a5734c3c24cc250859dac8 100644 (file)
@@ -14,7 +14,6 @@ struct uclient_backend {
 
        int (*read)(struct uclient *cl, char *buf, unsigned int len);
        int (*write)(struct uclient *cl, char *buf, unsigned int len);
-       int (*set_write_len)(struct uclient *cl, unsigned int len);
 };
 
 struct uclient_url {
index bf0b05170837f6de05ab059d741c13e7a3e462ab..8ff2e0ae20fde3e03e30049e37de87495c8279be 100644 (file)
@@ -47,8 +47,6 @@ struct uclient_http {
        enum request_type req_type;
        enum http_state state;
 
-       unsigned int send_len;
-
        struct blob_buf headers;
        struct blob_buf meta;
 };
@@ -361,8 +359,9 @@ uclient_http_send_headers(struct uclient_http *uh)
                return;
 
        ustream_printf(uh->us,
-               "%s /%s HTTP/1.0\r\n"
-               "Host: %s\r\n",
+               "%s /%s HTTP/1.1\r\n"
+               "Host: %s\r\n"
+               "Connection: close\r\n",
                request_types[uh->req_type],
                url->location, url->host);
 
@@ -381,30 +380,12 @@ uclient_http_send_headers(struct uclient_http *uh)
                ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
        }
 
-       if (uh->send_len > 0)
-               ustream_printf(uh->us, "Content-Length: %d", uh->send_len);
+       if (uh->req_type == REQ_POST)
+               ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
 
        ustream_printf(uh->us, "\r\n");
 }
 
-static int
-uclient_http_set_write_len(struct uclient *cl, unsigned int len)
-{
-       struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
-
-       if (uh->state >= HTTP_STATE_HEADERS_SENT)
-               return -1;
-
-       if (uh->req_type == REQ_GET || uh->req_type == REQ_HEAD) {
-               fprintf(stderr, "Sending data is not supported for %s requests\n",
-                       request_types[uh->req_type]);
-               return -1;
-       }
-
-       uh->send_len = len;
-       return 0;
-}
-
 static int
 uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
 {
@@ -415,12 +396,9 @@ uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
 
        uclient_http_send_headers(uh);
 
-       if (len > uh->send_len) {
-               fprintf(stderr, "%s: ignoring %d extra data bytes\n", __func__, uh->send_len - len);
-               len = uh->send_len;
-       }
-
+       ustream_printf(uh->us, "%X\r\n", len);
        ustream_write(uh->us, buf, len, false);
+       ustream_printf(uh->us, "\r\n");
 
        return len;
 }
@@ -433,9 +411,6 @@ uclient_http_request_done(struct uclient *cl)
        if (uh->state >= HTTP_STATE_REQUEST_DONE)
                return -1;
 
-       if (uh->send_len > 0)
-               fprintf(stderr, "%s: missing %d POST data bytes\n", __func__, uh->send_len);
-
        uclient_http_send_headers(uh);
        uh->state = HTTP_STATE_REQUEST_DONE;
 
@@ -476,6 +451,4 @@ const struct uclient_backend uclient_backend_http __hidden = {
        .read = uclient_http_read,
        .write = uclient_http_send_data,
        .request = uclient_http_request_done,
-
-       .set_write_len = uclient_http_set_write_len,
 };