1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
//
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include <grpc/support/port_platform.h>
#include "src/core/lib/http/parser.h"
#include <string.h>
#include <algorithm>
#include "y_absl/status/status.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
grpc_core::TraceFlag grpc_http1_trace(false, "http1");
static char* buf2str(void* buffer, size_t length) {
char* out = static_cast<char*>(gpr_malloc(length + 1));
memcpy(out, buffer, length);
out[length] = 0;
return out;
}
static grpc_error_handle handle_response_line(grpc_http_parser* parser) {
uint8_t* beg = parser->cur_line;
uint8_t* cur = beg;
uint8_t* end = beg + parser->cur_line_length;
if (cur == end || *cur++ != 'H') {
return GRPC_ERROR_CREATE("Expected 'H'");
}
if (cur == end || *cur++ != 'T') {
return GRPC_ERROR_CREATE("Expected 'T'");
}
if (cur == end || *cur++ != 'T') {
return GRPC_ERROR_CREATE("Expected 'T'");
}
if (cur == end || *cur++ != 'P') {
return GRPC_ERROR_CREATE("Expected 'P'");
}
if (cur == end || *cur++ != '/') {
return GRPC_ERROR_CREATE("Expected '/'");
}
if (cur == end || *cur++ != '1') {
return GRPC_ERROR_CREATE("Expected '1'");
}
if (cur == end || *cur++ != '.') {
return GRPC_ERROR_CREATE("Expected '.'");
}
if (cur == end || *cur < '0' || *cur++ > '1') {
return GRPC_ERROR_CREATE("Expected HTTP/1.0 or HTTP/1.1");
}
if (cur == end || *cur++ != ' ') {
return GRPC_ERROR_CREATE("Expected ' '");
}
if (cur == end || *cur < '1' || *cur++ > '9') {
return GRPC_ERROR_CREATE("Expected status code");
}
if (cur == end || *cur < '0' || *cur++ > '9') {
return GRPC_ERROR_CREATE("Expected status code");
}
if (cur == end || *cur < '0' || *cur++ > '9') {
return GRPC_ERROR_CREATE("Expected status code");
}
parser->http.response->status =
(cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0');
if (cur == end || *cur++ != ' ') {
return GRPC_ERROR_CREATE("Expected ' '");
}
// we don't really care about the status code message
return y_absl::OkStatus();
}
static grpc_error_handle handle_request_line(grpc_http_parser* parser) {
uint8_t* beg = parser->cur_line;
uint8_t* cur = beg;
uint8_t* end = beg + parser->cur_line_length;
uint8_t vers_major = 0;
uint8_t vers_minor = 0;
while (cur != end && *cur++ != ' ') {
}
if (cur == end) {
return GRPC_ERROR_CREATE("No method on HTTP request line");
}
parser->http.request->method =
buf2str(beg, static_cast<size_t>(cur - beg - 1));
beg = cur;
while (cur != end && *cur++ != ' ') {
}
if (cur == end) {
return GRPC_ERROR_CREATE("No path on HTTP request line");
}
parser->http.request->path = buf2str(beg, static_cast<size_t>(cur - beg - 1));
if (cur == end || *cur++ != 'H') {
return GRPC_ERROR_CREATE("Expected 'H'");
}
if (cur == end || *cur++ != 'T') {
return GRPC_ERROR_CREATE("Expected 'T'");
}
if (cur == end || *cur++ != 'T') {
return GRPC_ERROR_CREATE("Expected 'T'");
}
if (cur == end || *cur++ != 'P') {
return GRPC_ERROR_CREATE("Expected 'P'");
}
if (cur == end || *cur++ != '/') {
return GRPC_ERROR_CREATE("Expected '/'");
}
vers_major = static_cast<uint8_t>(*cur++ - '1' + 1);
++cur;
if (cur == end) {
return GRPC_ERROR_CREATE("End of line in HTTP version string");
}
vers_minor = static_cast<uint8_t>(*cur++ - '1' + 1);
if (vers_major == 1) {
if (vers_minor == 0) {
parser->http.request->version = GRPC_HTTP_HTTP10;
} else if (vers_minor == 1) {
parser->http.request->version = GRPC_HTTP_HTTP11;
} else {
return GRPC_ERROR_CREATE(
"Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
}
} else if (vers_major == 2) {
if (vers_minor == 0) {
parser->http.request->version = GRPC_HTTP_HTTP20;
} else {
return GRPC_ERROR_CREATE(
"Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
}
} else {
return GRPC_ERROR_CREATE("Expected one of HTTP/1.0, HTTP/1.1, or HTTP/2.0");
}
return y_absl::OkStatus();
}
static grpc_error_handle handle_first_line(grpc_http_parser* parser) {
switch (parser->type) {
case GRPC_HTTP_REQUEST:
return handle_request_line(parser);
case GRPC_HTTP_RESPONSE:
return handle_response_line(parser);
}
GPR_UNREACHABLE_CODE(return GRPC_ERROR_CREATE("Should never reach here"));
}
static grpc_error_handle add_header(grpc_http_parser* parser) {
uint8_t* beg = parser->cur_line;
uint8_t* cur = beg;
uint8_t* end = beg + parser->cur_line_length;
size_t* hdr_count = nullptr;
size_t size = 0;
grpc_http_header** hdrs = nullptr;
grpc_http_header hdr = {nullptr, nullptr};
grpc_error_handle error;
GPR_ASSERT(cur != end);
if (*cur == ' ' || *cur == '\t') {
error = GRPC_ERROR_CREATE("Continued header lines not supported yet");
goto done;
}
while (cur != end && *cur != ':') {
cur++;
}
if (cur == end) {
error = GRPC_ERROR_CREATE("Didn't find ':' in header string");
goto done;
}
GPR_ASSERT(cur >= beg);
hdr.key = buf2str(beg, static_cast<size_t>(cur - beg));
cur++; // skip :
while (cur != end && (*cur == ' ' || *cur == '\t')) {
cur++;
}
GPR_ASSERT((size_t)(end - cur) >= parser->cur_line_end_length);
size = static_cast<size_t>(end - cur) - parser->cur_line_end_length;
if ((size != 0) && (cur[size - 1] == '\r')) {
size--;
}
hdr.value = buf2str(cur, size);
switch (parser->type) {
case GRPC_HTTP_RESPONSE:
hdr_count = &parser->http.response->hdr_count;
hdrs = &parser->http.response->hdrs;
if ((strcmp(hdr.key, "Transfer-Encoding") == 0) &&
(strcmp(hdr.value, "chunked") == 0)) {
parser->http.response->chunked_state = GRPC_HTTP_CHUNKED_LENGTH;
}
break;
case GRPC_HTTP_REQUEST:
hdr_count = &parser->http.request->hdr_count;
hdrs = &parser->http.request->hdrs;
break;
}
if (*hdr_count == parser->hdr_capacity) {
parser->hdr_capacity =
std::max(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2);
*hdrs = static_cast<grpc_http_header*>(
gpr_realloc(*hdrs, parser->hdr_capacity * sizeof(**hdrs)));
}
(*hdrs)[(*hdr_count)++] = hdr;
done:
if (!error.ok()) {
gpr_free(hdr.key);
gpr_free(hdr.value);
}
return error;
}
static grpc_error_handle finish_line(grpc_http_parser* parser,
bool* found_body_start) {
grpc_error_handle err;
switch (parser->state) {
case GRPC_HTTP_FIRST_LINE:
err = handle_first_line(parser);
if (!err.ok()) return err;
parser->state = GRPC_HTTP_HEADERS;
break;
case GRPC_HTTP_HEADERS:
case GRPC_HTTP_TRAILERS:
if (parser->cur_line_length == parser->cur_line_end_length) {
if (parser->state == GRPC_HTTP_HEADERS) {
parser->state = GRPC_HTTP_BODY;
*found_body_start = true;
} else {
parser->state = GRPC_HTTP_END;
}
break;
} else {
err = add_header(parser);
if (!err.ok()) {
return err;
}
}
break;
case GRPC_HTTP_BODY:
case GRPC_HTTP_END:
GPR_UNREACHABLE_CODE(return GRPC_ERROR_CREATE("Should never reach here"));
}
parser->cur_line_length = 0;
return y_absl::OkStatus();
}
static grpc_error_handle addbyte_body(grpc_http_parser* parser, uint8_t byte) {
size_t* body_length = nullptr;
char** body = nullptr;
if (parser->type == GRPC_HTTP_RESPONSE) {
switch (parser->http.response->chunked_state) {
case GRPC_HTTP_CHUNKED_LENGTH:
if ((byte == '\r') || (byte == ';')) {
parser->http.response->chunked_state =
GRPC_HTTP_CHUNKED_IGNORE_ALL_UNTIL_LF;
} else if ((byte >= '0') && (byte <= '9')) {
parser->http.response->chunk_length *= 16;
parser->http.response->chunk_length += byte - '0';
} else if ((byte >= 'a') && (byte <= 'f')) {
parser->http.response->chunk_length *= 16;
parser->http.response->chunk_length += byte - 'a' + 10;
} else if ((byte >= 'A') && (byte <= 'F')) {
parser->http.response->chunk_length *= 16;
parser->http.response->chunk_length += byte - 'A' + 10;
} else {
return GRPC_ERROR_CREATE("Expected chunk size in hexadecimal");
}
return y_absl::OkStatus();
case GRPC_HTTP_CHUNKED_IGNORE_ALL_UNTIL_LF:
if (byte == '\n') {
if (parser->http.response->chunk_length == 0) {
parser->state = GRPC_HTTP_TRAILERS;
} else {
parser->http.response->chunked_state = GRPC_HTTP_CHUNKED_BODY;
}
}
return y_absl::OkStatus();
case GRPC_HTTP_CHUNKED_BODY:
if (parser->http.response->chunk_length == 0) {
if (byte != '\r') {
return GRPC_ERROR_CREATE("Expected '\\r\\n' after chunk body");
}
parser->http.response->chunked_state = GRPC_HTTP_CHUNKED_CONSUME_LF;
parser->http.response->chunk_length = 0;
return y_absl::OkStatus();
} else {
parser->http.response->chunk_length--;
// fallback to the normal body appending code below
}
break;
case GRPC_HTTP_CHUNKED_CONSUME_LF:
if (byte != '\n') {
return GRPC_ERROR_CREATE("Expected '\\r\\n' after chunk body");
}
parser->http.response->chunked_state = GRPC_HTTP_CHUNKED_LENGTH;
return y_absl::OkStatus();
case GRPC_HTTP_CHUNKED_PLAIN:
// avoiding warning; just fallback to normal codepath
break;
}
body_length = &parser->http.response->body_length;
body = &parser->http.response->body;
} else if (parser->type == GRPC_HTTP_REQUEST) {
body_length = &parser->http.request->body_length;
body = &parser->http.request->body;
} else {
GPR_UNREACHABLE_CODE(return GRPC_ERROR_CREATE("Should never reach here"));
}
if (*body_length == parser->body_capacity) {
parser->body_capacity = std::max(size_t{8}, parser->body_capacity * 3 / 2);
*body = static_cast<char*>(gpr_realloc(*body, parser->body_capacity));
}
(*body)[*body_length] = static_cast<char>(byte);
(*body_length)++;
return y_absl::OkStatus();
}
static bool 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 true;
}
// 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 true;
}
// 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 true;
}
return false;
}
static grpc_error_handle addbyte(grpc_http_parser* parser, uint8_t byte,
bool* found_body_start) {
switch (parser->state) {
case GRPC_HTTP_FIRST_LINE:
case GRPC_HTTP_HEADERS:
case GRPC_HTTP_TRAILERS:
if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) {
if (GRPC_TRACE_FLAG_ENABLED(grpc_http1_trace)) {
gpr_log(GPR_ERROR, "HTTP header max line length (%d) exceeded",
GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
}
return GRPC_ERROR_CREATE("HTTP header max line length exceeded");
}
parser->cur_line[parser->cur_line_length] = byte;
parser->cur_line_length++;
if (check_line(parser)) {
return finish_line(parser, found_body_start);
}
return y_absl::OkStatus();
case GRPC_HTTP_BODY:
return addbyte_body(parser, byte);
case GRPC_HTTP_END:
return GRPC_ERROR_CREATE("Unexpected byte after end");
}
GPR_UNREACHABLE_CODE(return y_absl::OkStatus());
}
void grpc_http_parser_init(grpc_http_parser* parser, grpc_http_type type,
void* request_or_response) {
memset(parser, 0, sizeof(*parser));
parser->state = GRPC_HTTP_FIRST_LINE;
parser->type = type;
parser->http.request_or_response = request_or_response;
parser->cur_line_end_length = 2;
}
void grpc_http_parser_destroy(grpc_http_parser* /*parser*/) {}
void grpc_http_request_destroy(grpc_http_request* request) {
size_t i;
gpr_free(request->body);
for (i = 0; i < request->hdr_count; i++) {
gpr_free(request->hdrs[i].key);
gpr_free(request->hdrs[i].value);
}
gpr_free(request->hdrs);
gpr_free(request->method);
gpr_free(request->path);
}
void grpc_http_response_destroy(grpc_http_response* response) {
size_t i;
gpr_free(response->body);
for (i = 0; i < response->hdr_count; i++) {
gpr_free(response->hdrs[i].key);
gpr_free(response->hdrs[i].value);
}
gpr_free(response->hdrs);
}
grpc_error_handle grpc_http_parser_parse(grpc_http_parser* parser,
const grpc_slice& slice,
size_t* start_of_body) {
for (size_t i = 0; i < GRPC_SLICE_LENGTH(slice); i++) {
bool found_body_start = false;
grpc_error_handle err =
addbyte(parser, GRPC_SLICE_START_PTR(slice)[i], &found_body_start);
if (!err.ok()) return err;
if (found_body_start && start_of_body != nullptr) *start_of_body = i + 1;
}
return y_absl::OkStatus();
}
grpc_error_handle grpc_http_parser_eof(grpc_http_parser* parser) {
if ((parser->state != GRPC_HTTP_BODY) && (parser->state != GRPC_HTTP_END)) {
return GRPC_ERROR_CREATE("Did not finish headers");
}
return y_absl::OkStatus();
}
|