From 38feb8d7b953ad1b2e4e2ff23d6b8113a6b1bcff Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Fri, 17 Aug 2012 21:02:24 -0400 Subject: [PATCH] Better --resolve-url handling Chop any trailing '/' char, be more careful about handling incoming large HTTP responses, print the HTTP request and response in --verbose --verbose mode. --- client/http_resolve_host.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/client/http_resolve_host.c b/client/http_resolve_host.c index 8312fbce..4eaa5393 100644 --- a/client/http_resolve_host.c +++ b/client/http_resolve_host.c @@ -124,6 +124,9 @@ try_url(struct url *url, fko_cli_options_t *options) freeaddrinfo(result); + if(options->verbose > 1) + printf("\nHTTP request: %s\n", http_buf); + res = send(sock, http_buf, http_buf_len, 0); if(res < 0) @@ -143,6 +146,8 @@ try_url(struct url *url, fko_cli_options_t *options) memset(http_buf, 0x0, sizeof(http_buf)); bytes_read = recv(sock, http_buf, sizeof(http_buf), 0); if ( bytes_read > 0 ) { + if(position + bytes_read >= HTTP_MAX_RESPONSE_LEN) + break; memcpy(&http_response[position], http_buf, bytes_read); position += bytes_read; } @@ -157,6 +162,9 @@ try_url(struct url *url, fko_cli_options_t *options) close(sock); #endif + if(options->verbose > 1) + printf("\nHTTP response: %s\n", http_response); + /* Move to the end of the HTTP header and to the start of the content. */ ndx = strstr(http_response, "\r\n\r\n"); @@ -191,9 +199,6 @@ try_url(struct url *url, fko_cli_options_t *options) { strlcpy(options->allow_ip_str, ndx, MAX_IPV4_STR_LEN); - if(options->verbose > 1) - printf("\nHTTP response: %s\n", http_response); - if(options->verbose) printf("\n[+] Resolved external IP (via http://%s%s) as: %s\n", url->host, @@ -256,6 +261,11 @@ parse_url(char *res_url, struct url* url) tlen_offset = 0; } + /* Get rid of any trailing slash + */ + if(res_url[strlen(res_url)-1] == '/') + res_url[strlen(res_url)-1] = '\0'; + e_ndx = strchr(s_ndx, '/'); if(e_ndx == NULL) tlen = strlen(s_ndx)+1; @@ -282,7 +292,11 @@ parse_url(char *res_url, struct url* url) strlcpy(url->path, e_ndx, MAX_URL_PATH_LEN); } else - *(url->path) = '\0'; + { + /* default to "GET /" if there isn't a more specific URL + */ + strlcpy(url->path, "/", MAX_URL_PATH_LEN); + } return(0); }