uclient: fix http regression
[project/uclient.git] / uclient-http.c
index 349e69cdd4aeb7a13859d0db71f5f277d3b7e580..deeb456cdfcb9bb96bcdbf1dc54487f0c82e1ef4 100644 (file)
@@ -72,11 +72,14 @@ struct uclient_http {
        struct ustream_ssl_ctx *ssl_ctx;
        struct ustream *us;
 
-       struct ustream_fd ufd;
-       struct ustream_ssl ussl;
+       union {
+               struct ustream_fd ufd;
+               struct ustream_ssl ussl;
+       };
 
        struct uloop_timeout disconnect_t;
        unsigned int seq;
+       int fd;
 
        bool ssl_require_validation;
        bool ssl;
@@ -131,7 +134,7 @@ static int uclient_do_connect(struct uclient_http *uh, const char *port)
                return -1;
 
        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
-       ustream_fd_init(&uh->ufd, fd);
+       uh->fd = fd;
 
        sl = sizeof(uh->uc.local_addr);
        memset(&uh->uc.local_addr, 0, sl);
@@ -148,9 +151,10 @@ static void uclient_http_disconnect(struct uclient_http *uh)
 
        if (uh->ssl)
                ustream_free(&uh->ussl.stream);
-       ustream_free(&uh->ufd.stream);
-       if(uh->ufd.fd.fd)
-               close(uh->ufd.fd.fd);
+       else
+               ustream_free(&uh->ufd.stream);
+       if(uh->fd >= 0)
+               close(uh->fd >= 0);
        uh->us = NULL;
 }
 
@@ -655,7 +659,8 @@ static void uclient_http_headers_complete(struct uclient_http *uh)
        if (uh->eof || seq != uh->uc.seq)
                return;
 
-       if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
+       if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204 ||
+                       uh->content_length == 0) {
                uh->eof = true;
                uclient_notify_eof(uh);
        }
@@ -779,9 +784,7 @@ static void __uclient_notify_read(struct uclient_http *uh)
        if (uh->state == HTTP_STATE_RECV_DATA) {
                /* Now it's uclient user turn to read some data */
                uloop_timeout_cancel(&uc->connection_timeout);
-
-               if (uc->cb->data_read)
-                       uc->cb->data_read(uc);
+               uclient_backend_read_notify(uc);
        }
 }
 
@@ -823,6 +826,7 @@ static int uclient_setup_http(struct uclient_http *uh)
        struct ustream *us = &uh->ufd.stream;
        int ret;
 
+       memset(&uh->ufd, 0, sizeof(uh->ufd));
        uh->us = us;
        uh->ssl = false;
 
@@ -835,6 +839,8 @@ static int uclient_setup_http(struct uclient_http *uh)
        if (ret)
                return UCLIENT_ERROR_CONNECT;
 
+       ustream_fd_init(&uh->ufd, uh->fd);
+
        return 0;
 }
 
@@ -862,17 +868,23 @@ static void uclient_ssl_notify_state(struct ustream *us)
 static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
 {
        struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
+       struct uclient *uc = &uh->uc;
 
+       if (uc->cb->log_msg)
+               uc->cb->log_msg(uc, UCLIENT_LOG_SSL_ERROR, str);
        uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
 }
 
 static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
 {
        struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
+       struct uclient *uc = &uh->uc;
 
        if (!uh->ssl_require_validation)
                return;
 
+       if (uc->cb->log_msg)
+               uc->cb->log_msg(uc, UCLIENT_LOG_SSL_VERIFY_ERROR, str);
        uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
 }
 
@@ -892,6 +904,7 @@ static int uclient_setup_https(struct uclient_http *uh)
        struct ustream *us = &uh->ussl.stream;
        int ret;
 
+       memset(&uh->ussl, 0, sizeof(uh->ussl));
        uh->ssl = true;
        uh->us = us;
 
@@ -910,7 +923,7 @@ static int uclient_setup_https(struct uclient_http *uh)
        uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
        uh->ussl.notify_connected = uclient_ssl_notify_connected;
        uh->ussl.server_name = uh->uc.url->host;
-       uh->ssl_ops->init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
+       uh->ssl_ops->init_fd(&uh->ussl, uh->fd, uh->ssl_ctx, false);
        uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host);
 
        return 0;
@@ -1078,8 +1091,12 @@ uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
                return 0;
 
        data = ustream_get_read_buf(uh->us, &read_len);
-       if (!data || !read_len)
-               return 0;
+       if (!data || !read_len) {
+               ustream_poll(uh->us);
+               data = ustream_get_read_buf(uh->us, &read_len);
+               if (!data || !read_len)
+                       return 0;
+       }
 
        data_end = data + read_len;
        read_len = 0;
@@ -1158,14 +1175,8 @@ int uclient_http_redirect(struct uclient *cl)
        if (cl->backend != &uclient_backend_http)
                return false;
 
-       switch (cl->status_code) {
-       case 301:
-       case 302:
-       case 307:
-               break;
-       default:
+       if (!uclient_http_status_redirect(cl))
                return false;
-       }
 
        blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
        if (!tb)
@@ -1232,6 +1243,14 @@ int uclient_http_set_address_family(struct uclient *cl, int af)
        return 0;
 }
 
+static int
+uclient_http_pending_bytes(struct uclient *cl, bool write)
+{
+       struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
+
+       return ustream_pending_data(uh->us, write);
+}
+
 const struct uclient_backend uclient_backend_http = {
        .prefix = uclient_http_prefix,
 
@@ -1245,4 +1264,5 @@ const struct uclient_backend uclient_backend_http = {
        .read = uclient_http_read,
        .write = uclient_http_send_data,
        .request = uclient_http_request_done,
+       .pending_bytes = uclient_http_pending_bytes,
 };