OSDN Git Service

Fix HTTPproxy not working on http URL.
authorSATO Kentaro <kentaro@ranvis.com>
Tue, 21 Jun 2011 09:53:27 +0000 (18:53 +0900)
committerSATO Kentaro <kentaro@ranvis.com>
Sat, 24 May 2014 09:46:08 +0000 (18:46 +0900)
libarc/url_http.c

index 875d47d..7cef9fa 100644 (file)
@@ -92,11 +92,37 @@ static void timeout(int sig)
     timeout_flag = 1;
 }
 
+static char *get_http_url_host_port(const char *url, char *buffer, int buffer_size)
+{
+       const char *host = url;
+       const char *host_end;
+       int length;
+
+       if (strncmp(host, "http://", 7) == 0)
+               host += 7;
+       if (*host == '[')
+       {
+               if (!(host_end = strchr(host, ']')))
+                       return NULL;
+               host_end++;
+       }
+       else
+               host_end = host;
+       for (; *host_end && *host_end != '/'; host_end++)
+               ;
+       length = host_end - host;
+       if (length >= buffer_size)
+               return NULL;
+       memcpy(buffer, host, length);
+       buffer[length] = '\0';
+       return buffer;
+}
+
 URL url_http_open(char *name)
 {
     URL_http *url;
     SOCKET fd;
-    char *host, *path = NULL, *p;
+    char *host, *p;
     unsigned short port;
     char buff[BUFSIZ];
     char wwwserver[256];
@@ -125,39 +151,17 @@ URL url_http_open(char *name)
     /* private members */
     url->fp = NULL;
 
+    if (get_http_url_host_port(name, wwwserver, sizeof wwwserver) == NULL)
+       return NULL;
     if(url_http_proxy_host)
     {
-       char *q;
-       int len;
-
        host = url_http_proxy_host;
        port = url_http_proxy_port;
-
-       p = name;
-       if(strncmp(p, "http://", 7) == 0)
-           p += 7;
-        if (p[0] == '[')
-        {
-            if (!(q = strchr(p, ']')))
-                return NULL;
-            *q = '\0';
-            ++p;
-        } else
-           for(q = p; *q && *q != ':' && *q != '/'; q++)
-               ;
-       len = q - p;
-       if(len >= sizeof(wwwserver) - 1) { /* What?? */
-           strcpy(wwwserver, "localhost");
-       } else {
-           strncpy(wwwserver, p, len);
-       }
     }
     else
     {
-       if(strncmp(name, "http://", 7) == 0)
-           name += 7;
-       n = strlen(name);
-       if(n + REQUEST_OFFSET >= BUFSIZ)
+       n = strlen(wwwserver);
+       if(n + REQUEST_OFFSET >= sizeof buff)
        {
            url_http_close((URL)url);
            url_errno = URLERR_URLTOOLONG;
@@ -165,42 +169,34 @@ URL url_http_open(char *name)
            return NULL;
        }
 
-       memcpy(buff, name, n + 1);
+       memcpy(buff, wwwserver, n + 1);
 
        host = buff;
 
         if (host[0] == '[')
         {
+            ++host;
             if (!(p = strchr(host, ']')))
                 return NULL;
-            *p = '\0';
-            ++host;
-            ++p;
+            *p++ = '\0'; /* terminate `host' string */
         } else
-            for(p = host; *p && *p != ':' && *p != '/'; p++)
+            for(p = host; *p && *p != ':'; p++)
                 ;
 
        if(*p == ':')
        {
-           char *pp;
-
            *p++ = '\0'; /* terminate `host' string */
            port = atoi(p);
-           pp = strchr(p, '/');
-           if(pp == NULL)
-               p[0] = '\0';
-           else
-               p = pp;
        }
        else
            port = 80;
-       path = p;
-
-       if(*path == '\0')
-           *(path + 1) = '\0';
 
-       *path = '\0'; /* terminate `host' string */
-       strncpy(wwwserver, host, sizeof(wwwserver));
+       if(strncmp(name, "http://", 7) == 0)
+           name += 7;
+       if((p = strchr(name, '/')) == NULL)
+           name = "/";
+       else
+           name = p;
     }
 
 #ifdef DEBUG
@@ -246,13 +242,7 @@ URL url_http_open(char *name)
        return NULL;
     }
 
-    if(url_http_proxy_host)
-       sprintf(buff, "GET %s HTTP/1.0\r\n", name);
-    else
-    {
-       *path = '/';
-       sprintf(buff, "GET %s HTTP/1.0\r\n", path);
-    }
+    sprintf(buff, "GET %s HTTP/1.0\r\n", name);
     socket_write(fd, buff, (long)strlen(buff));
 
 #ifdef DEBUG