Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit 091ebb8

Browse files
kolbyjackindutny
authored andcommitted
src: simple Connection header multi-value parsing
Reviewed-By: Fedor Indutny <[email protected]> PR-URL: #100
1 parent 959f4cb commit 091ebb8

3 files changed

Lines changed: 129 additions & 8 deletions

File tree

‎http_parser.c‎

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,16 @@ enum header_states
369369
, h_upgrade
370370

371371
, h_matching_transfer_encoding_chunked
372+
, h_matching_connection_token_start
372373
, h_matching_connection_keep_alive
373374
, h_matching_connection_close
375+
, h_matching_connection_upgrade
376+
, h_matching_connection_token
374377

375378
, h_transfer_encoding_chunked
376379
, h_connection_keep_alive
377380
, h_connection_close
381+
, h_connection_upgrade
378382
};
379383

380384
enum http_host_state
@@ -406,6 +410,8 @@ enum http_host_state
406410
(c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
407411
(c) == '$' || (c) == ',')
408412

413+
#define STRICT_TOKEN(c) (tokens[(unsigned char)c])
414+
409415
#if HTTP_PARSER_STRICT
410416
#define TOKEN(c) (tokens[(unsigned char)c])
411417
#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c))
@@ -1481,11 +1487,17 @@ size_t http_parser_execute (http_parser *parser,
14811487
/* looking for 'Connection: close' */
14821488
} else if (c == 'c') {
14831489
parser->header_state = h_matching_connection_close;
1490+
} else if (c == 'u') {
1491+
parser->header_state = h_matching_connection_upgrade;
14841492
} else {
1485-
parser->header_state = h_general;
1493+
parser->header_state = h_matching_connection_token;
14861494
}
14871495
break;
14881496

1497+
/* Multi-value `Connection` header */
1498+
case h_matching_connection_token_start:
1499+
break;
1500+
14891501
default:
14901502
parser->header_state = h_general;
14911503
break;
@@ -1585,12 +1597,28 @@ size_t http_parser_execute (http_parser *parser,
15851597
}
15861598
break;
15871599

1600+
case h_matching_connection_token_start:
1601+
/* looking for 'Connection: keep-alive' */
1602+
if (c == 'k') {
1603+
h_state = h_matching_connection_keep_alive;
1604+
/* looking for 'Connection: close' */
1605+
} else if (c == 'c') {
1606+
h_state = h_matching_connection_close;
1607+
} else if (c == 'u') {
1608+
h_state = h_matching_connection_upgrade;
1609+
} else if (STRICT_TOKEN(c)) {
1610+
h_state = h_matching_connection_token;
1611+
} else {
1612+
h_state = h_general;
1613+
}
1614+
break;
1615+
15881616
/* looking for 'Connection: keep-alive' */
15891617
case h_matching_connection_keep_alive:
15901618
parser->index++;
15911619
if (parser->index > sizeof(KEEP_ALIVE)-1
15921620
|| c != KEEP_ALIVE[parser->index]) {
1593-
h_state = h_general;
1621+
h_state = h_matching_connection_token;
15941622
} else if (parser->index == sizeof(KEEP_ALIVE)-2) {
15951623
h_state = h_connection_keep_alive;
15961624
}
@@ -1600,16 +1628,50 @@ size_t http_parser_execute (http_parser *parser,
16001628
case h_matching_connection_close:
16011629
parser->index++;
16021630
if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) {
1603-
h_state = h_general;
1631+
h_state = h_matching_connection_token;
16041632
} else if (parser->index == sizeof(CLOSE)-2) {
16051633
h_state = h_connection_close;
16061634
}
16071635
break;
16081636

1637+
/* looking for 'Connection: upgrade' */
1638+
case h_matching_connection_upgrade:
1639+
parser->index++;
1640+
if (parser->index > sizeof(UPGRADE) - 1 ||
1641+
c != UPGRADE[parser->index]) {
1642+
h_state = h_matching_connection_token;
1643+
} else if (parser->index == sizeof(UPGRADE)-2) {
1644+
h_state = h_connection_upgrade;
1645+
}
1646+
break;
1647+
1648+
case h_matching_connection_token:
1649+
if (ch == ',') {
1650+
h_state = h_matching_connection_token_start;
1651+
parser->index = 0;
1652+
}
1653+
break;
1654+
16091655
case h_transfer_encoding_chunked:
1656+
if (ch != ' ') h_state = h_general;
1657+
break;
1658+
16101659
case h_connection_keep_alive:
16111660
case h_connection_close:
1612-
if (ch != ' ') h_state = h_general;
1661+
case h_connection_upgrade:
1662+
if (ch == ',') {
1663+
if (h_state == h_connection_keep_alive) {
1664+
parser->flags |= F_CONNECTION_KEEP_ALIVE;
1665+
} else if (h_state == h_connection_close) {
1666+
parser->flags |= F_CONNECTION_CLOSE;
1667+
} else if (h_state == h_connection_upgrade) {
1668+
parser->flags |= F_CONNECTION_UPGRADE;
1669+
}
1670+
h_state = h_matching_connection_token_start;
1671+
parser->index = 0;
1672+
} else if (ch != ' ') {
1673+
h_state = h_matching_connection_token;
1674+
}
16131675
break;
16141676

