blob: a7b16aa257d298904cb6bec71f85c6259b1f3fbb [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau3667d5d2009-10-18 19:50:43 +02002 * include/types/proto_http.h
3 * This file contains HTTP protocol definitions.
4 *
Willy Tarreauff011f22011-01-06 17:51:27 +01005 * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu
Willy Tarreau3667d5d2009-10-18 19:50:43 +02006 *
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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_PROTO_HTTP_H
23#define _TYPES_PROTO_HTTP_H
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau3bac9ff2007-03-18 17:31:28 +010027#include <types/buffers.h>
28#include <types/hdr_idx.h>
29
Willy Tarreaufcffa692010-01-10 14:21:19 +010030/* These are the flags that are found in txn->flags */
Willy Tarreau3d300592007-03-18 18:34:41 +010031
32/* action flags */
33#define TX_CLDENY 0x00000001 /* a client header matches a deny regex */
34#define TX_CLALLOW 0x00000002 /* a client header matches an allow regex */
35#define TX_SVDENY 0x00000004 /* a server header matches a deny regex */
36#define TX_SVALLOW 0x00000008 /* a server header matches an allow regex */
37#define TX_CLTARPIT 0x00000010 /* the session is tarpitted (anti-dos) */
Willy Tarreaub608feb2010-01-02 22:47:18 +010038
Willy Tarreau92954fd2010-10-06 19:38:55 +020039/* transaction flags dedicated to cookies : bits values 0x20 to 0x80 (0-7 shift 5) */
Willy Tarreau3d300592007-03-18 18:34:41 +010040#define TX_CK_NONE 0x00000000 /* this session had no cookie */
Willy Tarreau92954fd2010-10-06 19:38:55 +020041#define TX_CK_INVALID 0x00000020 /* this session had a cookie which matches no server */
42#define TX_CK_DOWN 0x00000040 /* this session had cookie matching a down server */
43#define TX_CK_VALID 0x00000060 /* this session had cookie matching a valid server */
Willy Tarreaub761ec42010-10-07 15:28:23 +020044#define TX_CK_EXPIRED 0x00000080 /* this session had an expired cookie (idle for too long) */
45#define TX_CK_OLD 0x000000A0 /* this session had too old a cookie (offered too long ago) */
Willy Tarreau92954fd2010-10-06 19:38:55 +020046#define TX_CK_MASK 0x000000E0 /* mask to get this session's cookie flags */
47#define TX_CK_SHIFT 5 /* bit shift */
Willy Tarreau3d300592007-03-18 18:34:41 +010048
Willy Tarreauf1348312010-10-07 15:54:11 +020049/* response cookie information, bits values 0x100 to 0x700 (0-7 shift 8) */
50#define TX_SCK_NONE 0x00000000 /* no cookie found in the response */
51#define TX_SCK_FOUND 0x00000100 /* a persistence cookie was found and forwarded */
52#define TX_SCK_DELETED 0x00000200 /* an existing persistence cookie was deleted */
53#define TX_SCK_INSERTED 0x00000300 /* a persistence cookie was inserted */
54#define TX_SCK_REPLACED 0x00000400 /* a persistence cookie was present and rewritten */
55#define TX_SCK_UPDATED 0x00000500 /* an expirable persistence cookie was updated */
Willy Tarreau3d300592007-03-18 18:34:41 +010056#define TX_SCK_MASK 0x00000700 /* mask to get the set-cookie field */
Willy Tarreau3d300592007-03-18 18:34:41 +010057#define TX_SCK_SHIFT 8 /* bit shift */
Willy Tarreauf1348312010-10-07 15:54:11 +020058
59#define TX_SCK_PRESENT 0x00000800 /* a cookie was found in the server's response */
Willy Tarreau3d300592007-03-18 18:34:41 +010060
61/* cacheability management, bits values 0x1000 to 0x3000 (0-3 shift 12) */
62#define TX_CACHEABLE 0x00001000 /* at least part of the response is cacheable */
63#define TX_CACHE_COOK 0x00002000 /* a cookie in the response is cacheable */
64#define TX_CACHE_SHIFT 12 /* bit shift */
65
Willy Tarreau5b154472009-12-21 20:11:07 +010066/* request and response HTTP version */
67#define TX_REQ_VER_11 0x00004000 /* the request is HTTP/1.1 or above */
68#define TX_RES_VER_11 0x00008000 /* the response is HTTP/1.1 or above */
Willy Tarreau3667d5d2009-10-18 19:50:43 +020069
70/* report presence of transfer-encoding:chunked and content-length headers */
71#define TX_REQ_CNT_LEN 0x00010000 /* content-length present in the request */
72#define TX_REQ_TE_CHNK 0x00020000 /* transfer-encoding: chunked present in the request */
73#define TX_RES_CNT_LEN 0x00040000 /* content-length present in the response */
74#define TX_RES_TE_CHNK 0x00080000 /* transfer-encoding: chunked present in the response */
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau5b154472009-12-21 20:11:07 +010076/* indicate how we *want* the connection to behave, regardless of what is in
77 * the headers. We have 4 possible values right now :
78 * - WANT_TUN : will be a tunnel (default when nothing configured or with CONNECT).
79 * - WANT_KAL : try to maintain keep-alive
80 * - WANT_SCL : enforce close on the server side
81 * - WANT_CLO : enforce close on both sides
82 */
83#define TX_CON_WANT_TUN 0x00000000 /* note: it's important that it is 0 (init) */
84#define TX_CON_WANT_KAL 0x00100000
85#define TX_CON_WANT_SCL 0x00200000
86#define TX_CON_WANT_CLO 0x00300000
87#define TX_CON_WANT_MSK 0x00300000 /* this is the mask to get the bits */
88
Willy Tarreaubbf0b372010-01-18 16:54:40 +010089#define TX_CON_CLO_SET 0x00400000 /* "connection: close" is now set */
90#define TX_CON_KAL_SET 0x00800000 /* "connection: keep-alive" is now set */
Willy Tarreau5b154472009-12-21 20:11:07 +010091
Willy Tarreaue8e785b2009-12-26 15:34:26 +010092/* if either of these flags is not set, we may be forced to complete an
93 * connection as a half-way tunnel. For instance, if no content-length
94 * appears in a 1.1 response, but the request is correctly sized.
95 */
96#define TX_REQ_XFER_LEN 0x01000000 /* request xfer size can be determined */
97#define TX_RES_XFER_LEN 0x02000000 /* response xfer size can be determined */
Willy Tarreaufcffa692010-01-10 14:21:19 +010098#define TX_WAIT_NEXT_RQ 0x04000000 /* waiting for the second request to start, use keep-alive timeout */
Willy Tarreau5b154472009-12-21 20:11:07 +010099
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100100#define TX_HDR_CONN_PRS 0x08000000 /* "connection" header already parsed (req or res), results below */
101#define TX_HDR_CONN_CLO 0x10000000 /* "Connection: close" was present at least once */
102#define TX_HDR_CONN_KAL 0x20000000 /* "Connection: keep-alive" was present at least once */
Willy Tarreau88d349d2010-01-25 12:15:43 +0100103#define TX_USE_PX_CONN 0x40000000 /* Use "Proxy-Connection" instead of "Connection" */
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100104
Willy Tarreau92954fd2010-10-06 19:38:55 +0200105/* used only for keep-alive purposes, to indicate we're on a second transaction */
106#define TX_NOT_FIRST 0x80000000 /* the transaction is not the first one */
107/* no more room for transaction flags ! */
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100109/* The HTTP parser is more complex than it looks like, because we have to
110 * support multi-line headers and any number of spaces between the colon and
111 * the value.
112 *
113 * All those examples must work :
114
115 Hdr1:val1\r\n
116 Hdr1: val1\r\n
117 Hdr1:\t val1\r\n
118 Hdr1: \r\n
119 val1\r\n
120 Hdr1:\r\n
121 val1\n
122 \tval2\r\n
123 val3\n
124
125 *
126 */
127
Willy Tarreau58f10d72006-12-04 02:26:12 +0100128/* Possible states while parsing HTTP messages (request|response) */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100129#define HTTP_MSG_RQBEFORE 0 // request: leading LF, before start line
130#define HTTP_MSG_RQBEFORE_CR 1 // request: leading CRLF, before start line
131
132/* these ones define a request start line */
133#define HTTP_MSG_RQMETH 2 // parsing the Method
134#define HTTP_MSG_RQMETH_SP 3 // space(s) after the ethod
135#define HTTP_MSG_RQURI 4 // parsing the Request URI
136#define HTTP_MSG_RQURI_SP 5 // space(s) after the Request URI
137#define HTTP_MSG_RQVER 6 // parsing the Request Version
138#define HTTP_MSG_RQLINE_END 7 // end of request line (CR or LF)
139
140#define HTTP_MSG_RPBEFORE 8 // response: leading LF, before start line
141#define HTTP_MSG_RPBEFORE_CR 9 // response: leading CRLF, before start line
142
143/* these ones define a response start line */
144#define HTTP_MSG_RPVER 10 // parsing the Response Version
145#define HTTP_MSG_RPVER_SP 11 // space(s) after the Response Version
146#define HTTP_MSG_RPCODE 12 // response code
147#define HTTP_MSG_RPCODE_SP 13 // space(s) after the response code
148#define HTTP_MSG_RPREASON 14 // response reason
149#define HTTP_MSG_RPLINE_END 15 // end of response line (CR or LF)
150
151/* common header processing */
152
153#define HTTP_MSG_HDR_FIRST 16 // waiting for first header or last CRLF (no LWS possible)
154#define HTTP_MSG_HDR_NAME 17 // parsing header name
155#define HTTP_MSG_HDR_COL 18 // parsing header colon
156#define HTTP_MSG_HDR_L1_SP 19 // parsing header LWS (SP|HT) before value
157#define HTTP_MSG_HDR_L1_LF 20 // parsing header LWS (LF) before value
158#define HTTP_MSG_HDR_L1_LWS 21 // checking whether it's a new header or an LWS
159#define HTTP_MSG_HDR_VAL 22 // parsing header value
160#define HTTP_MSG_HDR_L2_LF 23 // parsing header LWS (LF) inside/after value
161#define HTTP_MSG_HDR_L2_LWS 24 // checking whether it's a new header or an LWS
162
163#define HTTP_MSG_LAST_LF 25 // parsing last LF
Willy Tarreau655dce92009-11-08 13:10:58 +0100164
165/* error state : must be before HTTP_MSG_BODY so that (>=BODY) always indicates
166 * that data are being processed.
167 */
168
169#define HTTP_MSG_ERROR 26 // an error occurred
170
171/* Body processing.
172 * The state HTTP_MSG_BODY is a delimiter to know if we're waiting for headers
173 * or body. All the sub-states below also indicate we're processing the body,
174 * with some additional information.
175 */
176#define HTTP_MSG_BODY 27 // parsing body at end of headers
177#define HTTP_MSG_100_SENT 28 // parsing body after a 100-Continue was sent
178#define HTTP_MSG_CHUNK_SIZE 29 // parsing the chunk size (RFC2616 #3.6.1)
179#define HTTP_MSG_DATA 30 // skipping data chunk / content-length data
180#define HTTP_MSG_DATA_CRLF 31 // skipping CRLF after data chunk
181#define HTTP_MSG_TRAILERS 32 // trailers (post-data entity headers)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100182
Willy Tarreau5523b322009-12-29 12:05:52 +0100183/* we enter this state when we've received the end of the current message */
184#define HTTP_MSG_DONE 33 // message end received, waiting for resync or close
185#define HTTP_MSG_CLOSING 34 // shutdown_w done, not all bytes sent yet
186#define HTTP_MSG_CLOSED 35 // shutdown_w done, all bytes sent
Willy Tarreau610ecce2010-01-04 21:15:02 +0100187#define HTTP_MSG_TUNNEL 36 // tunneled data after DONE
Willy Tarreau58f10d72006-12-04 02:26:12 +0100188
Willy Tarreau79da4692008-11-19 20:03:04 +0100189/* Redirect flags */
190enum {
191 REDIRECT_FLAG_NONE = 0,
192 REDIRECT_FLAG_DROP_QS = 1, /* drop query string */
Willy Tarreau81e3b4f2010-01-10 00:42:19 +0100193 REDIRECT_FLAG_APPEND_SLASH = 2, /* append a slash if missing at the end */
Willy Tarreau79da4692008-11-19 20:03:04 +0100194};
Willy Tarreaub463dfb2008-06-07 23:08:56 +0200195
196/* Redirect types (location, prefix, extended ) */
197enum {
198 REDIRECT_TYPE_NONE = 0, /* no redirection */
199 REDIRECT_TYPE_LOCATION, /* location redirect */
200 REDIRECT_TYPE_PREFIX, /* prefix redirect */
201};
202
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200203/* Perist types (force-persist, ignore-persist) */
204enum {
205 PERSIST_TYPE_NONE = 0, /* no persistence */
206 PERSIST_TYPE_FORCE, /* force-persist */
207 PERSIST_TYPE_IGNORE, /* ignore-persist */
208};
209
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100210/* Known HTTP methods */
211typedef enum {
212 HTTP_METH_NONE = 0,
213 HTTP_METH_OPTIONS,
214 HTTP_METH_GET,
215 HTTP_METH_HEAD,
216 HTTP_METH_POST,
217 HTTP_METH_PUT,
218 HTTP_METH_DELETE,
219 HTTP_METH_TRACE,
220 HTTP_METH_CONNECT,
221 HTTP_METH_OTHER,
222} http_meth_t;
223
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100224enum {
225 HTTP_AUTH_WRONG = -1, /* missing or unknown */
226 HTTP_AUTH_UNKNOWN = 0,
227 HTTP_AUTH_BASIC,
228 HTTP_AUTH_DIGEST,
229};
230
Willy Tarreauff011f22011-01-06 17:51:27 +0100231enum {
232 HTTP_REQ_ACT_UNKNOWN = 0,
233 HTTP_REQ_ACT_ALLOW,
234 HTTP_REQ_ACT_DENY,
235 HTTP_REQ_ACT_HTTP_AUTH,
236 HTTP_REQ_ACT_MAX
237};
238
Willy Tarreau436d9ed2011-05-11 16:10:11 +0200239/*
240 * All implemented return codes
241 */
242enum {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200243 HTTP_ERR_200 = 0,
244 HTTP_ERR_400,
Willy Tarreau436d9ed2011-05-11 16:10:11 +0200245 HTTP_ERR_403,
246 HTTP_ERR_408,
247 HTTP_ERR_500,
248 HTTP_ERR_502,
249 HTTP_ERR_503,
250 HTTP_ERR_504,
251 HTTP_ERR_SIZE
252};
253
Cyril Bontécf8d9ae2012-04-04 12:57:18 +0200254/* Actions available for the stats admin forms */
255enum {
256 ST_ADM_ACTION_NONE = 0,
257 ST_ADM_ACTION_DISABLE,
258 ST_ADM_ACTION_ENABLE,
259};
260
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100261/* This is an HTTP message, as described in RFC2616. It can be either a request
262 * message or a response message.
263 *
264 * The values there are a little bit obscure, because their meaning can change
265 * during the parsing :
266 *
267 * - som (Start of Message) : relative offset in the buffer of first byte of
268 * the request being processed or parsed. Reset to
Willy Tarreau962c3f42010-01-10 00:15:35 +0100269 * zero during accept(), and changes while parsing
270 * chunks.
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100271 * - eoh (End of Headers) : relative offset in the buffer of first byte that
272 * is not part of a completely processed header.
273 * During parsing, it points to last header seen
Willy Tarreaufa355d42009-11-29 18:12:29 +0100274 * for states after START. When in HTTP_MSG_BODY,
275 * eoh points to the first byte of the last CRLF
276 * preceeding data.
277 * - col and sov : When in HTTP_MSG_BODY, will point to the first
Willy Tarreau1d3bcce2009-12-27 15:50:06 +0100278 * byte of data (relative to buffer).
279 * - sol (start of line) : start of line, also start of message when fully parsed.
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100280 * - eol (End of Line) : relative offset in the buffer of the first byte
281 * which marks the end of the line (LF or CRLF).
Willy Tarreau1d3bcce2009-12-27 15:50:06 +0100282 * Note that all offsets are relative to the beginning of the buffer. To get
Willy Tarreau962c3f42010-01-10 00:15:35 +0100283 * them relative to the current request, subtract ->som or ->sol.
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100284 */
285struct http_msg {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200286 unsigned int msg_state; /* where we are in the current message parsing */
Willy Tarreaufa355d42009-11-29 18:12:29 +0100287 unsigned int col, sov; /* current header: colon, start of value */
288 unsigned int eoh; /* End Of Headers, relative to buffer */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200289 char *sol; /* start of line, also start of message when fully parsed */
290 char *eol; /* end of line */
291 unsigned int som; /* Start Of Message, relative to buffer */
Willy Tarreaufa355d42009-11-29 18:12:29 +0100292 int err_pos; /* err handling: -2=block, -1=pass, 0+=detected */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100293 union { /* useful start line pointers, relative to ->sol */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100294 struct {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 int l; /* request line length (not including CR) */
296 int m_l; /* METHOD length (method starts at ->som) */
297 int u, u_l; /* URI, length */
298 int v, v_l; /* VERSION, length */
299 } rq; /* request line : field, length */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100300 struct {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200301 int l; /* status line length (not including CR) */
302 int v_l; /* VERSION length (version starts at ->som) */
303 int c, c_l; /* CODE, length */
304 int r, r_l; /* REASON, length */
305 } st; /* status line : field, length */
306 } sl; /* start line */
Willy Tarreau124d9912011-03-01 20:30:48 +0100307 unsigned long long chunk_len; /* cache for last chunk size or content-length header value */
308 unsigned long long body_len; /* total known length of the body, excluding encoding */
Willy Tarreaufa355d42009-11-29 18:12:29 +0100309 char **cap; /* array of captured headers (may be NULL) */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100310};
311
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100312struct http_auth_data {
313 int method; /* one of HTTP_AUTH_* */
314 struct chunk method_data; /* points to the creditial part from 'Authorization:' header */
315 char *user, *pass; /* extracted username & password */
316};
317
Willy Tarreauff011f22011-01-06 17:51:27 +0100318struct http_req_rule {
319 struct list list;
320 struct acl_cond *cond; /* acl condition to meet */
321 unsigned int action;
322 struct {
323 char *realm;
324 } http_auth;
325};
326
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100327/* This is an HTTP transaction. It contains both a request message and a
328 * response message (which can be empty).
329 */
330struct http_txn {
Willy Tarreau520bbb22010-01-10 11:31:22 +0100331 struct http_msg req; /* HTTP request message */
Willy Tarreauac1932d2011-10-24 19:14:41 +0200332 struct hdr_idx hdr_idx; /* array of header indexes (max: global.tune.max_http_hdr) */
Willy Tarreau520bbb22010-01-10 11:31:22 +0100333 unsigned int flags; /* transaction flags */
334 http_meth_t meth; /* HTTP method */
335
336 int status; /* HTTP status from the server, negative if from proxy */
337 struct http_msg rsp; /* HTTP response message */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100338
Willy Tarreau520bbb22010-01-10 11:31:22 +0100339 char *uri; /* first line if log needed, NULL otherwise */
340 char *cli_cookie; /* cookie presented by the client, in capture mode */
341 char *srv_cookie; /* cookie presented by the server, in capture mode */
Willy Tarreaua3377ee2010-01-10 10:49:11 +0100342 char *sessid; /* the appsession id, if found in the request or in the response */
Willy Tarreauf64d1412010-10-07 20:06:11 +0200343 int cookie_first_date; /* if non-zero, first date the expirable cookie was set/seen */
344 int cookie_last_date; /* if non-zero, last date the expirable cookie was set/seen */
Willy Tarreau520bbb22010-01-10 11:31:22 +0100345
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100346 struct http_auth_data auth; /* HTTP auth data */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100347};
348
Willy Tarreau33a7e692007-06-10 19:45:56 +0200349/* This structure is used by http_find_header() to return values of headers.
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200350 * The header starts at <line>, the value (excluding leading and trailing white
351 * spaces) at <line>+<val> for <vlen> bytes, followed by optional <tws> trailing
352 * white spaces, and sets <line>+<del> to point to the last delimitor (colon or
353 * comma) before this value. <prev> points to the index of the header whose next
354 * is this one.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200355 */
356struct hdr_ctx {
Willy Tarreau68085d82010-01-18 14:54:04 +0100357 char *line;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200358 int idx;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200359 int val; /* relative to line, may skip some leading white spaces */
360 int vlen; /* relative to line+val, stops before trailing white spaces */
361 int tws; /* added to vlen if some trailing white spaces are present */
Willy Tarreau68085d82010-01-18 14:54:04 +0100362 int del; /* relative to line */
363 int prev; /* index of previous header */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200364};
Willy Tarreau58f10d72006-12-04 02:26:12 +0100365
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366#endif /* _TYPES_PROTO_HTTP_H */
367
368/*
369 * Local variables:
370 * c-indent-level: 8
371 * c-basic-offset: 8
372 * End:
373 */