summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Brown <ben@427.org.uk>2013-02-19 20:03:57 +0000
committerBen Brown <ben@427.org.uk>2013-02-19 20:03:57 +0000
commit23181d6f4f09ebcfa7a7a071252a36da7db3193f (patch)
tree7c41cd6c185cd60c859ddeeb18081e835cd4aba6
parentb720a89d5524a964070bc3c9e9fb0053de8e298b (diff)
downloadweighttp-23181d6f4f09ebcfa7a7a071252a36da7db3193f.tar.gz
weighttp-23181d6f4f09ebcfa7a7a071252a36da7db3193f.zip
Corrected Host header handling (fixes #2477)
-rw-r--r--src/weighttp.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/weighttp.c b/src/weighttp.c
index 77504f1..a7744b5 100644
--- a/src/weighttp.c
+++ b/src/weighttp.c
@@ -76,9 +76,11 @@ static char *forge_request(char *url, char keep_alive, char **host, uint16_t *po
uint32_t len;
uint8_t i;
uint8_t have_user_agent;
+ char *header_host;
*host = NULL;
*port = 0;
+ header_host = NULL;
if (strncmp(url, "http://", 7) == 0)
url += 7;
@@ -137,6 +139,25 @@ static char *forge_request(char *url, char keep_alive, char **host, uint16_t *po
have_user_agent = 0;
for (i = 0; i < headers_num; i++) {
+ if (strncmp(headers[i], "Host:", sizeof("Host:")-1) == 0) {
+ if (header_host) {
+ W_ERROR("%s", "Duplicate Host header");
+ free(*host);
+ return NULL;
+ }
+ header_host = headers[i] + 5;
+ if (*header_host == ' ')
+ header_host++;
+
+ if (strlen(header_host) == 0) {
+ W_ERROR("%s", "Invalid Host header");
+ free(*host);
+ return NULL;
+ }
+
+ len += strlen(header_host);
+ continue;
+ }
len += strlen(headers[i]) + strlen("\r\n");
if (strncmp(headers[i], "User-Agent: ", sizeof("User-Agent: ")-1) == 0)
have_user_agent = 1;
@@ -150,9 +171,13 @@ static char *forge_request(char *url, char keep_alive, char **host, uint16_t *po
strcpy(req, "GET ");
strcat(req, url);
strcat(req, " HTTP/1.1\r\nHost: ");
- strcat(req, *host);
- if (*port != 80)
- sprintf(req + strlen(req), ":%"PRIu16, *port);
+ if (header_host) {
+ strcat(req, header_host);
+ } else {
+ strcat(req, *host);
+ if (*port != 80)
+ sprintf(req + strlen(req), ":%"PRIu16, *port);
+ }
strcat(req, "\r\n");
@@ -160,6 +185,8 @@ static char *forge_request(char *url, char keep_alive, char **host, uint16_t *po
sprintf(req + strlen(req), "User-Agent: weighttp/" VERSION "\r\n");
for (i = 0; i < headers_num; i++) {
+ if (strncmp(headers[i], "Host:", sizeof("Host:")-1) == 0)
+ continue;
strcat(req, headers[i]);
strcat(req, "\r\n");
}