blob: b7229507b9dd9c3499a905d24ef143818ecbae9b [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 Tarreau794f9af2017-07-26 09:07:47 +020028#include <common/http-hdr.h>
Willy Tarreaudb4893d2017-09-21 08:40:02 +020029#include <common/standard.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020030#include <types/h1.h>
Willy Tarreaudb4893d2017-09-21 08:40:02 +020031#include <types/proto_http.h>
Willy Tarreau8740c8b2017-09-21 10:22:25 +020032#include <proto/hdr_idx.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020033
34extern const uint8_t h1_char_classes[256];
Willy Tarreau8740c8b2017-09-21 10:22:25 +020035const char *http_parse_reqline(struct http_msg *msg,
36 enum h1_state state, const char *ptr, const char *end,
37 unsigned int *ret_ptr, enum h1_state *ret_state);
38const char *http_parse_stsline(struct http_msg *msg,
39 enum h1_state state, const char *ptr, const char *end,
40 unsigned int *ret_ptr, enum h1_state *ret_state);
41void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx);
Willy Tarreaudb4893d2017-09-21 08:40:02 +020042int http_forward_trailers(struct http_msg *msg);
Willy Tarreau794f9af2017-07-26 09:07:47 +020043int h1_headers_to_hdr_list(char *start, const char *stop,
44 struct http_hdr *hdr, unsigned int hdr_num,
45 struct h1m *h1m);
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020046
47#define H1_FLG_CTL 0x01
48#define H1_FLG_SEP 0x02
49#define H1_FLG_LWS 0x04
50#define H1_FLG_SPHT 0x08
51#define H1_FLG_CRLF 0x10
52#define H1_FLG_TOK 0x20
53#define H1_FLG_VER 0x40
54
55#define HTTP_IS_CTL(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CTL)
56#define HTTP_IS_SEP(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SEP)
57#define HTTP_IS_LWS(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_LWS)
58#define HTTP_IS_SPHT(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SPHT)
59#define HTTP_IS_CRLF(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CRLF)
60#define HTTP_IS_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_TOK)
61#define HTTP_IS_VER_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_VER)
62
63
64/* Macros used in the HTTP/1 parser, to check for the expected presence of
65 * certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
66 */
67
68
69/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
70 * <bad>.
71 */
72#define EXPECT_LF_HERE(ptr, bad, state, where) \
73 do { \
74 if (unlikely(*(ptr) != '\n')) { \
75 state = (where); \
76 goto bad; \
77 } \
78 } while (0)
79
80/* Increments pointer <ptr>, continues to label <more> if it's still below
81 * pointer <end>, or goes to <stop> and sets <state> to <where> if the end
82 * of buffer was reached.
83 */
84#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
85 do { \
86 if (likely(++(ptr) < (end))) \
87 goto more; \
88 else { \
89 state = (where); \
90 goto stop; \
91 } \
92 } while (0)
93
94/* for debugging, reports the HTTP/1 message state name */
95static inline const char *h1_msg_state_str(enum h1_state msg_state)
96{
97 switch (msg_state) {
98 case HTTP_MSG_RQBEFORE: return "MSG_RQBEFORE";
99 case HTTP_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
100 case HTTP_MSG_RQMETH: return "MSG_RQMETH";
101 case HTTP_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
102 case HTTP_MSG_RQURI: return "MSG_RQURI";
103 case HTTP_MSG_RQURI_SP: return "MSG_RQURI_SP";
104 case HTTP_MSG_RQVER: return "MSG_RQVER";
105 case HTTP_MSG_RQLINE_END: return "MSG_RQLINE_END";
106 case HTTP_MSG_RPBEFORE: return "MSG_RPBEFORE";
107 case HTTP_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
108 case HTTP_MSG_RPVER: return "MSG_RPVER";
109 case HTTP_MSG_RPVER_SP: return "MSG_RPVER_SP";
110 case HTTP_MSG_RPCODE: return "MSG_RPCODE";
111 case HTTP_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
112 case HTTP_MSG_RPREASON: return "MSG_RPREASON";
113 case HTTP_MSG_RPLINE_END: return "MSG_RPLINE_END";
114 case HTTP_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
115 case HTTP_MSG_HDR_NAME: return "MSG_HDR_NAME";
116 case HTTP_MSG_HDR_COL: return "MSG_HDR_COL";
117 case HTTP_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
118 case HTTP_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
119 case HTTP_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
120 case HTTP_MSG_HDR_VAL: return "MSG_HDR_VAL";
121 case HTTP_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
122 case HTTP_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
123 case HTTP_MSG_LAST_LF: return "MSG_LAST_LF";
124 case HTTP_MSG_ERROR: return "MSG_ERROR";
125 case HTTP_MSG_BODY: return "MSG_BODY";
126 case HTTP_MSG_100_SENT: return "MSG_100_SENT";
127 case HTTP_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
128 case HTTP_MSG_DATA: return "MSG_DATA";
129 case HTTP_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
130 case HTTP_MSG_TRAILERS: return "MSG_TRAILERS";
131 case HTTP_MSG_ENDING: return "MSG_ENDING";
132 case HTTP_MSG_DONE: return "MSG_DONE";
133 case HTTP_MSG_CLOSING: return "MSG_CLOSING";
134 case HTTP_MSG_CLOSED: return "MSG_CLOSED";
135 case HTTP_MSG_TUNNEL: return "MSG_TUNNEL";
136 default: return "MSG_??????";
137 }
138}
139
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200140/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
141 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
142 * in order to include this part into the next forwarding phase. Note that the
143 * caller must ensure that ->p points to the first byte to parse. It returns
144 * the number of bytes parsed on success, so the caller can set msg_state to
145 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
Willy Tarreaub2892562017-09-21 11:33:54 +0200146 * change anything and returns zero. Otherwise it returns a negative value
147 * indicating the error positionn relative to <stop>. Note: this function is
148 * designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200149 */
Willy Tarreaub2892562017-09-21 11:33:54 +0200150static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200151{
Willy Tarreaub2892562017-09-21 11:33:54 +0200152 const char *ptr = b_ptr(buf, start);
153 int bytes = 1;
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200154
155 /* NB: we'll check data availabilty at the end. It's not a
156 * problem because whatever we match first will be checked
157 * against the correct length.
158 */
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200159 if (*ptr == '\r') {
160 bytes++;
161 ptr++;
162 if (ptr >= buf->data + buf->size)
163 ptr = buf->data;
164 }
165
Willy Tarreaub2892562017-09-21 11:33:54 +0200166 if (bytes > stop - start)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200167 return 0;
168
Willy Tarreaub2892562017-09-21 11:33:54 +0200169 if (*ptr != '\n')
170 return -buffer_count(buf, ptr, b_ptr(buf, stop));
171
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200172 return bytes;
173}
174
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200175/* Parse the chunk size start at buf->p + start and stops before buf->p + stop.
176 * It returns the chunk size in <res> and the amount of bytes read this way :
177 * < 0 : error at this position relative to <stop>
178 * = 0 : not enough bytes to read a complete chunk size
179 * > 0 : number of bytes successfully read that the caller can skip
180 * On success, the caller should adjust its msg->next to point to the first
181 * byte of data after the chunk size, so that we know we can forward exactly
182 * msg->next bytes, and msg->sol to contain the exact number of bytes forming
183 * the chunk size. That way it is always possible to differentiate between the
184 * start of the body and the start of the data. Note: this function is designed
185 * to parse wrapped CRLF at the end of the buffer.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200186 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200187static inline int h1_parse_chunk_size(const struct buffer *buf, int start, int stop, unsigned int *res)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200188{
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200189 const char *ptr = b_ptr(buf, start);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200190 const char *ptr_old = ptr;
191 const char *end = buf->data + buf->size;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200192 const char *ptr_stop = b_ptr(buf, stop);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200193 unsigned int chunk = 0;
194
195 /* The chunk size is in the following form, though we are only
196 * interested in the size and CRLF :
197 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
198 */
199 while (1) {
200 int c;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200201 if (ptr == ptr_stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200202 return 0;
203 c = hex2i(*ptr);
204 if (c < 0) /* not a hex digit anymore */
205 break;
206 if (unlikely(++ptr >= end))
207 ptr = buf->data;
208 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
209 goto error;
210 chunk = (chunk << 4) + c;
211 }
212
213 /* empty size not allowed */
214 if (unlikely(ptr == ptr_old))
215 goto error;
216
217 while (HTTP_IS_SPHT(*ptr)) {
218 if (++ptr >= end)
219 ptr = buf->data;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200220 if (unlikely(ptr == ptr_stop))
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200221 return 0;
222 }
223
224 /* Up to there, we know that at least one byte is present at *ptr. Check
225 * for the end of chunk size.
226 */
227 while (1) {
228 if (likely(HTTP_IS_CRLF(*ptr))) {
229 /* we now have a CR or an LF at ptr */
230 if (likely(*ptr == '\r')) {
231 if (++ptr >= end)
232 ptr = buf->data;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200233 if (ptr == ptr_stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200234 return 0;
235 }
236
237 if (unlikely(*ptr != '\n'))
238 goto error;
239 if (++ptr >= end)
240 ptr = buf->data;
241 /* done */
242 break;
243 }
244 else if (likely(*ptr == ';')) {
245 /* chunk extension, ends at next CRLF */
246 if (++ptr >= end)
247 ptr = buf->data;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200248 if (ptr == ptr_stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200249 return 0;
250
251 while (!HTTP_IS_CRLF(*ptr)) {
252 if (++ptr >= end)
253 ptr = buf->data;
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200254 if (ptr == ptr_stop)
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200255 return 0;
256 }
257 /* we have a CRLF now, loop above */
258 continue;
259 }
260 else
261 goto error;
262 }
263
264 /* OK we found our CRLF and now <ptr> points to the next byte, which may
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200265 * or may not be present. Let's return the number of bytes parsed.
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200266 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200267 *res = chunk;
268 return (ptr - ptr_old) >= 0 ? (ptr - ptr_old) : (ptr - ptr_old + buf->size);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200269 error:
Willy Tarreaue56cdd32017-09-21 08:36:33 +0200270 *res = 0; // just to stop gcc's -Wuninitialized warning :-(
271 return -buffer_count(buf, ptr, ptr_stop);
Willy Tarreaudb4893d2017-09-21 08:40:02 +0200272}
273
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200274/* initializes an H1 message */
275static inline struct h1m *h1m_init(struct h1m *h1m)
276{
277 h1m->state = HTTP_MSG_RQBEFORE;
Willy Tarreaud22e83a2017-10-31 08:02:24 +0100278 h1m->status = 0;
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200279 h1m->flags = 0;
280 h1m->curr_len = 0;
281 h1m->body_len = 0;
282 h1m->err_pos = 0;
283 h1m->err_state = 0;
284 return h1m;
285}
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200286
287#endif /* _PROTO_H1_H */