Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / src / core / lib / http / parser.cc
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
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
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
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
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
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
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
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 =
285         static_cast<char*>(gpr_realloc((void*)*body, parser->body_capacity));
286   }
287   (*body)[*body_length] = static_cast<char>(byte);
288   (*body_length)++;
289
290   return GRPC_ERROR_NONE;
291 }
292
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') {
297     return true;
298   }
299
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') {
304     return true;
305   }
306
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;
311     return true;
312   }
313
314   return false;
315 }
316
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);
326         }
327         return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
328             "HTTP header max line length exceeded");
329       }
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);
334       }
335       return GRPC_ERROR_NONE;
336     case GRPC_HTTP_BODY:
337       return addbyte_body(parser, byte);
338   }
339   GPR_UNREACHABLE_CODE(return GRPC_ERROR_NONE);
340 }
341
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;
346   parser->type = type;
347   parser->http.request_or_response = request_or_response;
348   parser->cur_line_end_length = 2;
349 }
350
351 void grpc_http_parser_destroy(grpc_http_parser* /*parser*/) {}
352
353 void grpc_http_request_destroy(grpc_http_request* request) {
354   size_t i;
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);
359   }
360   gpr_free(request->hdrs);
361   gpr_free(request->method);
362   gpr_free(request->path);
363 }
364
365 void grpc_http_response_destroy(grpc_http_response* response) {
366   size_t i;
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);
371   }
372   gpr_free(response->hdrs);
373 }
374
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;
380     grpc_error* err =
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;
384   }
385   return GRPC_ERROR_NONE;
386 }
387
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");
391   }
392   return GRPC_ERROR_NONE;
393 }