[xiph-commits] r3128 - liboggplay/trunk/src/liboggplay

shans at svn.annodex.net shans at svn.annodex.net
Thu Jun 28 08:23:24 PDT 2007


Author: shans
Date: 2007-06-28 08:23:23 -0700 (Thu, 28 Jun 2007)
New Revision: 3128

Modified:
   liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
Log:
better implementation of oggplay_hostname_and_path



Modified: liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c
===================================================================
--- liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 15:02:19 UTC (rev 3127)
+++ liboggplay/trunk/src/liboggplay/oggplay_tcp_reader.c	2007-06-28 15:23:23 UTC (rev 3128)
@@ -144,82 +144,76 @@
   return sock;
 }
 
+/*
+ * this function guarantees it will return malloced versions of host and 
+ * path
+ */
 void
 oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
                                   char **host, int *port, char **path) {
 
-  char                * colon;
-  int                   len;
-  int                   has_http;
 
-  /* no proxy: split the location into host, port and path. */
-  if (proxy == NULL) {
-    char *_location;
-    len = strlen(location);
-    _location = (char*)malloc(len + 2);
-    strcpy(_location, location);
+  char  * colon;
+  char  * slash;
+  char  * end_of_host;
 
-    /* find location of slash and possibly colon (colon -> port) */
-    if (strncmp(_location, "http://", 7) == 0) {
-      *path = strchr(_location + 7, '/');
-      colon = strchr(_location + 7, ':');
-      has_http = 1;
-    } else {
-      *path = strchr(_location, '/');
-      colon = strchr(_location, ':');
-      has_http = 0;
-    }
+  /* if we have a proxy installed this is all dead simple */
+  if (proxy != NULL) {
+    *host = strdup(proxy);
+    *port = proxy_port;
+    *path = strdup(location);
+    return;
+  }
 
+  /* find start_pos */
+  if (strncmp(location, "http://", 7) == 0) {
+    location += 7;
+  }
+
+  colon = strchr(location, ':');
+  slash = strchr(location, '/');
+
+  /*
+   * if both are null, then just set the simple defaults and return
+   */
+  if (colon == NULL && slash == NULL) {
+    *host = strdup(location);
     *port = 80;
-    /* if there's a colon before the path starts, then override default
-     * port (i.e. if we have http://example.com:22/my/path but not 
-     * http://example.com/my:22/path */
-    if (colon != NULL) {
-      if (*path == NULL || colon < *path) {
-        *port = (int)strtol(colon+1, NULL, 10);
-        if (*path != NULL) {
-          /* move path back a bit so that we copy the correct amount
-           * into host later */
-          strcpy(colon, location + (*path - _location));
-          *path = colon;
-        } else {
-          *colon = '\0';
-        }
-      }
-    }
-    
-    if (*path == NULL) {
-      _location[len] = '/';
-      _location[len + 1] = '\0';
-      *path = _location + len;
-    }
-    
-    if (has_http) {
-      *host = (char*)malloc((*path - _location) - 6);
-      strncpy(*host, _location + 7, (*path - _location) - 7);
-      (*host)[(*path - _location) - 7] = '\0';
-    } else {
-      *host = (char*)malloc((*path - _location) + 1);
-      strncpy(*host, _location, *path - _location);
-      (*host)[*path - _location] = '\0';
-    }
+    *path = strdup("/");
+    return;
+  }
 
-    /* create a malloced copy of the path if not null, otherwise
-     * create a path of "/"  */    
-    if (*path != NULL) {
-      char *tmp = *path;
-      *path = strdup(tmp);
-    }
-    
-    free(_location);
+  /* 
+   * if there's a slash and it's before colon, there's no port.  Hence, after
+   * this code, the only time that there's a port is when colon is non-NULL
+   */
+  if (slash != NULL && colon > slash) {
+    colon = NULL;
   }
-  else
-  {
-    *host = strdup(proxy);
-    *port = proxy_port;
-    *path = strdup(location);
+
+  /*
+   * we might as well extract the port now.  We can also work out where
+   * the end of the hostname is, as it's either the colon (if there's a port)
+   * or the slash (if there's no port)
+   */
+  if (colon != NULL) {
+    *port = (int)strtol(colon+1, NULL, 10);
+    end_of_host = colon;
+  } else {
+    *port = 80;
+    end_of_host = slash;
   }
 
+  *host = strdup(location);
+  (*host)[end_of_host - location] = '\0';
+
+  if (slash == NULL) {
+    *path = strdup("/");
+    return;
+  }
+
+  *path = strdup(slash);
+
 }
 
 OggPlayErrorCode
@@ -320,6 +314,7 @@
     "Connection: Keep-Alive\n\n", path, host);
 
   he = gethostbyname(host);
+
   free(host);
   free(path);
   



More information about the commits mailing list