blob: 7d8ea61b570d4edee6074981987b696e78d36ead [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 */
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +020043static inline const char *h1m_state_str(enum h1m_state msg_state)
Willy Tarreau801250e2018-09-11 11:45:04 +020044{
45 switch (msg_state) {
46 case H1_MSG_RQBEFORE: return "MSG_RQBEFORE";
47 case H1_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
48 case H1_MSG_RQMETH: return "MSG_RQMETH";
49 case H1_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
50 case H1_MSG_RQURI: return "MSG_RQURI";
51 case H1_MSG_RQURI_SP: return "MSG_RQURI_SP";
52 case H1_MSG_RQVER: return "MSG_RQVER";
53 case H1_MSG_RQLINE_END: return "MSG_RQLINE_END";
54 case H1_MSG_RPBEFORE: return "MSG_RPBEFORE";
55 case H1_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
56 case H1_MSG_RPVER: return "MSG_RPVER";
57 case H1_MSG_RPVER_SP: return "MSG_RPVER_SP";
58 case H1_MSG_RPCODE: return "MSG_RPCODE";
59 case H1_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
60 case H1_MSG_RPREASON: return "MSG_RPREASON";
61 case H1_MSG_RPLINE_END: return "MSG_RPLINE_END";
62 case H1_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
63 case H1_MSG_HDR_NAME: return "MSG_HDR_NAME";
64 case H1_MSG_HDR_COL: return "MSG_HDR_COL";
65 case H1_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
66 case H1_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
67 case H1_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
68 case H1_MSG_HDR_VAL: return "MSG_HDR_VAL";
69 case H1_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
70 case H1_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
71 case H1_MSG_LAST_LF: return "MSG_LAST_LF";
Willy Tarreau801250e2018-09-11 11:45:04 +020072 case H1_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
73 case H1_MSG_DATA: return "MSG_DATA";
74 case H1_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
75 case H1_MSG_TRAILERS: return "MSG_TRAILERS";
Willy Tarreau801250e2018-09-11 11:45:04 +020076 case H1_MSG_DONE: return "MSG_DONE";
Willy Tarreau801250e2018-09-11 11:45:04 +020077 case H1_MSG_TUNNEL: return "MSG_TUNNEL";
78 default: return "MSG_??????";
79 }
80}
81
Willy Tarreaudb4893d2017-09-21 08:40:02 +020082/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
83 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
84 * in order to include this part into the next forwarding phase. Note that the
Willy Tarreauc0973c62018-06-14 15:53:21 +020085 * caller must ensure that head+start points to the first byte to parse. It
86 * returns the number of bytes parsed on success, so the caller can set msg_state
87 * to HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
Willy Tarreaub2892562017-09-21 11:33:54 +020088 * change anything and returns zero. Otherwise it returns a negative value
89 * indicating the error positionn relative to <stop>. Note: this function is
90 * designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +020091 */
Willy Tarreaub2892562017-09-21 11:33:54 +020092static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +020093{
Willy Tarreauc0973c62018-06-14 15:53:21 +020094 const char *ptr = b_peek(buf, start);
Willy Tarreaub2892562017-09-21 11:33:54 +020095 int bytes = 1;
Willy Tarreaudb4893d2017-09-21 08:40:02 +020096
Joseph Herlant30bc5092018-11-25 10:52:20 -080097 /* NB: we'll check data availability at the end. It's not a
Willy Tarreaudb4893d2017-09-21 08:40:02 +020098 * problem because whatever we match first will be checked
99 * against the correct length.
100 */
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200101 if (*ptr == '\r') {
102 bytes++;
103 ptr++;
Willy Tarreauc0973c62018-06-14 15:53:21 +0200104 if (ptr >= b_wrap(buf))
Willy Tarreau591d4452018-06-15 17:21:00 +0200105 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200106 }
107
Willy Tarreaub2892562017-09-21 11:33:54 +0200108 if (bytes > stop - start)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200109 return 0;
110
Willy Tarreauc0973c62018-06-14 15:53:21 +0200111 if (*ptr != '\n') // negative position to stop
112 return ptr - __b_peek(buf, stop);
Willy Tarreaub2892562017-09-21 11:33:54 +0200113
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200114 return bytes;
115}
116
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200117/* Parse the chunk size start at buf + start and stops before buf + stop. The
118 * positions are relative to the buffer's head.
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200119 * It returns the chunk size in <res> and the amount of bytes read this way :
120 * < 0 : error at this position relative to <stop>
121 * = 0 : not enough bytes to read a complete chunk size
122 * > 0 : number of bytes successfully read that the caller can skip
123 * On success, the caller should adjust its msg->next to point to the first
124 * byte of data after the chunk size, so that we know we can forward exactly
125 * msg->next bytes, and msg->sol to contain the exact number of bytes forming
126 * the chunk size. That way it is always possible to differentiate between the
127 * start of the body and the start of the data. Note: this function is designed
128 * to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200129 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200130static inline int h1_parse_chunk_size(const struct buffer *buf, int start, int stop, unsigned int *res)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200131{
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200132 const char *ptr = b_peek(buf, start);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200133 const char *ptr_old = ptr;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +0200134 const char *end = b_wrap(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200135 unsigned int chunk = 0;
136
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100137 stop -= start; // bytes left
138 start = stop; // bytes to transfer
139
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200140 /* The chunk size is in the following form, though we are only
141 * interested in the size and CRLF :
142 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
143 */
144 while (1) {
145 int c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100146 if (!stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200147 return 0;
148 c = hex2i(*ptr);
149 if (c < 0) /* not a hex digit anymore */
150 break;
151 if (unlikely(++ptr >= end))
Willy Tarreau591d4452018-06-15 17:21:00 +0200152 ptr = b_orig(buf);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200153 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
154 goto error;
155 chunk = (chunk << 4) + c;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100156 stop--;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200157 }
158
159 /* empty size not allowed */
160 if (unlikely(ptr == ptr_old))
161 goto error;
162
163 while (HTTP_IS_SPHT(*ptr)) {
164 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200165 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100166 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200167 return 0;
168 }
169
170 /* Up to there, we know that at least one byte is present at *ptr. Check
171 * for the end of chunk size.
172 */
173 while (1) {
174 if (likely(HTTP_IS_CRLF(*ptr))) {
175 /* we now have a CR or an LF at ptr */
176 if (likely(*ptr == '\r')) {
177 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200178 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100179 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200180 return 0;
181 }
182
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100183 if (*ptr != '\n')
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200184 goto error;
185 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200186 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100187 --stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200188 /* done */
189 break;
190 }
191 else if (likely(*ptr == ';')) {
192 /* chunk extension, ends at next CRLF */
193 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200194 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100195 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200196 return 0;
197
198 while (!HTTP_IS_CRLF(*ptr)) {
199 if (++ptr >= end)
Willy Tarreau591d4452018-06-15 17:21:00 +0200200 ptr = b_orig(buf);
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100201 if (--stop == 0)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200202 return 0;
203 }
204 /* we have a CRLF now, loop above */
205 continue;
206 }
207 else
208 goto error;
209 }
210
211 /* OK we found our CRLF and now <ptr> points to the next byte, which may
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200212 * or may not be present. Let's return the number of bytes parsed.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200213 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200214 *res = chunk;
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100215 return start - stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200216 error:
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200217 *res = 0; // just to stop gcc's -Wuninitialized warning :-(
Willy Tarreaub15e3fe2017-11-10 11:17:08 +0100218 return -stop;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200219}
220
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200221/* initializes an H1 message for a request */
222static inline struct h1m *h1m_init_req(struct h1m *h1m)
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200223{
Willy Tarreau801250e2018-09-11 11:45:04 +0200224 h1m->state = H1_MSG_RQBEFORE;
Willy Tarreaub3b01522018-09-11 11:51:31 +0200225 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200226 h1m->flags = H1_MF_NONE;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200227 h1m->curr_len = 0;
228 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200229 h1m->err_pos = -2;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200230 h1m->err_state = 0;
231 return h1m;
232}
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200233
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200234/* initializes an H1 message for a response */
235static inline struct h1m *h1m_init_res(struct h1m *h1m)
236{
237 h1m->state = H1_MSG_RPBEFORE;
238 h1m->next = 0;
Willy Tarreauccaf2332018-09-11 16:47:23 +0200239 h1m->flags = H1_MF_RESP;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200240 h1m->curr_len = 0;
241 h1m->body_len = 0;
Willy Tarreaubbf38232018-09-12 09:08:54 +0200242 h1m->err_pos = -2;
Willy Tarreau7f437ff2018-09-11 13:51:19 +0200243 h1m->err_state = 0;
244 return h1m;
245}
246
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200247#endif /* _PROTO_H1_H */