blob: 68ba106f296cb2f6c1f55e360537e9988c1b76cd [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>
32
Willy Tarreau794f9af2017-07-26 09:07:47 +020033int h1_headers_to_hdr_list(char *start, const char *stop,
34 struct http_hdr *hdr, unsigned int hdr_num,
Willy Tarreaua41393f2018-09-11 15:34:50 +020035 struct h1m *h1m, union h1_sl *slp);
Willy Tarreauf40e6822018-06-14 16:52:02 +020036int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max);
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020037
Christopher Faulet55d6be72018-10-17 11:05:51 +020038int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value);
39void h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value);
40void h1_parse_connection_header(struct h1m *h1m, struct ist value);
41
Willy Tarreau801250e2018-09-11 11:45:04 +020042/* for debugging, reports the HTTP/1 message state name (legacy version) */
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020043static inline const char *h1_msg_state_str(enum h1_state msg_state)
44{
45 switch (msg_state) {
46 case HTTP_MSG_RQBEFORE: return "MSG_RQBEFORE";
47 case HTTP_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
48 case HTTP_MSG_RQMETH: return "MSG_RQMETH";
49 case HTTP_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
50 case HTTP_MSG_RQURI: return "MSG_RQURI";
51 case HTTP_MSG_RQURI_SP: return "MSG_RQURI_SP";
52 case HTTP_MSG_RQVER: return "MSG_RQVER";
53 case HTTP_MSG_RQLINE_END: return "MSG_RQLINE_END";
54 case HTTP_MSG_RPBEFORE: return "MSG_RPBEFORE";
55 case HTTP_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
56 case HTTP_MSG_RPVER: return "MSG_RPVER";
57 case HTTP_MSG_RPVER_SP: return "MSG_RPVER_SP";
58 case HTTP_MSG_RPCODE: return "MSG_RPCODE";
59 case HTTP_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
60 case HTTP_MSG_RPREASON: return "MSG_RPREASON";
61 case HTTP_MSG_RPLINE_END: return "MSG_RPLINE_END";
62 case HTTP_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
63 case HTTP_MSG_HDR_NAME: return "MSG_HDR_NAME";
64 case HTTP_MSG_HDR_COL: return "MSG_HDR_COL";
65 case HTTP_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
66 case HTTP_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
67 case HTTP_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
68 case HTTP_MSG_HDR_VAL: return "MSG_HDR_VAL";
69 case HTTP_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
70 case HTTP_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
71 case HTTP_MSG_LAST_LF: return "MSG_LAST_LF";
72 case HTTP_MSG_ERROR: return "MSG_ERROR";
73 case HTTP_MSG_BODY: return "MSG_BODY";
74 case HTTP_MSG_100_SENT: return "MSG_100_SENT";
75 case HTTP_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
76 case HTTP_MSG_DATA: return "MSG_DATA";
77 case HTTP_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
78 case HTTP_MSG_TRAILERS: return "MSG_TRAILERS";
79 case HTTP_MSG_ENDING: return "MSG_ENDING";
80 case HTTP_MSG_DONE: return "MSG_DONE";
81 case HTTP_MSG_CLOSING: return "MSG_CLOSING";
82 case HTTP_MSG_CLOSED: return "MSG_CLOSED";
83 case HTTP_MSG_TUNNEL: return "MSG_TUNNEL";
84 default: return "MSG_??????";
85 }
86}
87
Willy Tarreau801250e2018-09-11 11:45:04 +020088/* for debugging, reports the HTTP/1 message state name */
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +020089static inline const char *h1m_state_str(enum h1m_state msg_state)
Willy Tarreau801250e2018-09-11 11:45:04 +020090{
91 switch (msg_state) {
92 case H1_MSG_RQBEFORE: return "MSG_RQBEFORE";
93 case H1_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
94 case H1_MSG_RQMETH: return "MSG_RQMETH";
95 case H1_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
96 case H1_MSG_RQURI: return "MSG_RQURI";
97 case H1_MSG_RQURI_SP: return "MSG_RQURI_SP";
98 case H1_MSG_RQVER: return "MSG_RQVER";
99 case H1_MSG_RQLINE_END: return "MSG_RQLINE_END";
100 case H1_MSG_RPBEFORE: return "MSG_RPBEFORE";
101 case H1_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
102 case H1_MSG_RPVER: return "MSG_RPVER";
103 case H1_MSG_RPVER_SP: return "MSG_RPVER_SP";
104 case H1_MSG_RPCODE: return "MSG_RPCODE";
105 case H1_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
106 case H1_MSG_RPREASON: return "MSG_RPREASON";
107 case H1_MSG_RPLINE_END: return "MSG_RPLINE_END";
108 case H1_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
109 case H1_MSG_HDR_NAME: return "MSG_HDR_NAME";
110 case H1_MSG_HDR_COL: return "MSG_HDR_COL";
111 case H1_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
112 case H1_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
113 case H1_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
114 case H1_MSG_HDR_VAL: return "MSG_HDR_VAL";
115 case H1_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
116 case H1_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
117 case H1_MSG_LAST_LF: return "MSG_LAST_LF";
Willy Tarreau801250e2018-09-11 11:45:04 +0200118 case H1_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
119 case H1_MSG_DATA: return "MSG_DATA";
120 case H1_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
121 case H1_MSG_TRAILERS: return "MSG_TRAILERS";
Willy Tarreau801250e2018-09-11 11:45:04 +0200122 case H1_MSG_DONE: return "MSG_DONE";
Willy Tarreau801250e2018-09-11 11:45:04 +0200123 case H1_MSG_TUNNEL: return "MSG_TUNNEL";
124 default: return "MSG_??????";
125 }
126}
127
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200128/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
129 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
130 * in order to include this part into the next forwarding phase. Note that the
Willy Tarreauc0973c62018-06-14 15:53:21 +0200131 * caller must ensure that head+start points to the first byte to parse. It
132 * returns the number of bytes parsed on success, so the caller can set msg_state
133 * to HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
Willy Tarreaub2892562017-09-21 11:33:54 +0200134 * change anything and returns zero. Otherwise it returns a negative value
135 * indicating the error positionn relative to <stop>. Note: this function is
136 * designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200137 */
Willy Tarreaub2892562017-09-21 11:33:54 +0200138static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200139{
Willy Tarreauc0973c62018-06-14 15:53:21 +0200140 const char *ptr = b_peek(buf, start);
Willy Tarreaub2892562017-09-21 11:33:54 +0200141 int bytes = 1;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200142
Joseph Herlant30bc5092018-11-25 10:52:20 -0800143 /* NB: we'll check data availability at the end. It's not a
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200144 * problem because whatever we match first will be checked
145 * against the correct length.
146 */
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200147 if (*ptr == '\r') {
148 bytes++;
149 ptr++;
Willy Tarreauc0973c62018-06-14 15:53:21 +0200150 if (ptr >= b_wrap(buf))
Willy Tarreau591d4452018-06-15 17:21:00 +0200151 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200152 }
153
Willy Tarreaub2892562017-09-21 11:33:54 +0200154 if (bytes > stop - start)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200155 return 0;
156
Willy Tarreauc0973c62018-06-14 15:53:21 +0200157 if (*ptr != '\n') // negative position to stop
158 return ptr - __b_peek(buf, stop);
Willy Tarreaub2892562017-09-21 11:33:54 +0200159
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200160 return bytes;
161}
162
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200163/* Parse the chunk size start at buf + start and stops before buf + stop. The
164 * positions are relative to the buffer's head.
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200165 * It returns the chunk size in <res> and the amount of bytes read this way :
166 * < 0 : error at this position relative to <stop>
167 * = 0 : not enough bytes to read a complete chunk size
168 * > 0 : number of bytes successfully read that the caller can skip
169 * On success, the caller should adjust its msg->next to point to the first
170 * byte of data after the chunk size, so that we know we can forward exactly
171 * msg->next bytes, and msg->sol to contain the exact number of bytes forming
172 * the chunk size. That way it is always possible to differentiate between the
173 * start of the body and the start of the data. Note: this function is designed
174 * to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200175 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200176static inline int h1_parse_chunk_size(const struct buffer *buf, int start, int stop, unsigned int *res)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200177{
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200178 const char *ptr = b_peek(buf, start);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200179 const char *ptr_old = ptr;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200180 const char *end = b_wrap(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200181 unsigned int chunk = 0;
182
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100183 stop -= start; // bytes left
184 start = stop; // bytes to transfer
185
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200186 /* The chunk size is in the following form, though we are only
187 * interested in the size and CRLF :
188 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
189 */
190 while (1) {
191 int c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100192 if (!stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200193 return 0;
194 c = hex2i(*ptr);
195 if (c < 0) /* not a hex digit anymore */
196 break;
197 if (unlikely(++ptr >= end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200198 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200199 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
200 goto error;
201 chunk = (chunk << 4) + c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100202 stop--;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200203 }
204
205 /* empty size not allowed */
206 if (unlikely(ptr == ptr_old))
207 goto error;
208
209 while (HTTP_IS_SPHT(*ptr)) {
210 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200211 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100212 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200213 return 0;
214 }
215
216 /* Up to there, we know that at least one byte is present at *ptr. Check
217 * for the end of chunk size.
218 */
219 while (1) {
220 if (likely(HTTP_IS_CRLF(*ptr))) {
221 /* we now have a CR or an LF at ptr */
222 if (likely(*ptr == '\r')) {
223 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200224 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100225 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200226 return 0;
227 }
228
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100229 if (*ptr != '\n')
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200230 goto error;
231 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200232 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100233 --stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200234 /* done */
235 break;
236 }
237 else if (likely(*ptr == ';')) {
238 /* chunk extension, ends at next CRLF */
239 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200240 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100241 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200242 return 0;
243
244 while (!HTTP_IS_CRLF(*ptr)) {
245 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200246 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100247 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200248 return 0;
249 }
250 /* we have a CRLF now, loop above */
251 continue;
252 }
253 else
254 goto error;
255 }
256
257 /* OK we found our CRLF and now <ptr> points to the next byte, which may
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200258 * or may not be present. Let's return the number of bytes parsed.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200259 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200260 *res = chunk;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100261 return start - stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200262 error:
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200263 *res = 0; // just to stop gcc's -Wuninitialized warning :-(
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100264 return -stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200265}
266
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200267/* initializes an H1 message for a request */
268static inline struct h1m *h1m_init_req(struct h1m *h1m)
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200269{
Willy Tarreau801250e2018-09-11 11:45:04 +0200270 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreaub3b01522018-09-11 11:51:31 +0200271 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200272 h1m->flags = H1_MF_NONE;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200273 h1m->curr_len = 0;
274 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200275 h1m->err_pos = -2;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200276 h1m->err_state = 0;
277 return h1m;
278}
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200279
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200280/* initializes an H1 message for a response */
281static inline struct h1m *h1m_init_res(struct h1m *h1m)
282{
283 h1m->state = H1_MSG_RPBEFORE;
284 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200285 h1m->flags = H1_MF_RESP;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200286 h1m->curr_len = 0;
287 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200288 h1m->err_pos = -2;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200289 h1m->err_state = 0;
290 return h1m;
291}
292
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200293#endif /* _PROTO_H1_H */