1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/http/parser.h"
22
23 #include <stdbool.h>
24 #include <string.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/useful.h"
30
31 grpc_core::TraceFlag grpc_http1_trace(false, "http1");
32
buf2str(void * buffer,size_t length)33 static char* buf2str(void* buffer, size_t length) {
34 char* out = static_cast<char*>(gpr_malloc(length + 1));
35 memcpy(out, buffer, length);
36 out[length] = 0;
37 return out;
38 }
39
handle_response_line(grpc_http_parser * parser)40 static grpc_error* handle_response_line(grpc_http_parser* parser) {
41 uint8_t* beg = parser->cur_line;
42 uint8_t* cur = beg;
43 uint8_t* end = beg + parser->cur_line_length;
44
45 if (cur == end || *cur++ != 'H') {
46 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'H'");
47 }
48 if (cur == end || *cur++ != 'T') {
49 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
50 }
51 if (cur == end || *cur++ != 'T') {
52 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
53 }
54 if (cur == end || *cur++ != 'P') {
55 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'P'");
56 }
57 if (cur == end || *cur++ != '/') {
58 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '/'");
59 }
60 if (cur == end || *cur++ != '1') {
61 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '1'");
62 }
63 if (cur == end || *cur++ != '.') {
64 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '.'");
65 }
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");
69 }
70 if (cur == end || *cur++ != ' ') {
71 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected ' '");
72 }
73 if (cur == end || *cur < '1' || *cur++ > '9') {
74 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
75 }
76 if (cur == end || *cur < '0' || *cur++ > '9') {
77 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
78 }
79 if (cur == end || *cur < '0' || *cur++ > '9') {
80 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected status code");
81 }
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 ' '");
86 }
87
88 /* we don't really care about the status code message */
89
90 return GRPC_ERROR_NONE;
91 }
92
handle_request_line(grpc_http_parser * parser)93 static grpc_error* handle_request_line(grpc_http_parser* parser) {
94 uint8_t* beg = parser->cur_line;
95 uint8_t* cur = beg;
96 uint8_t* end = beg + parser->cur_line_length;
97 uint8_t vers_major = 0;
98 uint8_t vers_minor = 0;
99
100 while (cur != end && *cur++ != ' ') {
101 }
102 if (cur == end) {
103 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
104 "No method on HTTP request line");
105 }
106 parser->http.request->method =
107 buf2str(beg, static_cast<size_t>(cur - beg - 1));
108
109 beg = cur;
110 while (cur != end && *cur++ != ' ') {
111 }
112 if (cur == end) {
113 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("No path on HTTP request line");
114 }
115 parser->http.request->path = buf2str(beg, static_cast<size_t>(cur - beg - 1));
116
117 if (cur == end || *cur++ != 'H') {
118 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'H'");
119 }
120 if (cur == end || *cur++ != 'T') {
121 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
122 }
123 if (cur == end || *cur++ != 'T') {
124 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'T'");
125 }
126 if (cur == end || *cur++ != 'P') {
127 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected 'P'");
128 }
129 if (cur == end || *cur++ != '/') {
130 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Expected '/'");
131 }
132 vers_major = static_cast<uint8_t>(*cur++ - '1' + 1);
133 ++cur;
134 if (cur == end) {
135 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
136 "End of line in HTTP version string");
137 }
138 vers_minor = static_cast<uint8_t>(*cur++ - '1' + 1);
139
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;
145 } else {
146 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
147 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
148 }
149 } else if (vers_major == 2) {
150 if (vers_minor == 0) {
151 parser->http.request->version = GRPC_HTTP_HTTP20;
152 } else {
153 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
154 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
155 }
156 } else {
157 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
158 "Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
159 }
160
161 return GRPC_ERROR_NONE;
162 }
163
handle_first_line(grpc_http_parser * parser)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);
170 }
171 GPR_UNREACHABLE_CODE(
172 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here"));
173 }
174
add_header(grpc_http_parser * parser)175 static grpc_error* add_header(grpc_http_parser* parser) {
176 uint8_t* beg = parser->cur_line;
177 uint8_t* cur = beg;
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;
183
184 GPR_ASSERT(cur != end);
185
186 if (*cur == ' ' || *cur == '\t') {
187 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
188 "Continued header lines not supported yet");
189 goto done;
190 }
191
192 while (cur != end && *cur != ':') {
193 cur++;
194 }
195 if (cur == end) {
196 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING(
197 "Didn't find ':' in header string");
198 goto done;
199 }
200 GPR_ASSERT(cur >= beg);
201 hdr.key = buf2str(beg, static_cast<size_t>(cur - beg));
202 cur++; /* skip : */
203
204 while (cur != end && (*cur == ' ' || *cur == '\t')) {
205 cur++;
206 }
207 GPR_ASSERT((size_t)(end - cur) >= parser->cur_line_end_length);
208 hdr.value = buf2str(
209 cur, static_cast<size_t>(end - cur) - parser->cur_line_end_length);
210
211 switch (parser->type) {
212 case GRPC_HTTP_RESPONSE:
213 hdr_count = &parser->http.response->hdr_count;
214 hdrs = &parser->http.response->hdrs;
215 break;
216 case GRPC_HTTP_REQUEST:
217 hdr_count = &parser->http.request->hdr_count;
218 hdrs = &parser->http.request->hdrs;
219 break;
220 }
221
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)));
227 }
228 (*hdrs)[(*hdr_count)++] = hdr;
229
230 done:
231 if (error != GRPC_ERROR_NONE) {
232 gpr_free(hdr.key);
233 gpr_free(hdr.value);
234 }
235 return error;
236 }
237
finish_line(grpc_http_parser * parser,bool * found_body_start)238 static grpc_error* finish_line(grpc_http_parser* parser,
239 bool* found_body_start) {
240 grpc_error* err;
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;
246 break;
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;
251 break;
252 }
253 err = add_header(parser);
254 if (err != GRPC_ERROR_NONE) {
255 return err;
256 }
257 break;
258 case GRPC_HTTP_BODY:
259 GPR_UNREACHABLE_CODE(return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
260 "Should never reach here"));
261 }
262
263 parser->cur_line_length = 0;
264 return GRPC_ERROR_NONE;
265 }
266
addbyte_body(grpc_http_parser * parser,uint8_t byte)267 static grpc_error* addbyte_body(grpc_http_parser* parser, uint8_t byte) {
268 size_t* body_length = nullptr;
269 char** body = nullptr;
270
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;
277 } else {
278 GPR_UNREACHABLE_CODE(
279 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Should never reach here"));
280 }
281
282 if (*body_length == parser->body_capacity) {
283 parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2);
284 *body = static_cast<char*>(gpr_realloc(*body, parser->body_capacity));
285 }
286 (*body)[*body_length] = static_cast<char>(byte);
287 (*body_length)++;
288
289 return GRPC_ERROR_NONE;
290 }
291
check_line(grpc_http_parser * parser)292 static bool check_line(grpc_http_parser* parser) {
293 if (parser->cur_line_length >= 2 &&
294 parser->cur_line[parser->cur_line_length - 2] == '\r' &&
295 parser->cur_line[parser->cur_line_length - 1] == '\n') {
296 return true;
297 }
298
299 // HTTP request with \n\r line termiantors.
300 else if (parser->cur_line_length >= 2 &&
301 parser->cur_line[parser->cur_line_length - 2] == '\n' &&
302 parser->cur_line[parser->cur_line_length - 1] == '\r') {
303 return true;
304 }
305
306 // HTTP request with only \n line terminators.
307 else if (parser->cur_line_length >= 1 &&
308 parser->cur_line[parser->cur_line_length - 1] == '\n') {
309 parser->cur_line_end_length = 1;
310 return true;
311 }
312
313 return false;
314 }
315
addbyte(grpc_http_parser * parser,uint8_t byte,bool * found_body_start)316 static grpc_error* addbyte(grpc_http_parser* parser, uint8_t byte,
317 bool* found_body_start) {
318 switch (parser->state) {
319 case GRPC_HTTP_FIRST_LINE:
320 case GRPC_HTTP_HEADERS:
321 if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) {
322 if (GRPC_TRACE_FLAG_ENABLED(grpc_http1_trace)) {
323 gpr_log(GPR_ERROR, "HTTP header max line length (%d) exceeded",
324 GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
325 }
326 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
327 "HTTP header max line length exceeded");
328 }
329 parser->cur_line[parser->cur_line_length] = byte;
330 parser->cur_line_length++;
331 if (check_line(parser)) {
332 return finish_line(parser, found_body_start);
333 }
334 return GRPC_ERROR_NONE;
335 case GRPC_HTTP_BODY:
336 return addbyte_body(parser, byte);
337 }
338 GPR_UNREACHABLE_CODE(return GRPC_ERROR_NONE);
339 }
340
grpc_http_parser_init(grpc_http_parser * parser,grpc_http_type type,void * request_or_response)341 void grpc_http_parser_init(grpc_http_parser* parser, grpc_http_type type,
342 void* request_or_response) {
343 memset(parser, 0, sizeof(*parser));
344 parser->state = GRPC_HTTP_FIRST_LINE;
345 parser->type = type;
346 parser->http.request_or_response = request_or_response;
347 parser->cur_line_end_length = 2;
348 }
349
grpc_http_parser_destroy(grpc_http_parser *)350 void grpc_http_parser_destroy(grpc_http_parser* /*parser*/) {}
351
grpc_http_request_destroy(grpc_http_request * request)352 void grpc_http_request_destroy(grpc_http_request* request) {
353 size_t i;
354 gpr_free(request->body);
355 for (i = 0; i < request->hdr_count; i++) {
356 gpr_free(request->hdrs[i].key);
357 gpr_free(request->hdrs[i].value);
358 }
359 gpr_free(request->hdrs);
360 gpr_free(request->method);
361 gpr_free(request->path);
362 }
363
grpc_http_response_destroy(grpc_http_response * response)364 void grpc_http_response_destroy(grpc_http_response* response) {
365 size_t i;
366 gpr_free(response->body);
367 for (i = 0; i < response->hdr_count; i++) {
368 gpr_free(response->hdrs[i].key);
369 gpr_free(response->hdrs[i].value);
370 }
371 gpr_free(response->hdrs);
372 }
373
grpc_http_parser_parse(grpc_http_parser * parser,const grpc_slice & slice,size_t * start_of_body)374 grpc_error* grpc_http_parser_parse(grpc_http_parser* parser,
375 const grpc_slice& slice,
376 size_t* start_of_body) {
377 for (size_t i = 0; i < GRPC_SLICE_LENGTH(slice); i++) {
378 bool found_body_start = false;
379 grpc_error* err =
380 addbyte(parser, GRPC_SLICE_START_PTR(slice)[i], &found_body_start);
381 if (err != GRPC_ERROR_NONE) return err;
382 if (found_body_start && start_of_body != nullptr) *start_of_body = i + 1;
383 }
384 return GRPC_ERROR_NONE;
385 }
386
grpc_http_parser_eof(grpc_http_parser * parser)387 grpc_error* grpc_http_parser_eof(grpc_http_parser* parser) {
388 if (parser->state != GRPC_HTTP_BODY) {
389 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("Did not finish headers");
390 }
391 return GRPC_ERROR_NONE;
392 }
393