uclient-fetch: use package name pattern in message for missing SSL library
[project/uclient.git] / uclient-http.c
index f080e41..ef8de98 100644 (file)
@@ -20,6 +20,7 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <string.h>
 #include <fcntl.h>
 
 #include <libubox/ustream.h>
@@ -123,7 +124,9 @@ static int uclient_do_connect(struct uclient_http *uh, const char *port)
 
        memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
 
-       fd = usock_inet(USOCK_TCP | uh->usock_flags, uh->uc.url->host, port, &uh->uc.remote_addr);
+       fd = usock_inet_timeout(USOCK_TCP | USOCK_NONBLOCK | uh->usock_flags,
+                               uh->uc.url->host, port, &uh->uc.remote_addr,
+                               uh->uc.timeout_msecs);
        if (fd < 0)
                return -1;
 
@@ -284,6 +287,18 @@ static void uclient_http_process_headers(struct uclient_http *uh)
        uh->auth_type = uclient_http_update_auth_type(uh);
 }
 
+static bool uclient_request_supports_body(enum request_type req_type)
+{
+       switch (req_type) {
+       case REQ_POST:
+       case REQ_PUT:
+       case REQ_DELETE:
+               return true;
+       default:
+               return false;
+       }
+}
+
 static void
 uclient_http_add_auth_basic(struct uclient_http *uh)
 {
@@ -365,11 +380,14 @@ get_cnonce(char *dest)
 {
        uint32_t val = 0;
        FILE *f;
+       size_t n;
 
        f = fopen("/dev/urandom", "r");
        if (f) {
-               fread(&val, sizeof(val), 1, f);
+               n = fread(&val, sizeof(val), 1, f);
                fclose(f);
+               if (n != 1)
+                       return;
        }
 
        bin_to_hex(dest, &val, sizeof(val));
@@ -545,6 +563,7 @@ uclient_http_send_headers(struct uclient_http *uh)
        struct uclient_url *url = uh->uc.url;
        struct blob_attr *cur;
        enum request_type req_type = uh->req_type;
+       bool literal_ipv6;
        int rem;
 
        if (uh->state >= HTTP_STATE_HEADERS_SENT)
@@ -553,16 +572,22 @@ uclient_http_send_headers(struct uclient_http *uh)
        if (uh->uc.proxy_url)
                url = uh->uc.proxy_url;
 
+       literal_ipv6 = strchr(url->host, ':');
+
        ustream_printf(uh->us,
                "%s %s HTTP/1.1\r\n"
-               "Host: %s\r\n",
-               request_types[req_type],
-               url->location, url->host);
+               "Host: %s%s%s%s%s\r\n",
+               request_types[req_type], url->location,
+               literal_ipv6 ? "[" : "",
+               url->host,
+               literal_ipv6 ? "]" : "",
+               url->port ? ":" : "",
+               url->port ? url->port : "");
 
        blobmsg_for_each_attr(cur, uh->headers.head, rem)
                ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
 
-       if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
+       if (uclient_request_supports_body(uh->req_type))
                ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
 
        uclient_http_add_auth_header(uh);
@@ -990,7 +1015,7 @@ uclient_http_request_done(struct uclient *cl)
                return -1;
 
        uclient_http_send_headers(uh);
-       if (uh->req_type == REQ_POST || uh->req_type == REQ_PUT)
+       if (uclient_request_supports_body(uh->req_type))
                ustream_printf(uh->us, "0\r\n\r\n");
        uh->state = HTTP_STATE_REQUEST_DONE;
 
@@ -1100,7 +1125,7 @@ int uclient_http_redirect(struct uclient *cl)
        if (!tb)
                return false;
 
-       url = uclient_get_url(blobmsg_data(tb), url->auth);
+       url = uclient_get_url_location(url, blobmsg_data(tb));
        if (!url)
                return false;