Skip to content

Commit 1b1029e

Browse files
author
Eric Wong
committed
http: ignore Host: continuation lines with absolute URIs
This probably doesn't affect anyone with HTTP/1.1, but future versions of HTTP will use absolute URIs and maybe we'll eventually get clients that (mistakenly) send us Host: headers along with absolute URIs.
1 parent 2213d73 commit 1b1029e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

ext/unicorn_http/unicorn_http.rl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct http_parser {
3838
size_t field_len; /* only used during header processing */
3939
size_t dest_offset; /* only used during body processing */
4040
} s;
41-
VALUE cont;
41+
VALUE cont; /* Qfalse: unset, Qnil: ignored header, T_STRING: append */
4242
union {
4343
off_t content;
4444
off_t chunk;
@@ -139,9 +139,12 @@ static void write_cont_value(struct http_parser *hp,
139139
{
140140
char *vptr;
141141

142-
if (!hp->cont)
143-
rb_raise(eHttpParserError, "invalid continuation line");
142+
if (hp->cont == Qfalse)
143+
rb_raise(eHttpParserError, "invalid continuation line");
144+
if (NIL_P(hp->cont))
145+
return; /* we're ignoring this header (probably Host:) */
144146

147+
assert(TYPE(hp->cont) == T_STRING && "continuation line is not a string");
145148
assert(hp->mark > 0 && "impossible continuation line offset");
146149

147150
if (LEN(mark, p) == 0)
@@ -200,6 +203,7 @@ static void write_value(VALUE req, struct http_parser *hp,
200203
* ignored, absolute URLs in REQUEST_URI take precedence over
201204
* the Host: header (ref: rfc 2616, section 5.2.1)
202205
*/
206+
hp->cont = Qnil;
203207
} else {
204208
rb_str_buf_cat(e, ",", 1);
205209
hp->cont = rb_str_buf_append(e, v);
@@ -333,6 +337,7 @@ static void http_parser_init(struct http_parser *hp)
333337
{
334338
int cs = 0;
335339
memset(hp, 0, sizeof(struct http_parser));
340+
hp->cont = Qfalse; /* zero on MRI, should be optimized away by above */
336341
%% write init;
337342
hp->cs = cs;
338343
}

test/unit/test_http_parser.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ def test_continuation_eats_scattered_leading_spaces
180180
assert_equal 'hi y x ASDF', req['HTTP_X_ASDF']
181181
end
182182

183+
def test_continuation_with_absolute_uri_and_ignored_host_header
184+
parser = HttpParser.new
185+
header = "GET http://example.com/ HTTP/1.1\r\n" \
186+
"Host: \r\n" \
187+
" YHBT.net\r\n" \
188+
"\r\n"
189+
req = {}
190+
assert_equal req, parser.headers(req, header)
191+
assert_equal 'example.com', req['HTTP_HOST']
192+
end
193+
183194
# this may seem to be testing more of an implementation detail, but
184195
# it also helps ensure we're safe in the presence of multiple parsers
185196
# in case we ever go multithreaded/evented...

0 commit comments

Comments
 (0)