aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2016-04-25 11:21:29 -0700
committerGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2016-04-25 11:21:29 -0700
commitd64be98091c66f81c30de9c2a32825ee8eda97fd (patch)
treea882a87a75fdc48a3612598dfffea060d1598f3a /src
parent1c35498ed6d9cfead6482f6a6a93db2115a74d3f (diff)
parent7151af94659f935c5e1f45b117caf7b33b5f2471 (diff)
Merge pull request #6105 from miselin/http-parser-allow-more-newlines
Allow grpc_http_parser to optionally accept a wider range of line endings
Diffstat (limited to 'src')
-rw-r--r--src/core/lib/http/parser.c35
-rw-r--r--src/core/lib/http/parser.h1
2 files changed, 30 insertions, 6 deletions
diff --git a/src/core/lib/http/parser.c b/src/core/lib/http/parser.c
index 921c772453..a7efb5e73e 100644
--- a/src/core/lib/http/parser.c
+++ b/src/core/lib/http/parser.c
@@ -172,8 +172,8 @@ static int add_header(grpc_http_parser *parser) {
while (cur != end && (*cur == ' ' || *cur == '\t')) {
cur++;
}
- GPR_ASSERT(end - cur >= 2);
- hdr.value = buf2str(cur, (size_t)(end - cur) - 2);
+ GPR_ASSERT((size_t)(end - cur) >= parser->cur_line_end_length);
+ hdr.value = buf2str(cur, (size_t)(end - cur) - parser->cur_line_end_length);
if (parser->type == GRPC_HTTP_RESPONSE) {
hdr_count = &parser->http.response.hdr_count;
@@ -208,7 +208,7 @@ static int finish_line(grpc_http_parser *parser) {
parser->state = GRPC_HTTP_HEADERS;
break;
case GRPC_HTTP_HEADERS:
- if (parser->cur_line_length == 2) {
+ if (parser->cur_line_length == parser->cur_line_end_length) {
parser->state = GRPC_HTTP_BODY;
break;
}
@@ -248,6 +248,30 @@ static int addbyte_body(grpc_http_parser *parser, uint8_t byte) {
return 1;
}
+static int check_line(grpc_http_parser *parser) {
+ if (parser->cur_line_length >= 2 &&
+ parser->cur_line[parser->cur_line_length - 2] == '\r' &&
+ parser->cur_line[parser->cur_line_length - 1] == '\n') {
+ return 1;
+ }
+
+ // HTTP request with \n\r line termiantors.
+ else if (parser->cur_line_length >= 2 &&
+ parser->cur_line[parser->cur_line_length - 2] == '\n' &&
+ parser->cur_line[parser->cur_line_length - 1] == '\r') {
+ return 1;
+ }
+
+ // HTTP request with only \n line terminators.
+ else if (parser->cur_line_length >= 1 &&
+ parser->cur_line[parser->cur_line_length - 1] == '\n') {
+ parser->cur_line_end_length = 1;
+ return 1;
+ }
+
+ return 0;
+}
+
static int addbyte(grpc_http_parser *parser, uint8_t byte) {
switch (parser->state) {
case GRPC_HTTP_FIRST_LINE:
@@ -260,9 +284,7 @@ static int addbyte(grpc_http_parser *parser, uint8_t byte) {
}
parser->cur_line[parser->cur_line_length] = byte;
parser->cur_line_length++;
- if (parser->cur_line_length >= 2 &&
- parser->cur_line[parser->cur_line_length - 2] == '\r' &&
- parser->cur_line[parser->cur_line_length - 1] == '\n') {
+ if (check_line(parser)) {
return finish_line(parser);
} else {
return 1;
@@ -278,6 +300,7 @@ void grpc_http_parser_init(grpc_http_parser *parser) {
memset(parser, 0, sizeof(*parser));
parser->state = GRPC_HTTP_FIRST_LINE;
parser->type = GRPC_HTTP_UNKNOWN;
+ parser->cur_line_end_length = 2;
}
void grpc_http_parser_destroy(grpc_http_parser *parser) {
diff --git a/src/core/lib/http/parser.h b/src/core/lib/http/parser.h
index 42fa5181b8..536637e9a2 100644
--- a/src/core/lib/http/parser.h
+++ b/src/core/lib/http/parser.h
@@ -105,6 +105,7 @@ typedef struct {
uint8_t cur_line[GRPC_HTTP_PARSER_MAX_HEADER_LENGTH];
size_t cur_line_length;
+ size_t cur_line_end_length;
} grpc_http_parser;
void grpc_http_parser_init(grpc_http_parser *parser);