blob: a155fecf118de807a6fba5386e0144ed90dc8436 [file] [log] [blame]
Matthew Iselin1824f052016-02-10 12:16:06 +11001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Matthew Iselin1824f052016-02-10 12:16:06 +11004 * 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_HTTP_PARSER_H
35#define GRPC_CORE_LIB_HTTP_PARSER_H
Matthew Iselin1824f052016-02-10 12:16:06 +110036
Craig Tillerb37d53e2016-10-26 16:16:35 -070037#include <grpc/slice.h>
Craig Tiller28b72422016-10-26 21:15:29 -070038#include <grpc/support/port_platform.h>
Craig Tiller84f75d42017-05-03 13:06:35 -070039#include "src/core/lib/debug/trace.h"
Craig Tiller5b15afd2016-05-04 15:00:14 -070040#include "src/core/lib/iomgr/error.h"
Matthew Iselin1824f052016-02-10 12:16:06 +110041
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 */
46typedef struct grpc_http_header {
47 char *key;
48 char *value;
49} grpc_http_header;
50
51typedef enum {
52 GRPC_HTTP_FIRST_LINE,
53 GRPC_HTTP_HEADERS,
54 GRPC_HTTP_BODY
55} grpc_http_parser_state;
56
57typedef enum {
58 GRPC_HTTP_HTTP10,
59 GRPC_HTTP_HTTP11,
60 GRPC_HTTP_HTTP20,
61} grpc_http_version;
62
63typedef enum {
64 GRPC_HTTP_RESPONSE,
65 GRPC_HTTP_REQUEST,
Matthew Iselin1824f052016-02-10 12:16:06 +110066} grpc_http_type;
67
68/* A request */
69typedef 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 */
85typedef 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
96typedef struct {
97 grpc_http_parser_state state;
98 grpc_http_type type;
99
100 union {
Craig Tiller5b15afd2016-05-04 15:00:14 -0700101 grpc_http_response *response;
102 grpc_http_request *request;
103 void *request_or_response;
Matthew Iselin1824f052016-02-10 12:16:06 +1100104 } 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 Iselin7151af92016-04-06 16:32:08 -0700110 size_t cur_line_end_length;
Matthew Iselin1824f052016-02-10 12:16:06 +1100111} grpc_http_parser;
112
Craig Tiller5b15afd2016-05-04 15:00:14 -0700113void grpc_http_parser_init(grpc_http_parser *parser, grpc_http_type type,
114 void *request_or_response);
Matthew Iselin1824f052016-02-10 12:16:06 +1100115void grpc_http_parser_destroy(grpc_http_parser *parser);
116
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700117/* Sets \a start_of_body to the offset in \a slice of the start of the body. */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700118grpc_error *grpc_http_parser_parse(grpc_http_parser *parser, grpc_slice slice,
Mark D. Roth714c7ec2016-08-04 12:58:16 -0700119 size_t *start_of_body);
Craig Tiller5b15afd2016-05-04 15:00:14 -0700120grpc_error *grpc_http_parser_eof(grpc_http_parser *parser);
121
122void grpc_http_request_destroy(grpc_http_request *request);
123void grpc_http_response_destroy(grpc_http_response *response);
Matthew Iselin1824f052016-02-10 12:16:06 +1100124
Craig Tiller84f75d42017-05-03 13:06:35 -0700125extern grpc_tracer_flag grpc_http1_trace;
Craig Tiller3ab2fe02016-04-11 20:11:18 -0700126
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700127#endif /* GRPC_CORE_LIB_HTTP_PARSER_H */