about summary refs log tree commit diff
path: root/misc/http_client.c
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-10-31 10:09:37 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-10-31 10:09:37 +0000
commit80dcfecbe5f3909fa24bdbe2f0b013f5ab0f6485 (patch)
tree683087a98751260f42ce33b160097464de39a2ad /misc/http_client.c
parentf939819e286b186e0ff35658187484ac4781e03e (diff)
downloadbtpd-80dcfecbe5f3909fa24bdbe2f0b013f5ab0f6485.tar.gz
btpd-80dcfecbe5f3909fa24bdbe2f0b013f5ab0f6485.zip
Rename callback type, add timeouts and and allow any http version in the
reply.

Diffstat (limited to 'misc/http_client.c')
-rw-r--r--misc/http_client.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/misc/http_client.c b/misc/http_client.c
index 2669213..61644a2 100644
--- a/misc/http_client.c
+++ b/misc/http_client.c
@@ -18,6 +18,8 @@
 #include "subr.h"
 #include "http_client.h"
 
+#define TIMEOUT (& (struct timeval) { 45, 0 })
+
 struct http_url *
 http_url_parse(const char *url)
 {
@@ -92,7 +94,7 @@ struct http_req {
     struct http_url *url;
     int sd;
     struct event ev;
-    http_cb cb;
+    http_cb_t cb;
     void *arg;
     int cancel;
 
@@ -135,7 +137,7 @@ http_error(struct http_req *req)
 static int
 headers_parse(struct http_req *req, char *buf, char *end)
 {
-    int code;
+    int code, majv, minv;
     char *cur, *crlf;
     char name[128], value[872];
     struct http_response res;
@@ -143,7 +145,7 @@ headers_parse(struct http_req *req, char *buf, char *end)
     req->chunked = 0;
     req->length = -1;
 
-    if (sscanf(buf, "HTTP/1.1 %d", &code) == 0)
+    if (sscanf(buf, "HTTP/%d.%d %d", &majv, &minv, &code) != 3)
         return 0;
     res.type = HTTP_T_CODE;
     res.v.code = code;
@@ -348,7 +350,7 @@ http_read_cb(int sd, short type, void *arg)
         return;
     req->state = HTTP_RECEIVE;
 more:
-    if (event_add(&req->ev, NULL) != 0)
+    if (event_add(&req->ev, TIMEOUT) != 0)
         http_error(req);
 }
 
@@ -369,12 +371,12 @@ http_write_cb(int sd, short type, void *arg)
     }
 out:
     if (req->buf->off != 0) {
-        if (event_add(&req->ev, NULL) != 0)
+        if (event_add(&req->ev, TIMEOUT) != 0)
             goto error;
     } else {
         req->state = HTTP_RECEIVE;
         event_set(&req->ev, req->sd, EV_READ, http_read_cb, req);
-        if (event_add(&req->ev, NULL) != 0)
+        if (event_add(&req->ev, TIMEOUT) != 0)
             goto error;
     }
     return;
@@ -404,7 +406,7 @@ http_dnscb(int result, char type, int count, int ttl, void *addrs, void *arg)
                 && errno != EINPROGRESS))
             goto error;
         event_set(&req->ev, req->sd, EV_WRITE, http_write_cb, req);
-        if (event_add(&req->ev, NULL) != 0)
+        if (event_add(&req->ev, TIMEOUT) != 0)
             goto error;
     } else
         goto error;
@@ -415,8 +417,8 @@ error:
 }
 
 int
-http_get(struct http_req **out, const char *url, const char *hdrs, http_cb cb,
-    void *arg)
+http_get(struct http_req **out, const char *url, const char *hdrs,
+    http_cb_t cb, void *arg)
 {
     struct http_req *req = calloc(1, sizeof(*req));
     if (req == NULL)