From bf22966f558ca95222c94706124a3a385f5aa0bf Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Wed, 4 Feb 2004 19:57:40 +0000 Subject: (strip_return_port): Patch from "alex" to strip the port from the host string and return the port. I cleaned up and added error handling to the code, but it's basically "alex"'s fix. (extract_http_url): Rewrote this function to remove all the sscanf() calls. It's much easier to just split on the path slash (if it's present) and then strip the user name/password and port from the host string. Less code, handles more cases! --- src/reqs.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/reqs.c b/src/reqs.c index f00fee9..3cab70c 100644 --- a/src/reqs.c +++ b/src/reqs.c @@ -1,4 +1,4 @@ -/* $Id: reqs.c,v 1.109 2004-01-26 19:11:51 rjkaes Exp $ +/* $Id: reqs.c,v 1.110 2004-02-04 19:57:40 rjkaes Exp $ * * This is where all the work in tinyproxy is actually done. Incoming * connections have a new child created for them. The child then @@ -217,6 +217,26 @@ strip_username_password(char* host) } } +/* + * Take a host string and if there is a port part, strip + * it off and set proper port variable i.e. for www.host.com:8001 + */ +static int +strip_return_port(char* host) +{ + char *ptr1; + int port; + + ptr1 = strchr(host, ':'); + if (ptr1 == NULL) + return 0; + + *ptr1++ = '\0'; + if (sscanf(ptr1, "%d", &port) != 1) /* one conversion required */ + return 0; + return port; +} + /* * Pull the information out of the URL line. This will handle both HTTP * and FTP (proxied) URLs. @@ -224,31 +244,33 @@ strip_username_password(char* host) static int extract_http_url(const char *url, struct request_s *request) { - request->host = (char*)safemalloc(strlen(url) + 1); - request->path = (char*)safemalloc(strlen(url) + 1); + char *p; + int len; + int port; + + /* Split the URL on the slash to separate host from path */ + p = strchr(url, '/'); + if (p != NULL) { + len = p - url; + request->host = safemalloc(len + 1); + memcpy(request->host, url, len); + request->host[len] = '\0'; + request->path = safestrdup(p); + } else { + request->host = safestrdup(url); + request->path = safestrdup("/"); + } if (!request->host || !request->path) goto ERROR_EXIT; - if (sscanf - (url, "%[^:/]:%hu%s", request->host, &request->port, - request->path) == 3) ; - else if (sscanf(url, "%[^/]%s", request->host, request->path) == 2) - request->port = HTTP_PORT; - else if (sscanf(url, "%[^:/]:%hu", request->host, &request->port) - == 2) - strcpy(request->path, "/"); - else if (sscanf(url, "%[^/]", request->host) == 1) { - request->port = HTTP_PORT; - strcpy(request->path, "/"); - } else { - log_message(LOG_ERR, "extract_http_url: Can't parse URL."); - goto ERROR_EXIT; - } - /* Remove the username/password if they're present */ strip_username_password(request->host); + /* Find a proper port in www.site.com:8001 URLs */ + port = strip_return_port(request->host); + request->port = (port != 0) ? port : HTTP_PORT; + return 0; ERROR_EXIT: -- cgit v1.2.3