1 /*
2  * include/types/proto_http.h
3  * This file contains HTTP protocol definitions.
4  *
5  * Copyright (C) 2000-2011 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 _TYPES_PROTO_HTTP_H
23 #define _TYPES_PROTO_HTTP_H
24 
25 #include <common/buf.h>
26 #include <common/config.h>
27 #include <common/http.h>
28 #include <common/mini-clist.h>
29 #include <common/regex.h>
30 
31 #include <types/channel.h>
32 #include <types/hdr_idx.h>
33 #include <types/filters.h>
34 //#include <types/sample.h>
35 
36 /* These are the flags that are found in txn->flags */
37 
38 /* action flags */
39 #define TX_CLDENY	0x00000001	/* a client header matches a deny regex */
40 #define TX_CLALLOW	0x00000002	/* a client header matches an allow regex */
41 #define TX_SVDENY	0x00000004	/* a server header matches a deny regex */
42 #define TX_SVALLOW	0x00000008	/* a server header matches an allow regex */
43 #define TX_CLTARPIT	0x00000010	/* the transaction is tarpitted (anti-dos) */
44 
45 /* transaction flags dedicated to cookies : bits values 0x20 to 0x80 (0-7 shift 5) */
46 #define TX_CK_NONE	0x00000000	/* this transaction had no cookie */
47 #define TX_CK_INVALID	0x00000020	/* this transaction had a cookie which matches no server */
48 #define TX_CK_DOWN	0x00000040	/* this transaction had cookie matching a down server */
49 #define TX_CK_VALID	0x00000060	/* this transaction had cookie matching a valid server */
50 #define TX_CK_EXPIRED	0x00000080	/* this transaction had an expired cookie (idle for too long) */
51 #define TX_CK_OLD	0x000000A0	/* this transaction had too old a cookie (offered too long ago) */
52 #define TX_CK_UNUSED	0x000000C0	/* this transaction had a cookie but it was not used (eg: use-server was preferred) */
53 #define TX_CK_MASK	0x000000E0	/* mask to get this transaction's cookie flags */
54 #define TX_CK_SHIFT	5		/* bit shift */
55 
56 /* response cookie information, bits values 0x100 to 0x700 (0-7 shift 8) */
57 #define TX_SCK_NONE	0x00000000	/* no cookie found in the response */
58 #define TX_SCK_FOUND    0x00000100	/* a persistence cookie was found and forwarded */
59 #define TX_SCK_DELETED	0x00000200	/* an existing persistence cookie was deleted */
60 #define TX_SCK_INSERTED	0x00000300	/* a persistence cookie was inserted */
61 #define TX_SCK_REPLACED	0x00000400	/* a persistence cookie was present and rewritten */
62 #define TX_SCK_UPDATED	0x00000500	/* an expirable persistence cookie was updated */
63 #define TX_SCK_MASK	0x00000700	/* mask to get the set-cookie field */
64 #define TX_SCK_SHIFT	8		/* bit shift */
65 
66 #define TX_SCK_PRESENT  0x00000800	/* a cookie was found in the server's response */
67 
68 /* cacheability management, bits values 0x1000 to 0x3000 (0-3 shift 12) */
69 #define TX_CACHEABLE	0x00001000	/* at least part of the response is cacheable */
70 #define TX_CACHE_COOK	0x00002000	/* a cookie in the response is cacheable */
71 #define TX_CACHE_IGNORE 0x00004000	/* do not retrieve object from cache */
72 #define TX_CACHE_SHIFT	12		/* bit shift */
73 
74 /* Unused: 0x8000 */
75 
76 #define TX_WAIT_CLEANUP	0x0010000	/* this transaction is waiting for a clean up */
77 
78 /* Unused: 0x20000, 0x80000 */
79 
80 
81 /* indicate how we *want* the connection to behave, regardless of what is in
82  * the headers. We have 4 possible values right now :
83  * - WANT_KAL : try to maintain keep-alive (default when nothing configured)
84  * - WANT_TUN : will be a tunnel (CONNECT).
85  * - WANT_SCL : enforce close on the server side
86  * - WANT_CLO : enforce close on both sides
87  */
88 #define TX_CON_WANT_KAL 0x00000000	/* note: it's important that it is 0 (init) */
89 #define TX_CON_WANT_TUN 0x00100000
90 #define TX_CON_WANT_SCL 0x00200000
91 #define TX_CON_WANT_CLO 0x00300000
92 #define TX_CON_WANT_MSK 0x00300000	/* this is the mask to get the bits */
93 
94 #define TX_CON_CLO_SET  0x00400000	/* "connection: close" is now set */
95 #define TX_CON_KAL_SET  0x00800000	/* "connection: keep-alive" is now set */
96 
97 /* unused: 0x01000000 */
98 
99 #define TX_HDR_CONN_UPG 0x02000000	/* The "Upgrade" token was found in the "Connection" header */
100 #define TX_WAIT_NEXT_RQ	0x04000000	/* waiting for the second request to start, use keep-alive timeout */
101 
102 #define TX_HDR_CONN_PRS	0x08000000	/* "connection" header already parsed (req or res), results below */
103 #define TX_HDR_CONN_CLO	0x10000000	/* "Connection: close" was present at least once */
104 #define TX_HDR_CONN_KAL	0x20000000	/* "Connection: keep-alive" was present at least once */
105 #define TX_USE_PX_CONN	0x40000000	/* Use "Proxy-Connection" instead of "Connection" */
106 
107 /* used only for keep-alive purposes, to indicate we're on a second transaction */
108 #define TX_NOT_FIRST	0x80000000	/* the transaction is not the first one */
109 /* no more room for transaction flags ! */
110 
111 /* The HTTP parser is more complex than it looks like, because we have to
112  * support multi-line headers and any number of spaces between the colon and
113  * the value.
114  *
115  * All those examples must work :
116 
117  Hdr1:val1\r\n
118  Hdr1: val1\r\n
119  Hdr1:\t val1\r\n
120  Hdr1: \r\n
121   val1\r\n
122  Hdr1:\r\n
123   val1\n
124  \tval2\r\n
125   val3\n
126 
127  *
128  */
129 
130 /*
131  * HTTP message status flags (msg->flags)
132  */
133 #define HTTP_MSGF_CNT_LEN     0x00000001  /* content-length was found in the message */
134 #define HTTP_MSGF_TE_CHNK     0x00000002  /* transfer-encoding: chunked was found */
135 
136 /* if this flags is not set in either direction, we may be forced to complete a
137  * connection as a half-way tunnel (eg if no content-length appears in a 1.1
138  * response, but the request is correctly sized)
139  */
140 #define HTTP_MSGF_XFER_LEN    0x00000004  /* message xfer size can be determined */
141 #define HTTP_MSGF_VER_11      0x00000008  /* the message is HTTP/1.1 or above */
142 
143 /* If this flag is set, we don't process the body until the connect() is confirmed.
144  * This is only used by the request forwarding function to protect the buffer
145  * contents if something needs them during a redispatch.
146  */
147 #define HTTP_MSGF_WAIT_CONN   0x00000010  /* Wait for connect() to be confirmed before processing body */
148 #define HTTP_MSGF_COMPRESSING 0x00000020  /* data compression is in progress */
149 
150 #define HTTP_MSGF_BODYLESS    0x00000040  /* The message has no body (content-length = 0) */
151 
152 
153 /* Redirect flags */
154 enum {
155 	REDIRECT_FLAG_NONE = 0,
156 	REDIRECT_FLAG_DROP_QS = 1,	/* drop query string */
157 	REDIRECT_FLAG_APPEND_SLASH = 2,	/* append a slash if missing at the end */
158 	REDIRECT_FLAG_FROM_REQ = 4,     /* redirect rule on the request path */
159 };
160 
161 /* Redirect types (location, prefix, extended ) */
162 enum {
163 	REDIRECT_TYPE_NONE = 0,         /* no redirection */
164 	REDIRECT_TYPE_LOCATION,         /* location redirect */
165 	REDIRECT_TYPE_PREFIX,           /* prefix redirect */
166 	REDIRECT_TYPE_SCHEME,           /* scheme redirect (eg: switch from http to https) */
167 };
168 
169 /* Perist types (force-persist, ignore-persist) */
170 enum {
171 	PERSIST_TYPE_NONE = 0,          /* no persistence */
172 	PERSIST_TYPE_FORCE,             /* force-persist */
173 	PERSIST_TYPE_IGNORE,            /* ignore-persist */
174 };
175 
176 /* final results for http-request rules */
177 enum rule_result {
178 	HTTP_RULE_RES_CONT = 0,  /* nothing special, continue rules evaluation */
179 	HTTP_RULE_RES_YIELD,     /* call me later because some data is missing. */
180 	HTTP_RULE_RES_STOP,      /* stopped processing on an accept */
181 	HTTP_RULE_RES_DENY,      /* deny (or tarpit if TX_CLTARPIT)  */
182 	HTTP_RULE_RES_ABRT,      /* abort request, msg already sent (eg: auth) */
183 	HTTP_RULE_RES_DONE,      /* processing done, stop processing (eg: redirect) */
184 	HTTP_RULE_RES_BADREQ,    /* bad request */
185 };
186 
187 /* Legacy version of the HTTP/1 message state, used by the channels, should
188  * ultimately be removed.
189  */
190 enum h1_state {
191 	HTTP_MSG_RQBEFORE     =  0, // request: leading LF, before start line
192 	HTTP_MSG_RQBEFORE_CR  =  1, // request: leading CRLF, before start line
193 	/* these ones define a request start line */
194 	HTTP_MSG_RQMETH       =  2, // parsing the Method
195 	HTTP_MSG_RQMETH_SP    =  3, // space(s) after the Method
196 	HTTP_MSG_RQURI        =  4, // parsing the Request URI
197 	HTTP_MSG_RQURI_SP     =  5, // space(s) after the Request URI
198 	HTTP_MSG_RQVER        =  6, // parsing the Request Version
199 	HTTP_MSG_RQLINE_END   =  7, // end of request line (CR or LF)
200 
201 	HTTP_MSG_RPBEFORE     =  8, // response: leading LF, before start line
202 	HTTP_MSG_RPBEFORE_CR  =  9, // response: leading CRLF, before start line
203 
204 	/* these ones define a response start line */
205 	HTTP_MSG_RPVER        = 10, // parsing the Response Version
206 	HTTP_MSG_RPVER_SP     = 11, // space(s) after the Response Version
207 	HTTP_MSG_RPCODE       = 12, // response code
208 	HTTP_MSG_RPCODE_SP    = 13, // space(s) after the response code
209 	HTTP_MSG_RPREASON     = 14, // response reason
210 	HTTP_MSG_RPLINE_END   = 15, // end of response line (CR or LF)
211 
212 	/* common header processing */
213 	HTTP_MSG_HDR_FIRST    = 16, // waiting for first header or last CRLF (no LWS possible)
214 	HTTP_MSG_HDR_NAME     = 17, // parsing header name
215 	HTTP_MSG_HDR_COL      = 18, // parsing header colon
216 	HTTP_MSG_HDR_L1_SP    = 19, // parsing header LWS (SP|HT) before value
217 	HTTP_MSG_HDR_L1_LF    = 20, // parsing header LWS (LF) before value
218 	HTTP_MSG_HDR_L1_LWS   = 21, // checking whether it's a new header or an LWS
219 	HTTP_MSG_HDR_VAL      = 22, // parsing header value
220 	HTTP_MSG_HDR_L2_LF    = 23, // parsing header LWS (LF) inside/after value
221 	HTTP_MSG_HDR_L2_LWS   = 24, // checking whether it's a new header or an LWS
222 
223 	HTTP_MSG_LAST_LF      = 25, // parsing last LF
224 
225 	/* error state : must be before HTTP_MSG_BODY so that (>=BODY) always indicates
226 	 * that data are being processed.
227 	 */
228 	HTTP_MSG_ERROR        = 26, // an error occurred
229 	/* Body processing.
230 	 * The state HTTP_MSG_BODY is a delimiter to know if we're waiting for headers
231 	 * or body. All the sub-states below also indicate we're processing the body,
232 	 * with some additional information.
233 	 */
234 	HTTP_MSG_BODY         = 27, // parsing body at end of headers
235 	HTTP_MSG_100_SENT     = 28, // parsing body after a 100-Continue was sent
236 	HTTP_MSG_CHUNK_SIZE   = 29, // parsing the chunk size (RFC7230 #4.1)
237 	HTTP_MSG_DATA         = 30, // skipping data chunk / content-length data
238 	HTTP_MSG_CHUNK_CRLF   = 31, // skipping CRLF after data chunk
239 	HTTP_MSG_TRAILERS     = 32, // trailers (post-data entity headers)
240 	/* we enter this state when we've received the end of the current message */
241 	HTTP_MSG_ENDING       = 33, // message end received, wait that the filters end too
242 	HTTP_MSG_DONE         = 34, // message end received, waiting for resync or close
243 	HTTP_MSG_CLOSING      = 35, // shutdown_w done, not all bytes sent yet
244 	HTTP_MSG_CLOSED       = 36, // shutdown_w done, all bytes sent
245 	HTTP_MSG_TUNNEL       = 37, // tunneled data after DONE
246 } __attribute__((packed));
247 
248 
249 /* This is an HTTP message, as described in RFC7230. It can be either a request
250  * message or a response message.
251  *
252  * The values there are a little bit obscure, because their meaning can change
253  * during the parsing. Please read carefully doc/internal/body-parsing.txt if
254  * you need to manipulate them. Quick reminder :
255  *
256  *  - eoh (End of Headers)   : relative offset in the buffer of first byte that
257  *                             is not part of a completely processed header.
258  *                             During parsing, it points to last header seen
259  *                             for states after START. When in HTTP_MSG_BODY,
260  *                             eoh points to the first byte of the last CRLF
261  *                             preceding data. Relative to buffer's origin.
262  *                             This value then remains unchanged till the end
263  *                             so that we can rewind the buffer to change some
264  *                             headers if needed (eg: http-send-name-header).
265  *
266  *  - sov (start of value)   : Before HTTP_MSG_BODY, points to the value of
267  *                             the header being parsed. Starting from
268  *                             HTTP_MSG_BODY, will point to the start of the
269  *                             body (relative to buffer's origin). It can be
270  *                             negative when forwarding data. It stops growing
271  *                             once data start to leave the buffer.
272  *
273  *  - next (parse pointer)   : next relative byte to be parsed. Always points
274  *                             to a byte matching the current state.
275  *
276  *  - sol (start of line)    : start of current line before MSG_BODY. Starting
277  *                             from MSG_BODY and until MSG_TRAILERS, contains
278  *                             the length of the last parsed chunk size so that
279  *                             when added to sov it always points to the
280  *                             beginning of the current data chunk.
281  *                             in MSG_TRAILERS state, it contains the length of
282  *                             the last parsed part of the trailer headers.
283  *
284  *  - eol (End of Line)      : Before HTTP_MSG_BODY, relative offset in the
285  *                             buffer of the first byte which marks the end of
286  *                             the line current (LF or CRLF).
287  *                             From HTTP_MSG_BODY to the end, contains the
288  *                             length of the last CRLF (1 for a plain LF, or 2
289  *                             for a true CRLF). So eoh+eol always contain the
290  *                             exact size of the header size.
291  *
292  * Note that all offsets are relative to the origin of the buffer (buf->p)
293  * which always points to the beginning of the message (request or response).
294  * Since a message may not wrap, pointer computations may be one without any
295  * care for wrapping (no addition overflow nor subtract underflow).
296  */
297 struct http_msg {
298 	enum h1_state msg_state;               /* where we are in the current message parsing */
299 	enum h1_state err_state;               /* the state where the parsing error was detected, only is MSG_ERROR */
300 	unsigned char flags;                   /* flags describing the message (HTTP version, ...) */
301 	/* 5 bytes unused here */
302 	struct channel *chn;                   /* pointer to the channel transporting the message */
303 	unsigned int next;                     /* pointer to next byte to parse, relative to buf->p */
304 	int sov;                               /* current header: start of value ; data: start of body */
305 	unsigned int eoh;                      /* End Of Headers, relative to buffer */
306 	unsigned int sol;                      /* start of current line during parsing otherwise zero */
307 	unsigned int eol;                      /* end of line */
308 	int err_pos;                           /* err handling: -2=block, -1=pass, 0+=detected */
309 	union {                                /* useful start line pointers, relative to ->sol */
310 		struct {
311 			int l;                 /* request line length (not including CR) */
312 			int m_l;               /* METHOD length (method starts at buf->p) */
313 			int u, u_l;            /* URI, length */
314 			int v, v_l;            /* VERSION, length */
315 		} rq;                          /* request line : field, length */
316 		struct {
317 			int l;                 /* status line length (not including CR) */
318 			int v_l;               /* VERSION length (version starts at buf->p) */
319 			int c, c_l;            /* CODE, length */
320 			int r, r_l;            /* REASON, length */
321 		} st;                          /* status line : field, length */
322 	} sl;                                  /* start line */
323 	unsigned long long chunk_len;          /* cache for last chunk size or content-length header value */
324 	unsigned long long body_len;           /* total known length of the body, excluding encoding */
325 };
326 
327 struct proxy;
328 struct http_txn;
329 struct stream;
330 
331 /* This is an HTTP transaction. It contains both a request message and a
332  * response message (which can be empty).
333  */
334 struct http_txn {
335 	struct hdr_idx hdr_idx;         /* array of header indexes (max: global.tune.max_http_hdr) */
336 	struct http_msg rsp;            /* HTTP response message */
337 	struct http_msg req;            /* HTTP request message */
338 	unsigned int flags;             /* transaction flags */
339 	enum http_meth_t meth;          /* HTTP method */
340 	/* 1 unused byte here */
341 	short status;                   /* HTTP status from the server, negative if from proxy */
342 
343 	char cache_hash[20];               /* Store the cache hash  */
344 	char *uri;                      /* first line if log needed, NULL otherwise */
345 	char *cli_cookie;               /* cookie presented by the client, in capture mode */
346 	char *srv_cookie;               /* cookie presented by the server, in capture mode */
347 	int cookie_first_date;          /* if non-zero, first date the expirable cookie was set/seen */
348 	int cookie_last_date;           /* if non-zero, last date the expirable cookie was set/seen */
349 
350 	struct http_auth_data auth;	/* HTTP auth data */
351 };
352 
353 
354 /* This structure is used by http_find_header() to return values of headers.
355  * The header starts at <line>, the value (excluding leading and trailing white
356  * spaces) at <line>+<val> for <vlen> bytes, followed by optional <tws> trailing
357  * white spaces, and sets <line>+<del> to point to the last delimitor (colon or
358  * comma) before this value. <prev> points to the index of the header whose next
359  * is this one.
360  */
361 struct hdr_ctx {
362 	char *line;
363 	int  idx;
364 	int  val;  /* relative to line, may skip some leading white spaces */
365 	int  vlen; /* relative to line+val, stops before trailing white spaces */
366 	int  tws;  /* added to vlen if some trailing white spaces are present */
367 	int  del;  /* relative to line */
368 	int  prev; /* index of previous header */
369 };
370 
371 extern struct pool_head *pool_head_http_txn;
372 
373 #endif /* _TYPES_PROTO_HTTP_H */
374 
375 /*
376  * Local variables:
377  *  c-indent-level: 8
378  *  c-basic-offset: 8
379  * End:
380  */
381