3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/http/parser.h"
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
29 #include "src/core/lib/gpr/useful.h"
31 grpc_core::TraceFlag grpc_http1_trace(false, "http1");
33 static char* buf2str(void* buffer, size_t length) {
34 char* out = static_cast<char*>(gpr_malloc(length + 1));
35 memcpy(out, buffer, length);
40 static grpc_error* handle_response_line(grpc_http_parser* parser) {
41 uint8_t* beg = parser->cur_line;
43 uint8_t* end = beg + parser->cur_line_length;
45 if (cur == end || *cur++ != 'H') {
46 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'H'");
48 if (cur == end || *cur++ != 'T') {
49 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
51 if (cur == end || *cur++ != 'T') {
52 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
54 if (cur == end || *cur++ != 'P') {
55 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'P'");
57 if (cur == end || *cur++ != '/') {
58 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '/'");
60 if (cur == end || *cur++ != '1') {
61 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '1'");
63 if (cur == end || *cur++ != '.') {
64 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '.'");
66 if (cur == end || *cur < '0' || *cur++ > '1') {
67 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
68 "Expected HTTP/1.0 or HTTP/1.1");
70 if (cur == end || *cur++ != ' ') {
71 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected ' '");
73 if (cur == end || *cur < '1' || *cur++ > '9') {
74 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
76 if (cur == end || *cur < '0' || *cur++ > '9') {
77 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
79 if (cur == end || *cur < '0' || *cur++ > '9') {
80 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
82 parser->http.response->status =
83 (cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0');
84 if (cur == end || *cur++ != ' ') {
85 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected ' '");
88 /* we don't really care about the status code message */
90 return GRPC_ERROR_NONE;
93 static grpc_error* handle_request_line(grpc_http_parser* parser) {
94 uint8_t* beg = parser->cur_line;
96 uint8_t* end = beg + parser->cur_line_length;
97 uint8_t vers_major = 0;
98 uint8_t vers_minor = 0;
100 while (cur != end && *cur++ != ' ') {
103 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
104 "No method on HTTP request line");
106 parser->http.request->method =
107 buf2str(beg, static_cast<size_t>(cur - beg - 1));
110 while (cur != end && *cur++ != ' ') {
113 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("No path on HTTP request line");
115 parser->http.request->path = buf2str(beg, static_cast<size_t>(cur - beg - 1));
117 if (cur == end || *cur++ != 'H') {
118 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'H'");
120 if (cur == end || *cur++ != 'T') {
121 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
123 if (cur == end || *cur++ != 'T') {
124 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
126 if (cur == end || *cur++ != 'P') {
127 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'P'");
129 if (cur == end || *cur++ != '/') {
130 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '/'");
132 vers_major = static_cast<uint8_t>(*cur++ - '1' + 1);
135 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
136 "End of line in HTTP version string");
138 vers_minor = static_cast<uint8_t>(*cur++ - '1' + 1);
140 if (vers_major == 1) {
141 if (vers_minor == 0) {
142 parser->http.request->version = GRPC_HTTP_HTTP10;
143 } else if (vers_minor == 1) {
144 parser->http.request->version = GRPC_HTTP_HTTP11;
146 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
147 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
149 } else if (vers_major == 2) {
150 if (vers_minor == 0) {
151 parser->http.request->version = GRPC_HTTP_HTTP20;
153 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
154 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
157 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
158 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
161 return GRPC_ERROR_NONE;
164 static grpc_error* handle_first_line(grpc_http_parser* parser) {
165 switch (parser->type) {
166 case GRPC_HTTP_REQUEST:
167 return handle_request_line(parser);
168 case GRPC_HTTP_RESPONSE:
169 return handle_response_line(parser);
171 GPR_UNREACHABLE_CODE(
172 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here"));
175 static grpc_error* add_header(grpc_http_parser* parser) {
176 uint8_t* beg = parser->cur_line;
178 uint8_t* end = beg + parser->cur_line_length;
179 size_t* hdr_count = nullptr;
180 grpc_http_header** hdrs = nullptr;
181 grpc_http_header hdr = {nullptr, nullptr};
182 grpc_error* error = GRPC_ERROR_NONE;
184 GPR_ASSERT(cur != end);
186 if (*cur == ' ' || *cur == '\t') {
187 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
188 "Continued header lines not supported yet");
192 while (cur != end && *cur != ':') {
196 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
197 "Didn't find ':' in header string");
200 GPR_ASSERT(cur >= beg);
201 hdr.key = buf2str(beg, static_cast<size_t>(cur - beg));
204 while (cur != end && (*cur == ' ' || *cur == '\t')) {
207 GPR_ASSERT((size_t)(end - cur) >= parser->cur_line_end_length);
209 cur, static_cast<size_t>(end - cur) - parser->cur_line_end_length);
211 switch (parser->type) {
212 case GRPC_HTTP_RESPONSE:
213 hdr_count = &parser->http.response->hdr_count;
214 hdrs = &parser->http.response->hdrs;
216 case GRPC_HTTP_REQUEST:
217 hdr_count = &parser->http.request->hdr_count;
218 hdrs = &parser->http.request->hdrs;
222 if (*hdr_count == parser->hdr_capacity) {
223 parser->hdr_capacity =
224 GPR_MAX(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2);
225 *hdrs = static_cast<grpc_http_header*>(
226 gpr_realloc(*hdrs, parser->hdr_capacity * sizeof(**hdrs)));
228 (*hdrs)[(*hdr_count)++] = hdr;
231 if (error != GRPC_ERROR_NONE) {
238 static grpc_error* finish_line(grpc_http_parser* parser,
239 bool* found_body_start) {
241 switch (parser->state) {
242 case GRPC_HTTP_FIRST_LINE:
243 err = handle_first_line(parser);
244 if (err != GRPC_ERROR_NONE) return err;
245 parser->state = GRPC_HTTP_HEADERS;
247 case GRPC_HTTP_HEADERS:
248 if (parser->cur_line_length == parser->cur_line_end_length) {
249 parser->state = GRPC_HTTP_BODY;
250 *found_body_start = true;
253 err = add_header(parser);
254 if (err != GRPC_ERROR_NONE) {
259 GPR_UNREACHABLE_CODE(return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
260 "Should never reach here"));
263 parser->cur_line_length = 0;
264 return GRPC_ERROR_NONE;
267 static grpc_error* addbyte_body(grpc_http_parser* parser, uint8_t byte) {
268 size_t* body_length = nullptr;
269 char** body = nullptr;
271 if (parser->type == GRPC_HTTP_RESPONSE) {
272 body_length = &parser->http.response->body_length;
273 body = &parser->http.response->body;
274 } else if (parser->type == GRPC_HTTP_REQUEST) {
275 body_length = &parser->http.request->body_length;
276 body = &parser->http.request->body;
278 GPR_UNREACHABLE_CODE(
279 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here"));
282 if (*body_length == parser->body_capacity) {
283 parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2);
285 static_cast<char*>(gpr_realloc((void*)*body, parser->body_capacity));
287 (*body)[*body_length] = static_cast<char>(byte);
290 return GRPC_ERROR_NONE;
293 static bool check_line(grpc_http_parser* parser) {
294 if (parser->cur_line_length >= 2 &&
295 parser->cur_line[parser->cur_line_length - 2] == '\r' &&
296 parser->cur_line[parser->cur_line_length - 1] == '\n') {
300 // HTTP request with \n\r line termiantors.
301 else if (parser->cur_line_length >= 2 &&
302 parser->cur_line[parser->cur_line_length - 2] == '\n' &&
303 parser->cur_line[parser->cur_line_length - 1] == '\r') {
307 // HTTP request with only \n line terminators.
308 else if (parser->cur_line_length >= 1 &&
309 parser->cur_line[parser->cur_line_length - 1] == '\n') {
310 parser->cur_line_end_length = 1;
317 static grpc_error* addbyte(grpc_http_parser* parser, uint8_t byte,
318 bool* found_body_start) {
319 switch (parser->state) {
320 case GRPC_HTTP_FIRST_LINE:
321 case GRPC_HTTP_HEADERS:
322 if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) {
323 if (GRPC_TRACE_FLAG_ENABLED(grpc_http1_trace)) {
324 gpr_log(GPR_ERROR, "HTTP header max line length (%d) exceeded",
325 GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
327 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
328 "HTTP header max line length exceeded");
330 parser->cur_line[parser->cur_line_length] = byte;
331 parser->cur_line_length++;
332 if (check_line(parser)) {
333 return finish_line(parser, found_body_start);
335 return GRPC_ERROR_NONE;
337 return addbyte_body(parser, byte);
339 GPR_UNREACHABLE_CODE(return GRPC_ERROR_NONE);
342 void grpc_http_parser_init(grpc_http_parser* parser, grpc_http_type type,
343 void* request_or_response) {
344 memset(parser, 0, sizeof(*parser));
345 parser->state = GRPC_HTTP_FIRST_LINE;
347 parser->http.request_or_response = request_or_response;
348 parser->cur_line_end_length = 2;
351 void grpc_http_parser_destroy(grpc_http_parser* /*parser*/) {}
353 void grpc_http_request_destroy(grpc_http_request* request) {
355 gpr_free(request->body);
356 for (i = 0; i < request->hdr_count; i++) {
357 gpr_free(request->hdrs[i].key);
358 gpr_free(request->hdrs[i].value);
360 gpr_free(request->hdrs);
361 gpr_free(request->method);
362 gpr_free(request->path);
365 void grpc_http_response_destroy(grpc_http_response* response) {
367 gpr_free(response->body);
368 for (i = 0; i < response->hdr_count; i++) {
369 gpr_free(response->hdrs[i].key);
370 gpr_free(response->hdrs[i].value);
372 gpr_free(response->hdrs);
375 grpc_error* grpc_http_parser_parse(grpc_http_parser* parser,
376 const grpc_slice& slice,
377 size_t* start_of_body) {
378 for (size_t i = 0; i < GRPC_SLICE_LENGTH(slice); i++) {
379 bool found_body_start = false;
381 addbyte(parser, GRPC_SLICE_START_PTR(slice)[i], &found_body_start);
382 if (err != GRPC_ERROR_NONE) return err;
383 if (found_body_start && start_of_body != nullptr) *start_of_body = i + 1;
385 return GRPC_ERROR_NONE;
388 grpc_error* grpc_http_parser_eof(grpc_http_parser* parser) {
389 if (parser->state != GRPC_HTTP_BODY) {
390 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Did not finish headers");
392 return GRPC_ERROR_NONE;