blob: 28518a6bf710a63db6d3833d66fd737e05220103 [file] [log] [blame]
Willy Tarreauf24ea8e2017-11-21 19:55:27 +01001/*
2 * HTTP/2 protocol processing
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2017 HAProxy Technologies
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <stdint.h>
29#include <common/config.h>
30#include <common/h2.h>
31#include <common/http-hdr.h>
32#include <common/ist.h>
33
34
35/* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers
36 * stored in <phdr[]>. <fields> indicates what was found so far. This should be
37 * called once at the detection of the first general header field or at the end
38 * of the request if no general header field was found yet. Returns 0 on success
39 * or a negative error code on failure.
40 */
41static int h2_prepare_h1_reqline(uint32_t fields, struct ist *phdr, char **ptr, char *end)
42{
43 char *out = *ptr;
44 int uri_idx = H2_PHDR_IDX_PATH;
45
46 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
47 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
48 * MUST be omitted ; ":authority" contains the host and port
49 * to connect to.
50 */
51 if (fields & H2_PHDR_FND_SCHM) {
52 /* scheme not allowed */
53 goto fail;
54 }
55 else if (fields & H2_PHDR_FND_PATH) {
56 /* path not allowed */
57 goto fail;
58 }
59 else if (!(fields & H2_PHDR_FND_AUTH)) {
60 /* missing authority */
61 goto fail;
62 }
63 // otherwise OK ; let's use the authority instead of the URI
64 uri_idx = H2_PHDR_IDX_AUTH;
65 }
66 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
67 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
68 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
69 * valid value for the ":method", ":scheme" and ":path" phdr
70 * unless it is a CONNECT request.
71 */
72 if (!(fields & H2_PHDR_FND_METH)) {
73 /* missing method */
74 goto fail;
75 }
76 else if (!(fields & H2_PHDR_FND_SCHM)) {
77 /* missing scheme */
78 goto fail;
79 }
80 else {
81 /* missing path */
82 goto fail;
83 }
84 }
85
86 if (out + phdr[uri_idx].len + 1 + phdr[uri_idx].len + 11 > end) {
87 /* too large */
88 goto fail;
89 }
90
91 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
92 out += phdr[H2_PHDR_IDX_METH].len;
93 *(out++) = ' ';
94
95 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
96 out += phdr[uri_idx].len;
97 memcpy(out, " HTTP/1.1\r\n", 11);
98 out += 11;
99
100 *ptr = out;
101 return 0;
102 fail:
103 return -1;
104}
105
106/* Takes an H2 request present in the headers list <list> terminated by a name
107 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
108 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
109 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
110 * case of error, a negative error code is returned.
111 *
112 * The headers list <list> must be composed of :
113 * - n.name != NULL, n.len > 0 : literal header name
114 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
115 * among H2_PHDR_IDX_*
116 * - n.name ignored, n.len == 0 : end of list
117 * - in all cases except the end of list, v.name and v.len must designate a
118 * valid value.
119 */
120int h2_make_h1_request(struct http_hdr *list, char *out, int osize)
121{
122 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
123 char *out_end = out + osize;
124 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
125 uint32_t idx;
126 int phdr;
127 int ret;
128
129 fields = 0;
130 for (idx = 0; list[idx].n.len != 0; idx++) {
131 if (!list[idx].n.ptr) {
132 /* this is an indexed pseudo-header */
133 phdr = list[idx].n.len;
134 }
135 else {
136 /* this can be any type of header */
137 phdr = h2_str_to_phdr(list[idx].n);
138 }
139
140 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
141 /* insert a pseudo header by its index (in phdr) and value (in value) */
142 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
143 if (fields & H2_PHDR_FND_NONE) {
144 /* pseudo header field after regular headers */
145 goto fail;
146 }
147 else {
148 /* repeated pseudo header field */
149 goto fail;
150 }
151 }
152 fields |= 1 << phdr;
153 phdr_val[phdr] = list[idx].v;
154 continue;
155 }
156 else if (phdr != 0) {
157 /* invalid pseudo header -- should never happen here */
158 goto fail;
159 }
160
161 /* regular header field in (name,value) */
162 if (!(fields & H2_PHDR_FND_NONE)) {
163 /* no more pseudo-headers, time to build the request line */
164 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end);
165 if (ret != 0)
166 goto leave;
167 fields |= H2_PHDR_FND_NONE;
168 }
169
170 if (isteq(list[idx].n, ist("host")))
171 fields |= H2_PHDR_FND_HOST;
172
173 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
174 /* too large */
175 goto fail;
176 }
177
178 /* copy "name: value" */
179 memcpy(out, list[idx].n.ptr, list[idx].n.len);
180 out += list[idx].n.len;
181 *(out++) = ':';
182 *(out++) = ' ';
183
184 memcpy(out, list[idx].v.ptr, list[idx].v.len);
185 out += list[idx].v.len;
186 *(out++) = '\r';
187 *(out++) = '\n';
188 }
189
190 /* Let's dump the request now if not yet emitted. */
191 if (!(fields & H2_PHDR_FND_NONE)) {
192 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end);
193 if (ret != 0)
194 goto leave;
195 }
196
197 /* complete with missing Host if needed */
198 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
199 /* missing Host field, use :authority instead */
200 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
201 /* too large */
202 goto fail;
203 }
204
205 memcpy(out, "host: ", 6);
206 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
207 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
208 *(out++) = '\r';
209 *(out++) = '\n';
210 }
211
212 /* And finish */
213 if (out + 2 > out_end) {
214 /* too large */
215 goto fail;
216 }
217
218 *(out++) = '\r';
219 *(out++) = '\n';
220 ret = out + osize - out_end;
221 leave:
222 return ret;
223
224 fail:
225 return -1;
226}