16151677
default:
@@ -1653,6 +1715,9 @@ size_t http_parser_execute (http_parser *parser,
16531715
case h_transfer_encoding_chunked:
16541716
parser->flags |= F_CHUNKED;
16551717
break;
1718+
case h_connection_upgrade:
1719+
parser->flags |= F_CONNECTION_UPGRADE;
1720+
break;
16561721
default:
16571722
break;
16581723
}
@@ -1674,6 +1739,23 @@ size_t http_parser_execute (http_parser *parser,
16741739
UPDATE_STATE(s_header_value_discard_ws);
16751740
break;
16761741
} else {
1742+
switch (parser->header_state) {
1743+
case h_connection_keep_alive:
1744+
parser->flags |= F_CONNECTION_KEEP_ALIVE;
1745+
break;
1746+
case h_connection_close:
1747+
parser->flags |= F_CONNECTION_CLOSE;
1748+
break;
1749+
case h_connection_upgrade:
1750+
parser->flags |= F_CONNECTION_UPGRADE;
1751+
break;
1752+
case h_transfer_encoding_chunked:
1753+
parser->flags |= F_CHUNKED;
1754+
break;
1755+
default:
1756+
break;
1757+
}
1758+
16771759
/* header value was empty */
16781760
MARK(header_value);
16791761
UPDATE_STATE(s_header_field_start);
@@ -1697,7 +1779,9 @@ size_t http_parser_execute (http_parser *parser,
16971779

16981780
/* Set this here so that on_headers_complete() callbacks can see it */
16991781
parser->upgrade =
1700-
(parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT);
1782+
((parser->flags & (F_UPGRADE | F_CONNECTION_UPGRADE)) ==
1783+
(F_UPGRADE | F_CONNECTION_UPGRADE) ||
1784+
parser->method == HTTP_CONNECT);
17011785

17021786
/* Here we call the headers_complete callback. This is somewhat
17031787
* different than other callbacks because if the user returns 1, we

‎http_parser.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ enum flags
136136
{ F_CHUNKED = 1 << 0
137137
, F_CONNECTION_KEEP_ALIVE = 1 << 1
138138
, F_CONNECTION_CLOSE = 1 << 2
139-
, F_TRAILING = 1 << 3
140-
, F_UPGRADE = 1 << 4
141-
, F_SKIPBODY = 1 << 5
139+
, F_CONNECTION_UPGRADE = 1 << 3
140+
, F_TRAILING = 1 << 4
141+
, F_UPGRADE = 1 << 5
142+
, F_SKIPBODY = 1 << 6
142143
};
143144

144145

‎test.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,42 @@ const struct message requests[] =
950950
,.body= ""
951951
}
952952

953+
#define CONNECTION_MULTI 35
954+
, {.name = "multiple connection header values with folding"
955+
,.type= HTTP_REQUEST
956+
,.raw= "GET /demo HTTP/1.1\r\n"
957+
"Host: example.com\r\n"
958+
"Connection: Something,\r\n"
959+
" Upgrade, ,Keep-Alive\r\n"
960+
"Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
961+
"Sec-WebSocket-Protocol: sample\r\n"
962+
"Upgrade: WebSocket\r\n"
963+
"Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
964+
"Origin: http://example.com\r\n"
965+
"\r\n"
966+
"Hot diggity dogg"
967+
,.should_keep_alive= TRUE
968+
,.message_complete_on_eof= FALSE
969+
,.http_major= 1
970+
,.http_minor= 1
971+
,.method= HTTP_GET
972+
,.query_string= ""
973+
,.fragment= ""
974+
,.request_path= "/demo"
975+
,.request_url= "/demo"
976+
,.num_headers= 7
977+
,.upgrade="Hot diggity dogg"
978+
,.headers= { { "Host", "example.com" }
979+
, { "Connection", "Something, Upgrade, ,Keep-Alive" }
980+
, { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
981+
, { "Sec-WebSocket-Protocol", "sample" }
982+
, { "Upgrade", "WebSocket" }
983+
, { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
984+
, { "Origin", "http://example.com" }
985+
}
986+
,.body= ""
987+
}
988+
953989

954990
, {.name= NULL } /* sentinel */
955991
};

0 commit comments

Comments
 (0)