From ed463404e94d294ac474e6dd6921fdda54415daf Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 24 Jun 2011 16:38:44 -0400 Subject: [PATCH] Clean up HTTP request header generation a little Use a list of headers rather than trying to printf every header that might exist. --- src/or/directory.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/or/directory.c b/src/or/directory.c index cd0b9b4de7..6055ae38dd 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -1117,11 +1117,11 @@ directory_send_command(dir_connection_t *conn, time_t if_modified_since) { char proxystring[256]; - char proxyauthstring[256]; char hoststring[128]; - char imsstring[RFC1123_TIME_LEN+32]; + smartlist_t *headers = smartlist_create(); char *url; char request[8192]; + char *header; const char *httpcommand = NULL; size_t len; @@ -1141,12 +1141,11 @@ directory_send_command(dir_connection_t *conn, } /* Format if-modified-since */ - if (!if_modified_since) { - imsstring[0] = '\0'; - } else { + if (if_modified_since) { char b[RFC1123_TIME_LEN+1]; format_rfc1123_time(b, if_modified_since); - tor_snprintf(imsstring, sizeof(imsstring), "\r\nIf-Modified-Since: %s", b); + tor_asprintf(&header, "If-Modified-Since: %s\r\n", b); + smartlist_add(headers, header); } /* come up with some proxy lines, if we're using one. */ @@ -1161,16 +1160,14 @@ directory_send_command(dir_connection_t *conn, log_warn(LD_BUG, "Encoding http authenticator failed"); } if (base64_authenticator) { - tor_snprintf(proxyauthstring, sizeof(proxyauthstring), - "\r\nProxy-Authorization: Basic %s", + tor_asprintf(&header, + "Proxy-Authorization: Basic %s\r\n", base64_authenticator); tor_free(base64_authenticator); - } else { - proxyauthstring[0] = 0; + smartlist_add(headers, header); } } else { proxystring[0] = 0; - proxyauthstring[0] = 0; } switch (purpose) { @@ -1287,26 +1284,26 @@ directory_send_command(dir_connection_t *conn, connection_write_to_buf(url, strlen(url), TO_CONN(conn)); tor_free(url); - if (!strcmp(httpcommand, "GET") && !payload) { - tor_snprintf(request, sizeof(request), - " HTTP/1.0\r\nHost: %s%s%s\r\n\r\n", - hoststring, - imsstring, - proxyauthstring); - } else { - tor_snprintf(request, sizeof(request), - " HTTP/1.0\r\nContent-Length: %lu\r\nHost: %s%s%s\r\n\r\n", - payload ? (unsigned long)payload_len : 0, - hoststring, - imsstring, - proxyauthstring); + if (!strcmp(httpcommand, "POST") || payload) { + tor_asprintf(&header, "Content-Length: %lu\r\n", + payload ? (unsigned long)payload_len : 0); + smartlist_add(headers, header); } + + header = smartlist_join_strings(headers, "", 0, NULL); + tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n", + hoststring, header); + tor_free(header); + connection_write_to_buf(request, strlen(request), TO_CONN(conn)); if (payload) { /* then send the payload afterwards too */ connection_write_to_buf(payload, payload_len, TO_CONN(conn)); } + + SMARTLIST_FOREACH(headers, char *, h, tor_free(h)); + smartlist_free(headers); } /** Parse an HTTP request string headers of the form