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 | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/http/parser.h" |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/useful.h> |
| 41 | |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 42 | int grpc_http1_trace = 0; |
Craig Tiller | 0418d38 | 2016-03-23 16:54:06 -0700 | [diff] [blame] | 43 | |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 44 | static char *buf2str(void *buffer, size_t length) { |
| 45 | char *out = gpr_malloc(length + 1); |
| 46 | memcpy(out, buffer, length); |
| 47 | out[length] = 0; |
| 48 | return out; |
| 49 | } |
| 50 | |
| 51 | static int handle_response_line(grpc_http_parser *parser) { |
| 52 | uint8_t *beg = parser->cur_line; |
| 53 | uint8_t *cur = beg; |
| 54 | uint8_t *end = beg + parser->cur_line_length; |
| 55 | |
| 56 | if (cur == end || *cur++ != 'H') goto error; |
| 57 | if (cur == end || *cur++ != 'T') goto error; |
| 58 | if (cur == end || *cur++ != 'T') goto error; |
| 59 | if (cur == end || *cur++ != 'P') goto error; |
| 60 | if (cur == end || *cur++ != '/') goto error; |
| 61 | if (cur == end || *cur++ != '1') goto error; |
| 62 | if (cur == end || *cur++ != '.') goto error; |
| 63 | if (cur == end || *cur < '0' || *cur++ > '1') goto error; |
| 64 | if (cur == end || *cur++ != ' ') goto error; |
| 65 | if (cur == end || *cur < '1' || *cur++ > '9') goto error; |
| 66 | if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
| 67 | if (cur == end || *cur < '0' || *cur++ > '9') goto error; |
| 68 | parser->http.response.status = |
| 69 | (cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0'); |
| 70 | if (cur == end || *cur++ != ' ') goto error; |
| 71 | |
| 72 | /* we don't really care about the status code message */ |
| 73 | |
| 74 | return 1; |
| 75 | |
| 76 | error: |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 77 | if (grpc_http1_trace) gpr_log(GPR_ERROR, "Failed parsing response line"); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int handle_request_line(grpc_http_parser *parser) { |
| 82 | uint8_t *beg = parser->cur_line; |
| 83 | uint8_t *cur = beg; |
| 84 | uint8_t *end = beg + parser->cur_line_length; |
| 85 | uint8_t vers_major = 0; |
| 86 | uint8_t vers_minor = 0; |
| 87 | |
| 88 | while (cur != end && *cur++ != ' ') |
| 89 | ; |
| 90 | if (cur == end) goto error; |
| 91 | parser->http.request.method = buf2str(beg, (size_t)(cur - beg - 1)); |
| 92 | |
| 93 | beg = cur; |
| 94 | while (cur != end && *cur++ != ' ') |
| 95 | ; |
| 96 | if (cur == end) goto error; |
| 97 | parser->http.request.path = buf2str(beg, (size_t)(cur - beg - 1)); |
| 98 | |
| 99 | if (cur == end || *cur++ != 'H') goto error; |
| 100 | if (cur == end || *cur++ != 'T') goto error; |
| 101 | if (cur == end || *cur++ != 'T') goto error; |
| 102 | if (cur == end || *cur++ != 'P') goto error; |
| 103 | if (cur == end || *cur++ != '/') goto error; |
| 104 | vers_major = (uint8_t)(*cur++ - '1' + 1); |
| 105 | ++cur; |
| 106 | if (cur == end) goto error; |
| 107 | vers_minor = (uint8_t)(*cur++ - '1' + 1); |
| 108 | |
| 109 | if (vers_major == 1) { |
| 110 | if (vers_minor == 0) { |
| 111 | parser->http.request.version = GRPC_HTTP_HTTP10; |
| 112 | } else if (vers_minor == 1) { |
| 113 | parser->http.request.version = GRPC_HTTP_HTTP11; |
| 114 | } else { |
| 115 | goto error; |
| 116 | } |
| 117 | } else if (vers_major == 2) { |
| 118 | if (vers_minor == 0) { |
| 119 | parser->http.request.version = GRPC_HTTP_HTTP20; |
| 120 | } else { |
| 121 | goto error; |
| 122 | } |
| 123 | } else { |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | return 1; |
| 128 | |
| 129 | error: |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 130 | if (grpc_http1_trace) gpr_log(GPR_ERROR, "Failed parsing request line"); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int handle_first_line(grpc_http_parser *parser) { |
| 135 | if (parser->cur_line[0] == 'H') { |
| 136 | parser->type = GRPC_HTTP_RESPONSE; |
| 137 | return handle_response_line(parser); |
| 138 | } else { |
| 139 | parser->type = GRPC_HTTP_REQUEST; |
| 140 | return handle_request_line(parser); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static int add_header(grpc_http_parser *parser) { |
| 145 | uint8_t *beg = parser->cur_line; |
| 146 | uint8_t *cur = beg; |
| 147 | uint8_t *end = beg + parser->cur_line_length; |
| 148 | size_t *hdr_count = NULL; |
| 149 | grpc_http_header **hdrs = NULL; |
| 150 | grpc_http_header hdr = {NULL, NULL}; |
| 151 | |
| 152 | GPR_ASSERT(cur != end); |
| 153 | |
| 154 | if (*cur == ' ' || *cur == '\t') { |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 155 | if (grpc_http1_trace) |
Craig Tiller | 0418d38 | 2016-03-23 16:54:06 -0700 | [diff] [blame] | 156 | gpr_log(GPR_ERROR, "Continued header lines not supported yet"); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 157 | goto error; |
| 158 | } |
| 159 | |
| 160 | while (cur != end && *cur != ':') { |
| 161 | cur++; |
| 162 | } |
| 163 | if (cur == end) { |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 164 | if (grpc_http1_trace) |
| 165 | gpr_log(GPR_ERROR, "Didn't find ':' in header string"); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 166 | goto error; |
| 167 | } |
| 168 | GPR_ASSERT(cur >= beg); |
| 169 | hdr.key = buf2str(beg, (size_t)(cur - beg)); |
| 170 | cur++; /* skip : */ |
| 171 | |
| 172 | while (cur != end && (*cur == ' ' || *cur == '\t')) { |
| 173 | cur++; |
| 174 | } |
| 175 | GPR_ASSERT(end - cur >= 2); |
| 176 | hdr.value = buf2str(cur, (size_t)(end - cur) - 2); |
| 177 | |
| 178 | if (parser->type == GRPC_HTTP_RESPONSE) { |
| 179 | hdr_count = &parser->http.response.hdr_count; |
| 180 | hdrs = &parser->http.response.hdrs; |
| 181 | } else if (parser->type == GRPC_HTTP_REQUEST) { |
| 182 | hdr_count = &parser->http.request.hdr_count; |
| 183 | hdrs = &parser->http.request.hdrs; |
| 184 | } else { |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | if (*hdr_count == parser->hdr_capacity) { |
| 189 | parser->hdr_capacity = |
| 190 | GPR_MAX(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2); |
| 191 | *hdrs = gpr_realloc(*hdrs, parser->hdr_capacity * sizeof(**hdrs)); |
| 192 | } |
| 193 | (*hdrs)[(*hdr_count)++] = hdr; |
| 194 | return 1; |
| 195 | |
| 196 | error: |
| 197 | gpr_free(hdr.key); |
| 198 | gpr_free(hdr.value); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int finish_line(grpc_http_parser *parser) { |
| 203 | switch (parser->state) { |
| 204 | case GRPC_HTTP_FIRST_LINE: |
| 205 | if (!handle_first_line(parser)) { |
| 206 | return 0; |
| 207 | } |
| 208 | parser->state = GRPC_HTTP_HEADERS; |
| 209 | break; |
| 210 | case GRPC_HTTP_HEADERS: |
| 211 | if (parser->cur_line_length == 2) { |
| 212 | parser->state = GRPC_HTTP_BODY; |
| 213 | break; |
| 214 | } |
| 215 | if (!add_header(parser)) { |
| 216 | return 0; |
| 217 | } |
| 218 | break; |
| 219 | case GRPC_HTTP_BODY: |
| 220 | GPR_UNREACHABLE_CODE(return 0); |
| 221 | } |
| 222 | |
| 223 | parser->cur_line_length = 0; |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | static int addbyte_body(grpc_http_parser *parser, uint8_t byte) { |
| 228 | size_t *body_length = NULL; |
| 229 | char **body = NULL; |
| 230 | |
| 231 | if (parser->type == GRPC_HTTP_RESPONSE) { |
| 232 | body_length = &parser->http.response.body_length; |
| 233 | body = &parser->http.response.body; |
| 234 | } else if (parser->type == GRPC_HTTP_REQUEST) { |
| 235 | body_length = &parser->http.request.body_length; |
| 236 | body = &parser->http.request.body; |
| 237 | } else { |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | if (*body_length == parser->body_capacity) { |
| 242 | parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2); |
| 243 | *body = gpr_realloc((void *)*body, parser->body_capacity); |
| 244 | } |
| 245 | (*body)[*body_length] = (char)byte; |
| 246 | (*body_length)++; |
| 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static int addbyte(grpc_http_parser *parser, uint8_t byte) { |
| 252 | switch (parser->state) { |
| 253 | case GRPC_HTTP_FIRST_LINE: |
| 254 | case GRPC_HTTP_HEADERS: |
| 255 | if (parser->cur_line_length >= GRPC_HTTP_PARSER_MAX_HEADER_LENGTH) { |
Craig Tiller | 3ab2fe0 | 2016-04-11 20:11:18 -0700 | [diff] [blame] | 256 | if (grpc_http1_trace) |
Craig Tiller | 0418d38 | 2016-03-23 16:54:06 -0700 | [diff] [blame] | 257 | gpr_log(GPR_ERROR, "HTTP client max line length (%d) exceeded", |
| 258 | GRPC_HTTP_PARSER_MAX_HEADER_LENGTH); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | parser->cur_line[parser->cur_line_length] = byte; |
| 262 | parser->cur_line_length++; |
| 263 | if (parser->cur_line_length >= 2 && |
| 264 | parser->cur_line[parser->cur_line_length - 2] == '\r' && |
| 265 | parser->cur_line[parser->cur_line_length - 1] == '\n') { |
| 266 | return finish_line(parser); |
| 267 | } else { |
| 268 | return 1; |
| 269 | } |
| 270 | GPR_UNREACHABLE_CODE(return 0); |
| 271 | case GRPC_HTTP_BODY: |
| 272 | return addbyte_body(parser, byte); |
| 273 | } |
| 274 | GPR_UNREACHABLE_CODE(return 0); |
| 275 | } |
| 276 | |
| 277 | void grpc_http_parser_init(grpc_http_parser *parser) { |
| 278 | memset(parser, 0, sizeof(*parser)); |
| 279 | parser->state = GRPC_HTTP_FIRST_LINE; |
| 280 | parser->type = GRPC_HTTP_UNKNOWN; |
| 281 | } |
| 282 | |
| 283 | void grpc_http_parser_destroy(grpc_http_parser *parser) { |
| 284 | size_t i; |
| 285 | if (parser->type == GRPC_HTTP_RESPONSE) { |
| 286 | gpr_free(parser->http.response.body); |
| 287 | for (i = 0; i < parser->http.response.hdr_count; i++) { |
| 288 | gpr_free(parser->http.response.hdrs[i].key); |
| 289 | gpr_free(parser->http.response.hdrs[i].value); |
| 290 | } |
| 291 | gpr_free(parser->http.response.hdrs); |
| 292 | } else if (parser->type == GRPC_HTTP_REQUEST) { |
| 293 | gpr_free(parser->http.request.body); |
| 294 | for (i = 0; i < parser->http.request.hdr_count; i++) { |
| 295 | gpr_free(parser->http.request.hdrs[i].key); |
| 296 | gpr_free(parser->http.request.hdrs[i].value); |
| 297 | } |
| 298 | gpr_free(parser->http.request.hdrs); |
| 299 | gpr_free(parser->http.request.method); |
| 300 | gpr_free(parser->http.request.path); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | int grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice) { |
| 305 | size_t i; |
| 306 | |
| 307 | for (i = 0; i < GPR_SLICE_LENGTH(slice); i++) { |
| 308 | if (!addbyte(parser, GPR_SLICE_START_PTR(slice)[i])) { |
| 309 | return 0; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | int grpc_http_parser_eof(grpc_http_parser *parser) { |
| 317 | return parser->state == GRPC_HTTP_BODY; |
| 318 | } |