blob: a0fa7a7c5932b8944a447ec8ae4ea701da7b32cd [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 Tarreaudb4893d2017-09-21 08:40:02 +020028#include <common/standard.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020029#include <types/h1.h>
Willy Tarreaudb4893d2017-09-21 08:40:02 +020030#include <types/proto_http.h>
Willy Tarreau8740c8b2017-09-21 10:22:25 +020031#include <proto/hdr_idx.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020032
33extern const uint8_t h1_char_classes[256];
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 Tarreau0da5b3b2017-09-21 09:30:46 +020042
43#define H1_FLG_CTL 0x01
44#define H1_FLG_SEP 0x02
45#define H1_FLG_LWS 0x04
46#define H1_FLG_SPHT 0x08
47#define H1_FLG_CRLF 0x10
48#define H1_FLG_TOK 0x20
49#define H1_FLG_VER 0x40
50
51#define HTTP_IS_CTL(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CTL)
52#define HTTP_IS_SEP(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SEP)
53#define HTTP_IS_LWS(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_LWS)
54#define HTTP_IS_SPHT(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SPHT)
55#define HTTP_IS_CRLF(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CRLF)
56#define HTTP_IS_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_TOK)
57#define HTTP_IS_VER_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_VER)
58
59
60/* Macros used in the HTTP/1 parser, to check for the expected presence of
61 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
62 */
63
64
65/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
66 * <bad>.
67 */
68#define EXPECT_LF_HERE(ptr, bad, state, where) \
69 do { \
70 if (unlikely(*(ptr) != '\n')) { \
71 state = (where); \
72 goto bad; \
73 } \
74 } while (0)
75
76/* Increments pointer <ptr>, continues to label <more> if it's still below
77 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
78 * of buffer was reached.
79 */
80#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
81 do { \
82 if (likely(++(ptr) < (end))) \
83 goto more; \
84 else { \
85 state = (where); \
86 goto stop; \
87 } \
88 } while (0)
89
90/* for debugging, reports the HTTP/1 message state name */
91static inline const char *h1_msg_state_str(enum h1_state msg_state)
92{
93 switch (msg_state) {
94 case HTTP_MSG_RQBEFORE: return "MSG_RQBEFORE";
95 case HTTP_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
96 case HTTP_MSG_RQMETH: return "MSG_RQMETH";
97 case HTTP_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
98 case HTTP_MSG_RQURI: return "MSG_RQURI";
99 case HTTP_MSG_RQURI_SP: return "MSG_RQURI_SP";
100 case HTTP_MSG_RQVER: return "MSG_RQVER";
101 case HTTP_MSG_RQLINE_END: return "MSG_RQLINE_END";
102 case HTTP_MSG_RPBEFORE: return "MSG_RPBEFORE";
103 case HTTP_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
104 case HTTP_MSG_RPVER: return "MSG_RPVER";
105 case HTTP_MSG_RPVER_SP: return "MSG_RPVER_SP";
106 case HTTP_MSG_RPCODE: return "MSG_RPCODE";
107 case HTTP_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
108 case HTTP_MSG_RPREASON: return "MSG_RPREASON";
109 case HTTP_MSG_RPLINE_END: return "MSG_RPLINE_END";
110 case HTTP_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
111 case HTTP_MSG_HDR_NAME: return "MSG_HDR_NAME";
112 case HTTP_MSG_HDR_COL: return "MSG_HDR_COL";
113 case HTTP_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
114 case HTTP_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
115 case HTTP_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
116 case HTTP_MSG_HDR_VAL: return "MSG_HDR_VAL";
117 case HTTP_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
118 case HTTP_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
119 case HTTP_MSG_LAST_LF: return "MSG_LAST_LF";
120 case HTTP_MSG_ERROR: return "MSG_ERROR";
121 case HTTP_MSG_BODY: return "MSG_BODY";
122 case HTTP_MSG_100_SENT: return "MSG_100_SENT";
123 case HTTP_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
124 case HTTP_MSG_DATA: return "MSG_DATA";
125 case HTTP_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
126 case HTTP_MSG_TRAILERS: return "MSG_TRAILERS";
127 case HTTP_MSG_ENDING: return "MSG_ENDING";
128 case HTTP_MSG_DONE: return "MSG_DONE";
129 case HTTP_MSG_CLOSING: return "MSG_CLOSING";
130 case HTTP_MSG_CLOSED: return "MSG_CLOSED";
131 case HTTP_MSG_TUNNEL: return "MSG_TUNNEL";
132 default: return "MSG_??????";
133 }
134}
135
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200136/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
137 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
138 * in order to include this part into the next forwarding phase. Note that the
139 * caller must ensure that ->p points to the first byte to parse. It returns
140 * the number of bytes parsed on success, so the caller can set msg_state to
141 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
142 * change anything and returns zero. If a parse error is encountered, the
143 * function returns < 0. Note: this function is designed to parse wrapped CRLF
144 * at the end of the buffer.
145 */
146static inline int http_skip_chunk_crlf(struct http_msg *msg)
147{
148 const struct buffer *buf = msg->chn->buf;
149 const char *ptr;
150 int bytes;
151
152 /* NB: we'll check data availabilty at the end. It's not a
153 * problem because whatever we match first will be checked
154 * against the correct length.
155 */
156 bytes = 1;
157 ptr = b_ptr(buf, msg->next);
158 if (*ptr == '\r') {
159 bytes++;
160 ptr++;
161 if (ptr >= buf->data + buf->size)
162 ptr = buf->data;
163 }
164
165 if (msg->next + bytes > buf->i)
166 return 0;
167
168 if (*ptr != '\n') {
169 msg->err_pos = buffer_count(buf, buf->p, ptr);
170 return -1;
171 }
172 return bytes;
173}
174
175/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
176 * point to the first byte of data after the chunk size, so that we know we can
177 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
178 * forming the chunk size. That way it is always possible to differentiate
179 * between the start of the body and the start of the data. Return the number
180 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
181 * this function is designed to parse wrapped CRLF at the end of the buffer.
182 */
183static inline int http_parse_chunk_size(struct http_msg *msg)
184{
185 const struct buffer *buf = msg->chn->buf;
186 const char *ptr = b_ptr(buf, msg->next);
187 const char *ptr_old = ptr;
188 const char *end = buf->data + buf->size;
189 const char *stop = bi_end(buf);
190 unsigned int chunk = 0;
191
192 /* The chunk size is in the following form, though we are only
193 * interested in the size and CRLF :
194 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
195 */
196 while (1) {
197 int c;
198 if (ptr == stop)
199 return 0;
200 c = hex2i(*ptr);
201 if (c < 0) /* not a hex digit anymore */
202 break;
203 if (unlikely(++ptr >= end))
204 ptr = buf->data;
205 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
206 goto error;
207 chunk = (chunk << 4) + c;
208 }
209
210 /* empty size not allowed */
211 if (unlikely(ptr == ptr_old))
212 goto error;
213
214 while (HTTP_IS_SPHT(*ptr)) {
215 if (++ptr >= end)
216 ptr = buf->data;
217 if (unlikely(ptr == stop))
218 return 0;
219 }
220
221 /* Up to there, we know that at least one byte is present at *ptr. Check
222 * for the end of chunk size.
223 */
224 while (1) {
225 if (likely(HTTP_IS_CRLF(*ptr))) {
226 /* we now have a CR or an LF at ptr */
227 if (likely(*ptr == '\r')) {
228 if (++ptr >= end)
229 ptr = buf->data;
230 if (ptr == stop)
231 return 0;
232 }
233
234 if (unlikely(*ptr != '\n'))
235 goto error;
236 if (++ptr >= end)
237 ptr = buf->data;
238 /* done */
239 break;
240 }
241 else if (likely(*ptr == ';')) {
242 /* chunk extension, ends at next CRLF */
243 if (++ptr >= end)
244 ptr = buf->data;
245 if (ptr == stop)
246 return 0;
247
248 while (!HTTP_IS_CRLF(*ptr)) {
249 if (++ptr >= end)
250 ptr = buf->data;
251 if (ptr == stop)
252 return 0;
253 }
254 /* we have a CRLF now, loop above */
255 continue;
256 }
257 else
258 goto error;
259 }
260
261 /* OK we found our CRLF and now <ptr> points to the next byte, which may
262 * or may not be present. We save the number of bytes parsed into
263 * msg->sol.
264 */
265 msg->sol = ptr - ptr_old;
266 if (unlikely(ptr < ptr_old))
267 msg->sol += buf->size;
268 msg->chunk_len = chunk;
269 msg->body_len += chunk;
270 return msg->sol;
271 error:
272 msg->err_pos = buffer_count(buf, buf->p, ptr);
273 return -1;
274}
275
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200276
277#endif /* _PROTO_H1_H */