uclient-http: allow .header_done() callback to reset the connection
[project/uclient.git] / uclient-http.c
index c25e52f..7e930d7 100644 (file)
@@ -41,6 +41,7 @@ enum request_type {
        REQ_HEAD,
        REQ_POST,
        REQ_PUT,
+       REQ_DELETE,
        __REQ_MAX
 };
 
@@ -58,6 +59,7 @@ static const char * const request_types[__REQ_MAX] = {
        [REQ_HEAD] = "HEAD",
        [REQ_POST] = "POST",
        [REQ_PUT] = "PUT",
+       [REQ_DELETE] = "DELETE",
 };
 
 struct uclient_http {
@@ -71,6 +73,7 @@ struct uclient_http {
        struct ustream_ssl ussl;
 
        struct uloop_timeout disconnect_t;
+       unsigned int seq;
 
        bool ssl_require_validation;
        bool ssl;
@@ -194,6 +197,7 @@ static void uclient_notify_eof(struct uclient_http *uh)
 
 static void uclient_http_reset_state(struct uclient_http *uh)
 {
+       uh->seq++;
        uclient_backend_reset_state(&uh->uc);
        uh->read_chunked = -1;
        uh->content_length = -1;
@@ -639,6 +643,7 @@ error:
 static void __uclient_notify_read(struct uclient_http *uh)
 {
        struct uclient *uc = &uh->uc;
+       unsigned int seq = uh->seq;
        char *data;
        int len;
 
@@ -673,6 +678,9 @@ static void __uclient_notify_read(struct uclient_http *uh)
                        *sep = 0;
                        cur_len = sep + 2 - data;
                        uclient_parse_http_line(uh, data);
+                       if (seq != uh->seq)
+                               return;
+
                        ustream_consume(uh->us, cur_len);
                        len -= cur_len;
 
@@ -689,8 +697,13 @@ static void __uclient_notify_read(struct uclient_http *uh)
        if (uh->eof)
                return;
 
-       if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
-               uc->cb->data_read(uc);
+       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);
+       }
 }
 
 static void __uclient_notify_write(struct uclient_http *uh)
@@ -926,7 +939,7 @@ uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
 }
 
 static int
-uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
+uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len)
 {
        struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
 
@@ -1030,6 +1043,10 @@ uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
 
        uclient_notify_eof(uh);
 
+       /* Now that we consumed something and if this isn't EOF, start timer again */
+       if (!uh->uc.eof && !cl->connection_timeout.pending)
+               uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
+
        return len;
 }