blob: a39cc0b2e06a723e8ec66ede61fae10d70677396 [file] [log] [blame]
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02001/*
2 * include/proto/h1.h
3 * This file contains HTTP/1 protocol definitions.
4 *
5 * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _PROTO_H1_H
23#define _PROTO_H1_H
24
Willy Tarreaudb4893d2017-09-21 08:40:02 +020025#include <common/buffer.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020026#include <common/compiler.h>
27#include <common/config.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020028#include <common/http.h>
Willy Tarreau794f9af2017-07-26 09:07:47 +020029#include <common/http-hdr.h>
Willy Tarreaudb4893d2017-09-21 08:40:02 +020030#include <common/standard.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020031#include <types/h1.h>
Willy Tarreau8740c8b2017-09-21 10:22:25 +020032#include <proto/hdr_idx.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020033
Willy Tarreau8740c8b2017-09-21 10:22:25 +020034const char *http_parse_reqline(struct http_msg *msg,
35 enum h1_state state, const char *ptr, const char *end,
36 unsigned int *ret_ptr, enum h1_state *ret_state);
37const char *http_parse_stsline(struct http_msg *msg,
38 enum h1_state state, const char *ptr, const char *end,
39 unsigned int *ret_ptr, enum h1_state *ret_state);
40void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx);
Willy Tarreaudb4893d2017-09-21 08:40:02 +020041int http_forward_trailers(struct http_msg *msg);
Willy Tarreau794f9af2017-07-26 09:07:47 +020042int h1_headers_to_hdr_list(char *start, const char *stop,
43 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +020044 struct h1m *h1m, union h1_sl *slp);
Willy Tarreauf40e6822018-06-14 16:52:02 +020045int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max);
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020046
Christopher Faulet55d6be72018-10-17 11:05:51 +020047int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value);
48void h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value);
49void h1_parse_connection_header(struct h1m *h1m, struct ist value);
50
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020051/* Macros used in the HTTP/1 parser, to check for the expected presence of
52 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
53 */
54
55
56/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
57 * <bad>.
58 */
59#define EXPECT_LF_HERE(ptr, bad, state, where) \
60 do { \
61 if (unlikely(*(ptr) != '\n')) { \
62 state = (where); \
63 goto bad; \
64 } \
65 } while (0)
66
67/* Increments pointer <ptr>, continues to label <more> if it's still below
68 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
69 * of buffer was reached.
70 */
71#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
72 do { \
73 if (likely(++(ptr) < (end))) \
74 goto more; \
75 else { \
76 state = (where); \
77 goto stop; \
78 } \
79 } while (0)
80
Willy Tarreau801250e2018-09-11 11:45:04 +020081/* for debugging, reports the HTTP/1 message state name (legacy version) */
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020082static inline const char *h1_msg_state_str(enum h1_state msg_state)
83{
84 switch (msg_state) {
85 case HTTP_MSG_RQBEFORE: return "MSG_RQBEFORE";
86 case HTTP_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
87 case HTTP_MSG_RQMETH: return "MSG_RQMETH";
88 case HTTP_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
89 case HTTP_MSG_RQURI: return "MSG_RQURI";
90 case HTTP_MSG_RQURI_SP: return "MSG_RQURI_SP";
91 case HTTP_MSG_RQVER: return "MSG_RQVER";
92 case HTTP_MSG_RQLINE_END: return "MSG_RQLINE_END";
93 case HTTP_MSG_RPBEFORE: return "MSG_RPBEFORE";
94 case HTTP_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
95 case HTTP_MSG_RPVER: return "MSG_RPVER";
96 case HTTP_MSG_RPVER_SP: return "MSG_RPVER_SP";
97 case HTTP_MSG_RPCODE: return "MSG_RPCODE";
98 case HTTP_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
99 case HTTP_MSG_RPREASON: return "MSG_RPREASON";
100 case HTTP_MSG_RPLINE_END: return "MSG_RPLINE_END";
101 case HTTP_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
102 case HTTP_MSG_HDR_NAME: return "MSG_HDR_NAME";
103 case HTTP_MSG_HDR_COL: return "MSG_HDR_COL";
104 case HTTP_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
105 case HTTP_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
106 case HTTP_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
107 case HTTP_MSG_HDR_VAL: return "MSG_HDR_VAL";
108 case HTTP_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
109 case HTTP_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
110 case HTTP_MSG_LAST_LF: return "MSG_LAST_LF";
111 case HTTP_MSG_ERROR: return "MSG_ERROR";
112 case HTTP_MSG_BODY: return "MSG_BODY";
113 case HTTP_MSG_100_SENT: return "MSG_100_SENT";
114 case HTTP_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
115 case HTTP_MSG_DATA: return "MSG_DATA";
116 case HTTP_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
117 case HTTP_MSG_TRAILERS: return "MSG_TRAILERS";
118 case HTTP_MSG_ENDING: return "MSG_ENDING";
119 case HTTP_MSG_DONE: return "MSG_DONE";
120 case HTTP_MSG_CLOSING: return "MSG_CLOSING";
121 case HTTP_MSG_CLOSED: return "MSG_CLOSED";
122 case HTTP_MSG_TUNNEL: return "MSG_TUNNEL";
123 default: return "MSG_??????";
124 }
125}
126
Willy Tarreau801250e2018-09-11 11:45:04 +0200127/* for debugging, reports the HTTP/1 message state name */
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +0200128static inline const char *h1m_state_str(enum h1m_state msg_state)
Willy Tarreau801250e2018-09-11 11:45:04 +0200129{
130 switch (msg_state) {
131 case H1_MSG_RQBEFORE: return "MSG_RQBEFORE";
132 case H1_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
133 case H1_MSG_RQMETH: return "MSG_RQMETH";
134 case H1_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
135 case H1_MSG_RQURI: return "MSG_RQURI";
136 case H1_MSG_RQURI_SP: return "MSG_RQURI_SP";
137 case H1_MSG_RQVER: return "MSG_RQVER";
138 case H1_MSG_RQLINE_END: return "MSG_RQLINE_END";
139 case H1_MSG_RPBEFORE: return "MSG_RPBEFORE";
140 case H1_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
141 case H1_MSG_RPVER: return "MSG_RPVER";
142 case H1_MSG_RPVER_SP: return "MSG_RPVER_SP";
143 case H1_MSG_RPCODE: return "MSG_RPCODE";
144 case H1_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
145 case H1_MSG_RPREASON: return "MSG_RPREASON";
146 case H1_MSG_RPLINE_END: return "MSG_RPLINE_END";
147 case H1_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
148 case H1_MSG_HDR_NAME: return "MSG_HDR_NAME";
149 case H1_MSG_HDR_COL: return "MSG_HDR_COL";
150 case H1_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
151 case H1_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
152 case H1_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
153 case H1_MSG_HDR_VAL: return "MSG_HDR_VAL";
154 case H1_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
155 case H1_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
156 case H1_MSG_LAST_LF: return "MSG_LAST_LF";
Willy Tarreau801250e2018-09-11 11:45:04 +0200157 case H1_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
158 case H1_MSG_DATA: return "MSG_DATA";
159 case H1_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
160 case H1_MSG_TRAILERS: return "MSG_TRAILERS";
Willy Tarreau801250e2018-09-11 11:45:04 +0200161 case H1_MSG_DONE: return "MSG_DONE";
Willy Tarreau801250e2018-09-11 11:45:04 +0200162 case H1_MSG_TUNNEL: return "MSG_TUNNEL";
163 default: return "MSG_??????";
164 }
165}
166
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200167/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
168 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
169 * in order to include this part into the next forwarding phase. Note that the
Willy Tarreauc0973c62018-06-14 15:53:21 +0200170 * caller must ensure that head+start points to the first byte to parse. It
171 * returns the number of bytes parsed on success, so the caller can set msg_state
172 * to HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
Willy Tarreaub2892562017-09-21 11:33:54 +0200173 * change anything and returns zero. Otherwise it returns a negative value
174 * indicating the error positionn relative to <stop>. Note: this function is
175 * designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200176 */
Willy Tarreaub2892562017-09-21 11:33:54 +0200177static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200178{
Willy Tarreauc0973c62018-06-14 15:53:21 +0200179 const char *ptr = b_peek(buf, start);
Willy Tarreaub2892562017-09-21 11:33:54 +0200180 int bytes = 1;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200181
182 /* NB: we'll check data availabilty at the end. It's not a
183 * problem because whatever we match first will be checked
184 * against the correct length.
185 */
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200186 if (*ptr == '\r') {
187 bytes++;
188 ptr++;
Willy Tarreauc0973c62018-06-14 15:53:21 +0200189 if (ptr >= b_wrap(buf))
Willy Tarreau591d4452018-06-15 17:21:00 +0200190 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200191 }
192
Willy Tarreaub2892562017-09-21 11:33:54 +0200193 if (bytes > stop - start)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200194 return 0;
195
Willy Tarreauc0973c62018-06-14 15:53:21 +0200196 if (*ptr != '\n') // negative position to stop
197 return ptr - __b_peek(buf, stop);
Willy Tarreaub2892562017-09-21 11:33:54 +0200198
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200199 return bytes;
200}
201
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200202/* Parse the chunk size start at buf + start and stops before buf + stop. The
203 * positions are relative to the buffer's head.
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200204 * It returns the chunk size in <res> and the amount of bytes read this way :
205 * < 0 : error at this position relative to <stop>
206 * = 0 : not enough bytes to read a complete chunk size
207 * > 0 : number of bytes successfully read that the caller can skip
208 * On success, the caller should adjust its msg->next to point to the first
209 * byte of data after the chunk size, so that we know we can forward exactly
210 * msg->next bytes, and msg->sol to contain the exact number of bytes forming
211 * the chunk size. That way it is always possible to differentiate between the
212 * start of the body and the start of the data. Note: this function is designed
213 * to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200214 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200215static inline int h1_parse_chunk_size(const struct buffer *buf, int start, int stop, unsigned int *res)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200216{
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200217 const char *ptr = b_peek(buf, start);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200218 const char *ptr_old = ptr;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200219 const char *end = b_wrap(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200220 unsigned int chunk = 0;
221
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100222 stop -= start; // bytes left
223 start = stop; // bytes to transfer
224
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200225 /* The chunk size is in the following form, though we are only
226 * interested in the size and CRLF :
227 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
228 */
229 while (1) {
230 int c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100231 if (!stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200232 return 0;
233 c = hex2i(*ptr);
234 if (c < 0) /* not a hex digit anymore */
235 break;
236 if (unlikely(++ptr >= end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200237 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200238 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
239 goto error;
240 chunk = (chunk << 4) + c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100241 stop--;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200242 }
243
244 /* empty size not allowed */
245 if (unlikely(ptr == ptr_old))
246 goto error;
247
248 while (HTTP_IS_SPHT(*ptr)) {
249 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200250 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100251 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200252 return 0;
253 }
254
255 /* Up to there, we know that at least one byte is present at *ptr. Check
256 * for the end of chunk size.
257 */
258 while (1) {
259 if (likely(HTTP_IS_CRLF(*ptr))) {
260 /* we now have a CR or an LF at ptr */
261 if (likely(*ptr == '\r')) {
262 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200263 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100264 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200265 return 0;
266 }
267
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100268 if (*ptr != '\n')
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200269 goto error;
270 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200271 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100272 --stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200273 /* done */
274 break;
275 }
276 else if (likely(*ptr == ';')) {
277 /* chunk extension, ends at next CRLF */
278 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200279 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100280 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200281 return 0;
282
283 while (!HTTP_IS_CRLF(*ptr)) {
284 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200285 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100286 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200287 return 0;
288 }
289 /* we have a CRLF now, loop above */
290 continue;
291 }
292 else
293 goto error;
294 }
295
296 /* OK we found our CRLF and now <ptr> points to the next byte, which may
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200297 * or may not be present. Let's return the number of bytes parsed.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200298 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200299 *res = chunk;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100300 return start - stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200301 error:
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200302 *res = 0; // just to stop gcc's -Wuninitialized warning :-(
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100303 return -stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200304}
305
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200306/* initializes an H1 message for a request */
307static inline struct h1m *h1m_init_req(struct h1m *h1m)
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200308{
Willy Tarreau801250e2018-09-11 11:45:04 +0200309 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreaub3b01522018-09-11 11:51:31 +0200310 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200311 h1m->flags = H1_MF_NONE;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200312 h1m->curr_len = 0;
313 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200314 h1m->err_pos = -2;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200315 h1m->err_state = 0;
316 return h1m;
317}
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200318
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200319/* initializes an H1 message for a response */
320static inline struct h1m *h1m_init_res(struct h1m *h1m)
321{
322 h1m->state = H1_MSG_RPBEFORE;
323 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200324 h1m->flags = H1_MF_RESP;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200325 h1m->curr_len = 0;
326 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200327 h1m->err_pos = -2;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200328 h1m->err_state = 0;
329 return h1m;
330}
331
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200332#endif /* _PROTO_H1_H */