WPS UPnP: Fix HTTP client timeout event code
[libeap.git] / src / wps / http_client.c
index d5c1efc..92fe36f 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * http_client - HTTP client
  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  *
@@ -21,7 +21,7 @@
 #include "http_client.h"
 
 
-#define HTTP_CLIENT_TIMEOUT 30
+#define HTTP_CLIENT_TIMEOUT_SEC 30
 
 
 struct http_client {
@@ -122,7 +122,7 @@ static void http_client_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
        c->req = NULL;
 
        c->hread = httpread_create(c->sd, http_client_got_response, c,
-                                  c->max_response, HTTP_CLIENT_TIMEOUT);
+                                  c->max_response, HTTP_CLIENT_TIMEOUT_SEC);
        if (c->hread == NULL) {
                c->cb(c->cb_ctx, c, HTTP_CLIENT_FAILED);
                return;
@@ -181,8 +181,8 @@ struct http_client * http_client_addr(struct sockaddr_in *dst,
                return NULL;
        }
 
-       if (eloop_register_timeout(HTTP_CLIENT_TIMEOUT, 0, http_client_timeout,
-                                  c, NULL)) {
+       if (eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
+                                  http_client_timeout, c, NULL)) {
                http_client_free(c);
                return NULL;
        }
@@ -193,25 +193,17 @@ struct http_client * http_client_addr(struct sockaddr_in *dst,
 }
 
 
-struct http_client * http_client_url(const char *url,
-                                    struct wpabuf *req, size_t max_response,
-                                    void (*cb)(void *ctx,
-                                               struct http_client *c,
-                                               enum http_client_event event),
-                                    void *cb_ctx)
+char * http_client_url_parse(const char *url, struct sockaddr_in *dst,
+                            char **ret_path)
 {
-       struct sockaddr_in dst;
-       struct http_client *c;
        char *u, *addr, *port, *path;
-       struct wpabuf *req_buf = NULL;
 
-       if (os_strncmp(url, "http://", 7) != 0)
-               return NULL;
        u = os_strdup(url);
        if (u == NULL)
                return NULL;
-       os_memset(&dst, 0, sizeof(dst));
-       dst.sin_family = AF_INET;
+
+       os_memset(dst, 0, sizeof(*dst));
+       dst->sin_family = AF_INET;
        addr = u + 7;
        path = os_strchr(addr, '/');
        port = os_strchr(addr, ':');
@@ -225,7 +217,7 @@ struct http_client * http_client_url(const char *url,
        if (port)
                *port++ = '\0';
 
-       if (inet_aton(addr, &dst.sin_addr) == 0) {
+       if (inet_aton(addr, &dst->sin_addr) == 0) {
                /* TODO: name lookup */
                wpa_printf(MSG_DEBUG, "HTTP: Unsupported address in URL '%s' "
                           "(addr='%s' port='%s')",
@@ -235,15 +227,39 @@ struct http_client * http_client_url(const char *url,
        }
 
        if (port)
-               dst.sin_port = htons(atoi(port));
+               dst->sin_port = htons(atoi(port));
        else
-               dst.sin_port = htons(80);
+               dst->sin_port = htons(80);
 
        if (*path == '\0') {
                /* remove temporary nul termination for address */
                *path = '/';
        }
 
+       *ret_path = path;
+
+       return u;
+}
+
+
+struct http_client * http_client_url(const char *url,
+                                    struct wpabuf *req, size_t max_response,
+                                    void (*cb)(void *ctx,
+                                               struct http_client *c,
+                                               enum http_client_event event),
+                                    void *cb_ctx)
+{
+       struct sockaddr_in dst;
+       struct http_client *c;
+       char *u, *path;
+       struct wpabuf *req_buf = NULL;
+
+       if (os_strncmp(url, "http://", 7) != 0)
+               return NULL;
+       u = http_client_url_parse(url, &dst, &path);
+       if (u == NULL)
+               return NULL;
+
        if (req == NULL) {
                req_buf = wpabuf_alloc(os_strlen(url) + 1000);
                if (req_buf == NULL) {
@@ -299,6 +315,14 @@ struct wpabuf * http_client_get_body(struct http_client *c)
 }
 
 
+char * http_client_get_hdr_line(struct http_client *c, const char *tag)
+{
+       if (c->hread == NULL)
+               return NULL;
+       return httpread_hdr_line_get(c->hread, tag);
+}
+
+
 char * http_link_update(char *url, const char *base)
 {
        char *n;
@@ -328,7 +352,7 @@ char * http_link_update(char *url, const char *base)
                        os_snprintf(n, len, "%s%s", base, url);
                } else {
                        os_memcpy(n, base, pos - base);
-                       os_memcpy(n + (pos - base), url, os_strlen(url));
+                       os_memcpy(n + (pos - base), url, os_strlen(url) + 1);
                }
        } else {
                pos = os_strrchr(base + 7, '/');
@@ -336,7 +360,8 @@ char * http_link_update(char *url, const char *base)
                        os_snprintf(n, len, "%s/%s", base, url);
                } else {
                        os_memcpy(n, base, pos - base + 1);
-                       os_memcpy(n + (pos - base) + 1, url, os_strlen(url));
+                       os_memcpy(n + (pos - base) + 1, url, os_strlen(url) +
+                                 1);
                }
        }