blob: 8bd73f649abf0dba98b29d033715d324cc7eae7c [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
37#include <grpc/support/port_platform.h>
38#include <grpc/support/slice.h>
39
40/* Maximum length of a header string of the form 'Key: Value\r\n' */
41#define GRPC_HTTP_PARSER_MAX_HEADER_LENGTH 4096
42
43/* A single header to be passed in a request */
44typedef struct grpc_http_header {
45 char *key;
46 char *value;
47} grpc_http_header;
48
49typedef enum {
50 GRPC_HTTP_FIRST_LINE,
51 GRPC_HTTP_HEADERS,
52 GRPC_HTTP_BODY
53} grpc_http_parser_state;
54
55typedef enum {
56 GRPC_HTTP_HTTP10,
57 GRPC_HTTP_HTTP11,
58 GRPC_HTTP_HTTP20,
59} grpc_http_version;
60
61typedef enum {
62 GRPC_HTTP_RESPONSE,
63 GRPC_HTTP_REQUEST,
64 GRPC_HTTP_UNKNOWN
65} grpc_http_type;
66
67/* A request */
68typedef struct grpc_http_request {
69 /* Method of the request (e.g. GET, POST) */
70 char *method;
71 /* The path of the resource to fetch */
72 char *path;
73 /* HTTP version to use */
74 grpc_http_version version;
75 /* Headers attached to the request */
76 size_t hdr_count;
77 grpc_http_header *hdrs;
78 /* Body: length and contents; contents are NOT null-terminated */
79 size_t body_length;
80 char *body;
81} grpc_http_request;
82
83/* A response */
84typedef struct grpc_http_response {
85 /* HTTP status code */
86 int status;
87 /* Headers: count and key/values */
88 size_t hdr_count;
89 grpc_http_header *hdrs;
90 /* Body: length and contents; contents are NOT null-terminated */
91 size_t body_length;
92 char *body;
93} grpc_http_response;
94
95typedef struct {
96 grpc_http_parser_state state;
97 grpc_http_type type;
98
99 union {
100 grpc_http_response response;
101 grpc_http_request request;
102 } http;
103 size_t body_capacity;
104 size_t hdr_capacity;
105
106 uint8_t cur_line[GRPC_HTTP_PARSER_MAX_HEADER_LENGTH];
107 size_t cur_line_length;
108} grpc_http_parser;
109
110void grpc_http_parser_init(grpc_http_parser *parser);
111void grpc_http_parser_destroy(grpc_http_parser *parser);
112
113int grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice);
114int grpc_http_parser_eof(grpc_http_parser *parser);
115
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700116#endif /* GRPC_CORE_LIB_HTTP_PARSER_H */