Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_HTTP_PARSER_H |
| 35 | #define GRPC_CORE_LIB_HTTP_PARSER_H |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 36 | |
Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 37 | #include <grpc/slice.h> |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 38 | #include <grpc/support/port_platform.h> |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame^] | 39 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 40 | #include "src/core/lib/iomgr/error.h" |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 41 | |
| 42 | /* Maximum length of a header string of the form 'Key: Value\r\n' */ |
| 43 | #define GRPC_HTTP_PARSER_MAX_HEADER_LENGTH 4096 |
| 44 | |
| 45 | /* A single header to be passed in a request */ |
| 46 | typedef struct grpc_http_header { |
| 47 | char *key; |
| 48 | char *value; |
| 49 | } grpc_http_header; |
| 50 | |
| 51 | typedef enum { |
| 52 | GRPC_HTTP_FIRST_LINE, |
| 53 | GRPC_HTTP_HEADERS, |
| 54 | GRPC_HTTP_BODY |
| 55 | } grpc_http_parser_state; |
| 56 | |
| 57 | typedef enum { |
| 58 | GRPC_HTTP_HTTP10, |
| 59 | GRPC_HTTP_HTTP11, |
| 60 | GRPC_HTTP_HTTP20, |
| 61 | } grpc_http_version; |
| 62 | |
| 63 | typedef enum { |
| 64 | GRPC_HTTP_RESPONSE, |
| 65 | GRPC_HTTP_REQUEST, |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 66 | } grpc_http_type; |
| 67 | |
| 68 | /* A request */ |
| 69 | typedef struct grpc_http_request { |
| 70 | /* Method of the request (e.g. GET, POST) */ |
| 71 | char *method; |
| 72 | /* The path of the resource to fetch */ |
| 73 | char *path; |
| 74 | /* HTTP version to use */ |
| 75 | grpc_http_version version; |
| 76 | /* Headers attached to the request */ |
| 77 | size_t hdr_count; |
| 78 | grpc_http_header *hdrs; |
| 79 | /* Body: length and contents; contents are NOT null-terminated */ |
| 80 | size_t body_length; |
| 81 | char *body; |
| 82 | } grpc_http_request; |
| 83 | |
| 84 | /* A response */ |
| 85 | typedef struct grpc_http_response { |
| 86 | /* HTTP status code */ |
| 87 | int status; |
| 88 | /* Headers: count and key/values */ |
| 89 | size_t hdr_count; |
| 90 | grpc_http_header *hdrs; |
| 91 | /* Body: length and contents; contents are NOT null-terminated */ |
| 92 | size_t body_length; |
| 93 | char *body; |
| 94 | } grpc_http_response; |
| 95 | |
| 96 | typedef struct { |
| 97 | grpc_http_parser_state state; |
| 98 | grpc_http_type type; |
| 99 | |
| 100 | union { |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 101 | grpc_http_response *response; |
| 102 | grpc_http_request *request; |
| 103 | void *request_or_response; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 104 | } http; |
| 105 | size_t body_capacity; |
| 106 | size_t hdr_capacity; |
| 107 | |
| 108 | uint8_t cur_line[GRPC_HTTP_PARSER_MAX_HEADER_LENGTH]; |
| 109 | size_t cur_line_length; |
Matthew Iselin | 7151af9 | 2016-04-06 16:32:08 -0700 | [diff] [blame] | 110 | size_t cur_line_end_length; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 111 | } grpc_http_parser; |
| 112 | |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 113 | void grpc_http_parser_init(grpc_http_parser *parser, grpc_http_type type, |
| 114 | void *request_or_response); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 115 | void grpc_http_parser_destroy(grpc_http_parser *parser); |
| 116 | |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame] | 117 | /* Sets \a start_of_body to the offset in \a slice of the start of the body. */ |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 118 | grpc_error *grpc_http_parser_parse(grpc_http_parser *parser, grpc_slice slice, |
Mark D. Roth | 714c7ec | 2016-08-04 12:58:16 -0700 | [diff] [blame] | 119 | size_t *start_of_body); |
Craig Tiller | 5b15afd | 2016-05-04 15:00:14 -0700 | [diff] [blame] | 120 | grpc_error *grpc_http_parser_eof(grpc_http_parser *parser); |
| 121 | |
| 122 | void grpc_http_request_destroy(grpc_http_request *request); |
| 123 | void grpc_http_response_destroy(grpc_http_response *response); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 124 | |
Craig Tiller | 84f75d4 | 2017-05-03 13:06:35 -0700 | [diff] [blame^] | 125 | extern grpc_tracer_flag grpc_http1_trace; |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 126 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 127 | #endif /* GRPC_CORE_LIB_HTTP_PARSER_H */ |