Clean up HTTP request header generation a little

Use a list of headers rather than trying to printf every header that
might exist.
This commit is contained in:
Nick Mathewson 2011-06-24 16:38:44 -04:00
parent 1f4b6944c0
commit ed463404e9

View File

@ -1117,11 +1117,11 @@ directory_send_command(dir_connection_t *conn,
time_t if_modified_since) time_t if_modified_since)
{ {
char proxystring[256]; char proxystring[256];
char proxyauthstring[256];
char hoststring[128]; char hoststring[128];
char imsstring[RFC1123_TIME_LEN+32]; smartlist_t *headers = smartlist_create();
char *url; char *url;
char request[8192]; char request[8192];
char *header;
const char *httpcommand = NULL; const char *httpcommand = NULL;
size_t len; size_t len;
@ -1141,12 +1141,11 @@ directory_send_command(dir_connection_t *conn,
} }
/* Format if-modified-since */ /* Format if-modified-since */
if (!if_modified_since) { if (if_modified_since) {
imsstring[0] = '\0';
} else {
char b[RFC1123_TIME_LEN+1]; char b[RFC1123_TIME_LEN+1];
format_rfc1123_time(b, if_modified_since); 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. */ /* 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"); log_warn(LD_BUG, "Encoding http authenticator failed");
} }
if (base64_authenticator) { if (base64_authenticator) {
tor_snprintf(proxyauthstring, sizeof(proxyauthstring), tor_asprintf(&header,
"\r\nProxy-Authorization: Basic %s", "Proxy-Authorization: Basic %s\r\n",
base64_authenticator); base64_authenticator);
tor_free(base64_authenticator); tor_free(base64_authenticator);
} else { smartlist_add(headers, header);
proxyauthstring[0] = 0;
} }
} else { } else {
proxystring[0] = 0; proxystring[0] = 0;
proxyauthstring[0] = 0;
} }
switch (purpose) { switch (purpose) {
@ -1287,26 +1284,26 @@ directory_send_command(dir_connection_t *conn,
connection_write_to_buf(url, strlen(url), TO_CONN(conn)); connection_write_to_buf(url, strlen(url), TO_CONN(conn));
tor_free(url); tor_free(url);
if (!strcmp(httpcommand, "GET") && !payload) { if (!strcmp(httpcommand, "POST") || payload) {
tor_snprintf(request, sizeof(request), tor_asprintf(&header, "Content-Length: %lu\r\n",
" HTTP/1.0\r\nHost: %s%s%s\r\n\r\n", payload ? (unsigned long)payload_len : 0);
hoststring, smartlist_add(headers, header);
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);
} }
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)); connection_write_to_buf(request, strlen(request), TO_CONN(conn));
if (payload) { if (payload) {
/* then send the payload afterwards too */ /* then send the payload afterwards too */
connection_write_to_buf(payload, payload_len, TO_CONN(conn)); 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 <b>headers</b> of the form /** Parse an HTTP request string <b>headers</b> of the form