blob: 41565c04bbd0da95d234aa5040c17839b0f123ad [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
Willy Tarreaucd4fe172017-12-03 11:51:31 +010086 /* 7540#8.1.2.3: :path must not be empty */
87 if (!phdr[uri_idx].len)
88 goto fail;
89
Willy Tarreau811ad122017-12-03 09:44:50 +010090 if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) {
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010091 /* too large */
92 goto fail;
93 }
94
95 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
96 out += phdr[H2_PHDR_IDX_METH].len;
97 *(out++) = ' ';
98
99 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
100 out += phdr[uri_idx].len;
101 memcpy(out, " HTTP/1.1\r\n", 11);
102 out += 11;
103
104 *ptr = out;
105 return 0;
106 fail:
107 return -1;
108}
109
110/* Takes an H2 request present in the headers list <list> terminated by a name
111 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
112 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
113 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
114 * case of error, a negative error code is returned.
115 *
116 * The headers list <list> must be composed of :
117 * - n.name != NULL, n.len > 0 : literal header name
118 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
119 * among H2_PHDR_IDX_*
120 * - n.name ignored, n.len == 0 : end of list
121 * - in all cases except the end of list, v.name and v.len must designate a
122 * valid value.
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100123 *
124 * The Cookie header will be reassembled at the end, and for this, the <list>
125 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100126 */
127int h2_make_h1_request(struct http_hdr *list, char *out, int osize)
128{
129 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
130 char *out_end = out + osize;
131 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
132 uint32_t idx;
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100133 int ck, lck; /* cookie index and last cookie index */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100134 int phdr;
135 int ret;
136
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100137 lck = ck = -1; // no cookie for now
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100138 fields = 0;
139 for (idx = 0; list[idx].n.len != 0; idx++) {
140 if (!list[idx].n.ptr) {
141 /* this is an indexed pseudo-header */
142 phdr = list[idx].n.len;
143 }
144 else {
145 /* this can be any type of header */
146 phdr = h2_str_to_phdr(list[idx].n);
147 }
148
149 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
150 /* insert a pseudo header by its index (in phdr) and value (in value) */
151 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
152 if (fields & H2_PHDR_FND_NONE) {
153 /* pseudo header field after regular headers */
154 goto fail;
155 }
156 else {
157 /* repeated pseudo header field */
158 goto fail;
159 }
160 }
161 fields |= 1 << phdr;
162 phdr_val[phdr] = list[idx].v;
163 continue;
164 }
165 else if (phdr != 0) {
166 /* invalid pseudo header -- should never happen here */
167 goto fail;
168 }
169
170 /* regular header field in (name,value) */
171 if (!(fields & H2_PHDR_FND_NONE)) {
172 /* no more pseudo-headers, time to build the request line */
173 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end);
174 if (ret != 0)
175 goto leave;
176 fields |= H2_PHDR_FND_NONE;
177 }
178
179 if (isteq(list[idx].n, ist("host")))
180 fields |= H2_PHDR_FND_HOST;
181
Willy Tarreaud8d2ac72017-12-03 18:41:31 +0100182 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
183 goto fail;
184
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100185 /* cookie requires special processing at the end */
186 if (isteq(list[idx].n, ist("cookie"))) {
187 list[idx].n.len = -1;
188
189 if (ck < 0)
190 ck = idx;
191 else
192 list[lck].n.len = idx;
193
194 lck = idx;
195 continue;
196 }
197
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100198 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
199 /* too large */
200 goto fail;
201 }
202
203 /* copy "name: value" */
204 memcpy(out, list[idx].n.ptr, list[idx].n.len);
205 out += list[idx].n.len;
206 *(out++) = ':';
207 *(out++) = ' ';
208
209 memcpy(out, list[idx].v.ptr, list[idx].v.len);
210 out += list[idx].v.len;
211 *(out++) = '\r';
212 *(out++) = '\n';
213 }
214
215 /* Let's dump the request now if not yet emitted. */
216 if (!(fields & H2_PHDR_FND_NONE)) {
217 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end);
218 if (ret != 0)
219 goto leave;
220 }
221
222 /* complete with missing Host if needed */
223 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
224 /* missing Host field, use :authority instead */
225 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
226 /* too large */
227 goto fail;
228 }
229
230 memcpy(out, "host: ", 6);
231 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
232 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
233 *(out++) = '\r';
234 *(out++) = '\n';
235 }
236
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100237 /* now we may have to build a cookie list. We'll dump the values of all
238 * visited headers.
239 */
240 if (ck >= 0) {
241 if (out + 8 > out_end) {
242 /* too large */
243 goto fail;
244 }
245 memcpy(out, "cookie: ", 8);
246 out += 8;
247
248 do {
249 if (out + list[ck].v.len + 2 > out_end) {
250 /* too large */
251 goto fail;
252 }
253 memcpy(out, list[ck].v.ptr, list[ck].v.len);
254 out += list[ck].v.len;
255 ck = list[ck].n.len;
256
257 if (ck >= 0) {
258 *(out++) = ';';
259 *(out++) = ' ';
260 }
261 } while (ck >= 0);
262
263 if (out + 2 > out_end) {
264 /* too large */
265 goto fail;
266 }
267 *(out++) = '\r';
268 *(out++) = '\n';
269 }
270
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100271 /* And finish */
272 if (out + 2 > out_end) {
273 /* too large */
274 goto fail;
275 }
276
277 *(out++) = '\r';
278 *(out++) = '\n';
279 ret = out + osize - out_end;
280 leave:
281 return ret;
282
283 fail:
284 return -1;
285}