blob: d1a0706bc30b32ac0ec612846c60120bb9d23928 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010045#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/backend.h>
47#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010048#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020051#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010053#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020054#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010061#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020062#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/task.h>
64
Willy Tarreau522d6c02009-12-06 18:49:18 +010065const char HTTP_100[] =
66 "HTTP/1.1 100 Continue\r\n\r\n";
67
68const struct chunk http_100_chunk = {
69 .str = (char *)&HTTP_100,
70 .len = sizeof(HTTP_100)-1
71};
72
Willy Tarreaua9679ac2010-01-03 17:32:57 +010073/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Location: "; /* not terminated since it will be concatenated with the URL */
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
86/* same as 302 except that the browser MUST retry with the GET method */
87const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
94const char *HTTP_401_fmt =
95 "HTTP/1.0 401 Unauthorized\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020098 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
100 "\r\n"
101 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
102
Willy Tarreau844a7e72010-01-31 21:46:18 +0100103const char *HTTP_407_fmt =
104 "HTTP/1.0 407 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Content-Type: text/html\r\n"
108 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200114 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100115 [HTTP_ERR_400] = 400,
116 [HTTP_ERR_403] = 403,
117 [HTTP_ERR_408] = 408,
118 [HTTP_ERR_500] = 500,
119 [HTTP_ERR_502] = 502,
120 [HTTP_ERR_503] = 503,
121 [HTTP_ERR_504] = 504,
122};
123
Willy Tarreau80587432006-12-24 17:47:20 +0100124static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200125 [HTTP_ERR_200] =
126 "HTTP/1.0 200 OK\r\n"
127 "Cache-Control: no-cache\r\n"
128 "Connection: close\r\n"
129 "Content-Type: text/html\r\n"
130 "\r\n"
131 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
132
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100134 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 "Cache-Control: no-cache\r\n"
136 "Connection: close\r\n"
137 "Content-Type: text/html\r\n"
138 "\r\n"
139 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
140
141 [HTTP_ERR_403] =
142 "HTTP/1.0 403 Forbidden\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
148
149 [HTTP_ERR_408] =
150 "HTTP/1.0 408 Request Time-out\r\n"
151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
156
157 [HTTP_ERR_500] =
158 "HTTP/1.0 500 Server Error\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
164
165 [HTTP_ERR_502] =
166 "HTTP/1.0 502 Bad Gateway\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
172
173 [HTTP_ERR_503] =
174 "HTTP/1.0 503 Service Unavailable\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
180
181 [HTTP_ERR_504] =
182 "HTTP/1.0 504 Gateway Time-out\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
188
189};
190
Cyril Bonté19979e12012-04-04 12:57:21 +0200191/* status codes available for the stats admin page (strictly 4 chars length) */
192const char *stat_status_codes[STAT_STATUS_SIZE] = {
193 [STAT_STATUS_DENY] = "DENY",
194 [STAT_STATUS_DONE] = "DONE",
195 [STAT_STATUS_ERRP] = "ERRP",
196 [STAT_STATUS_EXCD] = "EXCD",
197 [STAT_STATUS_NONE] = "NONE",
198 [STAT_STATUS_PART] = "PART",
199 [STAT_STATUS_UNKN] = "UNKN",
200};
201
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203/* We must put the messages here since GCC cannot initialize consts depending
204 * on strlen().
205 */
206struct chunk http_err_chunks[HTTP_ERR_SIZE];
207
Willy Tarreau42250582007-04-01 01:30:43 +0200208#define FD_SETS_ARE_BITFIELDS
209#ifdef FD_SETS_ARE_BITFIELDS
210/*
211 * This map is used with all the FD_* macros to check whether a particular bit
212 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
213 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
214 * byte should be encoded. Be careful to always pass bytes from 0 to 255
215 * exclusively to the macros.
216 */
217fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
218fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219
220#else
221#error "Check if your OS uses bitfields for fd_sets"
222#endif
223
Willy Tarreau80587432006-12-24 17:47:20 +0100224void init_proto_http()
225{
Willy Tarreau42250582007-04-01 01:30:43 +0200226 int i;
227 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100228 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200229
Willy Tarreau80587432006-12-24 17:47:20 +0100230 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
231 if (!http_err_msgs[msg]) {
232 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
233 abort();
234 }
235
236 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
237 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
238 }
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240 /* initialize the log header encoding map : '{|}"#' should be encoded with
241 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
242 * URL encoding only requires '"', '#' to be encoded as well as non-
243 * printable characters above.
244 */
245 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
246 memset(url_encode_map, 0, sizeof(url_encode_map));
247 for (i = 0; i < 32; i++) {
248 FD_SET(i, hdr_encode_map);
249 FD_SET(i, url_encode_map);
250 }
251 for (i = 127; i < 256; i++) {
252 FD_SET(i, hdr_encode_map);
253 FD_SET(i, url_encode_map);
254 }
255
256 tmp = "\"#{|}";
257 while (*tmp) {
258 FD_SET(*tmp, hdr_encode_map);
259 tmp++;
260 }
261
262 tmp = "\"#";
263 while (*tmp) {
264 FD_SET(*tmp, url_encode_map);
265 tmp++;
266 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200267
268 /* memory allocations */
269 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200270 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100271 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100272}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274/*
275 * We have 26 list of methods (1 per first letter), each of which can have
276 * up to 3 entries (2 valid, 1 null).
277 */
278struct http_method_desc {
279 http_meth_t meth;
280 int len;
281 const char text[8];
282};
283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100284const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100285 ['C' - 'A'] = {
286 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
287 },
288 ['D' - 'A'] = {
289 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
290 },
291 ['G' - 'A'] = {
292 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
293 },
294 ['H' - 'A'] = {
295 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
296 },
297 ['P' - 'A'] = {
298 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
299 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
300 },
301 ['T' - 'A'] = {
302 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
303 },
304 /* rest is empty like this :
305 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
306 */
307};
308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100309/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311 * RFC2616 for those chars.
312 */
313
314const char http_is_spht[256] = {
315 [' '] = 1, ['\t'] = 1,
316};
317
318const char http_is_crlf[256] = {
319 ['\r'] = 1, ['\n'] = 1,
320};
321
322const char http_is_lws[256] = {
323 [' '] = 1, ['\t'] = 1,
324 ['\r'] = 1, ['\n'] = 1,
325};
326
327const char http_is_sep[256] = {
328 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
329 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
330 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
331 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
332 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
333};
334
335const char http_is_ctl[256] = {
336 [0 ... 31] = 1,
337 [127] = 1,
338};
339
340/*
341 * A token is any ASCII char that is neither a separator nor a CTL char.
342 * Do not overwrite values in assignment since gcc-2.95 will not handle
343 * them correctly. Instead, define every non-CTL char's status.
344 */
345const char http_is_token[256] = {
346 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
347 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
348 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
349 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
350 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
351 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
352 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
353 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
354 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
355 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
356 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
357 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
358 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
359 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
360 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
361 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
362 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
363 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
364 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
365 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
366 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
367 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
368 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
369 ['|'] = 1, ['}'] = 0, ['~'] = 1,
370};
371
372
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100373/*
374 * An http ver_token is any ASCII which can be found in an HTTP version,
375 * which includes 'H', 'T', 'P', '/', '.' and any digit.
376 */
377const char http_is_ver_token[256] = {
378 ['.'] = 1, ['/'] = 1,
379 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
380 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
381 ['H'] = 1, ['P'] = 1, ['T'] = 1,
382};
383
384
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100386 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
387 */
388#if defined(DEBUG_FSM)
389static void http_silent_debug(int line, struct session *s)
390{
391 int size = 0;
392 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100404
405 write(-1, trash, size);
406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100412 * Adds a header and its CRLF at the tail of the message's buffer, just before
413 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100414 * The header is also automatically added to the index <hdr_idx>, and the end
415 * of headers is automatically adjusted. The number of bytes added is returned
416 * on success, otherwise <0 is returned indicating an error.
417 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100418int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100419{
420 int bytes, len;
421
422 len = strlen(text);
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100423 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100424 if (!bytes)
425 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100426 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100427 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
428}
429
430/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100431 * Adds a header and its CRLF at the tail of the message's buffer, just before
432 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 * the buffer is only opened and the space reserved, but nothing is copied.
434 * The header is also automatically added to the index <hdr_idx>, and the end
435 * of headers is automatically adjusted. The number of bytes added is returned
436 * on success, otherwise <0 is returned indicating an error.
437 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100438int http_header_add_tail2(struct http_msg *msg,
439 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440{
441 int bytes;
442
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100443 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100444 if (!bytes)
445 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100446 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100447 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
448}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
450/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100451 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
452 * If so, returns the position of the first non-space character relative to
453 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
454 * to return a pointer to the place after the first space. Returns 0 if the
455 * header name does not match. Checks are case-insensitive.
456 */
457int http_header_match2(const char *hdr, const char *end,
458 const char *name, int len)
459{
460 const char *val;
461
462 if (hdr + len >= end)
463 return 0;
464 if (hdr[len] != ':')
465 return 0;
466 if (strncasecmp(hdr, name, len) != 0)
467 return 0;
468 val = hdr + len + 1;
469 while (val < end && HTTP_IS_SPHT(*val))
470 val++;
471 if ((val >= end) && (len + 2 <= end - hdr))
472 return len + 2; /* we may replace starting from second space */
473 return val - hdr;
474}
475
Willy Tarreau68085d82010-01-18 14:54:04 +0100476/* Find the end of the header value contained between <s> and <e>. See RFC2616,
477 * par 2.2 for more information. Note that it requires a valid header to return
478 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100480char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481{
482 int quoted, qdpair;
483
484 quoted = qdpair = 0;
485 for (; s < e; s++) {
486 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200487 else if (quoted) {
488 if (*s == '\\') qdpair = 1;
489 else if (*s == '"') quoted = 0;
490 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 else if (*s == '"') quoted = 1;
492 else if (*s == ',') return s;
493 }
494 return s;
495}
496
497/* Find the first or next occurrence of header <name> in message buffer <sol>
498 * using headers index <idx>, and return it in the <ctx> structure. This
499 * structure holds everything necessary to use the header and find next
500 * occurrence. If its <idx> member is 0, the header is searched from the
501 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 * 1 when it finds a value, and 0 when there is no more. It is designed to work
503 * with headers defined as comma-separated lists. As a special case, if ctx->val
504 * is NULL when searching for a new values of a header, the current header is
505 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 */
507int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 struct hdr_ctx *ctx)
510{
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 char *eol, *sov;
512 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 cur_idx = ctx->idx;
515 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200516 /* We have previously returned a value, let's search
517 * another one on the same line.
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200520 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100521 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 eol = sol + idx->v[cur_idx].len;
523
524 if (sov >= eol)
525 /* no more values in this header */
526 goto next_hdr;
527
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 /* values remaining for this header, skip the comma but save it
529 * for later use (eg: for header deletion).
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sov++;
532 while (sov < eol && http_is_lws[(unsigned char)*sov])
533 sov++;
534
535 goto return_hdr;
536 }
537
538 /* first request for this header */
539 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 while (cur_idx) {
543 eol = sol + idx->v[cur_idx].len;
544
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200545 if (len == 0) {
546 /* No argument was passed, we want any header.
547 * To achieve this, we simply build a fake request. */
548 while (sol + len < eol && sol[len] != ':')
549 len++;
550 name = sol;
551 }
552
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 if ((len < eol - sol) &&
554 (sol[len] == ':') &&
555 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100556 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 sov = sol + len + 1;
558 while (sov < eol && http_is_lws[(unsigned char)*sov])
559 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100560
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 ctx->prev = old_idx;
563 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 ctx->idx = cur_idx;
565 ctx->val = sov - sol;
566
567 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200568 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200569 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 eol--;
571 ctx->tws++;
572 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->vlen = eol - sov;
574 return 1;
575 }
576 next_hdr:
577 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100578 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 cur_idx = idx->v[cur_idx].next;
580 }
581 return 0;
582}
583
584int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100585 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200586 struct hdr_ctx *ctx)
587{
588 return http_find_header2(name, strlen(name), sol, idx, ctx);
589}
590
Willy Tarreau68085d82010-01-18 14:54:04 +0100591/* Remove one value of a header. This only works on a <ctx> returned by one of
592 * the http_find_header functions. The value is removed, as well as surrounding
593 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100594 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100595 * message <msg>. The new index is returned. If it is zero, it means there is
596 * no more header, so any processing may stop. The ctx is always left in a form
597 * that can be handled by http_find_header2() to find next occurrence.
598 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100599int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100600{
601 int cur_idx = ctx->idx;
602 char *sol = ctx->line;
603 struct hdr_idx_elem *hdr;
604 int delta, skip_comma;
605
606 if (!cur_idx)
607 return 0;
608
609 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200610 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100611 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100612 delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 http_msg_move_end(msg, delta);
614 idx->used--;
615 hdr->len = 0; /* unused entry */
616 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100617 if (idx->tail == ctx->idx)
618 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100619 ctx->idx = ctx->prev; /* walk back to the end of previous header */
620 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
621 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 return ctx->idx;
624 }
625
626 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
628 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 */
630
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200631 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100632 delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 NULL, 0);
635 hdr->len += delta;
636 http_msg_move_end(msg, delta);
637 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200638 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 return ctx->idx;
640}
641
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100642/* This function handles a server error at the stream interface level. The
643 * stream interface is assumed to be already in a closed state. An optional
644 * message is copied into the input buffer, and an HTTP status code stored.
645 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100646 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648static void http_server_error(struct session *t, struct stream_interface *si,
649 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100651 buffer_auto_read(si->ob);
652 buffer_abort(si->ob);
653 buffer_auto_close(si->ob);
654 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200655 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100656 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100657 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100658 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100659 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 }
661 if (!(t->flags & SN_ERR_MASK))
662 t->flags |= err;
663 if (!(t->flags & SN_FINST_MASK))
664 t->flags |= finst;
665}
666
Willy Tarreau80587432006-12-24 17:47:20 +0100667/* This function returns the appropriate error location for the given session
668 * and message.
669 */
670
671struct chunk *error_message(struct session *s, int msgnum)
672{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200673 if (s->be->errmsg[msgnum].str)
674 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100675 else if (s->fe->errmsg[msgnum].str)
676 return &s->fe->errmsg[msgnum];
677 else
678 return &http_err_chunks[msgnum];
679}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Willy Tarreau53b6c742006-12-17 13:37:46 +0100681/*
682 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
683 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
684 */
685static http_meth_t find_http_meth(const char *str, const int len)
686{
687 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100689
690 m = ((unsigned)*str - 'A');
691
692 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100693 for (h = http_methods[m]; h->len > 0; h++) {
694 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100695 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100696 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 };
699 return HTTP_METH_OTHER;
700 }
701 return HTTP_METH_NONE;
702
703}
704
Willy Tarreau21d2af32008-02-14 20:25:24 +0100705/* Parse the URI from the given transaction (which is assumed to be in request
706 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
707 * It is returned otherwise.
708 */
709static char *
710http_get_path(struct http_txn *txn)
711{
712 char *ptr, *end;
713
Willy Tarreau3a215be2012-03-09 21:39:51 +0100714 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100715 end = ptr + txn->req.sl.rq.u_l;
716
717 if (ptr >= end)
718 return NULL;
719
720 /* RFC2616, par. 5.1.2 :
721 * Request-URI = "*" | absuri | abspath | authority
722 */
723
724 if (*ptr == '*')
725 return NULL;
726
727 if (isalpha((unsigned char)*ptr)) {
728 /* this is a scheme as described by RFC3986, par. 3.1 */
729 ptr++;
730 while (ptr < end &&
731 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
732 ptr++;
733 /* skip '://' */
734 if (ptr == end || *ptr++ != ':')
735 return NULL;
736 if (ptr == end || *ptr++ != '/')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 }
741 /* skip [user[:passwd]@]host[:[port]] */
742
743 while (ptr < end && *ptr != '/')
744 ptr++;
745
746 if (ptr == end)
747 return NULL;
748
749 /* OK, we got the '/' ! */
750 return ptr;
751}
752
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753/* Returns a 302 for a redirectable request. This may only be called just after
754 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
755 * left unchanged and will follow normal proxy processing.
756 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758{
759 struct http_txn *txn;
760 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
763 int len;
764
765 /* 1: create the response header */
766 rdr.len = strlen(HTTP_302);
767 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100768 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769 memcpy(rdr.str, HTTP_302, rdr.len);
770
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 srv = target_srv(&s->target);
772
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 return;
776
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100777 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
779 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
780 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100781 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* 3: add the request URI */
784 txn = &s->txn;
785 path = http_get_path(txn);
786 if (!path)
787 return;
788
Willy Tarreau3a215be2012-03-09 21:39:51 +0100789 len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200790 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100791 return;
792
793 memcpy(rdr.str + rdr.len, path, len);
794 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100795
796 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
797 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
798 rdr.len += 29;
799 } else {
800 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
801 rdr.len += 23;
802 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803
804 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100805 si->shutr(si);
806 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 si->err_type = SI_ET_NONE;
808 si->err_loc = NULL;
809 si->state = SI_ST_CLO;
810
811 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100812 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (srv)
816 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817}
818
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 * that the server side is closed. Note that err_type is actually a
821 * bitmask, where almost only aborts may be cumulated with other
822 * values. We consider that aborted operations are more important
823 * than timeouts or errors due to the fact that nobody else in the
824 * logs might explain incomplete retries. All others should avoid
825 * being cumulated. It should normally not be possible to have multiple
826 * aborts at once, but just in case, the first one in sequence is reported.
827 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831
832 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
834 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100836 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
837 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100838 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
840 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
843 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
846 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
849 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
852 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853}
854
Willy Tarreau42250582007-04-01 01:30:43 +0200855extern const char sess_term_cond[8];
856extern const char sess_fin_state[8];
857extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200858struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200859struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100860struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862/*
863 * Capture headers from message starting at <som> according to header list
864 * <cap_hdr>, and fill the <idx> structure appropriately.
865 */
866void capture_headers(char *som, struct hdr_idx *idx,
867 char **cap, struct cap_hdr *cap_hdr)
868{
869 char *eol, *sol, *col, *sov;
870 int cur_idx;
871 struct cap_hdr *h;
872 int len;
873
874 sol = som + hdr_idx_first_pos(idx);
875 cur_idx = hdr_idx_first_idx(idx);
876
877 while (cur_idx) {
878 eol = sol + idx->v[cur_idx].len;
879
880 col = sol;
881 while (col < eol && *col != ':')
882 col++;
883
884 sov = col + 1;
885 while (sov < eol && http_is_lws[(unsigned char)*sov])
886 sov++;
887
888 for (h = cap_hdr; h; h = h->next) {
889 if ((h->namelen == col - sol) &&
890 (strncasecmp(sol, h->name, h->namelen) == 0)) {
891 if (cap[h->index] == NULL)
892 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200893 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100894
895 if (cap[h->index] == NULL) {
896 Alert("HTTP capture : out of memory.\n");
897 continue;
898 }
899
900 len = eol - sov;
901 if (len > h->len)
902 len = h->len;
903
904 memcpy(cap[h->index], sov, len);
905 cap[h->index][len]=0;
906 }
907 }
908 sol = eol + idx->v[cur_idx].cr + 1;
909 cur_idx = idx->v[cur_idx].next;
910 }
911}
912
913
Willy Tarreau42250582007-04-01 01:30:43 +0200914/* either we find an LF at <ptr> or we jump to <bad>.
915 */
916#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
917
918/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
919 * otherwise to <http_msg_ood> with <state> set to <st>.
920 */
921#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
922 ptr++; \
923 if (likely(ptr < end)) \
924 goto good; \
925 else { \
926 state = (st); \
927 goto http_msg_ood; \
928 } \
929 } while (0)
930
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100933 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100934 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
935 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
936 * will give undefined results.
937 * Note that it is upon the caller's responsibility to ensure that ptr < end,
938 * and that msg->sol points to the beginning of the response.
939 * If a complete line is found (which implies that at least one CR or LF is
940 * found before <end>, the updated <ptr> is returned, otherwise NULL is
941 * returned indicating an incomplete line (which does not mean that parts have
942 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
943 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
944 * upon next call.
945 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
948 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100950 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100951const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
952 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100953 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100954{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100955 const char *msg_start = msg->buf->p;
956
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200959 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100960 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
962
963 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100964 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
966 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100967 state = HTTP_MSG_ERROR;
968 break;
969
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200971 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100973 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 goto http_msg_rpcode;
975 }
976 if (likely(HTTP_IS_SPHT(*ptr)))
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
978 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100979 state = HTTP_MSG_ERROR;
980 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100981
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200983 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 if (likely(!HTTP_IS_LWS(*ptr)))
985 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
986
987 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100988 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
990 }
991
992 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 http_msg_rsp_reason:
995 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100996 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 msg->sl.st.r_l = 0;
998 goto http_msg_rpline_eol;
999
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001001 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 goto http_msg_rpreason;
1005 }
1006 if (likely(HTTP_IS_SPHT(*ptr)))
1007 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1008 /* so it's a CR/LF, so there is no reason phrase */
1009 goto http_msg_rsp_reason;
1010
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001012 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 if (likely(!HTTP_IS_CRLF(*ptr)))
1014 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001015 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 http_msg_rpline_eol:
1017 /* We have seen the end of line. Note that we do not
1018 * necessarily have the \n yet, but at least we know that we
1019 * have EITHER \r OR \n, otherwise the response would not be
1020 * complete. We can then record the response length and return
1021 * to the caller which will be able to register it.
1022 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001023 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 return ptr;
1025
1026#ifdef DEBUG_FULL
1027 default:
1028 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1029 exit(1);
1030#endif
1031 }
1032
1033 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001034 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 if (ret_state)
1036 *ret_state = state;
1037 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001038 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040}
1041
Willy Tarreau8973c702007-01-21 23:58:29 +01001042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001063 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001065 const char *msg_start = msg->buf->p;
1066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001069 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 if (likely(HTTP_IS_TOKEN(*ptr)))
1071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001074 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_CRLF(*ptr))) {
1079 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 msg->sl.rq.v_l = 0;
1088 goto http_msg_rqline_eol;
1089 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001090 state = HTTP_MSG_ERROR;
1091 break;
1092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001094 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 goto http_msg_rquri;
1098 }
1099 if (likely(HTTP_IS_SPHT(*ptr)))
1100 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1101 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1102 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001105 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001106 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001110 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001114 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001115 /* non-ASCII chars are forbidden unless option
1116 * accept-invalid-http-request is enabled in the frontend.
1117 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001119 if (msg->err_pos < -1)
1120 goto invalid_char;
1121 if (msg->err_pos == -1)
1122 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1124 }
1125
1126 if (likely(HTTP_IS_CRLF(*ptr))) {
1127 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1128 goto http_msg_req09_uri_e;
1129 }
1130
1131 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001132 invalid_char:
1133 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 goto http_msg_rqver;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1145 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1146 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001152
1153 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 http_msg_rqline_eol:
1156 /* We have seen the end of line. Note that we do not
1157 * necessarily have the \n yet, but at least we know that we
1158 * have EITHER \r OR \n, otherwise the request would not be
1159 * complete. We can then record the request length and return
1160 * to the caller which will be able to register it.
1161 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001162 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163 return ptr;
1164 }
1165
1166 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001167 state = HTTP_MSG_ERROR;
1168 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170#ifdef DEBUG_FULL
1171 default:
1172 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1173 exit(1);
1174#endif
1175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001178 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 if (ret_state)
1180 *ret_state = state;
1181 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001182 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001186/*
1187 * Returns the data from Authorization header. Function may be called more
1188 * than once so data is stored in txn->auth_data. When no header is found
1189 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1190 * searching again for something we are unable to find anyway.
1191 */
1192
1193char get_http_auth_buff[BUFSIZE];
1194
1195int
1196get_http_auth(struct session *s)
1197{
1198
1199 struct http_txn *txn = &s->txn;
1200 struct chunk auth_method;
1201 struct hdr_ctx ctx;
1202 char *h, *p;
1203 int len;
1204
1205#ifdef DEBUG_AUTH
1206 printf("Auth for session %p: %d\n", s, txn->auth.method);
1207#endif
1208
1209 if (txn->auth.method == HTTP_AUTH_WRONG)
1210 return 0;
1211
1212 if (txn->auth.method)
1213 return 1;
1214
1215 txn->auth.method = HTTP_AUTH_WRONG;
1216
1217 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001218
1219 if (txn->flags & TX_USE_PX_CONN) {
1220 h = "Proxy-Authorization";
1221 len = strlen(h);
1222 } else {
1223 h = "Authorization";
1224 len = strlen(h);
1225 }
1226
Willy Tarreau3a215be2012-03-09 21:39:51 +01001227 if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001228 return 0;
1229
1230 h = ctx.line + ctx.val;
1231
1232 p = memchr(h, ' ', ctx.vlen);
1233 if (!p || p == h)
1234 return 0;
1235
1236 chunk_initlen(&auth_method, h, 0, p-h);
1237 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1238
1239 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1240
1241 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1242 get_http_auth_buff, BUFSIZE - 1);
1243
1244 if (len < 0)
1245 return 0;
1246
1247
1248 get_http_auth_buff[len] = '\0';
1249
1250 p = strchr(get_http_auth_buff, ':');
1251
1252 if (!p)
1253 return 0;
1254
1255 txn->auth.user = get_http_auth_buff;
1256 *p = '\0';
1257 txn->auth.pass = p+1;
1258
1259 txn->auth.method = HTTP_AUTH_BASIC;
1260 return 1;
1261 }
1262
1263 return 0;
1264}
1265
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8973c702007-01-21 23:58:29 +01001267/*
1268 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001269 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001270 * when data are missing and recalled at the exact same location with no
1271 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001273 * fields. Note that msg->som and msg->sol will be initialized after completing
1274 * the first state, so that none of the msg pointers has to be initialized
1275 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001277void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001279 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001281 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001283 state = msg->msg_state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001284 ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001285 end = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 if (unlikely(ptr >= end))
1288 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 /*
1292 * First, states that are specific to the response only.
1293 * We check them first so that request and headers are
1294 * closer to each other (accessed more often).
1295 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001297 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001299 /* we have a start of message, but we have to check
1300 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001301 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001302 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001303 char *beg = buf->p;
1304
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001306 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001308 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001311 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 hdr_idx_init(idx);
1313 state = HTTP_MSG_RPVER;
1314 goto http_msg_rpver;
1315 }
1316
1317 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1318 goto http_msg_invalid;
1319
1320 if (unlikely(*ptr == '\n'))
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1323 /* stop here */
1324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EXPECT_LF_HERE(ptr, http_msg_invalid);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 /* stop here */
1330
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001332 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPVER_SP:
1334 case HTTP_MSG_RPCODE:
1335 case HTTP_MSG_RPCODE_SP:
1336 case HTTP_MSG_RPREASON:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001337 ptr = (char *)http_parse_stsline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001338 state, ptr, end,
1339 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (unlikely(!ptr))
1341 return;
1342
1343 /* we have a full response and we know that we have either a CR
1344 * or an LF at <ptr>.
1345 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1347
Willy Tarreau3a215be2012-03-09 21:39:51 +01001348 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 if (likely(*ptr == '\r'))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1351 goto http_msg_rpline_end;
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 /* msg->sol must point to the first of CR or LF. */
1356 EXPECT_LF_HERE(ptr, http_msg_invalid);
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1358 /* stop here */
1359
1360 /*
1361 * Second, states that are specific to the request only
1362 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001366 /* we have a start of message, but we have to check
1367 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001368 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001370 char *beg = buf->p;
1371
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001373 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001375 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001378 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001379 /* we will need this when keep-alive will be supported
1380 hdr_idx_init(idx);
1381 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 state = HTTP_MSG_RQMETH;
1383 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1387 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(*ptr == '\n'))
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001392 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001395 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 EXPECT_LF_HERE(ptr, http_msg_invalid);
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001401 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQMETH_SP:
1403 case HTTP_MSG_RQURI:
1404 case HTTP_MSG_RQURI_SP:
1405 case HTTP_MSG_RQVER:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001406 ptr = (char *)http_parse_reqline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001407 state, ptr, end,
1408 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (unlikely(!ptr))
1410 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 /* we have a full request and we know that we have either a CR
1413 * or an LF at <ptr>.
1414 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau3a215be2012-03-09 21:39:51 +01001417 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 if (likely(*ptr == '\r'))
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 /* check for HTTP/0.9 request : no version information available.
1425 * msg->sol must point to the first of CR or LF.
1426 */
1427 if (unlikely(msg->sl.rq.v_l == 0))
1428 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 EXPECT_LF_HERE(ptr, http_msg_invalid);
1431 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /*
1435 * Common states below
1436 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001439 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(!HTTP_IS_CRLF(*ptr))) {
1441 goto http_msg_hdr_name;
1442 }
1443
1444 if (likely(*ptr == '\r'))
1445 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 /* assumes msg->sol points to the first char */
1451 if (likely(HTTP_IS_TOKEN(*ptr)))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001454 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001457 if (likely(msg->err_pos < -1) || *ptr == '\n')
1458 goto http_msg_invalid;
1459
1460 if (msg->err_pos == -1) /* capture error pointer */
1461 msg->err_pos = ptr - buf->data; /* >= 0 now */
1462
1463 /* and we still accept this non-token character */
1464 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001468 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 if (likely(HTTP_IS_SPHT(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001473 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 if (likely(!HTTP_IS_CRLF(*ptr))) {
1476 goto http_msg_hdr_val;
1477 }
1478
1479 if (likely(*ptr == '\r'))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1481 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001484 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 EXPECT_LF_HERE(ptr, http_msg_invalid);
1486 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001489 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 if (likely(HTTP_IS_SPHT(*ptr))) {
1491 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 for (; buf->p + msg->sov < ptr; msg->sov++)
1493 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 goto http_msg_hdr_l1_sp;
1495 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001496 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001497 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 goto http_msg_complete_header;
1499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001502 /* assumes msg->sol points to the first char, and msg->sov
1503 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 */
1505 if (likely(!HTTP_IS_CRLF(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau12e48b32012-03-05 16:57:34 +01001508 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 /* Note: we could also copy eol into ->eoh so that we have the
1510 * real header end in case it ends with lots of LWS, but is this
1511 * really needed ?
1512 */
1513 if (likely(*ptr == '\r'))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1515 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001518 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 EXPECT_LF_HERE(ptr, http_msg_invalid);
1520 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001523 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1525 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001526 for (; buf->p + msg->eol < ptr; msg->eol++)
1527 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 goto http_msg_hdr_val;
1529 }
1530 http_msg_complete_header:
1531 /*
1532 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001533 * Assumes msg->sol points to the first char, msg->sov points
1534 * to the first character of the value and msg->eol to the
1535 * first CR or LF so we know how the line ends. We insert last
1536 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001538 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 idx, idx->tail) < 0))
1540 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001541
Willy Tarreau3a215be2012-03-09 21:39:51 +01001542 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (likely(!HTTP_IS_CRLF(*ptr))) {
1544 goto http_msg_hdr_name;
1545 }
1546
1547 if (likely(*ptr == '\r'))
1548 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1549 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 /* Assumes msg->sol points to the first of either CR or LF */
1554 EXPECT_LF_HERE(ptr, http_msg_invalid);
1555 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001556 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001557 msg->eoh = msg->sol;
1558 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001559 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001561
1562 case HTTP_MSG_ERROR:
1563 /* this may only happen if we call http_msg_analyser() twice with an error */
1564 break;
1565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566#ifdef DEBUG_FULL
1567 default:
1568 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1569 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001570#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 }
1572 http_msg_ood:
1573 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001574 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001575 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 http_msg_invalid:
1579 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001581 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 return;
1583}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001584
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001585/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1586 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1587 * nothing is done and 1 is returned.
1588 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001589static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001590{
1591 int delta;
1592 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001593 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594
1595 if (msg->sl.rq.v_l != 0)
1596 return 1;
1597
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001598 cur_end = msg->buf->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001599 delta = 0;
1600
1601 if (msg->sl.rq.u_l == 0) {
1602 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001603 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001604 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001605 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 }
1607 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001608 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001609 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 cur_end += delta;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 cur_end = (char *)http_parse_reqline(msg, msg->buf->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 HTTP_MSG_RQMETH,
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001613 msg->buf->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 NULL, NULL);
1615 if (unlikely(!cur_end))
1616 return 0;
1617
1618 /* we have a full HTTP/1.0 request now and we know that
1619 * we have either a CR or an LF at <ptr>.
1620 */
1621 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1622 return 1;
1623}
1624
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001625/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001626 * and "keep-alive" values. If we already know that some headers may safely
1627 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1629 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1630 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1631 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1632 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001634 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001635void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001636{
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001638 const char *hdr_val = "Connection";
1639 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001640
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001641 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 return;
1643
Willy Tarreau88d349d2010-01-25 12:15:43 +01001644 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1645 hdr_val = "Proxy-Connection";
1646 hdr_len = 16;
1647 }
1648
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001650 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001651 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001652 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1653 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654 if (to_del & 2)
1655 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 else
1657 txn->flags |= TX_CON_KAL_SET;
1658 }
1659 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1660 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001661 if (to_del & 1)
1662 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 else
1664 txn->flags |= TX_CON_CLO_SET;
1665 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001666 }
1667
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001668 txn->flags |= TX_HDR_CONN_PRS;
1669 return;
1670}
Willy Tarreau5b154472009-12-21 20:11:07 +01001671
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672/* Apply desired changes on the Connection: header. Values may be removed and/or
1673 * added depending on the <wanted> flags, which are exclusively composed of
1674 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1675 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1676 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001677void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001678{
1679 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001680 const char *hdr_val = "Connection";
1681 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682
1683 ctx.idx = 0;
1684
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685
1686 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1687 hdr_val = "Proxy-Connection";
1688 hdr_len = 16;
1689 }
1690
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001692 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001693 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1694 if (wanted & TX_CON_KAL_SET)
1695 txn->flags |= TX_CON_KAL_SET;
1696 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001697 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001698 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1700 if (wanted & TX_CON_CLO_SET)
1701 txn->flags |= TX_CON_CLO_SET;
1702 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001703 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001704 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706
1707 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1708 return;
1709
1710 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1711 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001712 hdr_val = "Connection: close";
1713 hdr_len = 17;
1714 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1715 hdr_val = "Proxy-Connection: close";
1716 hdr_len = 23;
1717 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001718 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001719 }
1720
1721 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1722 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001723 hdr_val = "Connection: keep-alive";
1724 hdr_len = 22;
1725 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1726 hdr_val = "Proxy-Connection: keep-alive";
1727 hdr_len = 28;
1728 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 }
1731 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001732}
1733
Willy Tarreaua458b672012-03-05 11:17:50 +01001734/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735 * first byte of body, and increments msg->sov by the number of bytes parsed,
1736 * so that we know we can forward between ->som and ->sov. Note that due to
1737 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1738 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001741 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001743{
Willy Tarreaua458b672012-03-05 11:17:50 +01001744 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1745 char *ptr_old = ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 char *end = buf->data + buf->size;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001747 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 unsigned int chunk = 0;
1749
1750 /* The chunk size is in the following form, though we are only
1751 * interested in the size and CRLF :
1752 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1753 */
1754 while (1) {
1755 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001756 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001757 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001758 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 if (c < 0) /* not a hex digit anymore */
1760 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 if (++ptr >= end)
1762 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001763 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001764 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 chunk = (chunk << 4) + c;
1766 }
1767
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001769 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771
1772 while (http_is_spht[(unsigned char)*ptr]) {
1773 if (++ptr >= end)
1774 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001775 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001776 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001777 }
1778
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 /* Up to there, we know that at least one byte is present at *ptr. Check
1780 * for the end of chunk size.
1781 */
1782 while (1) {
1783 if (likely(HTTP_IS_CRLF(*ptr))) {
1784 /* we now have a CR or an LF at ptr */
1785 if (likely(*ptr == '\r')) {
1786 if (++ptr >= end)
1787 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001788 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 return 0;
1790 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001791
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001793 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001794 if (++ptr >= end)
1795 ptr = buf->data;
1796 /* done */
1797 break;
1798 }
1799 else if (*ptr == ';') {
1800 /* chunk extension, ends at next CRLF */
1801 if (++ptr >= end)
1802 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001803 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001804 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001805
1806 while (!HTTP_IS_CRLF(*ptr)) {
1807 if (++ptr >= end)
1808 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 return 0;
1811 }
1812 /* we have a CRLF now, loop above */
1813 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001814 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001816 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001817 }
1818
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001820 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001823 msg->sov += ptr - ptr_old;
1824 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001825 msg->chunk_len = chunk;
1826 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001829 error:
1830 msg->err_pos = ptr - buf->data;
1831 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001832}
1833
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834/* This function skips trailers in the buffer <buf> associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001835 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 * the trailers is found, it is automatically scheduled to be forwarded,
1837 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1838 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001840 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001842 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1843 * which implies that all non-trailers data have already been scheduled for
1844 * forwarding, and that the difference between msg->som and msg->sov exactly
1845 * matches the length of trailers already parsed and not forwarded. It is also
1846 * important to note that this function is designed to be able to parse wrapped
1847 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 */
1849int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1850{
Willy Tarreaua458b672012-03-05 11:17:50 +01001851 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 while (1) {
1853 char *p1 = NULL, *p2 = NULL;
Willy Tarreaua458b672012-03-05 11:17:50 +01001854 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001855 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001856 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857
1858 /* scan current line and stop at LF or CRLF */
1859 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001860 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 return 0;
1862
1863 if (*ptr == '\n') {
1864 if (!p1)
1865 p1 = ptr;
1866 p2 = ptr;
1867 break;
1868 }
1869
1870 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001871 if (p1) {
1872 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875 p1 = ptr;
1876 }
1877
1878 ptr++;
1879 if (ptr >= buf->data + buf->size)
1880 ptr = buf->data;
1881 }
1882
1883 /* after LF; point to beginning of next line */
1884 p2++;
1885 if (p2 >= buf->data + buf->size)
1886 p2 = buf->data;
1887
Willy Tarreaua458b672012-03-05 11:17:50 +01001888 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001889 if (bytes < 0)
1890 bytes += buf->size;
1891
1892 /* schedule this line for forwarding */
1893 msg->sov += bytes;
1894 if (msg->sov >= buf->size)
1895 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
Willy Tarreaua458b672012-03-05 11:17:50 +01001897 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 /* LF/CRLF at beginning of line => end of trailers at p2.
1899 * Everything was scheduled for forwarding, there's nothing
1900 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001901 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001902 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001903 msg->msg_state = HTTP_MSG_DONE;
1904 return 1;
1905 }
1906 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001907 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 }
1909}
1910
1911/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1912 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001913 * ->som, ->next in order to include this part into the next forwarding phase.
1914 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1916 * not enough data are available, the function does not change anything and
1917 * returns zero. If a parse error is encountered, the function returns < 0 and
1918 * does not change anything. Note: this function is designed to parse wrapped
1919 * CRLF at the end of the buffer.
1920 */
1921int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1922{
1923 char *ptr;
1924 int bytes;
1925
1926 /* NB: we'll check data availabilty at the end. It's not a
1927 * problem because whatever we match first will be checked
1928 * against the correct length.
1929 */
1930 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001931 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932 if (*ptr == '\r') {
1933 bytes++;
1934 ptr++;
1935 if (ptr >= buf->data + buf->size)
1936 ptr = buf->data;
1937 }
1938
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001939 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 return 0;
1941
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001942 if (*ptr != '\n') {
1943 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001945 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946
1947 ptr++;
1948 if (ptr >= buf->data + buf->size)
1949 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001950 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1951 msg->som = 0;
1952 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1954 return 1;
1955}
Willy Tarreau5b154472009-12-21 20:11:07 +01001956
Willy Tarreau89fa7062012-03-02 16:13:16 +01001957/* This function may only be used when the buffer's o is empty */
Willy Tarreau83e3af02009-12-28 17:39:57 +01001958void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1959{
Willy Tarreau89fa7062012-03-02 16:13:16 +01001960 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001961
1962 /* two possible cases :
1963 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001964 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001965 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001966 if (buf->i) {
1967 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001968 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001969 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001970 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001971 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001972 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001973 }
1974 if (block2)
1975 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001976 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001977 if (block2)
1978 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001979 }
1980
1981 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001982 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001983
Willy Tarreau83e3af02009-12-28 17:39:57 +01001984 if (msg->err_pos >= 0) {
1985 msg->err_pos += off;
1986 if (msg->err_pos >= buf->size)
1987 msg->err_pos -= buf->size;
1988 }
1989
1990 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001991 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01001992 buf->flags |= BF_FULL;
1993}
1994
Willy Tarreaud787e662009-07-07 10:14:51 +02001995/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1996 * processing can continue on next analysers, or zero if it either needs more
1997 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1998 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1999 * when it has nothing left to do, and may remove any analyser when it wants to
2000 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002002int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003{
Willy Tarreau59234e92008-11-30 23:51:27 +01002004 /*
2005 * We will parse the partial (or complete) lines.
2006 * We will check the request syntax, and also join multi-line
2007 * headers. An index of all the lines will be elaborated while
2008 * parsing.
2009 *
2010 * For the parsing, we use a 28 states FSM.
2011 *
2012 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002013 * req->p + msg->som = beginning of request
2014 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002015 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002016 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002017 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002018 *
2019 * At end of parsing, we may perform a capture of the error (if any), and
2020 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2021 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2022 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002024
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 struct http_txn *txn = &s->txn;
2028 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002029 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002030
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002031 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002032 now_ms, __FUNCTION__,
2033 s,
2034 req,
2035 req->rex, req->wex,
2036 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002037 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002038 req->analysers);
2039
Willy Tarreau52a0c602009-08-16 22:45:38 +02002040 /* we're speaking HTTP here, so let's speak HTTP to the client */
2041 s->srv_error = http_return_srv_error;
2042
Willy Tarreau83e3af02009-12-28 17:39:57 +01002043 /* There's a protected area at the end of the buffer for rewriting
2044 * purposes. We don't want to start to parse the request if the
2045 * protected area is affected, because we may have to move processed
2046 * data later, which is much more complicated.
2047 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002048 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002049 if ((txn->flags & TX_NOT_FIRST) &&
2050 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002051 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002052 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002053 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002054 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2055 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002056 /* some data has still not left the buffer, wake us once that's done */
2057 buffer_dont_connect(req);
2058 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2059 return 0;
2060 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002061 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002062 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002063 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002064 }
2065
Willy Tarreau065e8332010-01-08 00:30:20 +01002066 /* Note that we have the same problem with the response ; we
2067 * may want to send a redirect, error or anything which requires
2068 * some spare space. So we'll ensure that we have at least
2069 * maxrewrite bytes available in the response buffer before
2070 * processing that one. This will only affect pipelined
2071 * keep-alive requests.
2072 */
2073 if ((txn->flags & TX_NOT_FIRST) &&
2074 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002075 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) < buffer_wrap_add(s->rep, s->rep->p + txn->rsp.next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002076 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002077 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002078 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2079 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002080 /* don't let a connection request be initiated */
2081 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002082 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002083 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002084 return 0;
2085 }
2086 }
2087
Willy Tarreaua458b672012-03-05 11:17:50 +01002088 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002089 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002090 }
2091
Willy Tarreau59234e92008-11-30 23:51:27 +01002092 /* 1: we might have to print this header in debug mode */
2093 if (unlikely((global.mode & MODE_DEBUG) &&
2094 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002095 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002096 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002097 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002098
Willy Tarreauea1175a2012-03-05 15:52:30 +01002099 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 eol = sol + msg->sl.rq.l;
2101 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002102
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 sol += hdr_idx_first_pos(&txn->hdr_idx);
2104 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002105
Willy Tarreau59234e92008-11-30 23:51:27 +01002106 while (cur_idx) {
2107 eol = sol + txn->hdr_idx.v[cur_idx].len;
2108 debug_hdr("clihdr", s, sol, eol);
2109 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2110 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002112 }
2113
Willy Tarreau58f10d72006-12-04 02:26:12 +01002114
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 /*
2116 * Now we quickly check if we have found a full valid request.
2117 * If not so, we check the FD and buffer states before leaving.
2118 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002119 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002120 * requests are checked first. When waiting for a second request
2121 * on a keep-alive session, if we encounter and error, close, t/o,
2122 * we note the error in the session flags but don't set any state.
2123 * Since the error will be noted there, it will not be counted by
2124 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002125 * Last, we may increase some tracked counters' http request errors on
2126 * the cases that are deliberately the client's fault. For instance,
2127 * a timeout or connection reset is not counted as an error. However
2128 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002129 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002130
Willy Tarreau655dce92009-11-08 13:10:58 +01002131 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002132 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002134 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002135 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002136 session_inc_http_req_ctr(s);
2137 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002138 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002140 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002141
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 /* 1: Since we are in header mode, if there's no space
2143 * left for headers, we won't be able to free more
2144 * later, so the session will never terminate. We
2145 * must terminate it now.
2146 */
2147 if (unlikely(req->flags & BF_FULL)) {
2148 /* FIXME: check if URI is set and return Status
2149 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002150 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002151 session_inc_http_req_ctr(s);
2152 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002153 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002154 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002155 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002156 goto return_bad_req;
2157 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002158
Willy Tarreau59234e92008-11-30 23:51:27 +01002159 /* 2: have we encountered a read error ? */
2160 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002161 if (!(s->flags & SN_ERR_MASK))
2162 s->flags |= SN_ERR_CLICL;
2163
Willy Tarreaufcffa692010-01-10 14:21:19 +01002164 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002165 goto failed_keep_alive;
2166
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002168 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002169 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002170 session_inc_http_err_ctr(s);
2171 }
2172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 msg->msg_state = HTTP_MSG_ERROR;
2174 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002175
Willy Tarreauda7ff642010-06-23 11:44:09 +02002176 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002177 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002178 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179 if (s->listener->counters)
2180 s->listener->counters->failed_req++;
2181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 if (!(s->flags & SN_FINST_MASK))
2183 s->flags |= SN_FINST_R;
2184 return 0;
2185 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 /* 3: has the read timeout expired ? */
2188 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002189 if (!(s->flags & SN_ERR_MASK))
2190 s->flags |= SN_ERR_CLITO;
2191
Willy Tarreaufcffa692010-01-10 14:21:19 +01002192 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002193 goto failed_keep_alive;
2194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002196 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002197 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002198 session_inc_http_err_ctr(s);
2199 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 txn->status = 408;
2201 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2202 msg->msg_state = HTTP_MSG_ERROR;
2203 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002204
Willy Tarreauda7ff642010-06-23 11:44:09 +02002205 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002206 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002207 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002208 if (s->listener->counters)
2209 s->listener->counters->failed_req++;
2210
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 if (!(s->flags & SN_FINST_MASK))
2212 s->flags |= SN_FINST_R;
2213 return 0;
2214 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002215
Willy Tarreau59234e92008-11-30 23:51:27 +01002216 /* 4: have we encountered a close ? */
2217 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002218 if (!(s->flags & SN_ERR_MASK))
2219 s->flags |= SN_ERR_CLICL;
2220
Willy Tarreaufcffa692010-01-10 14:21:19 +01002221 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002222 goto failed_keep_alive;
2223
Willy Tarreau4076a152009-04-02 15:18:36 +02002224 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002225 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002226 txn->status = 400;
2227 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2228 msg->msg_state = HTTP_MSG_ERROR;
2229 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002230
Willy Tarreauda7ff642010-06-23 11:44:09 +02002231 session_inc_http_err_ctr(s);
2232 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002233 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002234 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002235 if (s->listener->counters)
2236 s->listener->counters->failed_req++;
2237
Willy Tarreau59234e92008-11-30 23:51:27 +01002238 if (!(s->flags & SN_FINST_MASK))
2239 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002240 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002241 }
2242
Willy Tarreau520d95e2009-09-19 21:04:57 +02002243 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002244 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002245 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002246#ifdef TCP_QUICKACK
2247 if (s->listener->options & LI_O_NOQUICKACK) {
2248 /* We need more data, we have to re-enable quick-ack in case we
2249 * previously disabled it, otherwise we might cause the client
2250 * to delay next data.
2251 */
2252 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2253 }
2254#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002255
Willy Tarreaufcffa692010-01-10 14:21:19 +01002256 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2257 /* If the client starts to talk, let's fall back to
2258 * request timeout processing.
2259 */
2260 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002261 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002262 }
2263
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002265 if (!tick_isset(req->analyse_exp)) {
2266 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2267 (txn->flags & TX_WAIT_NEXT_RQ) &&
2268 tick_isset(s->be->timeout.httpka))
2269 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2270 else
2271 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2272 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 /* we're not ready yet */
2275 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002276
2277 failed_keep_alive:
2278 /* Here we process low-level errors for keep-alive requests. In
2279 * short, if the request is not the first one and it experiences
2280 * a timeout, read error or shutdown, we just silently close so
2281 * that the client can try again.
2282 */
2283 txn->status = 0;
2284 msg->msg_state = HTTP_MSG_RQBEFORE;
2285 req->analysers = 0;
2286 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002287 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002288 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002289 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002291
Willy Tarreaud787e662009-07-07 10:14:51 +02002292 /* OK now we have a complete HTTP request with indexed headers. Let's
2293 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002294 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2295 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002296 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002297 * byte after the last LF. msg->sov points to the first byte of data.
2298 * msg->eol cannot be trusted because it may have been left uninitialized
2299 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002300 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002301
Willy Tarreauda7ff642010-06-23 11:44:09 +02002302 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002303 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2304
Willy Tarreaub16a5742010-01-10 14:46:16 +01002305 if (txn->flags & TX_WAIT_NEXT_RQ) {
2306 /* kill the pending keep-alive timeout */
2307 txn->flags &= ~TX_WAIT_NEXT_RQ;
2308 req->analyse_exp = TICK_ETERNITY;
2309 }
2310
2311
Willy Tarreaud787e662009-07-07 10:14:51 +02002312 /* Maybe we found in invalid header name while we were configured not
2313 * to block on that, so we have to capture it now.
2314 */
2315 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002316 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 /*
2319 * 1: identify the method
2320 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002321 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002322
2323 /* we can make use of server redirect on GET and HEAD */
2324 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2325 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 /*
2328 * 2: check if the URI matches the monitor_uri.
2329 * We have to do this for every request which gets in, because
2330 * the monitor-uri is defined by the frontend.
2331 */
2332 if (unlikely((s->fe->monitor_uri_len != 0) &&
2333 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002334 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 s->fe->monitor_uri,
2336 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002337 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002339 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002341
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002343 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002346 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2347 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002348
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 ret = acl_pass(ret);
2350 if (cond->pol == ACL_COND_UNLESS)
2351 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 if (ret) {
2354 /* we fail this request, let's return 503 service unavail */
2355 txn->status = 503;
2356 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2357 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002358 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 /* nothing to fail, let's reply normaly */
2362 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002363 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 goto return_prx_cond;
2365 }
2366
2367 /*
2368 * 3: Maybe we have to copy the original REQURI for the logs ?
2369 * Note: we cannot log anymore if the request has been
2370 * classified as invalid.
2371 */
2372 if (unlikely(s->logs.logwait & LW_REQ)) {
2373 /* we have a complete HTTP request that we must log */
2374 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2375 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002376
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 if (urilen >= REQURI_LEN)
2378 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002379 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002381
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 if (!(s->logs.logwait &= ~LW_REQ))
2383 s->do_log(s);
2384 } else {
2385 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002386 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 }
Willy Tarreau06619262006-12-17 08:37:22 +01002388
William Lallemanda73203e2012-03-12 12:48:57 +01002389 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2390 s->unique_id = pool_alloc2(pool2_uniqueid);
2391 }
2392
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002394 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002395 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002396
Willy Tarreau5b154472009-12-21 20:11:07 +01002397 /* ... and check if the request is HTTP/1.1 or above */
2398 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002399 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2400 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2401 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002402 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002403
2404 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002405 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002406
Willy Tarreau88d349d2010-01-25 12:15:43 +01002407 /* if the frontend has "option http-use-proxy-header", we'll check if
2408 * we have what looks like a proxied connection instead of a connection,
2409 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2410 * Note that this is *not* RFC-compliant, however browsers and proxies
2411 * happen to do that despite being non-standard :-(
2412 * We consider that a request not beginning with either '/' or '*' is
2413 * a proxied connection, which covers both "scheme://location" and
2414 * CONNECT ip:port.
2415 */
2416 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002417 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002418 txn->flags |= TX_USE_PX_CONN;
2419
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002420 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002421 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002424 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002425 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 /* 6: determine the transfer-length.
2429 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2430 * the presence of a message-body in a REQUEST and its transfer length
2431 * must be determined that way (in order of precedence) :
2432 * 1. The presence of a message-body in a request is signaled by the
2433 * inclusion of a Content-Length or Transfer-Encoding header field
2434 * in the request's header fields. When a request message contains
2435 * both a message-body of non-zero length and a method that does
2436 * not define any semantics for that request message-body, then an
2437 * origin server SHOULD either ignore the message-body or respond
2438 * with an appropriate error message (e.g., 413). A proxy or
2439 * gateway, when presented the same request, SHOULD either forward
2440 * the request inbound with the message- body or ignore the
2441 * message-body when determining a response.
2442 *
2443 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2444 * and the "chunked" transfer-coding (Section 6.2) is used, the
2445 * transfer-length is defined by the use of this transfer-coding.
2446 * If a Transfer-Encoding header field is present and the "chunked"
2447 * transfer-coding is not present, the transfer-length is defined
2448 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002450 * 3. If a Content-Length header field is present, its decimal value in
2451 * OCTETs represents both the entity-length and the transfer-length.
2452 * If a message is received with both a Transfer-Encoding header
2453 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002454 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002455 * 4. By the server closing the connection. (Closing the connection
2456 * cannot be used to indicate the end of a request body, since that
2457 * would leave no possibility for the server to send back a response.)
2458 *
2459 * Whenever a transfer-coding is applied to a message-body, the set of
2460 * transfer-codings MUST include "chunked", unless the message indicates
2461 * it is terminated by closing the connection. When the "chunked"
2462 * transfer-coding is used, it MUST be the last transfer-coding applied
2463 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 */
2465
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002468 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002469 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002470 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002471 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002472 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2473 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002474 /* bad transfer-encoding (chunked followed by something else) */
2475 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002476 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 break;
2478 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 }
2480
Willy Tarreau32b47f42009-10-18 20:55:02 +02002481 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002482 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002483 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 signed long long cl;
2485
Willy Tarreauad14f752011-09-02 20:33:27 +02002486 if (!ctx.vlen) {
2487 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002489 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490
Willy Tarreauad14f752011-09-02 20:33:27 +02002491 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2492 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002494 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495
Willy Tarreauad14f752011-09-02 20:33:27 +02002496 if (cl < 0) {
2497 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002501 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002502 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002504 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002505
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002506 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002507 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508 }
2509
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002510 /* bodyless requests have a known length */
2511 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002512 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002513
Willy Tarreaud787e662009-07-07 10:14:51 +02002514 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002515 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002516 req->analyse_exp = TICK_ETERNITY;
2517 return 1;
2518
2519 return_bad_req:
2520 /* We centralize bad requests processing here */
2521 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2522 /* we detected a parsing error. We want to archive this request
2523 * in the dedicated proxy area for later troubleshooting.
2524 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002525 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002526 }
2527
2528 txn->req.msg_state = HTTP_MSG_ERROR;
2529 txn->status = 400;
2530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002531
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002532 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533 if (s->listener->counters)
2534 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002535
2536 return_prx_cond:
2537 if (!(s->flags & SN_ERR_MASK))
2538 s->flags |= SN_ERR_PRXCOND;
2539 if (!(s->flags & SN_FINST_MASK))
2540 s->flags |= SN_FINST_R;
2541
2542 req->analysers = 0;
2543 req->analyse_exp = TICK_ETERNITY;
2544 return 0;
2545}
2546
Cyril Bonté70be45d2010-10-12 00:14:35 +02002547/* We reached the stats page through a POST request.
2548 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002549 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002551int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002552{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002553 struct proxy *px = NULL;
2554 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002555
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002556 char key[LINESIZE];
2557 int action = ST_ADM_ACTION_NONE;
2558 int reprocess = 0;
2559
2560 int total_servers = 0;
2561 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002562
2563 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002564 char *st_cur_param = NULL;
2565 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002566
Willy Tarreauea1175a2012-03-05 15:52:30 +01002567 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002568 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569
2570 cur_param = next_param = end_params;
2571
Cyril Bonté23b39d92011-02-10 22:54:44 +01002572 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002573 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002574 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575 return 1;
2576 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002577 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002578 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002579 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002580 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 }
2582
2583 *end_params = '\0';
2584
Willy Tarreau295a8372011-03-10 11:25:07 +01002585 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002586
2587 /*
2588 * Parse the parameters in reverse order to only store the last value.
2589 * From the html form, the backend and the action are at the end.
2590 */
2591 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002592 char *value;
2593 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002594
2595 cur_param--;
2596 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002597 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002598 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002599 poffset = (cur_param != first_param ? 1 : 0);
2600 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2601 if ((plen > 0) && (plen <= sizeof(key))) {
2602 strncpy(key, cur_param + poffset, plen);
2603 key[plen - 1] = '\0';
2604 } else {
2605 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2606 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002607 }
2608
2609 /* Parse the value */
2610 value = key;
2611 while (*value != '\0' && *value != '=') {
2612 value++;
2613 }
2614 if (*value == '=') {
2615 /* Ok, a value is found, we can mark the end of the key */
2616 *value++ = '\0';
2617 }
2618
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002619 if (!url_decode(key) || !url_decode(value))
2620 break;
2621
Cyril Bonté70be45d2010-10-12 00:14:35 +02002622 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002623 if (!px && (strcmp(key, "b") == 0)) {
2624 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2625 /* the backend name is unknown or ambiguous (duplicate names) */
2626 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2627 goto out;
2628 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002629 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002630 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002631 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002632 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002633 }
2634 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002635 action = ST_ADM_ACTION_ENABLE;
2636 }
2637 else {
2638 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2639 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002640 }
2641 }
2642 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002643 if (!(px && action)) {
2644 /*
2645 * Indicates that we'll need to reprocess the parameters
2646 * as soon as backend and action are known
2647 */
2648 if (!reprocess) {
2649 st_cur_param = cur_param;
2650 st_next_param = next_param;
2651 }
2652 reprocess = 1;
2653 }
2654 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002655 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002656 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002657 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002658 /* Not already in maintenance, we can change the server state */
2659 sv->state |= SRV_MAINTAIN;
2660 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002661 altered_servers++;
2662 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002663 }
2664 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002665 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002666 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002667 /* Already in maintenance, we can change the server state */
2668 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002669 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002670 altered_servers++;
2671 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002672 }
2673 break;
2674 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002675 } else {
2676 /* the server name is unknown or ambiguous (duplicate names) */
2677 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678 }
2679 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002680 if (reprocess && px && action) {
2681 /* Now, we know the backend and the action chosen by the user.
2682 * We can safely restart from the first server parameter
2683 * to reprocess them
2684 */
2685 cur_param = st_cur_param;
2686 next_param = st_next_param;
2687 reprocess = 0;
2688 goto reprocess_servers;
2689 }
2690
Cyril Bonté70be45d2010-10-12 00:14:35 +02002691 next_param = cur_param;
2692 }
2693 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002694
2695 if (total_servers == 0) {
2696 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2697 }
2698 else if (altered_servers == 0) {
2699 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2700 }
2701 else if (altered_servers == total_servers) {
2702 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2703 }
2704 else {
2705 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2706 }
2707 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002708 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002709}
2710
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002711/* returns a pointer to the first rule which forbids access (deny or http_auth),
2712 * or NULL if everything's OK.
2713 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002714static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002715http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2716{
Willy Tarreauff011f22011-01-06 17:51:27 +01002717 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002718
Willy Tarreauff011f22011-01-06 17:51:27 +01002719 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002720 int ret = 1;
2721
Willy Tarreauff011f22011-01-06 17:51:27 +01002722 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002723 continue;
2724
2725 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002726 if (rule->cond) {
2727 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002728 ret = acl_pass(ret);
2729
Willy Tarreauff011f22011-01-06 17:51:27 +01002730 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002731 ret = !ret;
2732 }
2733
2734 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002735 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002736 return NULL; /* no problem */
2737 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002738 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002739 }
2740 }
2741 return NULL;
2742}
2743
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744/* This stream analyser runs all HTTP request processing which is common to
2745 * frontends and backends, which means blocking ACLs, filters, connection-close,
2746 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002747 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 * either needs more data or wants to immediately abort the request (eg: deny,
2749 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002750 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002751int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002752{
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 struct http_txn *txn = &s->txn;
2754 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002756 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002758 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002759 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002760
Willy Tarreau655dce92009-11-08 13:10:58 +01002761 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002762 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002763 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002764 return 0;
2765 }
2766
Willy Tarreau3a816292009-07-07 10:55:49 +02002767 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002768 req->analyse_exp = TICK_ETERNITY;
2769
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002770 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 now_ms, __FUNCTION__,
2772 s,
2773 req,
2774 req->rex, req->wex,
2775 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002776 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 req->analysers);
2778
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 /* first check whether we have some ACLs set to block this request */
2780 list_for_each_entry(cond, &px->block_cond, list) {
2781 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783 ret = acl_pass(ret);
2784 if (cond->pol == ACL_COND_UNLESS)
2785 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002786
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 if (ret) {
2788 txn->status = 403;
2789 /* let's log the request time */
2790 s->logs.tv_request = now;
2791 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002792 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002795 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002796
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002797 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002798 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002799
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002800 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002801 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002802 do_stats = stats_check_uri(s->rep->prod, txn, px);
2803 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002804 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002805 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002806 else
2807 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002808
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002809 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002810 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002811 txn->status = 403;
2812 s->logs.tv_request = now;
2813 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002814 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002815 s->fe->fe_counters.denied_req++;
2816 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2817 s->be->be_counters.denied_req++;
2818 if (s->listener->counters)
2819 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002820 goto return_prx_cond;
2821 }
2822
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002823 /* try headers filters */
2824 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002825 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002826 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* has the request been denied ? */
2829 if (txn->flags & TX_CLDENY) {
2830 /* no need to go further */
2831 txn->status = 403;
2832 /* let's log the request time */
2833 s->logs.tv_request = now;
2834 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002835 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002836 goto return_prx_cond;
2837 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002838
2839 /* When a connection is tarpitted, we use the tarpit timeout,
2840 * which may be the same as the connect timeout if unspecified.
2841 * If unset, then set it to zero because we really want it to
2842 * eventually expire. We build the tarpit as an analyser.
2843 */
2844 if (txn->flags & TX_CLTARPIT) {
2845 buffer_erase(s->req);
2846 /* wipe the request out so that we can drop the connection early
2847 * if the client closes first.
2848 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002849 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002850 req->analysers = 0; /* remove switching rules etc... */
2851 req->analysers |= AN_REQ_HTTP_TARPIT;
2852 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2853 if (!req->analyse_exp)
2854 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002855 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002856 return 1;
2857 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002858 }
Willy Tarreau06619262006-12-17 08:37:22 +01002859
Willy Tarreau5b154472009-12-21 20:11:07 +01002860 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2861 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002862 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2863 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002864 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2865 * avoid to redo the same work if FE and BE have the same settings (common).
2866 * The method consists in checking if options changed between the two calls
2867 * (implying that either one is non-null, or one of them is non-null and we
2868 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002869 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002870
Willy Tarreaudc008c52010-02-01 16:20:08 +01002871 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2872 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2873 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2874 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002875 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002876
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002877 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2878 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002879 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002880 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2881 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002882 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002883 tmp = TX_CON_WANT_CLO;
2884
Willy Tarreau5b154472009-12-21 20:11:07 +01002885 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2886 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002887
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002888 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2889 /* parse the Connection header and possibly clean it */
2890 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002891 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002892 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2893 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002895 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002896 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002897 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002898 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002899
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002900 /* check if client or config asks for explicit close in KAL/SCL */
2901 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2902 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2903 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002904 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002905 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002906 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002907 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002908 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2909 }
Willy Tarreau78599912009-10-17 20:12:21 +02002910
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002911 /* we can be blocked here because the request needs to be authenticated,
2912 * either to pass or to access stats.
2913 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002914 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002915 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002916 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002917
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002918 if (!realm)
2919 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2920
Willy Tarreau844a7e72010-01-31 21:46:18 +01002921 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002922 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2923 txn->status = 401;
2924 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002925 /* on 401 we still count one error, because normal browsing
2926 * won't significantly increase the counter but brute force
2927 * attempts will.
2928 */
2929 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002930 goto return_prx_cond;
2931 }
2932
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002933 /* add request headers from the rule sets in the same order */
2934 list_for_each_entry(wl, &px->req_add, list) {
2935 if (wl->cond) {
2936 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2937 ret = acl_pass(ret);
2938 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2939 ret = !ret;
2940 if (!ret)
2941 continue;
2942 }
2943
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002944 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002945 goto return_bad_req;
2946 }
2947
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002948 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002949 struct stats_admin_rule *stats_admin_rule;
2950
2951 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002952 * FIXME!!! that one is rather dangerous, we want to
2953 * make it follow standard rules (eg: clear req->analysers).
2954 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002955
Cyril Bonté474be412010-10-12 00:14:36 +02002956 /* now check whether we have some admin rules for this request */
2957 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2958 int ret = 1;
2959
2960 if (stats_admin_rule->cond) {
2961 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2962 ret = acl_pass(ret);
2963 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2964 ret = !ret;
2965 }
2966
2967 if (ret) {
2968 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002970 break;
2971 }
2972 }
2973
Cyril Bonté70be45d2010-10-12 00:14:35 +02002974 /* Was the status page requested with a POST ? */
2975 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002976 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002977 if (msg->msg_state < HTTP_MSG_100_SENT) {
2978 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2979 * send an HTTP/1.1 100 Continue intermediate response.
2980 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002981 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002982 struct hdr_ctx ctx;
2983 ctx.idx = 0;
2984 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002985 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002986 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2987 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2988 }
2989 }
2990 msg->msg_state = HTTP_MSG_100_SENT;
2991 s->logs.tv_request = now; /* update the request timer to reflect full request */
2992 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002993 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002994 /* we need more data */
2995 req->analysers |= an_bit;
2996 buffer_dont_connect(req);
2997 return 0;
2998 }
Cyril Bonté474be412010-10-12 00:14:36 +02002999 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003000 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003001 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003002 }
3003
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003004 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003005 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003006 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003007 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003008 s->rep->prod->applet.private = s;
3009 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003010 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003011 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3012 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003013
3014 return 0;
3015
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003017
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003018 /* check whether we have some ACLs set to redirect this request */
3019 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003020 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003021
Willy Tarreauf285f542010-01-03 20:03:03 +01003022 if (rule->cond) {
3023 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3024 ret = acl_pass(ret);
3025 if (rule->cond->pol == ACL_COND_UNLESS)
3026 ret = !ret;
3027 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003028
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003030 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003033 /* build redirect message */
3034 switch(rule->code) {
3035 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 msg_fmt = HTTP_303;
3037 break;
3038 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 msg_fmt = HTTP_301;
3040 break;
3041 case 302:
3042 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 msg_fmt = HTTP_302;
3044 break;
3045 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003046
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003047 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003049
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003050 switch(rule->type) {
3051 case REDIRECT_TYPE_PREFIX: {
3052 const char *path;
3053 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 path = http_get_path(txn);
3056 /* build message using path */
3057 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003058 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3060 int qs = 0;
3061 while (qs < pathlen) {
3062 if (path[qs] == '?') {
3063 pathlen = qs;
3064 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003065 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003068 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 } else {
3070 path = "/";
3071 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003074 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 goto return_bad_req;
3076
3077 /* add prefix. Note that if prefix == "/", we don't want to
3078 * add anything, otherwise it makes it hard for the user to
3079 * configure a self-redirection.
3080 */
3081 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3083 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003084 }
3085
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086 /* add path */
3087 memcpy(rdr.str + rdr.len, path, pathlen);
3088 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003089
3090 /* append a slash at the end of the location is needed and missing */
3091 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3092 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3093 if (rdr.len > rdr.size - 5)
3094 goto return_bad_req;
3095 rdr.str[rdr.len] = '/';
3096 rdr.len++;
3097 }
3098
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003099 break;
3100 }
3101 case REDIRECT_TYPE_LOCATION:
3102 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003103 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003105
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003106 /* add location */
3107 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3108 rdr.len += rule->rdr_len;
3109 break;
3110 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003111
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112 if (rule->cookie_len) {
3113 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3114 rdr.len += 14;
3115 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3116 rdr.len += rule->cookie_len;
3117 memcpy(rdr.str + rdr.len, "\r\n", 2);
3118 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003119 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003120
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003121 /* add end of headers and the keep-alive/close status.
3122 * We may choose to set keep-alive if the Location begins
3123 * with a slash, because the client will come back to the
3124 * same server.
3125 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003126 txn->status = rule->code;
3127 /* let's log the request time */
3128 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003129
3130 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003131 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3132 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003133 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3134 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3135 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003136 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003137 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3138 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3139 rdr.len += 30;
3140 } else {
3141 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3142 rdr.len += 24;
3143 }
Willy Tarreau75661452010-01-10 10:35:01 +01003144 }
3145 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3146 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003147 buffer_write(req->prod->ob, rdr.str, rdr.len);
3148 /* "eat" the request */
3149 buffer_ignore(req, msg->sov - msg->som);
3150 msg->som = msg->sov;
3151 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003152 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3153 txn->req.msg_state = HTTP_MSG_CLOSED;
3154 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003155 break;
3156 } else {
3157 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003158 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3159 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3160 rdr.len += 29;
3161 } else {
3162 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3163 rdr.len += 23;
3164 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003165 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003166 goto return_prx_cond;
3167 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003168 }
3169 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003170
Willy Tarreau2be39392010-01-03 17:24:51 +01003171 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3172 * If this happens, then the data will not come immediately, so we must
3173 * send all what we have without waiting. Note that due to the small gain
3174 * in waiting for the body of the request, it's easier to simply put the
3175 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3176 * itself once used.
3177 */
3178 req->flags |= BF_SEND_DONTWAIT;
3179
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003180 /* that's OK for us now, let's move on to next analysers */
3181 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003182
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003183 return_bad_req:
3184 /* We centralize bad requests processing here */
3185 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3186 /* we detected a parsing error. We want to archive this request
3187 * in the dedicated proxy area for later troubleshooting.
3188 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003189 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003190 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003191
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003192 txn->req.msg_state = HTTP_MSG_ERROR;
3193 txn->status = 400;
3194 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003196 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003197 if (s->listener->counters)
3198 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 return_prx_cond:
3201 if (!(s->flags & SN_ERR_MASK))
3202 s->flags |= SN_ERR_PRXCOND;
3203 if (!(s->flags & SN_FINST_MASK))
3204 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003206 req->analysers = 0;
3207 req->analyse_exp = TICK_ETERNITY;
3208 return 0;
3209}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003210
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003211/* This function performs all the processing enabled for the current request.
3212 * It returns 1 if the processing can continue on next analysers, or zero if it
3213 * needs more data, encounters an error, or wants to immediately abort the
3214 * request. It relies on buffers flags, and updates s->req->analysers.
3215 */
3216int http_process_request(struct session *s, struct buffer *req, int an_bit)
3217{
3218 struct http_txn *txn = &s->txn;
3219 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003220
Willy Tarreau655dce92009-11-08 13:10:58 +01003221 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003222 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003223 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003224 return 0;
3225 }
3226
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003227 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 now_ms, __FUNCTION__,
3229 s,
3230 req,
3231 req->rex, req->wex,
3232 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003233 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003234 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003235
Willy Tarreau59234e92008-11-30 23:51:27 +01003236 /*
3237 * Right now, we know that we have processed the entire headers
3238 * and that unwanted requests have been filtered out. We can do
3239 * whatever we want with the remaining request. Also, now we
3240 * may have separate values for ->fe, ->be.
3241 */
Willy Tarreau06619262006-12-17 08:37:22 +01003242
Willy Tarreau59234e92008-11-30 23:51:27 +01003243 /*
3244 * If HTTP PROXY is set we simply get remote server address
3245 * parsing incoming request.
3246 */
3247 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003248 url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003250
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003252 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003253 * Note that doing so might move headers in the request, but
3254 * the fields will stay coherent and the URI will not move.
3255 * This should only be performed in the backend.
3256 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003257 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3259 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003260
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003262 * 8: the appsession cookie was looked up very early in 1.2,
3263 * so let's do the same now.
3264 */
3265
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003266 /* It needs to look into the URI unless persistence must be ignored */
3267 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003268 get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003269 }
3270
William Lallemanda73203e2012-03-12 12:48:57 +01003271 /* add unique-id if "header-unique-id" is specified */
3272
3273 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3274 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3275
3276 if (s->fe->header_unique_id && s->unique_id) {
3277 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3278 if (ret < 0 || ret > global.tune.bufsize)
3279 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003280 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003281 goto return_bad_req;
3282 }
3283
Cyril Bontéb21570a2009-11-29 20:04:48 +01003284 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 * 9: add X-Forwarded-For if either the frontend or the backend
3286 * asks for it.
3287 */
3288 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003289 struct hdr_ctx ctx = { .idx = 0 };
3290
3291 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003292 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003293 /* The header is set to be added only if none is present
3294 * and we found it, so don't do anything.
3295 */
3296 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003297 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003298 /* Add an X-Forwarded-For header unless the source IP is
3299 * in the 'except' network range.
3300 */
3301 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003302 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003303 != s->fe->except_net.s_addr) &&
3304 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003305 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003307 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003309 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003310
3311 /* Note: we rely on the backend to get the header name to be used for
3312 * x-forwarded-for, because the header is really meant for the backends.
3313 * However, if the backend did not specify any option, we have to rely
3314 * on the frontend's header name.
3315 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 if (s->be->fwdfor_hdr_len) {
3317 len = s->be->fwdfor_hdr_len;
3318 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003319 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 len = s->fe->fwdfor_hdr_len;
3321 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003322 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003323 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003324
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003325 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003326 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003327 }
3328 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003329 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003330 /* FIXME: for the sake of completeness, we should also support
3331 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003332 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 int len;
3334 char pn[INET6_ADDRSTRLEN];
3335 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003336 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003338
Willy Tarreau59234e92008-11-30 23:51:27 +01003339 /* Note: we rely on the backend to get the header name to be used for
3340 * x-forwarded-for, because the header is really meant for the backends.
3341 * However, if the backend did not specify any option, we have to rely
3342 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003343 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003344 if (s->be->fwdfor_hdr_len) {
3345 len = s->be->fwdfor_hdr_len;
3346 memcpy(trash, s->be->fwdfor_hdr_name, len);
3347 } else {
3348 len = s->fe->fwdfor_hdr_len;
3349 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003350 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003351 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003352
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003353 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 goto return_bad_req;
3355 }
3356 }
3357
3358 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003359 * 10: add X-Original-To if either the frontend or the backend
3360 * asks for it.
3361 */
3362 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3363
3364 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003365 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003366 /* Add an X-Original-To header unless the destination IP is
3367 * in the 'except' network range.
3368 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003369 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003370
Willy Tarreau6471afb2011-09-23 10:54:59 +02003371 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003372 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003373 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003374 != s->fe->except_to.s_addr) &&
3375 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003376 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003377 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003378 int len;
3379 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003380 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003381
3382 /* Note: we rely on the backend to get the header name to be used for
3383 * x-original-to, because the header is really meant for the backends.
3384 * However, if the backend did not specify any option, we have to rely
3385 * on the frontend's header name.
3386 */
3387 if (s->be->orgto_hdr_len) {
3388 len = s->be->orgto_hdr_len;
3389 memcpy(trash, s->be->orgto_hdr_name, len);
3390 } else {
3391 len = s->fe->orgto_hdr_len;
3392 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003393 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003394 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3395
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003396 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003397 goto return_bad_req;
3398 }
3399 }
3400 }
3401
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003402 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3403 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003404 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003405 unsigned int want_flags = 0;
3406
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003407 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003408 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3409 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3410 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003411 want_flags |= TX_CON_CLO_SET;
3412 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003413 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3414 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3415 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003416 want_flags |= TX_CON_KAL_SET;
3417 }
3418
3419 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003420 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003421 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003422
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003423
Willy Tarreau522d6c02009-12-06 18:49:18 +01003424 /* If we have no server assigned yet and we're balancing on url_param
3425 * with a POST request, we may be interested in checking the body for
3426 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 */
3428 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3429 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003431 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432 buffer_dont_connect(req);
3433 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003434 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003435
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003436 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003437 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003438#ifdef TCP_QUICKACK
3439 /* We expect some data from the client. Unless we know for sure
3440 * we already have a full request, we have to re-enable quick-ack
3441 * in case we previously disabled it, otherwise we might cause
3442 * the client to delay further data.
3443 */
3444 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003445 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003446 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003447 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3448#endif
3449 }
Willy Tarreau03945942009-12-22 16:50:27 +01003450
Willy Tarreau59234e92008-11-30 23:51:27 +01003451 /*************************************************************
3452 * OK, that's finished for the headers. We have done what we *
3453 * could. Let's switch to the DATA state. *
3454 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003455 req->analyse_exp = TICK_ETERNITY;
3456 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003457
Willy Tarreau59234e92008-11-30 23:51:27 +01003458 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003459 /* OK let's go on with the BODY now */
3460 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003461
Willy Tarreau59234e92008-11-30 23:51:27 +01003462 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003463 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003464 /* we detected a parsing error. We want to archive this request
3465 * in the dedicated proxy area for later troubleshooting.
3466 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003467 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003468 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 txn->req.msg_state = HTTP_MSG_ERROR;
3471 txn->status = 400;
3472 req->analysers = 0;
3473 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003474
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003475 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003476 if (s->listener->counters)
3477 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003478
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 if (!(s->flags & SN_ERR_MASK))
3480 s->flags |= SN_ERR_PRXCOND;
3481 if (!(s->flags & SN_FINST_MASK))
3482 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003483 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003484}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003485
Willy Tarreau60b85b02008-11-30 23:28:40 +01003486/* This function is an analyser which processes the HTTP tarpit. It always
3487 * returns zero, at the beginning because it prevents any other processing
3488 * from occurring, and at the end because it terminates the request.
3489 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003490int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003491{
3492 struct http_txn *txn = &s->txn;
3493
3494 /* This connection is being tarpitted. The CLIENT side has
3495 * already set the connect expiration date to the right
3496 * timeout. We just have to check that the client is still
3497 * there and that the timeout has not expired.
3498 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003499 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003500 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3501 !tick_is_expired(req->analyse_exp, now_ms))
3502 return 0;
3503
3504 /* We will set the queue timer to the time spent, just for
3505 * logging purposes. We fake a 500 server error, so that the
3506 * attacker will not suspect his connection has been tarpitted.
3507 * It will not cause trouble to the logs because we can exclude
3508 * the tarpitted connections by filtering on the 'PT' status flags.
3509 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003510 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3511
3512 txn->status = 500;
3513 if (req->flags != BF_READ_ERROR)
3514 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3515
3516 req->analysers = 0;
3517 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003518
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003519 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003520 if (s->listener->counters)
3521 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003522
Willy Tarreau60b85b02008-11-30 23:28:40 +01003523 if (!(s->flags & SN_ERR_MASK))
3524 s->flags |= SN_ERR_PRXCOND;
3525 if (!(s->flags & SN_FINST_MASK))
3526 s->flags |= SN_FINST_T;
3527 return 0;
3528}
3529
Willy Tarreaud34af782008-11-30 23:36:37 +01003530/* This function is an analyser which processes the HTTP request body. It looks
3531 * for parameters to be used for the load balancing algorithm (url_param). It
3532 * must only be called after the standard HTTP request processing has occurred,
3533 * because it expects the request to be parsed. It returns zero if it needs to
3534 * read more data, or 1 once it has completed its analysis.
3535 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003536int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003537{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003538 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003539 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003540 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003541
3542 /* We have to parse the HTTP request body to find any required data.
3543 * "balance url_param check_post" should have been the only way to get
3544 * into this. We were brought here after HTTP header analysis, so all
3545 * related structures are ready.
3546 */
3547
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3549 goto missing_data;
3550
3551 if (msg->msg_state < HTTP_MSG_100_SENT) {
3552 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3553 * send an HTTP/1.1 100 Continue intermediate response.
3554 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003555 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003556 struct hdr_ctx ctx;
3557 ctx.idx = 0;
3558 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003559 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003560 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3561 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3562 }
3563 }
3564 msg->msg_state = HTTP_MSG_100_SENT;
3565 }
3566
3567 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003568 /* we have msg->sov which points to the first byte of message body.
3569 * msg->som still points to the beginning of the message. We must
3570 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003571 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003572 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003573
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003574 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003575 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3576 else
3577 msg->msg_state = HTTP_MSG_DATA;
3578 }
3579
3580 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003581 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003582 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003583 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003584 */
3585 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003586
Willy Tarreau115acb92009-12-26 13:56:06 +01003587 if (!ret)
3588 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003589 else if (ret < 0) {
3590 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003592 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003593 }
3594
Willy Tarreaud98cf932009-12-27 22:54:55 +01003595 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003596 * We have the first data byte is in msg->sov. We're waiting for at
3597 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003598 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003599
Willy Tarreau124d9912011-03-01 20:30:48 +01003600 if (msg->body_len < limit)
3601 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003603 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003604 goto http_end;
3605
3606 missing_data:
3607 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003608 if (req->flags & BF_FULL) {
3609 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003610 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003611 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003612
Willy Tarreau522d6c02009-12-06 18:49:18 +01003613 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3614 txn->status = 408;
3615 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003616
3617 if (!(s->flags & SN_ERR_MASK))
3618 s->flags |= SN_ERR_CLITO;
3619 if (!(s->flags & SN_FINST_MASK))
3620 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003622 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003623
3624 /* we get here if we need to wait for more data */
3625 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003626 /* Not enough data. We'll re-use the http-request
3627 * timeout here. Ideally, we should set the timeout
3628 * relative to the accept() date. We just set the
3629 * request timeout once at the beginning of the
3630 * request.
3631 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003632 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003633 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003634 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003635 return 0;
3636 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003637
3638 http_end:
3639 /* The situation will not evolve, so let's give up on the analysis. */
3640 s->logs.tv_request = now; /* update the request timer to reflect full request */
3641 req->analysers &= ~an_bit;
3642 req->analyse_exp = TICK_ETERNITY;
3643 return 1;
3644
3645 return_bad_req: /* let's centralize all bad requests */
3646 txn->req.msg_state = HTTP_MSG_ERROR;
3647 txn->status = 400;
3648 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3649
Willy Tarreau79ebac62010-06-07 13:47:49 +02003650 if (!(s->flags & SN_ERR_MASK))
3651 s->flags |= SN_ERR_PRXCOND;
3652 if (!(s->flags & SN_FINST_MASK))
3653 s->flags |= SN_FINST_R;
3654
Willy Tarreau522d6c02009-12-06 18:49:18 +01003655 return_err_msg:
3656 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003657 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003658 if (s->listener->counters)
3659 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003660 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003661}
3662
Willy Tarreau45c0d982012-03-09 12:11:57 +01003663/* send a server's name with an outgoing request over an established connection */
3664int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003665
3666 struct hdr_ctx ctx;
3667
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668 char *hdr_name = be->server_id_hdr_name;
3669 int hdr_name_len = be->server_id_hdr_len;
3670
3671 char *hdr_val;
3672
William Lallemandd9e90662012-01-30 17:27:17 +01003673 ctx.idx = 0;
3674
Willy Tarreau45c0d982012-03-09 12:11:57 +01003675 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003676 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003677 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003678 }
3679
3680 /* Add the new header requested with the server value */
3681 hdr_val = trash;
3682 memcpy(hdr_val, hdr_name, hdr_name_len);
3683 hdr_val += hdr_name_len;
3684 *hdr_val++ = ':';
3685 *hdr_val++ = ' ';
3686 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003687 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003688
3689 return 0;
3690}
3691
Willy Tarreau610ecce2010-01-04 21:15:02 +01003692/* Terminate current transaction and prepare a new one. This is very tricky
3693 * right now but it works.
3694 */
3695void http_end_txn_clean_session(struct session *s)
3696{
3697 /* FIXME: We need a more portable way of releasing a backend's and a
3698 * server's connections. We need a safer way to reinitialize buffer
3699 * flags. We also need a more accurate method for computing per-request
3700 * data.
3701 */
3702 http_silent_debug(__LINE__, s);
3703
3704 s->req->cons->flags |= SI_FL_NOLINGER;
3705 s->req->cons->shutr(s->req->cons);
3706 s->req->cons->shutw(s->req->cons);
3707
3708 http_silent_debug(__LINE__, s);
3709
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003710 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003711 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003712 if (unlikely(s->srv_conn))
3713 sess_change_server(s, NULL);
3714 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715
3716 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3717 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003718 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719
3720 if (s->txn.status) {
3721 int n;
3722
3723 n = s->txn.status / 100;
3724 if (n < 1 || n > 5)
3725 n = 0;
3726
3727 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003728 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729
Willy Tarreau24657792010-02-26 10:30:28 +01003730 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003732 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733 }
3734
3735 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003736 s->logs.bytes_in -= s->req->i;
3737 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003738
3739 /* let's do a final log if we need it */
3740 if (s->logs.logwait &&
3741 !(s->flags & SN_MONITOR) &&
3742 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3743 s->do_log(s);
3744 }
3745
3746 s->logs.accept_date = date; /* user-visible date for logging */
3747 s->logs.tv_accept = now; /* corrected date for internal use */
3748 tv_zero(&s->logs.tv_request);
3749 s->logs.t_queue = -1;
3750 s->logs.t_connect = -1;
3751 s->logs.t_data = -1;
3752 s->logs.t_close = 0;
3753 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3754 s->logs.srv_queue_size = 0; /* we will get this number soon */
3755
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003756 s->logs.bytes_in = s->req->total = s->req->i;
3757 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003758
3759 if (s->pend_pos)
3760 pendconn_free(s->pend_pos);
3761
Willy Tarreau827aee92011-03-10 16:55:02 +01003762 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003763 if (s->flags & SN_CURR_SESS) {
3764 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003765 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003766 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003767 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3768 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769 }
3770
Willy Tarreau9e000c62011-03-10 14:03:36 +01003771 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003772
3773 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3774 s->req->cons->fd = -1; /* just to help with debugging */
3775 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003776 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777 s->req->cons->err_loc = NULL;
3778 s->req->cons->exp = TICK_ETERNITY;
3779 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003780 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3781 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL|BF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003782 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3784 s->txn.meth = 0;
3785 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003786 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003787 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 s->req->cons->flags |= SI_FL_INDEP_STR;
3789
Willy Tarreau96e31212011-05-30 18:10:30 +02003790 if (s->fe->options2 & PR_O2_NODELAY) {
3791 s->req->flags |= BF_NEVER_WAIT;
3792 s->rep->flags |= BF_NEVER_WAIT;
3793 }
3794
Willy Tarreau610ecce2010-01-04 21:15:02 +01003795 /* if the request buffer is not empty, it means we're
3796 * about to process another request, so send pending
3797 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003798 * Just don't do this if the buffer is close to be full,
3799 * because the request will wait for it to flush a little
3800 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003802 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003803 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003804 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003805 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003806 s->rep->flags |= BF_EXPECT_MORE;
3807 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003808
3809 /* we're removing the analysers, we MUST re-enable events detection */
3810 buffer_auto_read(s->req);
3811 buffer_auto_close(s->req);
3812 buffer_auto_read(s->rep);
3813 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814
Willy Tarreau342b11c2010-11-24 16:22:09 +01003815 s->req->analysers = s->listener->analysers;
3816 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 s->rep->analysers = 0;
3818
3819 http_silent_debug(__LINE__, s);
3820}
3821
3822
3823/* This function updates the request state machine according to the response
3824 * state machine and buffer flags. It returns 1 if it changes anything (flag
3825 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3826 * it is only used to find when a request/response couple is complete. Both
3827 * this function and its equivalent should loop until both return zero. It
3828 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3829 */
3830int http_sync_req_state(struct session *s)
3831{
3832 struct buffer *buf = s->req;
3833 struct http_txn *txn = &s->txn;
3834 unsigned int old_flags = buf->flags;
3835 unsigned int old_state = txn->req.msg_state;
3836
3837 http_silent_debug(__LINE__, s);
3838 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3839 return 0;
3840
3841 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003842 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003843 * We can shut the read side unless we want to abort_on_close,
3844 * or we have a POST request. The issue with POST requests is
3845 * that some browsers still send a CRLF after the request, and
3846 * this CRLF must be read so that it does not remain in the kernel
3847 * buffers, otherwise a close could cause an RST on some systems
3848 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003849 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003850 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003851 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852
3853 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3854 goto wait_other_side;
3855
3856 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3857 /* The server has not finished to respond, so we
3858 * don't want to move in order not to upset it.
3859 */
3860 goto wait_other_side;
3861 }
3862
3863 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3864 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003865 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 txn->req.msg_state = HTTP_MSG_TUNNEL;
3867 goto wait_other_side;
3868 }
3869
3870 /* When we get here, it means that both the request and the
3871 * response have finished receiving. Depending on the connection
3872 * mode, we'll have to wait for the last bytes to leave in either
3873 * direction, and sometimes for a close to be effective.
3874 */
3875
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003876 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3877 /* Server-close mode : queue a connection close to the server */
3878 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003880 }
3881 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3882 /* Option forceclose is set, or either side wants to close,
3883 * let's enforce it now that we're not expecting any new
3884 * data to come. The caller knows the session is complete
3885 * once both states are CLOSED.
3886 */
3887 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 buffer_shutr_now(buf);
3889 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003891 }
3892 else {
3893 /* The last possible modes are keep-alive and tunnel. Since tunnel
3894 * mode does not set the body analyser, we can't reach this place
3895 * in tunnel mode, so we're left with keep-alive only.
3896 * This mode is currently not implemented, we switch to tunnel mode.
3897 */
3898 buffer_auto_read(buf);
3899 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 }
3901
3902 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3903 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003904 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3905
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906 if (!(buf->flags & BF_OUT_EMPTY)) {
3907 txn->req.msg_state = HTTP_MSG_CLOSING;
3908 goto http_msg_closing;
3909 }
3910 else {
3911 txn->req.msg_state = HTTP_MSG_CLOSED;
3912 goto http_msg_closed;
3913 }
3914 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003915 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 }
3917
3918 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3919 http_msg_closing:
3920 /* nothing else to forward, just waiting for the output buffer
3921 * to be empty and for the shutw_now to take effect.
3922 */
3923 if (buf->flags & BF_OUT_EMPTY) {
3924 txn->req.msg_state = HTTP_MSG_CLOSED;
3925 goto http_msg_closed;
3926 }
3927 else if (buf->flags & BF_SHUTW) {
3928 txn->req.msg_state = HTTP_MSG_ERROR;
3929 goto wait_other_side;
3930 }
3931 }
3932
3933 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3934 http_msg_closed:
3935 goto wait_other_side;
3936 }
3937
3938 wait_other_side:
3939 http_silent_debug(__LINE__, s);
3940 return txn->req.msg_state != old_state || buf->flags != old_flags;
3941}
3942
3943
3944/* This function updates the response state machine according to the request
3945 * state machine and buffer flags. It returns 1 if it changes anything (flag
3946 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3947 * it is only used to find when a request/response couple is complete. Both
3948 * this function and its equivalent should loop until both return zero. It
3949 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3950 */
3951int http_sync_res_state(struct session *s)
3952{
3953 struct buffer *buf = s->rep;
3954 struct http_txn *txn = &s->txn;
3955 unsigned int old_flags = buf->flags;
3956 unsigned int old_state = txn->rsp.msg_state;
3957
3958 http_silent_debug(__LINE__, s);
3959 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3960 return 0;
3961
3962 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3963 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003964 * still monitor the server connection for a possible close
3965 * while the request is being uploaded, so we don't disable
3966 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003968 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969
3970 if (txn->req.msg_state == HTTP_MSG_ERROR)
3971 goto wait_other_side;
3972
3973 if (txn->req.msg_state < HTTP_MSG_DONE) {
3974 /* The client seems to still be sending data, probably
3975 * because we got an error response during an upload.
3976 * We have the choice of either breaking the connection
3977 * or letting it pass through. Let's do the later.
3978 */
3979 goto wait_other_side;
3980 }
3981
3982 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3983 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003984 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3986 goto wait_other_side;
3987 }
3988
3989 /* When we get here, it means that both the request and the
3990 * response have finished receiving. Depending on the connection
3991 * mode, we'll have to wait for the last bytes to leave in either
3992 * direction, and sometimes for a close to be effective.
3993 */
3994
3995 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3996 /* Server-close mode : shut read and wait for the request
3997 * side to close its output buffer. The caller will detect
3998 * when we're in DONE and the other is in CLOSED and will
3999 * catch that for the final cleanup.
4000 */
4001 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4002 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004004 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4005 /* Option forceclose is set, or either side wants to close,
4006 * let's enforce it now that we're not expecting any new
4007 * data to come. The caller knows the session is complete
4008 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004010 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4011 buffer_shutr_now(buf);
4012 buffer_shutw_now(buf);
4013 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014 }
4015 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004016 /* The last possible modes are keep-alive and tunnel. Since tunnel
4017 * mode does not set the body analyser, we can't reach this place
4018 * in tunnel mode, so we're left with keep-alive only.
4019 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004021 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 }
4024
4025 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4026 /* if we've just closed an output, let's switch */
4027 if (!(buf->flags & BF_OUT_EMPTY)) {
4028 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4029 goto http_msg_closing;
4030 }
4031 else {
4032 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4033 goto http_msg_closed;
4034 }
4035 }
4036 goto wait_other_side;
4037 }
4038
4039 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4040 http_msg_closing:
4041 /* nothing else to forward, just waiting for the output buffer
4042 * to be empty and for the shutw_now to take effect.
4043 */
4044 if (buf->flags & BF_OUT_EMPTY) {
4045 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4046 goto http_msg_closed;
4047 }
4048 else if (buf->flags & BF_SHUTW) {
4049 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004050 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004051 if (target_srv(&s->target))
4052 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004053 goto wait_other_side;
4054 }
4055 }
4056
4057 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4058 http_msg_closed:
4059 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004060 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 goto wait_other_side;
4064 }
4065
4066 wait_other_side:
4067 http_silent_debug(__LINE__, s);
4068 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4069}
4070
4071
4072/* Resync the request and response state machines. Return 1 if either state
4073 * changes.
4074 */
4075int http_resync_states(struct session *s)
4076{
4077 struct http_txn *txn = &s->txn;
4078 int old_req_state = txn->req.msg_state;
4079 int old_res_state = txn->rsp.msg_state;
4080
4081 http_silent_debug(__LINE__, s);
4082 http_sync_req_state(s);
4083 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004084 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004085 if (!http_sync_res_state(s))
4086 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004087 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 if (!http_sync_req_state(s))
4089 break;
4090 }
4091 http_silent_debug(__LINE__, s);
4092 /* OK, both state machines agree on a compatible state.
4093 * There are a few cases we're interested in :
4094 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4095 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4096 * directions, so let's simply disable both analysers.
4097 * - HTTP_MSG_CLOSED on the response only means we must abort the
4098 * request.
4099 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4100 * with server-close mode means we've completed one request and we
4101 * must re-initialize the server connection.
4102 */
4103
4104 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4105 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4106 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4107 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4108 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004109 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004110 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004112 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004115 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4116 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004117 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004118 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004119 s->rep->analysers = 0;
4120 buffer_auto_close(s->rep);
4121 buffer_auto_read(s->rep);
4122 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 buffer_abort(s->req);
4124 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004125 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004126 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004127 }
4128 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4129 txn->rsp.msg_state == HTTP_MSG_DONE &&
4130 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4131 /* server-close: terminate this server connection and
4132 * reinitialize a fresh-new transaction.
4133 */
4134 http_end_txn_clean_session(s);
4135 }
4136
4137 http_silent_debug(__LINE__, s);
4138 return txn->req.msg_state != old_req_state ||
4139 txn->rsp.msg_state != old_res_state;
4140}
4141
Willy Tarreaud98cf932009-12-27 22:54:55 +01004142/* This function is an analyser which forwards request body (including chunk
4143 * sizes if any). It is called as soon as we must forward, even if we forward
4144 * zero byte. The only situation where it must not be called is when we're in
4145 * tunnel mode and we want to forward till the close. It's used both to forward
4146 * remaining data and to resync after end of body. It expects the msg_state to
4147 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4148 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004149 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004150 * bytes of pending data + the headers if not already done (between som and sov).
4151 * It eventually adjusts som to match sov after the data in between have been sent.
4152 */
4153int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4154{
4155 struct http_txn *txn = &s->txn;
4156 struct http_msg *msg = &s->txn.req;
4157
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004158 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4159 return 0;
4160
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004161 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004162 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004163 /* Output closed while we were sending data. We must abort and
4164 * wake the other side up.
4165 */
4166 msg->msg_state = HTTP_MSG_ERROR;
4167 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004168 return 1;
4169 }
4170
Willy Tarreau4fe41902010-06-07 22:27:41 +02004171 /* in most states, we should abort in case of early close */
4172 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004173
4174 /* Note that we don't have to send 100-continue back because we don't
4175 * need the data to complete our job, and it's up to the server to
4176 * decide whether to return 100, 417 or anything else in return of
4177 * an "Expect: 100-continue" header.
4178 */
4179
4180 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004181 /* we have msg->sov which points to the first byte of message body.
4182 * msg->som still points to the beginning of the message. We must
4183 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004184 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004185 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004186
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004187 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4189 else {
4190 msg->msg_state = HTTP_MSG_DATA;
4191 }
4192 }
4193
Willy Tarreaud98cf932009-12-27 22:54:55 +01004194 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004195 int bytes;
4196
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004198 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004199 bytes = msg->sov - msg->som;
4200 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004201 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004202 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4203 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004204 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004205 msg->chunk_len += (unsigned int)bytes;
4206 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004207 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004208
Willy Tarreaucaabe412010-01-03 23:08:28 +01004209 if (msg->msg_state == HTTP_MSG_DATA) {
4210 /* must still forward */
4211 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004212 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004213
4214 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004215 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004216 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004218 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004219 }
4220 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004221 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004222 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004223 * TRAILERS state.
4224 */
4225 int ret = http_parse_chunk_size(req, msg);
4226
4227 if (!ret)
4228 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004229 else if (ret < 0) {
4230 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004231 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004232 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4238 /* we want the CRLF after the data */
4239 int ret;
4240
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 ret = http_skip_chunk_crlf(req, msg);
4242
4243 if (ret == 0)
4244 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004245 else if (ret < 0) {
4246 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004247 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004248 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004250 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004251 /* we're in MSG_CHUNK_SIZE now */
4252 }
4253 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4254 int ret = http_forward_trailers(req, msg);
4255
4256 if (ret == 0)
4257 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004258 else if (ret < 0) {
4259 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004260 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004261 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004262 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004263 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004264 /* we're in HTTP_MSG_DONE now */
4265 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004267 int old_state = msg->msg_state;
4268
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004270 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004271 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4272 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4273 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 if (http_resync_states(s)) {
4275 /* some state changes occurred, maybe the analyser
4276 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004277 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004278 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4279 if (req->flags & BF_SHUTW) {
4280 /* request errors are most likely due to
4281 * the server aborting the transfer.
4282 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004283 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004284 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004285 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004286 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004290 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004291
4292 /* If "option abortonclose" is set on the backend, we
4293 * want to monitor the client's connection and forward
4294 * any shutdown notification to the server, which will
4295 * decide whether to close or to go on processing the
4296 * request.
4297 */
4298 if (s->be->options & PR_O_ABRT_CLOSE) {
4299 buffer_auto_read(req);
4300 buffer_auto_close(req);
4301 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004302 else if (s->txn.meth == HTTP_METH_POST) {
4303 /* POST requests may require to read extra CRLF
4304 * sent by broken browsers and which could cause
4305 * an RST to be sent upon close on some systems
4306 * (eg: Linux).
4307 */
4308 buffer_auto_read(req);
4309 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004310
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312 }
4313 }
4314
Willy Tarreaud98cf932009-12-27 22:54:55 +01004315 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004316 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004317 if (req->flags & BF_SHUTR) {
4318 if (!(s->flags & SN_ERR_MASK))
4319 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004320 if (!(s->flags & SN_FINST_MASK)) {
4321 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4322 s->flags |= SN_FINST_H;
4323 else
4324 s->flags |= SN_FINST_D;
4325 }
4326
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004327 s->fe->fe_counters.cli_aborts++;
4328 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004329 if (target_srv(&s->target))
4330 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004331
4332 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004333 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004334
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004335 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004336 if (req->flags & BF_SHUTW)
4337 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004339 /* When TE: chunked is used, we need to get there again to parse remaining
4340 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4341 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004342 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004343 buffer_dont_close(req);
4344
Willy Tarreau5c620922011-05-11 19:56:11 +02004345 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004346 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4347 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004348 * modes are already handled by the stream sock layer. We must not do
4349 * this in content-length mode because it could present the MSG_MORE
4350 * flag with the last block of forwarded data, which would cause an
4351 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004352 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004353 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004354 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004355
Willy Tarreau610ecce2010-01-04 21:15:02 +01004356 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004357 return 0;
4358
Willy Tarreaud98cf932009-12-27 22:54:55 +01004359 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004360 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004361 if (s->listener->counters)
4362 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004363 return_bad_req_stats_ok:
4364 txn->req.msg_state = HTTP_MSG_ERROR;
4365 if (txn->status) {
4366 /* Note: we don't send any error if some data were already sent */
4367 stream_int_retnclose(req->prod, NULL);
4368 } else {
4369 txn->status = 400;
4370 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4371 }
4372 req->analysers = 0;
4373 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004374
4375 if (!(s->flags & SN_ERR_MASK))
4376 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004377 if (!(s->flags & SN_FINST_MASK)) {
4378 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4379 s->flags |= SN_FINST_H;
4380 else
4381 s->flags |= SN_FINST_D;
4382 }
4383 return 0;
4384
4385 aborted_xfer:
4386 txn->req.msg_state = HTTP_MSG_ERROR;
4387 if (txn->status) {
4388 /* Note: we don't send any error if some data were already sent */
4389 stream_int_retnclose(req->prod, NULL);
4390 } else {
4391 txn->status = 502;
4392 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4393 }
4394 req->analysers = 0;
4395 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4396
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004397 s->fe->fe_counters.srv_aborts++;
4398 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004399 if (target_srv(&s->target))
4400 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004401
4402 if (!(s->flags & SN_ERR_MASK))
4403 s->flags |= SN_ERR_SRVCL;
4404 if (!(s->flags & SN_FINST_MASK)) {
4405 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4406 s->flags |= SN_FINST_H;
4407 else
4408 s->flags |= SN_FINST_D;
4409 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 return 0;
4411}
4412
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004413/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4414 * processing can continue on next analysers, or zero if it either needs more
4415 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4416 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4417 * when it has nothing left to do, and may remove any analyser when it wants to
4418 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004419 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004420int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004421{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004422 struct http_txn *txn = &s->txn;
4423 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004424 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004425 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004426 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004427 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004428
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004429 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004430 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004431 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004432 rep,
4433 rep->rex, rep->wex,
4434 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004435 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004436 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004437
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004438 /*
4439 * Now parse the partial (or complete) lines.
4440 * We will check the response syntax, and also join multi-line
4441 * headers. An index of all the lines will be elaborated while
4442 * parsing.
4443 *
4444 * For the parsing, we use a 28 states FSM.
4445 *
4446 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004447 * rep->data + msg->som = beginning of response
4448 * rep->data + msg->eoh = end of processed headers / start of current one
4449 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004450 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004451 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004452 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453 */
4454
Willy Tarreau83e3af02009-12-28 17:39:57 +01004455 /* There's a protected area at the end of the buffer for rewriting
4456 * purposes. We don't want to start to parse the request if the
4457 * protected area is affected, because we may have to move processed
4458 * data later, which is much more complicated.
4459 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004460 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004461 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004462 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004463 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004464 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004465 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004466 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4467 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004468 buffer_dont_close(rep);
4469 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4470 return 0;
4471 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004472 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004473 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004474 }
4475
Willy Tarreaua458b672012-03-05 11:17:50 +01004476 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004477 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004478 }
4479
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 /* 1: we might have to print this header in debug mode */
4481 if (unlikely((global.mode & MODE_DEBUG) &&
4482 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004483 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004484 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004485 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004486
Willy Tarreauea1175a2012-03-05 15:52:30 +01004487 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004488 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004490
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 sol += hdr_idx_first_pos(&txn->hdr_idx);
4492 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004493
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 while (cur_idx) {
4495 eol = sol + txn->hdr_idx.v[cur_idx].len;
4496 debug_hdr("srvhdr", s, sol, eol);
4497 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4498 cur_idx = txn->hdr_idx.v[cur_idx].next;
4499 }
4500 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 /*
4503 * Now we quickly check if we have found a full valid response.
4504 * If not so, we check the FD and buffer states before leaving.
4505 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004506 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 * responses are checked first.
4508 *
4509 * Depending on whether the client is still there or not, we
4510 * may send an error response back or not. Note that normally
4511 * we should only check for HTTP status there, and check I/O
4512 * errors somewhere else.
4513 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004514
Willy Tarreau655dce92009-11-08 13:10:58 +01004515 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 /* Invalid response */
4517 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4518 /* we detected a parsing error. We want to archive this response
4519 * in the dedicated proxy area for later troubleshooting.
4520 */
4521 hdr_response_bad:
4522 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004523 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004525 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004526 if (target_srv(&s->target)) {
4527 target_srv(&s->target)->counters.failed_resp++;
4528 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004529 }
Willy Tarreau64648412010-03-05 10:41:54 +01004530 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004531 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004532 rep->analysers = 0;
4533 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004534 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004535 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004536 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4537
4538 if (!(s->flags & SN_ERR_MASK))
4539 s->flags |= SN_ERR_PRXCOND;
4540 if (!(s->flags & SN_FINST_MASK))
4541 s->flags |= SN_FINST_H;
4542
4543 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004544 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004545
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 /* too large response does not fit in buffer. */
4547 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004548 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004549 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 goto hdr_response_bad;
4551 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004552
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004553 /* read error */
4554 else if (rep->flags & BF_READ_ERROR) {
4555 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004556 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004557
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004558 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004559 if (target_srv(&s->target)) {
4560 target_srv(&s->target)->counters.failed_resp++;
4561 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004562 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004563
Willy Tarreau90deb182010-01-07 00:20:41 +01004564 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 rep->analysers = 0;
4566 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004567 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004568 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004570
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004571 if (!(s->flags & SN_ERR_MASK))
4572 s->flags |= SN_ERR_SRVCL;
4573 if (!(s->flags & SN_FINST_MASK))
4574 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004575 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004577
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004578 /* read timeout : return a 504 to the client. */
4579 else if (rep->flags & BF_READ_TIMEOUT) {
4580 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004581 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004582
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004583 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004584 if (target_srv(&s->target)) {
4585 target_srv(&s->target)->counters.failed_resp++;
4586 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004587 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004588
Willy Tarreau90deb182010-01-07 00:20:41 +01004589 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004590 rep->analysers = 0;
4591 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004592 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004593 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004594 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004595
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004596 if (!(s->flags & SN_ERR_MASK))
4597 s->flags |= SN_ERR_SRVTO;
4598 if (!(s->flags & SN_FINST_MASK))
4599 s->flags |= SN_FINST_H;
4600 return 0;
4601 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004602
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004603 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004605 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004606 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004607
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004608 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004609 if (target_srv(&s->target)) {
4610 target_srv(&s->target)->counters.failed_resp++;
4611 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004612 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004613
Willy Tarreau90deb182010-01-07 00:20:41 +01004614 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004615 rep->analysers = 0;
4616 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004617 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004618 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004620
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621 if (!(s->flags & SN_ERR_MASK))
4622 s->flags |= SN_ERR_SRVCL;
4623 if (!(s->flags & SN_FINST_MASK))
4624 s->flags |= SN_FINST_H;
4625 return 0;
4626 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004627
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628 /* write error to client (we don't send any message then) */
4629 else if (rep->flags & BF_WRITE_ERROR) {
4630 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004631 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004632
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004633 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004635 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004636
4637 if (!(s->flags & SN_ERR_MASK))
4638 s->flags |= SN_ERR_CLICL;
4639 if (!(s->flags & SN_FINST_MASK))
4640 s->flags |= SN_FINST_H;
4641
4642 /* process_session() will take care of the error */
4643 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004644 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004645
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 buffer_dont_close(rep);
4647 return 0;
4648 }
4649
4650 /* More interesting part now : we know that we have a complete
4651 * response which at least looks like HTTP. We have an indicator
4652 * of each header's length, so we can parse them quickly.
4653 */
4654
4655 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004656 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658 /*
4659 * 1: get the status code
4660 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004661 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 if (n < 1 || n > 5)
4663 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004664 /* when the client triggers a 4xx from the server, it's most often due
4665 * to a missing object or permission. These events should be tracked
4666 * because if they happen often, it may indicate a brute force or a
4667 * vulnerability scan.
4668 */
4669 if (n == 4)
4670 session_inc_http_err_ctr(s);
4671
Willy Tarreau827aee92011-03-10 16:55:02 +01004672 if (target_srv(&s->target))
4673 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004674
Willy Tarreau5b154472009-12-21 20:11:07 +01004675 /* check if the response is HTTP/1.1 or above */
4676 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004677 ((rep->p[msg->sol + 5] > '1') ||
4678 ((rep->p[msg->sol + 5] == '1') &&
4679 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004680 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004681
4682 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004683 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004684
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004685 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004686 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004687
Willy Tarreau3a215be2012-03-09 21:39:51 +01004688 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004689
Willy Tarreau39650402010-03-15 19:44:39 +01004690 /* Adjust server's health based on status code. Note: status codes 501
4691 * and 505 are triggered on demand by client request, so we must not
4692 * count them as server failures.
4693 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004694 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004695 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004696 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004697 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004698 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004699 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004700
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004701 /*
4702 * 2: check for cacheability.
4703 */
4704
4705 switch (txn->status) {
4706 case 200:
4707 case 203:
4708 case 206:
4709 case 300:
4710 case 301:
4711 case 410:
4712 /* RFC2616 @13.4:
4713 * "A response received with a status code of
4714 * 200, 203, 206, 300, 301 or 410 MAY be stored
4715 * by a cache (...) unless a cache-control
4716 * directive prohibits caching."
4717 *
4718 * RFC2616 @9.5: POST method :
4719 * "Responses to this method are not cacheable,
4720 * unless the response includes appropriate
4721 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004722 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723 if (likely(txn->meth != HTTP_METH_POST) &&
4724 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4725 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4726 break;
4727 default:
4728 break;
4729 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 /*
4732 * 3: we may need to capture headers
4733 */
4734 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004735 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004736 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004737 txn->rsp.cap, s->fe->rsp_cap);
4738
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004739 /* 4: determine the transfer-length.
4740 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4741 * the presence of a message-body in a RESPONSE and its transfer length
4742 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004743 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004744 * All responses to the HEAD request method MUST NOT include a
4745 * message-body, even though the presence of entity-header fields
4746 * might lead one to believe they do. All 1xx (informational), 204
4747 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4748 * message-body. All other responses do include a message-body,
4749 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004750 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004751 * 1. Any response which "MUST NOT" include a message-body (such as the
4752 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4753 * always terminated by the first empty line after the header fields,
4754 * regardless of the entity-header fields present in the message.
4755 *
4756 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4757 * the "chunked" transfer-coding (Section 6.2) is used, the
4758 * transfer-length is defined by the use of this transfer-coding.
4759 * If a Transfer-Encoding header field is present and the "chunked"
4760 * transfer-coding is not present, the transfer-length is defined by
4761 * the sender closing the connection.
4762 *
4763 * 3. If a Content-Length header field is present, its decimal value in
4764 * OCTETs represents both the entity-length and the transfer-length.
4765 * If a message is received with both a Transfer-Encoding header
4766 * field and a Content-Length header field, the latter MUST be ignored.
4767 *
4768 * 4. If the message uses the media type "multipart/byteranges", and
4769 * the transfer-length is not otherwise specified, then this self-
4770 * delimiting media type defines the transfer-length. This media
4771 * type MUST NOT be used unless the sender knows that the recipient
4772 * can parse it; the presence in a request of a Range header with
4773 * multiple byte-range specifiers from a 1.1 client implies that the
4774 * client can parse multipart/byteranges responses.
4775 *
4776 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004777 */
4778
4779 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004780 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004781 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004782 * FIXME: should we parse anyway and return an error on chunked encoding ?
4783 */
4784 if (txn->meth == HTTP_METH_HEAD ||
4785 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004786 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004787 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004788 goto skip_content_length;
4789 }
4790
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004791 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004792 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004793 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004794 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004795 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004796 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4797 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004798 /* bad transfer-encoding (chunked followed by something else) */
4799 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004800 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004801 break;
4802 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004803 }
4804
4805 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4806 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004807 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004808 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004809 signed long long cl;
4810
Willy Tarreauad14f752011-09-02 20:33:27 +02004811 if (!ctx.vlen) {
4812 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004813 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004814 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004815
Willy Tarreauad14f752011-09-02 20:33:27 +02004816 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4817 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004818 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004819 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004820
Willy Tarreauad14f752011-09-02 20:33:27 +02004821 if (cl < 0) {
4822 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004824 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004825
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004826 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004827 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004829 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004830
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004831 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004832 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833 }
4834
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004835 /* FIXME: we should also implement the multipart/byterange method.
4836 * For now on, we resort to close mode in this case (unknown length).
4837 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004838skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004839
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004840 /* end of job, return OK */
4841 rep->analysers &= ~an_bit;
4842 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004843 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844 return 1;
4845}
4846
4847/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004848 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4849 * and updates t->rep->analysers. It might make sense to explode it into several
4850 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004851 */
4852int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4853{
4854 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 struct http_msg *msg = &txn->rsp;
4856 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004857 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004858
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004859 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004860 now_ms, __FUNCTION__,
4861 t,
4862 rep,
4863 rep->rex, rep->wex,
4864 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004865 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866 rep->analysers);
4867
Willy Tarreau655dce92009-11-08 13:10:58 +01004868 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 return 0;
4870
4871 rep->analysers &= ~an_bit;
4872 rep->analyse_exp = TICK_ETERNITY;
4873
Willy Tarreau5b154472009-12-21 20:11:07 +01004874 /* Now we have to check if we need to modify the Connection header.
4875 * This is more difficult on the response than it is on the request,
4876 * because we can have two different HTTP versions and we don't know
4877 * how the client will interprete a response. For instance, let's say
4878 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4879 * HTTP/1.1 response without any header. Maybe it will bound itself to
4880 * HTTP/1.0 because it only knows about it, and will consider the lack
4881 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4882 * the lack of header as a keep-alive. Thus we will use two flags
4883 * indicating how a request MAY be understood by the client. In case
4884 * of multiple possibilities, we'll fix the header to be explicit. If
4885 * ambiguous cases such as both close and keepalive are seen, then we
4886 * will fall back to explicit close. Note that we won't take risks with
4887 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004888 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004889 */
4890
Willy Tarreaudc008c52010-02-01 16:20:08 +01004891 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4892 txn->status == 101)) {
4893 /* Either we've established an explicit tunnel, or we're
4894 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004895 * to understand the next protocols. We have to switch to tunnel
4896 * mode, so that we transfer the request and responses then let
4897 * this protocol pass unmodified. When we later implement specific
4898 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004899 * header which contains information about that protocol for
4900 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004901 */
4902 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4903 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004904 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4905 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4906 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004907 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004908
Willy Tarreau60466522010-01-18 19:08:45 +01004909 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004910 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004911 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4912 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004913
Willy Tarreau60466522010-01-18 19:08:45 +01004914 /* now adjust header transformations depending on current state */
4915 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4916 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4917 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004918 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004919 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004920 }
Willy Tarreau60466522010-01-18 19:08:45 +01004921 else { /* SCL / KAL */
4922 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004923 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004924 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004925 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004926
Willy Tarreau60466522010-01-18 19:08:45 +01004927 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004928 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004929
Willy Tarreau60466522010-01-18 19:08:45 +01004930 /* Some keep-alive responses are converted to Server-close if
4931 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004932 */
Willy Tarreau60466522010-01-18 19:08:45 +01004933 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4934 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004935 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004936 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004937 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004938 }
4939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004941 /*
4942 * 3: we will have to evaluate the filters.
4943 * As opposed to version 1.2, now they will be evaluated in the
4944 * filters order and not in the header order. This means that
4945 * each filter has to be validated among all headers.
4946 *
4947 * Filters are tried with ->be first, then with ->fe if it is
4948 * different from ->be.
4949 */
4950
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004951 cur_proxy = t->be;
4952 while (1) {
4953 struct proxy *rule_set = cur_proxy;
4954
4955 /* try headers filters */
4956 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004957 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004958 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004959 if (target_srv(&t->target)) {
4960 target_srv(&t->target)->counters.failed_resp++;
4961 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004962 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004963 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004964 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004965 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004966 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004967 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004968 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004969 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004970 if (!(t->flags & SN_ERR_MASK))
4971 t->flags |= SN_ERR_PRXCOND;
4972 if (!(t->flags & SN_FINST_MASK))
4973 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004974 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004975 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004976 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 /* has the response been denied ? */
4979 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004980 if (target_srv(&t->target))
4981 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004982
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004983 t->be->be_counters.denied_resp++;
4984 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004985 if (t->listener->counters)
4986 t->listener->counters->denied_resp++;
4987
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004989 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004990
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004992 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004993 if (txn->status < 200)
4994 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004995 if (wl->cond) {
4996 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4997 ret = acl_pass(ret);
4998 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4999 ret = !ret;
5000 if (!ret)
5001 continue;
5002 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005003 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005004 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005005 }
5006
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005007 /* check whether we're already working on the frontend */
5008 if (cur_proxy == t->fe)
5009 break;
5010 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005011 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005012
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005013 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005014 * We may be facing a 100-continue response, in which case this
5015 * is not the right response, and we're waiting for the next one.
5016 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005017 * next one.
5018 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005019 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005020 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005021 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005022 msg->msg_state = HTTP_MSG_RPBEFORE;
5023 txn->status = 0;
5024 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5025 return 1;
5026 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005027 else if (unlikely(txn->status < 200))
5028 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005029
5030 /* we don't have any 1xx status code now */
5031
5032 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005033 * 4: check for server cookie.
5034 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005035 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5036 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005037 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005038
Willy Tarreaubaaee002006-06-26 02:48:02 +02005039
Willy Tarreaua15645d2007-03-18 16:22:39 +01005040 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005043 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005044 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005045
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005046 /*
5047 * 6: add server cookie in the response if needed
5048 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005049 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005050 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005051 (!(t->flags & SN_DIRECT) ||
5052 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5053 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5054 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5055 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005056 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5057 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005058 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005059 /* the server is known, it's not the one the client requested, or the
5060 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 * insert a set-cookie here, except if we want to insert only on POST
5062 * requests and this one isn't. Note that servers which don't have cookies
5063 * (eg: some backup servers) will return a full cookie removal request.
5064 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005065 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005066 len = sprintf(trash,
5067 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5068 t->be->cookie_name);
5069 }
5070 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005071 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005072
5073 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5074 /* emit last_date, which is mandatory */
5075 trash[len++] = COOKIE_DELIM_DATE;
5076 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5077 if (t->be->cookie_maxlife) {
5078 /* emit first_date, which is either the original one or
5079 * the current date.
5080 */
5081 trash[len++] = COOKIE_DELIM_DATE;
5082 s30tob64(txn->cookie_first_date ?
5083 txn->cookie_first_date >> 2 :
5084 (date.tv_sec+3) >> 2, trash + len);
5085 len += 5;
5086 }
5087 }
5088 len += sprintf(trash + len, "; path=/");
5089 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005090
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 if (t->be->cookie_domain)
5092 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005093
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005094 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005095 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005096
Willy Tarreauf1348312010-10-07 15:54:11 +02005097 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005098 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005099 /* the server did not change, only the date was updated */
5100 txn->flags |= TX_SCK_UPDATED;
5101 else
5102 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005103
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005104 /* Here, we will tell an eventual cache on the client side that we don't
5105 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5106 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5107 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5108 */
5109 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005110
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005111 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5112
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005113 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005114 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005116 }
5117 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005118
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 /*
5120 * 7: check if result will be cacheable with a cookie.
5121 * We'll block the response if security checks have caught
5122 * nasty things such as a cacheable cookie.
5123 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005124 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5125 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005126 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005127
5128 /* we're in presence of a cacheable response containing
5129 * a set-cookie header. We'll block it as requested by
5130 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005131 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005132 if (target_srv(&t->target))
5133 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005134
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005135 t->be->be_counters.denied_resp++;
5136 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005137 if (t->listener->counters)
5138 t->listener->counters->denied_resp++;
5139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005141 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005142 send_log(t->be, LOG_ALERT,
5143 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005144 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145 goto return_srv_prx_502;
5146 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005147
5148 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005149 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005150 */
Willy Tarreau60466522010-01-18 19:08:45 +01005151 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5152 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5153 unsigned int want_flags = 0;
5154
5155 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5156 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5157 /* we want a keep-alive response here. Keep-alive header
5158 * required if either side is not 1.1.
5159 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005160 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005161 want_flags |= TX_CON_KAL_SET;
5162 }
5163 else {
5164 /* we want a close response here. Close header required if
5165 * the server is 1.1, regardless of the client.
5166 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005167 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005168 want_flags |= TX_CON_CLO_SET;
5169 }
5170
5171 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005172 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005173 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005174
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005175 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005176 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005177 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005178 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005179
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005180 /*************************************************************
5181 * OK, that's finished for the headers. We have done what we *
5182 * could. Let's switch to the DATA state. *
5183 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005186
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005187 /* if the user wants to log as soon as possible, without counting
5188 * bytes from the server, then this is the right moment. We have
5189 * to temporarily assign bytes_out to log what we currently have.
5190 */
5191 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5192 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5193 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005194 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 t->logs.bytes_out = 0;
5196 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005197
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005198 /* Note: we must not try to cheat by jumping directly to DATA,
5199 * otherwise we would not let the client side wake up.
5200 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005201
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005202 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005204 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005206
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207/* This function is an analyser which forwards response body (including chunk
5208 * sizes if any). It is called as soon as we must forward, even if we forward
5209 * zero byte. The only situation where it must not be called is when we're in
5210 * tunnel mode and we want to forward till the close. It's used both to forward
5211 * remaining data and to resync after end of body. It expects the msg_state to
5212 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5213 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005214 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005215 * bytes of pending data + the headers if not already done (between som and sov).
5216 * It eventually adjusts som to match sov after the data in between have been sent.
5217 */
5218int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5219{
5220 struct http_txn *txn = &s->txn;
5221 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005222 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005223
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005224 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5225 return 0;
5226
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005227 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005228 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005229 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005230 /* Output closed while we were sending data. We must abort and
5231 * wake the other side up.
5232 */
5233 msg->msg_state = HTTP_MSG_ERROR;
5234 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005235 return 1;
5236 }
5237
Willy Tarreau4fe41902010-06-07 22:27:41 +02005238 /* in most states, we should abort in case of early close */
5239 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005240
Willy Tarreaud98cf932009-12-27 22:54:55 +01005241 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005242 /* we have msg->sov which points to the first byte of message body.
5243 * msg->som still points to the beginning of the message. We must
5244 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005245 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005246 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005247
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005248 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5250 else {
5251 msg->msg_state = HTTP_MSG_DATA;
5252 }
5253 }
5254
Willy Tarreaud98cf932009-12-27 22:54:55 +01005255 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005256 int bytes;
5257
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005259 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005260 bytes = msg->sov - msg->som;
5261 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005262 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005263 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5264 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005265 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005266 msg->chunk_len += (unsigned int)bytes;
5267 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005268 }
5269
Willy Tarreaucaabe412010-01-03 23:08:28 +01005270 if (msg->msg_state == HTTP_MSG_DATA) {
5271 /* must still forward */
5272 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005273 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005274
5275 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005276 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005277 msg->msg_state = HTTP_MSG_DATA_CRLF;
5278 else
5279 msg->msg_state = HTTP_MSG_DONE;
5280 }
5281 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005282 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005283 * set ->sov and ->next to point to the body and switch to DATA or
5284 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005285 */
5286 int ret = http_parse_chunk_size(res, msg);
5287
5288 if (!ret)
5289 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005290 else if (ret < 0) {
5291 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005292 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005294 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005296 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005297 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5298 /* we want the CRLF after the data */
5299 int ret;
5300
Willy Tarreaud98cf932009-12-27 22:54:55 +01005301 ret = http_skip_chunk_crlf(res, msg);
5302
5303 if (!ret)
5304 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005305 else if (ret < 0) {
5306 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005307 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005308 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005309 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005310 /* we're in MSG_CHUNK_SIZE now */
5311 }
5312 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5313 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005314
Willy Tarreaud98cf932009-12-27 22:54:55 +01005315 if (ret == 0)
5316 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005317 else if (ret < 0) {
5318 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005319 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005320 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005321 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005322 /* we're in HTTP_MSG_DONE now */
5323 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005325 int old_state = msg->msg_state;
5326
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005328 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005329 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5330 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5331 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 if (http_resync_states(s)) {
5333 http_silent_debug(__LINE__, s);
5334 /* some state changes occurred, maybe the analyser
5335 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005336 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005337 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5338 if (res->flags & BF_SHUTW) {
5339 /* response errors are most likely due to
5340 * the client aborting the transfer.
5341 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005342 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005343 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005344 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005345 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005347 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005349 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005351 }
5352 }
5353
Willy Tarreaud98cf932009-12-27 22:54:55 +01005354 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005355 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005356 if (res->flags & BF_SHUTR) {
5357 if (!(s->flags & SN_ERR_MASK))
5358 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005359 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005360 if (target_srv(&s->target))
5361 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005362 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005363 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005364
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005365 if (res->flags & BF_SHUTW)
5366 goto aborted_xfer;
5367
Willy Tarreau40dba092010-03-04 18:14:51 +01005368 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 if (!s->req->analysers)
5370 goto return_bad_res;
5371
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005372 /* forward any pending data */
5373 bytes = msg->sov - msg->som;
5374 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005375 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005376 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5377 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005378 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005379 msg->chunk_len += (unsigned int)bytes;
5380 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381 }
5382
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005383 /* When TE: chunked is used, we need to get there again to parse remaining
5384 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5385 * Similarly, with keep-alive on the client side, we don't want to forward a
5386 * close.
5387 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005388 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005389 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5390 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5391 buffer_dont_close(res);
5392
Willy Tarreau5c620922011-05-11 19:56:11 +02005393 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005394 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5395 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005396 * modes are already handled by the stream sock layer. We must not do
5397 * this in content-length mode because it could present the MSG_MORE
5398 * flag with the last block of forwarded data, which would cause an
5399 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005400 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005401 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005402 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005403
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 return 0;
5407
Willy Tarreau40dba092010-03-04 18:14:51 +01005408 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005409 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005410 if (target_srv(&s->target))
5411 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005412
5413 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005415 /* don't send any error message as we're in the body */
5416 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005418 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005419 if (target_srv(&s->target))
5420 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421
5422 if (!(s->flags & SN_ERR_MASK))
5423 s->flags |= SN_ERR_PRXCOND;
5424 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005425 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005426 return 0;
5427
5428 aborted_xfer:
5429 txn->rsp.msg_state = HTTP_MSG_ERROR;
5430 /* don't send any error message as we're in the body */
5431 stream_int_retnclose(res->cons, NULL);
5432 res->analysers = 0;
5433 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5434
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005435 s->fe->fe_counters.cli_aborts++;
5436 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005437 if (target_srv(&s->target))
5438 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005439
5440 if (!(s->flags & SN_ERR_MASK))
5441 s->flags |= SN_ERR_CLICL;
5442 if (!(s->flags & SN_FINST_MASK))
5443 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 return 0;
5445}
5446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005447/* Iterate the same filter through all request headers.
5448 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005449 * Since it can manage the switch to another backend, it updates the per-proxy
5450 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005452int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005453{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005454 char term;
5455 char *cur_ptr, *cur_end, *cur_next;
5456 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005457 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005458 struct hdr_idx_elem *cur_hdr;
5459 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005461 last_hdr = 0;
5462
Willy Tarreau3a215be2012-03-09 21:39:51 +01005463 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464 old_idx = 0;
5465
5466 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005467 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005469 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005470 (exp->action == ACT_ALLOW ||
5471 exp->action == ACT_DENY ||
5472 exp->action == ACT_TARPIT))
5473 return 0;
5474
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005475 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 if (!cur_idx)
5477 break;
5478
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 cur_ptr = cur_next;
5481 cur_end = cur_ptr + cur_hdr->len;
5482 cur_next = cur_end + cur_hdr->cr + 1;
5483
5484 /* Now we have one header between cur_ptr and cur_end,
5485 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005486 */
5487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488 /* The annoying part is that pattern matching needs
5489 * that we modify the contents to null-terminate all
5490 * strings before testing them.
5491 */
5492
5493 term = *cur_end;
5494 *cur_end = '\0';
5495
5496 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5497 switch (exp->action) {
5498 case ACT_SETBE:
5499 /* It is not possible to jump a second time.
5500 * FIXME: should we return an HTTP/500 here so that
5501 * the admin knows there's a problem ?
5502 */
5503 if (t->be != t->fe)
5504 break;
5505
5506 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005507 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005508 last_hdr = 1;
5509 break;
5510
5511 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005512 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005513 last_hdr = 1;
5514 break;
5515
5516 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005517 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005518 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005519
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005520 t->fe->fe_counters.denied_req++;
5521 if (t->fe != t->be)
5522 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005523 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005524 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005526 break;
5527
5528 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005529 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005531
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005532 t->fe->fe_counters.denied_req++;
5533 if (t->fe != t->be)
5534 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005536 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 break;
5539
5540 case ACT_REPLACE:
5541 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5542 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5543 /* FIXME: if the user adds a newline in the replacement, the
5544 * index will not be recalculated for now, and the new line
5545 * will not be counted as a new header.
5546 */
5547
5548 cur_end += delta;
5549 cur_next += delta;
5550 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005551 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005552 break;
5553
5554 case ACT_REMOVE:
5555 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5556 cur_next += delta;
5557
Willy Tarreaufa355d42009-11-29 18:12:29 +01005558 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005559 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5560 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 cur_hdr->len = 0;
5562 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005563 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 break;
5565
5566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 if (cur_end)
5569 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 /* keep the link from this header to next one in case of later
5572 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 old_idx = cur_idx;
5575 }
5576 return 0;
5577}
5578
5579
5580/* Apply the filter to the request line.
5581 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5582 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005583 * Since it can manage the switch to another backend, it updates the per-proxy
5584 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005585 */
5586int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5587{
5588 char term;
5589 char *cur_ptr, *cur_end;
5590 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005591 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593
Willy Tarreau58f10d72006-12-04 02:26:12 +01005594
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005597 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 (exp->action == ACT_ALLOW ||
5599 exp->action == ACT_DENY ||
5600 exp->action == ACT_TARPIT))
5601 return 0;
5602 else if (exp->action == ACT_REMOVE)
5603 return 0;
5604
5605 done = 0;
5606
Willy Tarreau3a215be2012-03-09 21:39:51 +01005607 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005608 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609
5610 /* Now we have the request line between cur_ptr and cur_end */
5611
5612 /* The annoying part is that pattern matching needs
5613 * that we modify the contents to null-terminate all
5614 * strings before testing them.
5615 */
5616
5617 term = *cur_end;
5618 *cur_end = '\0';
5619
5620 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5621 switch (exp->action) {
5622 case ACT_SETBE:
5623 /* It is not possible to jump a second time.
5624 * FIXME: should we return an HTTP/500 here so that
5625 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005626 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 if (t->be != t->fe)
5628 break;
5629
5630 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005631 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005632 done = 1;
5633 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005636 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637 done = 1;
5638 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005641 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005642
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005643 t->fe->fe_counters.denied_req++;
5644 if (t->fe != t->be)
5645 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005647 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 done = 1;
5650 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005653 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005654
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005655 t->fe->fe_counters.denied_req++;
5656 if (t->fe != t->be)
5657 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005659 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 done = 1;
5662 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 case ACT_REPLACE:
5665 *cur_end = term; /* restore the string terminator */
5666 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5667 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5668 /* FIXME: if the user adds a newline in the replacement, the
5669 * index will not be recalculated for now, and the new line
5670 * will not be counted as a new header.
5671 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005672
Willy Tarreaufa355d42009-11-29 18:12:29 +01005673 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005675 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 HTTP_MSG_RQMETH,
5677 cur_ptr, cur_end + 1,
5678 NULL, NULL);
5679 if (unlikely(!cur_end))
5680 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 /* we have a full request and we know that we have either a CR
5683 * or an LF at <ptr>.
5684 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005685 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5686 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687 /* there is no point trying this regex on headers */
5688 return 1;
5689 }
5690 }
5691 *cur_end = term; /* restore the string terminator */
5692 return done;
5693}
Willy Tarreau97de6242006-12-27 17:18:38 +01005694
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695
Willy Tarreau58f10d72006-12-04 02:26:12 +01005696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005698 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005700 * unparsable request. Since it can manage the switch to another backend, it
5701 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005703int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005704{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005705 struct http_txn *txn = &s->txn;
5706 struct hdr_exp *exp;
5707
5708 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 /*
5712 * The interleaving of transformations and verdicts
5713 * makes it difficult to decide to continue or stop
5714 * the evaluation.
5715 */
5716
Willy Tarreau6c123b12010-01-28 20:22:06 +01005717 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5718 break;
5719
Willy Tarreau3d300592007-03-18 18:34:41 +01005720 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005722 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005724
5725 /* if this filter had a condition, evaluate it now and skip to
5726 * next filter if the condition does not match.
5727 */
5728 if (exp->cond) {
5729 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5730 ret = acl_pass(ret);
5731 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5732 ret = !ret;
5733
5734 if (!ret)
5735 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 }
5737
5738 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005739 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 if (unlikely(ret < 0))
5741 return -1;
5742
5743 if (likely(ret == 0)) {
5744 /* The filter did not match the request, it can be
5745 * iterated through all headers.
5746 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005747 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005749 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751}
5752
5753
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754
Willy Tarreau58f10d72006-12-04 02:26:12 +01005755/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005756 * Try to retrieve the server associated to the appsession.
5757 * If the server is found, it's assigned to the session.
5758 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005759void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005760 struct http_txn *txn = &t->txn;
5761 appsess *asession = NULL;
5762 char *sessid_temp = NULL;
5763
Cyril Bontéb21570a2009-11-29 20:04:48 +01005764 if (len > t->be->appsession_len) {
5765 len = t->be->appsession_len;
5766 }
5767
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005768 if (t->be->options2 & PR_O2_AS_REQL) {
5769 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005770 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005771 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005772 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005773 }
5774
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005775 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005776 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5777 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5778 return;
5779 }
5780
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005781 memcpy(txn->sessid, buf, len);
5782 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005783 }
5784
5785 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5786 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5787 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5788 return;
5789 }
5790
Cyril Bontéb21570a2009-11-29 20:04:48 +01005791 memcpy(sessid_temp, buf, len);
5792 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005793
5794 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5795 /* free previously allocated memory */
5796 pool_free2(apools.sessid, sessid_temp);
5797
5798 if (asession != NULL) {
5799 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5800 if (!(t->be->options2 & PR_O2_AS_REQL))
5801 asession->request_count++;
5802
5803 if (asession->serverid != NULL) {
5804 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005805
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005806 while (srv) {
5807 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005808 if ((srv->state & SRV_RUNNING) ||
5809 (t->be->options & PR_O_PERSIST) ||
5810 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005811 /* we found the server and it's usable */
5812 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005813 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005814 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005815 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005816
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005817 break;
5818 } else {
5819 txn->flags &= ~TX_CK_MASK;
5820 txn->flags |= TX_CK_DOWN;
5821 }
5822 }
5823 srv = srv->next;
5824 }
5825 }
5826 }
5827}
5828
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005829/* Find the end of a cookie value contained between <s> and <e>. It works the
5830 * same way as with headers above except that the semi-colon also ends a token.
5831 * See RFC2965 for more information. Note that it requires a valid header to
5832 * return a valid result.
5833 */
5834char *find_cookie_value_end(char *s, const char *e)
5835{
5836 int quoted, qdpair;
5837
5838 quoted = qdpair = 0;
5839 for (; s < e; s++) {
5840 if (qdpair) qdpair = 0;
5841 else if (quoted) {
5842 if (*s == '\\') qdpair = 1;
5843 else if (*s == '"') quoted = 0;
5844 }
5845 else if (*s == '"') quoted = 1;
5846 else if (*s == ',' || *s == ';') return s;
5847 }
5848 return s;
5849}
5850
5851/* Delete a value in a header between delimiters <from> and <next> in buffer
5852 * <buf>. The number of characters displaced is returned, and the pointer to
5853 * the first delimiter is updated if required. The function tries as much as
5854 * possible to respect the following principles :
5855 * - replace <from> delimiter by the <next> one unless <from> points to a
5856 * colon, in which case <next> is simply removed
5857 * - set exactly one space character after the new first delimiter, unless
5858 * there are not enough characters in the block being moved to do so.
5859 * - remove unneeded spaces before the previous delimiter and after the new
5860 * one.
5861 *
5862 * It is the caller's responsibility to ensure that :
5863 * - <from> points to a valid delimiter or the colon ;
5864 * - <next> points to a valid delimiter or the final CR/LF ;
5865 * - there are non-space chars before <from> ;
5866 * - there is a CR/LF at or after <next>.
5867 */
5868int del_hdr_value(struct buffer *buf, char **from, char *next)
5869{
5870 char *prev = *from;
5871
5872 if (*prev == ':') {
5873 /* We're removing the first value, preserve the colon and add a
5874 * space if possible.
5875 */
5876 if (!http_is_crlf[(unsigned char)*next])
5877 next++;
5878 prev++;
5879 if (prev < next)
5880 *prev++ = ' ';
5881
5882 while (http_is_spht[(unsigned char)*next])
5883 next++;
5884 } else {
5885 /* Remove useless spaces before the old delimiter. */
5886 while (http_is_spht[(unsigned char)*(prev-1)])
5887 prev--;
5888 *from = prev;
5889
5890 /* copy the delimiter and if possible a space if we're
5891 * not at the end of the line.
5892 */
5893 if (!http_is_crlf[(unsigned char)*next]) {
5894 *prev++ = *next++;
5895 if (prev + 1 < next)
5896 *prev++ = ' ';
5897 while (http_is_spht[(unsigned char)*next])
5898 next++;
5899 }
5900 }
5901 return buffer_replace2(buf, prev, next, NULL, 0);
5902}
5903
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005904/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005905 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005906 * desirable to call it only when needed. This code is quite complex because
5907 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5908 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 */
5910void manage_client_side_cookies(struct session *t, struct buffer *req)
5911{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005912 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005915 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5916 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005919 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005920 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005923 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005924 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005927 hdr_beg = hdr_next;
5928 hdr_end = hdr_beg + cur_hdr->len;
5929 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 /* We have one full header between hdr_beg and hdr_end, and the
5932 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 * "Cookie:" headers.
5934 */
5935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005936 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005937 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938 old_idx = cur_idx;
5939 continue;
5940 }
5941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 del_from = NULL; /* nothing to be deleted */
5943 preserve_hdr = 0; /* assume we may kill the whole header */
5944
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 /* Now look for cookies. Conforming to RFC2109, we have to support
5946 * attributes whose name begin with a '$', and associate them with
5947 * the right cookie, if we want to delete this cookie.
5948 * So there are 3 cases for each cookie read :
5949 * 1) it's a special attribute, beginning with a '$' : ignore it.
5950 * 2) it's a server id cookie that we *MAY* want to delete : save
5951 * some pointers on it (last semi-colon, beginning of cookie...)
5952 * 3) it's an application cookie : we *MAY* have to delete a previous
5953 * "special" cookie.
5954 * At the end of loop, if a "special" cookie remains, we may have to
5955 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005956 * *MUST* delete it.
5957 *
5958 * Note: RFC2965 is unclear about the processing of spaces around
5959 * the equal sign in the ATTR=VALUE form. A careful inspection of
5960 * the RFC explicitly allows spaces before it, and not within the
5961 * tokens (attrs or values). An inspection of RFC2109 allows that
5962 * too but section 10.1.3 lets one think that spaces may be allowed
5963 * after the equal sign too, resulting in some (rare) buggy
5964 * implementations trying to do that. So let's do what servers do.
5965 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5966 * allowed quoted strings in values, with any possible character
5967 * after a backslash, including control chars and delimitors, which
5968 * causes parsing to become ambiguous. Browsers also allow spaces
5969 * within values even without quotes.
5970 *
5971 * We have to keep multiple pointers in order to support cookie
5972 * removal at the beginning, middle or end of header without
5973 * corrupting the header. All of these headers are valid :
5974 *
5975 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5976 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5977 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5978 * | | | | | | | | |
5979 * | | | | | | | | hdr_end <--+
5980 * | | | | | | | +--> next
5981 * | | | | | | +----> val_end
5982 * | | | | | +-----------> val_beg
5983 * | | | | +--------------> equal
5984 * | | | +----------------> att_end
5985 * | | +---------------------> att_beg
5986 * | +--------------------------> prev
5987 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988 */
5989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5991 /* Iterate through all cookies on this line */
5992
5993 /* find att_beg */
5994 att_beg = prev + 1;
5995 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5996 att_beg++;
5997
5998 /* find att_end : this is the first character after the last non
5999 * space before the equal. It may be equal to hdr_end.
6000 */
6001 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006002
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006003 while (equal < hdr_end) {
6004 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006005 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006006 if (http_is_spht[(unsigned char)*equal++])
6007 continue;
6008 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009 }
6010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006011 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6012 * is between <att_beg> and <equal>, both may be identical.
6013 */
6014
6015 /* look for end of cookie if there is an equal sign */
6016 if (equal < hdr_end && *equal == '=') {
6017 /* look for the beginning of the value */
6018 val_beg = equal + 1;
6019 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6020 val_beg++;
6021
6022 /* find the end of the value, respecting quotes */
6023 next = find_cookie_value_end(val_beg, hdr_end);
6024
6025 /* make val_end point to the first white space or delimitor after the value */
6026 val_end = next;
6027 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6028 val_end--;
6029 } else {
6030 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006031 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 /* We have nothing to do with attributes beginning with '$'. However,
6034 * they will automatically be removed if a header before them is removed,
6035 * since they're supposed to be linked together.
6036 */
6037 if (*att_beg == '$')
6038 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006040 /* Ignore cookies with no equal sign */
6041 if (equal == next) {
6042 /* This is not our cookie, so we must preserve it. But if we already
6043 * scheduled another cookie for removal, we cannot remove the
6044 * complete header, but we can remove the previous block itself.
6045 */
6046 preserve_hdr = 1;
6047 if (del_from != NULL) {
6048 int delta = del_hdr_value(req, &del_from, prev);
6049 val_end += delta;
6050 next += delta;
6051 hdr_end += delta;
6052 hdr_next += delta;
6053 cur_hdr->len += delta;
6054 http_msg_move_end(&txn->req, delta);
6055 prev = del_from;
6056 del_from = NULL;
6057 }
6058 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006061 /* if there are spaces around the equal sign, we need to
6062 * strip them otherwise we'll get trouble for cookie captures,
6063 * or even for rewrites. Since this happens extremely rarely,
6064 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006065 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6067 int stripped_before = 0;
6068 int stripped_after = 0;
6069
6070 if (att_end != equal) {
6071 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6072 equal += stripped_before;
6073 val_beg += stripped_before;
6074 }
6075
6076 if (val_beg > equal + 1) {
6077 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6078 val_beg += stripped_after;
6079 stripped_before += stripped_after;
6080 }
6081
6082 val_end += stripped_before;
6083 next += stripped_before;
6084 hdr_end += stripped_before;
6085 hdr_next += stripped_before;
6086 cur_hdr->len += stripped_before;
6087 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006091 /* First, let's see if we want to capture this cookie. We check
6092 * that we don't already have a client side cookie, because we
6093 * can only capture one. Also as an optimisation, we ignore
6094 * cookies shorter than the declared name.
6095 */
6096 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6097 (val_end - att_beg >= t->fe->capture_namelen) &&
6098 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6099 int log_len = val_end - att_beg;
6100
6101 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6102 Alert("HTTP logging : out of memory.\n");
6103 } else {
6104 if (log_len > t->fe->capture_len)
6105 log_len = t->fe->capture_len;
6106 memcpy(txn->cli_cookie, att_beg, log_len);
6107 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreaubca99692010-10-06 19:25:55 +02006111 /* Persistence cookies in passive, rewrite or insert mode have the
6112 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006114 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006116 * For cookies in prefix mode, the form is :
6117 *
6118 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 */
6120 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6121 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6122 struct server *srv = t->be->srv;
6123 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6126 * have the server ID between val_beg and delim, and the original cookie between
6127 * delim+1 and val_end. Otherwise, delim==val_end :
6128 *
6129 * Cookie: NAME=SRV; # in all but prefix modes
6130 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6131 * | || || | |+-> next
6132 * | || || | +--> val_end
6133 * | || || +---------> delim
6134 * | || |+------------> val_beg
6135 * | || +-------------> att_end = equal
6136 * | |+-----------------> att_beg
6137 * | +------------------> prev
6138 * +-------------------------> hdr_beg
6139 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006141 if (t->be->options & PR_O_COOK_PFX) {
6142 for (delim = val_beg; delim < val_end; delim++)
6143 if (*delim == COOKIE_DELIM)
6144 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006145 } else {
6146 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006148 /* Now check if the cookie contains a date field, which would
6149 * appear after a vertical bar ('|') just after the server name
6150 * and before the delimiter.
6151 */
6152 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6153 if (vbar1) {
6154 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006155 * right is the last seen date. It is a base64 encoded
6156 * 30-bit value representing the UNIX date since the
6157 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006158 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006159 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006161 if (val_end - vbar1 >= 5) {
6162 val = b64tos30(vbar1);
6163 if (val > 0)
6164 txn->cookie_last_date = val << 2;
6165 }
6166 /* look for a second vertical bar */
6167 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6168 if (vbar1 && (val_end - vbar1 > 5)) {
6169 val = b64tos30(vbar1 + 1);
6170 if (val > 0)
6171 txn->cookie_first_date = val << 2;
6172 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006173 }
6174 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006175
Willy Tarreauf64d1412010-10-07 20:06:11 +02006176 /* if the cookie has an expiration date and the proxy wants to check
6177 * it, then we do that now. We first check if the cookie is too old,
6178 * then only if it has expired. We detect strict overflow because the
6179 * time resolution here is not great (4 seconds). Cookies with dates
6180 * in the future are ignored if their offset is beyond one day. This
6181 * allows an admin to fix timezone issues without expiring everyone
6182 * and at the same time avoids keeping unwanted side effects for too
6183 * long.
6184 */
6185 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006186 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6187 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006188 txn->flags &= ~TX_CK_MASK;
6189 txn->flags |= TX_CK_OLD;
6190 delim = val_beg; // let's pretend we have not found the cookie
6191 txn->cookie_first_date = 0;
6192 txn->cookie_last_date = 0;
6193 }
6194 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006195 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6196 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006197 txn->flags &= ~TX_CK_MASK;
6198 txn->flags |= TX_CK_EXPIRED;
6199 delim = val_beg; // let's pretend we have not found the cookie
6200 txn->cookie_first_date = 0;
6201 txn->cookie_last_date = 0;
6202 }
6203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 /* Here, we'll look for the first running server which supports the cookie.
6205 * This allows to share a same cookie between several servers, for example
6206 * to dedicate backup servers to specific servers only.
6207 * However, to prevent clients from sticking to cookie-less backup server
6208 * when they have incidentely learned an empty cookie, we simply ignore
6209 * empty cookies and mark them as invalid.
6210 * The same behaviour is applied when persistence must be ignored.
6211 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006212 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 while (srv) {
6216 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6217 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6218 if ((srv->state & SRV_RUNNING) ||
6219 (t->be->options & PR_O_PERSIST) ||
6220 (t->flags & SN_FORCE_PRST)) {
6221 /* we found the server and we can use it */
6222 txn->flags &= ~TX_CK_MASK;
6223 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6224 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006225 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 break;
6227 } else {
6228 /* we found a server, but it's down,
6229 * mark it as such and go on in case
6230 * another one is available.
6231 */
6232 txn->flags &= ~TX_CK_MASK;
6233 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 srv = srv->next;
6237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238
Willy Tarreauf64d1412010-10-07 20:06:11 +02006239 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006240 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006242 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6243 txn->flags |= TX_CK_UNUSED;
6244 else
6245 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 /* depending on the cookie mode, we may have to either :
6249 * - delete the complete cookie if we're in insert+indirect mode, so that
6250 * the server never sees it ;
6251 * - remove the server id from the cookie value, and tag the cookie as an
6252 * application cookie so that it does not get accidentely removed later,
6253 * if we're in cookie prefix mode
6254 */
6255 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6256 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6259 val_end += delta;
6260 next += delta;
6261 hdr_end += delta;
6262 hdr_next += delta;
6263 cur_hdr->len += delta;
6264 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 del_from = NULL;
6267 preserve_hdr = 1; /* we want to keep this cookie */
6268 }
6269 else if (del_from == NULL &&
6270 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6271 del_from = prev;
6272 }
6273 } else {
6274 /* This is not our cookie, so we must preserve it. But if we already
6275 * scheduled another cookie for removal, we cannot remove the
6276 * complete header, but we can remove the previous block itself.
6277 */
6278 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006280 if (del_from != NULL) {
6281 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006282 if (att_beg >= del_from)
6283 att_beg += delta;
6284 if (att_end >= del_from)
6285 att_end += delta;
6286 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 val_end += delta;
6288 next += delta;
6289 hdr_end += delta;
6290 hdr_next += delta;
6291 cur_hdr->len += delta;
6292 http_msg_move_end(&txn->req, delta);
6293 prev = del_from;
6294 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006298 /* Look for the appsession cookie unless persistence must be ignored */
6299 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6300 int cmp_len, value_len;
6301 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 if (t->be->options2 & PR_O2_AS_PFX) {
6304 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6305 value_begin = att_beg + t->be->appsession_name_len;
6306 value_len = val_end - att_beg - t->be->appsession_name_len;
6307 } else {
6308 cmp_len = att_end - att_beg;
6309 value_begin = val_beg;
6310 value_len = val_end - val_beg;
6311 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006312
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 /* let's see if the cookie is our appcookie */
6314 if (cmp_len == t->be->appsession_name_len &&
6315 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6316 manage_client_side_appsession(t, value_begin, value_len);
6317 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006318 }
6319
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 /* continue with next cookie on this header line */
6321 att_beg = next;
6322 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006323
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 /* There are no more cookies on this line.
6325 * We may still have one (or several) marked for deletion at the
6326 * end of the line. We must do this now in two ways :
6327 * - if some cookies must be preserved, we only delete from the
6328 * mark to the end of line ;
6329 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 if (preserve_hdr) {
6334 delta = del_hdr_value(req, &del_from, hdr_end);
6335 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 cur_hdr->len += delta;
6337 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006338 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006339
6340 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006341 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6342 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006344 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006347 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 }
6349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353}
6354
6355
Willy Tarreaua15645d2007-03-18 16:22:39 +01006356/* Iterate the same filter through all response headers contained in <rtr>.
6357 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6358 */
6359int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6360{
6361 char term;
6362 char *cur_ptr, *cur_end, *cur_next;
6363 int cur_idx, old_idx, last_hdr;
6364 struct http_txn *txn = &t->txn;
6365 struct hdr_idx_elem *cur_hdr;
6366 int len, delta;
6367
6368 last_hdr = 0;
6369
Willy Tarreau3a215be2012-03-09 21:39:51 +01006370 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006371 old_idx = 0;
6372
6373 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006374 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006375 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006376 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006377 (exp->action == ACT_ALLOW ||
6378 exp->action == ACT_DENY))
6379 return 0;
6380
6381 cur_idx = txn->hdr_idx.v[old_idx].next;
6382 if (!cur_idx)
6383 break;
6384
6385 cur_hdr = &txn->hdr_idx.v[cur_idx];
6386 cur_ptr = cur_next;
6387 cur_end = cur_ptr + cur_hdr->len;
6388 cur_next = cur_end + cur_hdr->cr + 1;
6389
6390 /* Now we have one header between cur_ptr and cur_end,
6391 * and the next header starts at cur_next.
6392 */
6393
6394 /* The annoying part is that pattern matching needs
6395 * that we modify the contents to null-terminate all
6396 * strings before testing them.
6397 */
6398
6399 term = *cur_end;
6400 *cur_end = '\0';
6401
6402 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6403 switch (exp->action) {
6404 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006405 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006406 last_hdr = 1;
6407 break;
6408
6409 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006410 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006411 last_hdr = 1;
6412 break;
6413
6414 case ACT_REPLACE:
6415 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6416 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6417 /* FIXME: if the user adds a newline in the replacement, the
6418 * index will not be recalculated for now, and the new line
6419 * will not be counted as a new header.
6420 */
6421
6422 cur_end += delta;
6423 cur_next += delta;
6424 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006425 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006426 break;
6427
6428 case ACT_REMOVE:
6429 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6430 cur_next += delta;
6431
Willy Tarreaufa355d42009-11-29 18:12:29 +01006432 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006433 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6434 txn->hdr_idx.used--;
6435 cur_hdr->len = 0;
6436 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006437 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438 break;
6439
6440 }
6441 }
6442 if (cur_end)
6443 *cur_end = term; /* restore the string terminator */
6444
6445 /* keep the link from this header to next one in case of later
6446 * removal of next header.
6447 */
6448 old_idx = cur_idx;
6449 }
6450 return 0;
6451}
6452
6453
6454/* Apply the filter to the status line in the response buffer <rtr>.
6455 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6456 * or -1 if a replacement resulted in an invalid status line.
6457 */
6458int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6459{
6460 char term;
6461 char *cur_ptr, *cur_end;
6462 int done;
6463 struct http_txn *txn = &t->txn;
6464 int len, delta;
6465
6466
Willy Tarreau3d300592007-03-18 18:34:41 +01006467 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006469 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006470 (exp->action == ACT_ALLOW ||
6471 exp->action == ACT_DENY))
6472 return 0;
6473 else if (exp->action == ACT_REMOVE)
6474 return 0;
6475
6476 done = 0;
6477
Willy Tarreau3a215be2012-03-09 21:39:51 +01006478 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006479 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480
6481 /* Now we have the status line between cur_ptr and cur_end */
6482
6483 /* The annoying part is that pattern matching needs
6484 * that we modify the contents to null-terminate all
6485 * strings before testing them.
6486 */
6487
6488 term = *cur_end;
6489 *cur_end = '\0';
6490
6491 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6492 switch (exp->action) {
6493 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006494 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006495 done = 1;
6496 break;
6497
6498 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006499 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500 done = 1;
6501 break;
6502
6503 case ACT_REPLACE:
6504 *cur_end = term; /* restore the string terminator */
6505 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6506 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6507 /* FIXME: if the user adds a newline in the replacement, the
6508 * index will not be recalculated for now, and the new line
6509 * will not be counted as a new header.
6510 */
6511
Willy Tarreaufa355d42009-11-29 18:12:29 +01006512 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006513 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006514 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006515 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 cur_ptr, cur_end + 1,
6517 NULL, NULL);
6518 if (unlikely(!cur_end))
6519 return -1;
6520
6521 /* we have a full respnse and we know that we have either a CR
6522 * or an LF at <ptr>.
6523 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006524 txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006525 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 /* there is no point trying this regex on headers */
6527 return 1;
6528 }
6529 }
6530 *cur_end = term; /* restore the string terminator */
6531 return done;
6532}
6533
6534
6535
6536/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006537 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6539 * unparsable response.
6540 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006541int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006543 struct http_txn *txn = &s->txn;
6544 struct hdr_exp *exp;
6545
6546 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547 int ret;
6548
6549 /*
6550 * The interleaving of transformations and verdicts
6551 * makes it difficult to decide to continue or stop
6552 * the evaluation.
6553 */
6554
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006555 if (txn->flags & TX_SVDENY)
6556 break;
6557
Willy Tarreau3d300592007-03-18 18:34:41 +01006558 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6560 exp->action == ACT_PASS)) {
6561 exp = exp->next;
6562 continue;
6563 }
6564
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006565 /* if this filter had a condition, evaluate it now and skip to
6566 * next filter if the condition does not match.
6567 */
6568 if (exp->cond) {
6569 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6570 ret = acl_pass(ret);
6571 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6572 ret = !ret;
6573 if (!ret)
6574 continue;
6575 }
6576
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006578 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579 if (unlikely(ret < 0))
6580 return -1;
6581
6582 if (likely(ret == 0)) {
6583 /* The filter did not match the response, it can be
6584 * iterated through all headers.
6585 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006586 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006588 }
6589 return 0;
6590}
6591
6592
Willy Tarreaua15645d2007-03-18 16:22:39 +01006593/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006594 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006595 * desirable to call it only when needed. This function is also used when we
6596 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006598void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006599{
6600 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006601 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006602 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006604 char *hdr_beg, *hdr_end, *hdr_next;
6605 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 /* Iterate through the headers.
6608 * we start with the start line.
6609 */
6610 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006611 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006612
6613 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6614 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006615 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
6617 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006618 hdr_beg = hdr_next;
6619 hdr_end = hdr_beg + cur_hdr->len;
6620 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006621
Willy Tarreau24581ba2010-08-31 22:39:35 +02006622 /* We have one full header between hdr_beg and hdr_end, and the
6623 * next header starts at hdr_next. We're only interested in
6624 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625 */
6626
Willy Tarreau24581ba2010-08-31 22:39:35 +02006627 is_cookie2 = 0;
6628 prev = hdr_beg + 10;
6629 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006630 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006631 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6632 if (!val) {
6633 old_idx = cur_idx;
6634 continue;
6635 }
6636 is_cookie2 = 1;
6637 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006638 }
6639
Willy Tarreau24581ba2010-08-31 22:39:35 +02006640 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6641 * <prev> points to the colon.
6642 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006643 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644
Willy Tarreau24581ba2010-08-31 22:39:35 +02006645 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6646 * check-cache is enabled) and we are not interested in checking
6647 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006648 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006649 if (t->be->cookie_name == NULL &&
6650 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006651 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652 return;
6653
Willy Tarreau24581ba2010-08-31 22:39:35 +02006654 /* OK so now we know we have to process this response cookie.
6655 * The format of the Set-Cookie header is slightly different
6656 * from the format of the Cookie header in that it does not
6657 * support the comma as a cookie delimiter (thus the header
6658 * cannot be folded) because the Expires attribute described in
6659 * the original Netscape's spec may contain an unquoted date
6660 * with a comma inside. We have to live with this because
6661 * many browsers don't support Max-Age and some browsers don't
6662 * support quoted strings. However the Set-Cookie2 header is
6663 * clean.
6664 *
6665 * We have to keep multiple pointers in order to support cookie
6666 * removal at the beginning, middle or end of header without
6667 * corrupting the header (in case of set-cookie2). A special
6668 * pointer, <scav> points to the beginning of the set-cookie-av
6669 * fields after the first semi-colon. The <next> pointer points
6670 * either to the end of line (set-cookie) or next unquoted comma
6671 * (set-cookie2). All of these headers are valid :
6672 *
6673 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6674 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6675 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6676 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6677 * | | | | | | | | | |
6678 * | | | | | | | | +-> next hdr_end <--+
6679 * | | | | | | | +------------> scav
6680 * | | | | | | +--------------> val_end
6681 * | | | | | +--------------------> val_beg
6682 * | | | | +----------------------> equal
6683 * | | | +------------------------> att_end
6684 * | | +----------------------------> att_beg
6685 * | +------------------------------> prev
6686 * +-----------------------------------------> hdr_beg
6687 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 for (; prev < hdr_end; prev = next) {
6690 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691
Willy Tarreau24581ba2010-08-31 22:39:35 +02006692 /* find att_beg */
6693 att_beg = prev + 1;
6694 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6695 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006696
Willy Tarreau24581ba2010-08-31 22:39:35 +02006697 /* find att_end : this is the first character after the last non
6698 * space before the equal. It may be equal to hdr_end.
6699 */
6700 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701
Willy Tarreau24581ba2010-08-31 22:39:35 +02006702 while (equal < hdr_end) {
6703 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6704 break;
6705 if (http_is_spht[(unsigned char)*equal++])
6706 continue;
6707 att_end = equal;
6708 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709
Willy Tarreau24581ba2010-08-31 22:39:35 +02006710 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6711 * is between <att_beg> and <equal>, both may be identical.
6712 */
6713
6714 /* look for end of cookie if there is an equal sign */
6715 if (equal < hdr_end && *equal == '=') {
6716 /* look for the beginning of the value */
6717 val_beg = equal + 1;
6718 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6719 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720
Willy Tarreau24581ba2010-08-31 22:39:35 +02006721 /* find the end of the value, respecting quotes */
6722 next = find_cookie_value_end(val_beg, hdr_end);
6723
6724 /* make val_end point to the first white space or delimitor after the value */
6725 val_end = next;
6726 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6727 val_end--;
6728 } else {
6729 /* <equal> points to next comma, semi-colon or EOL */
6730 val_beg = val_end = next = equal;
6731 }
6732
6733 if (next < hdr_end) {
6734 /* Set-Cookie2 supports multiple cookies, and <next> points to
6735 * a colon or semi-colon before the end. So skip all attr-value
6736 * pairs and look for the next comma. For Set-Cookie, since
6737 * commas are permitted in values, skip to the end.
6738 */
6739 if (is_cookie2)
6740 next = find_hdr_value_end(next, hdr_end);
6741 else
6742 next = hdr_end;
6743 }
6744
6745 /* Now everything is as on the diagram above */
6746
6747 /* Ignore cookies with no equal sign */
6748 if (equal == val_end)
6749 continue;
6750
6751 /* If there are spaces around the equal sign, we need to
6752 * strip them otherwise we'll get trouble for cookie captures,
6753 * or even for rewrites. Since this happens extremely rarely,
6754 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006756 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6757 int stripped_before = 0;
6758 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759
Willy Tarreau24581ba2010-08-31 22:39:35 +02006760 if (att_end != equal) {
6761 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6762 equal += stripped_before;
6763 val_beg += stripped_before;
6764 }
6765
6766 if (val_beg > equal + 1) {
6767 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6768 val_beg += stripped_after;
6769 stripped_before += stripped_after;
6770 }
6771
6772 val_end += stripped_before;
6773 next += stripped_before;
6774 hdr_end += stripped_before;
6775 hdr_next += stripped_before;
6776 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006777 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006778 }
6779
6780 /* First, let's see if we want to capture this cookie. We check
6781 * that we don't already have a server side cookie, because we
6782 * can only capture one. Also as an optimisation, we ignore
6783 * cookies shorter than the declared name.
6784 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006785 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006786 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006787 (val_end - att_beg >= t->fe->capture_namelen) &&
6788 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6789 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006790 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 Alert("HTTP logging : out of memory.\n");
6792 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006793 else {
6794 if (log_len > t->fe->capture_len)
6795 log_len = t->fe->capture_len;
6796 memcpy(txn->srv_cookie, att_beg, log_len);
6797 txn->srv_cookie[log_len] = 0;
6798 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 }
6800
Willy Tarreau827aee92011-03-10 16:55:02 +01006801 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006802 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006803 if (!(t->flags & SN_IGNORE_PRST) &&
6804 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6805 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006806 /* assume passive cookie by default */
6807 txn->flags &= ~TX_SCK_MASK;
6808 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
6810 /* If the cookie is in insert mode on a known server, we'll delete
6811 * this occurrence because we'll insert another one later.
6812 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006813 * a direct access.
6814 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006815 if (t->be->options2 & PR_O2_COOK_PSV) {
6816 /* The "preserve" flag was set, we don't want to touch the
6817 * server's cookie.
6818 */
6819 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006820 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006821 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006822 /* this cookie must be deleted */
6823 if (*prev == ':' && next == hdr_end) {
6824 /* whole header */
6825 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6826 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6827 txn->hdr_idx.used--;
6828 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006829 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006830 hdr_next += delta;
6831 http_msg_move_end(&txn->rsp, delta);
6832 /* note: while both invalid now, <next> and <hdr_end>
6833 * are still equal, so the for() will stop as expected.
6834 */
6835 } else {
6836 /* just remove the value */
6837 int delta = del_hdr_value(res, &prev, next);
6838 next = prev;
6839 hdr_end += delta;
6840 hdr_next += delta;
6841 cur_hdr->len += delta;
6842 http_msg_move_end(&txn->rsp, delta);
6843 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006844 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006845 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006848 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 * with this server since we know it.
6851 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006852 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 next += delta;
6854 hdr_end += delta;
6855 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006857 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858
Willy Tarreauf1348312010-10-07 15:54:11 +02006859 txn->flags &= ~TX_SCK_MASK;
6860 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006862 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006864 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006866 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006867 next += delta;
6868 hdr_end += delta;
6869 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006871 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872
Willy Tarreau827aee92011-03-10 16:55:02 +01006873 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006874 txn->flags &= ~TX_SCK_MASK;
6875 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 }
6877 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006878 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6879 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006880 int cmp_len, value_len;
6881 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882
Cyril Bontéb21570a2009-11-29 20:04:48 +01006883 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6885 value_begin = att_beg + t->be->appsession_name_len;
6886 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006887 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006888 cmp_len = att_end - att_beg;
6889 value_begin = val_beg;
6890 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006891 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006892
Cyril Bonté17530c32010-04-06 21:11:10 +02006893 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6895 /* free a possibly previously allocated memory */
6896 pool_free2(apools.sessid, txn->sessid);
6897
Cyril Bontéb21570a2009-11-29 20:04:48 +01006898 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006899 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006900 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6901 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6902 return;
6903 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006904 memcpy(txn->sessid, value_begin, value_len);
6905 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 }
6908 /* that's done for this cookie, check the next one on the same
6909 * line when next != hdr_end (only if is_cookie2).
6910 */
6911 }
6912 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006915
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006916 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006917 appsess *asession = NULL;
6918 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006919 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006920 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006921 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006922 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6923 Alert("Not enough Memory process_srv():asession:calloc().\n");
6924 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6925 return;
6926 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006927 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6928
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006929 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6930 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6931 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006932 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 return;
6934 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006935 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006936 asession->sessid[t->be->appsession_len] = 0;
6937
Willy Tarreau827aee92011-03-10 16:55:02 +01006938 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006940 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006942 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 return;
6944 }
6945 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006946 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947
6948 asession->request_count = 0;
6949 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6950 }
6951
6952 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6953 asession->request_count++;
6954 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955}
6956
6957
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958/*
6959 * Check if response is cacheable or not. Updates t->flags.
6960 */
6961void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6962{
6963 struct http_txn *txn = &t->txn;
6964 char *p1, *p2;
6965
6966 char *cur_ptr, *cur_end, *cur_next;
6967 int cur_idx;
6968
Willy Tarreau5df51872007-11-25 16:20:08 +01006969 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970 return;
6971
6972 /* Iterate through the headers.
6973 * we start with the start line.
6974 */
6975 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006976 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977
6978 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6979 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006980 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981
6982 cur_hdr = &txn->hdr_idx.v[cur_idx];
6983 cur_ptr = cur_next;
6984 cur_end = cur_ptr + cur_hdr->len;
6985 cur_next = cur_end + cur_hdr->cr + 1;
6986
6987 /* We have one full header between cur_ptr and cur_end, and the
6988 * next header starts at cur_next. We're only interested in
6989 * "Cookie:" headers.
6990 */
6991
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006992 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6993 if (val) {
6994 if ((cur_end - (cur_ptr + val) >= 8) &&
6995 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6996 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6997 return;
6998 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999 }
7000
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007001 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7002 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 continue;
7004
7005 /* OK, right now we know we have a cache-control header at cur_ptr */
7006
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007007 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008
7009 if (p1 >= cur_end) /* no more info */
7010 continue;
7011
7012 /* p1 is at the beginning of the value */
7013 p2 = p1;
7014
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007015 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016 p2++;
7017
7018 /* we have a complete value between p1 and p2 */
7019 if (p2 < cur_end && *p2 == '=') {
7020 /* we have something of the form no-cache="set-cookie" */
7021 if ((cur_end - p1 >= 21) &&
7022 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7023 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007024 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 continue;
7026 }
7027
7028 /* OK, so we know that either p2 points to the end of string or to a comma */
7029 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7030 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7031 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7032 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007033 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 return;
7035 }
7036
7037 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007038 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007039 continue;
7040 }
7041 }
7042}
7043
7044
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045/*
7046 * Try to retrieve a known appsession in the URI, then the associated server.
7047 * If the server is found, it's assigned to the session.
7048 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007049void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007051 char *end_params, *first_param, *cur_param, *next_param;
7052 char separator;
7053 int value_len;
7054
7055 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007056
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007057 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007058 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007059 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007061
Cyril Bontéb21570a2009-11-29 20:04:48 +01007062 first_param = NULL;
7063 switch (mode) {
7064 case PR_O2_AS_M_PP:
7065 first_param = memchr(begin, ';', len);
7066 break;
7067 case PR_O2_AS_M_QS:
7068 first_param = memchr(begin, '?', len);
7069 break;
7070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071
Cyril Bontéb21570a2009-11-29 20:04:48 +01007072 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
Cyril Bontéb21570a2009-11-29 20:04:48 +01007076 switch (mode) {
7077 case PR_O2_AS_M_PP:
7078 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7079 end_params = (char *) begin + len;
7080 }
7081 separator = ';';
7082 break;
7083 case PR_O2_AS_M_QS:
7084 end_params = (char *) begin + len;
7085 separator = '&';
7086 break;
7087 default:
7088 /* unknown mode, shouldn't happen */
7089 return;
7090 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Cyril Bontéb21570a2009-11-29 20:04:48 +01007092 cur_param = next_param = end_params;
7093 while (cur_param > first_param) {
7094 cur_param--;
7095 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7096 /* let's see if this is the appsession parameter */
7097 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7098 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7099 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7100 /* Cool... it's the right one */
7101 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7102 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7103 if (value_len > 0) {
7104 manage_client_side_appsession(t, cur_param, value_len);
7105 }
7106 break;
7107 }
7108 next_param = cur_param;
7109 }
7110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007112 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007113 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115}
7116
Willy Tarreaub2513902006-12-17 14:52:38 +01007117/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007118 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007119 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007120 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007121 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007122 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007123 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007124 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007125 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007126int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007127{
7128 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007129 struct http_msg *msg = &txn->req;
7130 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7131 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007132
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007133 if (!uri_auth)
7134 return 0;
7135
Cyril Bonté70be45d2010-10-12 00:14:35 +02007136 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007137 return 0;
7138
Willy Tarreau295a8372011-03-10 11:25:07 +01007139 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007140 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007143 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007144 return 0;
7145
Willy Tarreau3a215be2012-03-09 21:39:51 +01007146 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007147 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007148 return 0;
7149
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007150 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007151 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007152 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007153 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007154 break;
7155 }
7156 h++;
7157 }
7158
7159 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007160 h = uri + uri_auth->uri_len;
7161 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007162 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007163 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007164 break;
7165 }
7166 h++;
7167 }
7168 }
7169
Willy Tarreau3a215be2012-03-09 21:39:51 +01007170 h = uri + uri_auth->uri_len;
7171 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007172 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007173 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007174 break;
7175 }
7176 h++;
7177 }
7178
Willy Tarreau3a215be2012-03-09 21:39:51 +01007179 h = uri + uri_auth->uri_len;
7180 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007181 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007182 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007183 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007184 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7185 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7186 si->applet.ctx.stats.st_code = i;
7187 break;
7188 }
7189 }
7190 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007191 break;
7192 }
7193 h++;
7194 }
7195
Willy Tarreau295a8372011-03-10 11:25:07 +01007196 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007197
Willy Tarreaub2513902006-12-17 14:52:38 +01007198 return 1;
7199}
7200
Willy Tarreau4076a152009-04-02 15:18:36 +02007201/*
7202 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007203 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007204 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7205 * or response messages cannot wrap, this function may also be used with chunks
7206 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007207 */
7208void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007209 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007210 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007211{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007212 struct buffer *buf = msg->buf;
7213
Willy Tarreauea1175a2012-03-05 15:52:30 +01007214 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7215 int len1 = buf->size - msg->som - (buf->p - buf->data);
7216 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7217 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007218 if (es->len > len1 && len1 < sizeof(es->buf))
7219 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7220 }
7221 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007222 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7223 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007224 }
7225
Willy Tarreau4076a152009-04-02 15:18:36 +02007226 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007227 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7228 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7229 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007230 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007231 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007232
Willy Tarreau4076a152009-04-02 15:18:36 +02007233 es->when = date; // user-visible date
7234 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007235 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007236 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007237 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007238 es->state = state;
7239 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007240 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007241}
Willy Tarreaub2513902006-12-17 14:52:38 +01007242
Willy Tarreau294c4732011-12-16 21:35:50 +01007243/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7244 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7245 * performed over the whole headers. Otherwise it must contain a valid header
7246 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7247 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7248 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7249 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7250 * -1.
7251 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007252 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007253unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7254 struct hdr_idx *idx, int occ,
7255 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007256{
Willy Tarreau294c4732011-12-16 21:35:50 +01007257 struct hdr_ctx local_ctx;
7258 char *ptr_hist[MAX_HDR_HISTORY];
7259 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007260 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007261 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007262
Willy Tarreau294c4732011-12-16 21:35:50 +01007263 if (!ctx) {
7264 local_ctx.idx = 0;
7265 ctx = &local_ctx;
7266 }
7267
Willy Tarreaubce70882009-09-07 11:51:47 +02007268 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007269 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007270 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007271 occ--;
7272 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007273 *vptr = ctx->line + ctx->val;
7274 *vlen = ctx->vlen;
7275 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007276 }
7277 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007278 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007279 }
7280
7281 /* negative occurrence, we scan all the list then walk back */
7282 if (-occ > MAX_HDR_HISTORY)
7283 return 0;
7284
Willy Tarreau294c4732011-12-16 21:35:50 +01007285 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007286 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007287 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7288 len_hist[hist_ptr] = ctx->vlen;
7289 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007290 hist_ptr = 0;
7291 found++;
7292 }
7293 if (-occ > found)
7294 return 0;
7295 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7296 * find occurrence -occ, so we have to check [hist_ptr+occ].
7297 */
7298 hist_ptr += occ;
7299 if (hist_ptr >= MAX_HDR_HISTORY)
7300 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007301 *vptr = ptr_hist[hist_ptr];
7302 *vlen = len_hist[hist_ptr];
7303 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007304}
7305
Willy Tarreaubaaee002006-06-26 02:48:02 +02007306/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307 * Print a debug line with a header
7308 */
7309void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7310{
7311 int len, max;
7312 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007313 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314 max = end - start;
7315 UBOUND(max, sizeof(trash) - len - 1);
7316 len += strlcpy2(trash + len, start, max + 1);
7317 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007318 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007319}
7320
Willy Tarreau0937bc42009-12-22 15:03:09 +01007321/*
7322 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7323 * the required fields are properly allocated and that we only need to (re)init
7324 * them. This should be used before processing any new request.
7325 */
7326void http_init_txn(struct session *s)
7327{
7328 struct http_txn *txn = &s->txn;
7329 struct proxy *fe = s->fe;
7330
7331 txn->flags = 0;
7332 txn->status = -1;
7333
William Lallemand5f232402012-04-05 18:02:55 +02007334 global.req_count++;
7335
Willy Tarreauf64d1412010-10-07 20:06:11 +02007336 txn->cookie_first_date = 0;
7337 txn->cookie_last_date = 0;
7338
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007339 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007340 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007341 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007342 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007343 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007344 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007345 txn->req.chunk_len = 0LL;
7346 txn->req.body_len = 0LL;
7347 txn->rsp.chunk_len = 0LL;
7348 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007349 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7350 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007351 txn->req.buf = s->req;
7352 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007353
7354 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007355
7356 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7357 if (fe->options2 & PR_O2_REQBUG_OK)
7358 txn->req.err_pos = -1; /* let buggy requests pass */
7359
Willy Tarreau46023632010-01-07 22:51:47 +01007360 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007361 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7362
Willy Tarreau46023632010-01-07 22:51:47 +01007363 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007364 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7365
7366 if (txn->hdr_idx.v)
7367 hdr_idx_init(&txn->hdr_idx);
7368}
7369
7370/* to be used at the end of a transaction */
7371void http_end_txn(struct session *s)
7372{
7373 struct http_txn *txn = &s->txn;
7374
7375 /* these ones will have been dynamically allocated */
7376 pool_free2(pool2_requri, txn->uri);
7377 pool_free2(pool2_capture, txn->cli_cookie);
7378 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007379 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007380 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007381
William Lallemanda73203e2012-03-12 12:48:57 +01007382 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007383 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007384 txn->uri = NULL;
7385 txn->srv_cookie = NULL;
7386 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007387
7388 if (txn->req.cap) {
7389 struct cap_hdr *h;
7390 for (h = s->fe->req_cap; h; h = h->next)
7391 pool_free2(h->pool, txn->req.cap[h->index]);
7392 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7393 }
7394
7395 if (txn->rsp.cap) {
7396 struct cap_hdr *h;
7397 for (h = s->fe->rsp_cap; h; h = h->next)
7398 pool_free2(h->pool, txn->rsp.cap[h->index]);
7399 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7400 }
7401
Willy Tarreau0937bc42009-12-22 15:03:09 +01007402}
7403
7404/* to be used at the end of a transaction to prepare a new one */
7405void http_reset_txn(struct session *s)
7406{
7407 http_end_txn(s);
7408 http_init_txn(s);
7409
7410 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007411 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007412 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007413 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007414 /* re-init store persistence */
7415 s->store_count = 0;
7416
Willy Tarreau0937bc42009-12-22 15:03:09 +01007417 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007418
7419 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7420
Willy Tarreau739cfba2010-01-25 23:11:14 +01007421 /* We must trim any excess data from the response buffer, because we
7422 * may have blocked an invalid response from a server that we don't
7423 * want to accidentely forward once we disable the analysers, nor do
7424 * we want those data to come along with next response. A typical
7425 * example of such data would be from a buggy server responding to
7426 * a HEAD with some data, or sending more than the advertised
7427 * content-length.
7428 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007429 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007430 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007431
Willy Tarreau0937bc42009-12-22 15:03:09 +01007432 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007433 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007434
Willy Tarreaud04e8582010-05-31 12:31:35 +02007435 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007437
7438 s->req->rex = TICK_ETERNITY;
7439 s->req->wex = TICK_ETERNITY;
7440 s->req->analyse_exp = TICK_ETERNITY;
7441 s->rep->rex = TICK_ETERNITY;
7442 s->rep->wex = TICK_ETERNITY;
7443 s->rep->analyse_exp = TICK_ETERNITY;
7444}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445
Willy Tarreauff011f22011-01-06 17:51:27 +01007446void free_http_req_rules(struct list *r) {
7447 struct http_req_rule *tr, *pr;
7448
7449 list_for_each_entry_safe(pr, tr, r, list) {
7450 LIST_DEL(&pr->list);
7451 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7452 free(pr->http_auth.realm);
7453
7454 free(pr);
7455 }
7456}
7457
7458struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7459{
7460 struct http_req_rule *rule;
7461 int cur_arg;
7462
7463 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7464 if (!rule) {
7465 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7466 return NULL;
7467 }
7468
7469 if (!*args[0]) {
7470 goto req_error_parsing;
7471 } else if (!strcmp(args[0], "allow")) {
7472 rule->action = HTTP_REQ_ACT_ALLOW;
7473 cur_arg = 1;
7474 } else if (!strcmp(args[0], "deny")) {
7475 rule->action = HTTP_REQ_ACT_DENY;
7476 cur_arg = 1;
7477 } else if (!strcmp(args[0], "auth")) {
7478 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7479 cur_arg = 1;
7480
7481 while(*args[cur_arg]) {
7482 if (!strcmp(args[cur_arg], "realm")) {
7483 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7484 cur_arg+=2;
7485 continue;
7486 } else
7487 break;
7488 }
7489 } else {
7490req_error_parsing:
7491 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7492 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7493 return NULL;
7494 }
7495
7496 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7497 struct acl_cond *cond;
7498
7499 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7500 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7501 file, linenum, args[0]);
7502 return NULL;
7503 }
7504 rule->cond = cond;
7505 }
7506 else if (*args[cur_arg]) {
7507 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7508 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7509 file, linenum, args[0], args[cur_arg]);
7510 return NULL;
7511 }
7512
7513 return rule;
7514}
7515
Willy Tarreau8797c062007-05-07 00:55:35 +02007516/************************************************************************/
7517/* The code below is dedicated to ACL parsing and matching */
7518/************************************************************************/
7519
7520
7521
7522
7523/* 1. Check on METHOD
7524 * We use the pre-parsed method if it is known, and store its number as an
7525 * integer. If it is unknown, we use the pointer and the length.
7526 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007527static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007528{
7529 int len, meth;
7530
Willy Tarreauae8b7962007-06-09 23:10:04 +02007531 len = strlen(*text);
7532 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007533
7534 pattern->val.i = meth;
7535 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007536 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007537 if (!pattern->ptr.str)
7538 return 0;
7539 pattern->len = len;
7540 }
7541 return 1;
7542}
7543
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007544/* This function fetches the method of current HTTP request and stores
7545 * it in the global pattern struct as a chunk. There are two possibilities :
7546 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7547 * in <len> and <ptr> is NULL ;
7548 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7549 * <len> to its length.
7550 * This is intended to be used with acl_match_meth() only.
7551 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007552static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007553acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7554 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007555{
7556 int meth;
7557 struct http_txn *txn = l7;
7558
Willy Tarreaub6866442008-07-14 23:54:42 +02007559 if (!txn)
7560 return 0;
7561
Willy Tarreau655dce92009-11-08 13:10:58 +01007562 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007563 return 0;
7564
Willy Tarreau8797c062007-05-07 00:55:35 +02007565 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007566 temp_pattern.data.str.len = meth;
7567 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007568 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007569 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7570 /* ensure the indexes are not affected */
7571 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007572 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007573 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007574 }
7575 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7576 return 1;
7577}
7578
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007579/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007580static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7581{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007582 int icase;
7583
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007584
7585 if (temp_pattern.data.str.str == NULL) {
7586 /* well-known method */
7587 if (temp_pattern.data.str.len == pattern->val.i)
7588 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007589 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007590 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007591
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007592 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7593 if (pattern->val.i != HTTP_METH_OTHER)
7594 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007595
7596 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007597 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007598 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007599
7600 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007601 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7602 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007603 return ACL_PAT_FAIL;
7604 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007605}
7606
7607/* 2. Check on Request/Status Version
7608 * We simply compare strings here.
7609 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007610static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007611{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007612 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007613 if (!pattern->ptr.str)
7614 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007615 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007616 return 1;
7617}
7618
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007619static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007620acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7621 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007622{
7623 struct http_txn *txn = l7;
7624 char *ptr;
7625 int len;
7626
Willy Tarreaub6866442008-07-14 23:54:42 +02007627 if (!txn)
7628 return 0;
7629
Willy Tarreau655dce92009-11-08 13:10:58 +01007630 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007631 return 0;
7632
Willy Tarreau8797c062007-05-07 00:55:35 +02007633 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007634 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007635
7636 while ((len-- > 0) && (*ptr++ != '/'));
7637 if (len <= 0)
7638 return 0;
7639
Willy Tarreau664092c2011-12-16 19:11:42 +01007640 temp_pattern.data.str.str = ptr;
7641 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007642
7643 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7644 return 1;
7645}
7646
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007647static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007648acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7649 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007650{
7651 struct http_txn *txn = l7;
7652 char *ptr;
7653 int len;
7654
Willy Tarreaub6866442008-07-14 23:54:42 +02007655 if (!txn)
7656 return 0;
7657
Willy Tarreau655dce92009-11-08 13:10:58 +01007658 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007659 return 0;
7660
Willy Tarreau8797c062007-05-07 00:55:35 +02007661 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007662 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007663
7664 while ((len-- > 0) && (*ptr++ != '/'));
7665 if (len <= 0)
7666 return 0;
7667
Willy Tarreau664092c2011-12-16 19:11:42 +01007668 temp_pattern.data.str.str = ptr;
7669 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007670
7671 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7672 return 1;
7673}
7674
7675/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007676static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007677acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7678 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007679{
7680 struct http_txn *txn = l7;
7681 char *ptr;
7682 int len;
7683
Willy Tarreaub6866442008-07-14 23:54:42 +02007684 if (!txn)
7685 return 0;
7686
Willy Tarreau655dce92009-11-08 13:10:58 +01007687 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007688 return 0;
7689
Willy Tarreau8797c062007-05-07 00:55:35 +02007690 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007691 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007692
Willy Tarreaua5e37562011-12-16 17:06:15 +01007693 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007694 test->flags = ACL_TEST_F_VOL_1ST;
7695 return 1;
7696}
7697
7698/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007699static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007700acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7701 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007702{
7703 struct http_txn *txn = l7;
7704
Willy Tarreaub6866442008-07-14 23:54:42 +02007705 if (!txn)
7706 return 0;
7707
Willy Tarreau655dce92009-11-08 13:10:58 +01007708 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007709 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007710
Willy Tarreauc11416f2007-06-17 16:58:38 +02007711 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7712 /* ensure the indexes are not affected */
7713 return 0;
7714
Willy Tarreau664092c2011-12-16 19:11:42 +01007715 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007716 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007717
Willy Tarreauf3d25982007-05-08 22:45:09 +02007718 /* we do not need to set READ_ONLY because the data is in a buffer */
7719 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007720 return 1;
7721}
7722
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007723static int
7724acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7725 struct acl_expr *expr, struct acl_test *test)
7726{
7727 struct http_txn *txn = l7;
7728
Willy Tarreaub6866442008-07-14 23:54:42 +02007729 if (!txn)
7730 return 0;
7731
Willy Tarreau655dce92009-11-08 13:10:58 +01007732 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007733 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007734
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007735 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7736 /* ensure the indexes are not affected */
7737 return 0;
7738
7739 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007740 url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007741 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7742 return 0;
7743 temp_pattern.type = PATTERN_TYPE_IP;
7744 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007745
7746 /*
7747 * If we are parsing url in frontend space, we prepare backend stage
7748 * to not parse again the same url ! optimization lazyness...
7749 */
7750 if (px->options & PR_O_HTTP_PROXY)
7751 l4->flags |= SN_ADDR_SET;
7752
Willy Tarreauf4362b32011-12-16 17:49:52 +01007753 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007754 return 1;
7755}
7756
7757static int
7758acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7759 struct acl_expr *expr, struct acl_test *test)
7760{
7761 struct http_txn *txn = l7;
7762
Willy Tarreaub6866442008-07-14 23:54:42 +02007763 if (!txn)
7764 return 0;
7765
Willy Tarreau655dce92009-11-08 13:10:58 +01007766 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007767 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007768
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007769 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7770 /* ensure the indexes are not affected */
7771 return 0;
7772
7773 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007774 url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreaua5e37562011-12-16 17:06:15 +01007775 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007776
7777 if (px->options & PR_O_HTTP_PROXY)
7778 l4->flags |= SN_ADDR_SET;
7779
7780 test->flags = ACL_TEST_F_READ_ONLY;
7781 return 1;
7782}
7783
Willy Tarreauc11416f2007-06-17 16:58:38 +02007784/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7785 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7786 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007787static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007788acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007789 struct acl_expr *expr, struct acl_test *test)
7790{
7791 struct http_txn *txn = l7;
7792 struct hdr_idx *idx = &txn->hdr_idx;
7793 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007794
Willy Tarreaub6866442008-07-14 23:54:42 +02007795 if (!txn)
7796 return 0;
7797
Willy Tarreau33a7e692007-06-10 19:45:56 +02007798 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7799 /* search for header from the beginning */
7800 ctx->idx = 0;
7801
Willy Tarreau33a7e692007-06-10 19:45:56 +02007802 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7803 test->flags |= ACL_TEST_F_FETCH_MORE;
7804 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007805 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7806 temp_pattern.data.str.len = ctx->vlen;
7807
Willy Tarreau33a7e692007-06-10 19:45:56 +02007808 return 1;
7809 }
7810
7811 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7812 test->flags |= ACL_TEST_F_VOL_HDR;
7813 return 0;
7814}
7815
Willy Tarreau33a7e692007-06-10 19:45:56 +02007816static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007817acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7818 struct acl_expr *expr, struct acl_test *test)
7819{
7820 struct http_txn *txn = l7;
7821
Willy Tarreaub6866442008-07-14 23:54:42 +02007822 if (!txn)
7823 return 0;
7824
Willy Tarreau655dce92009-11-08 13:10:58 +01007825 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007826 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007827
Willy Tarreauc11416f2007-06-17 16:58:38 +02007828 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7829 /* ensure the indexes are not affected */
7830 return 0;
7831
Willy Tarreau3a215be2012-03-09 21:39:51 +01007832 return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007833}
7834
7835static int
7836acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7837 struct acl_expr *expr, struct acl_test *test)
7838{
7839 struct http_txn *txn = l7;
7840
Willy Tarreaub6866442008-07-14 23:54:42 +02007841 if (!txn)
7842 return 0;
7843
Willy Tarreau655dce92009-11-08 13:10:58 +01007844 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007845 return 0;
7846
Willy Tarreau3a215be2012-03-09 21:39:51 +01007847 return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007848}
7849
7850/* 6. Check on HTTP header count. The number of occurrences is returned.
7851 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7852 */
7853static int
7854acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007855 struct acl_expr *expr, struct acl_test *test)
7856{
7857 struct http_txn *txn = l7;
7858 struct hdr_idx *idx = &txn->hdr_idx;
7859 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007860 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007861
Willy Tarreaub6866442008-07-14 23:54:42 +02007862 if (!txn)
7863 return 0;
7864
Willy Tarreau33a7e692007-06-10 19:45:56 +02007865 ctx.idx = 0;
7866 cnt = 0;
7867 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7868 cnt++;
7869
Willy Tarreaua5e37562011-12-16 17:06:15 +01007870 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007871 test->flags = ACL_TEST_F_VOL_HDR;
7872 return 1;
7873}
7874
Willy Tarreauc11416f2007-06-17 16:58:38 +02007875static int
7876acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7877 struct acl_expr *expr, struct acl_test *test)
7878{
7879 struct http_txn *txn = l7;
7880
Willy Tarreaub6866442008-07-14 23:54:42 +02007881 if (!txn)
7882 return 0;
7883
Willy Tarreau655dce92009-11-08 13:10:58 +01007884 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007885 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007886
Willy Tarreauc11416f2007-06-17 16:58:38 +02007887 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7888 /* ensure the indexes are not affected */
7889 return 0;
7890
Willy Tarreau3a215be2012-03-09 21:39:51 +01007891 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007892}
7893
7894static int
7895acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7896 struct acl_expr *expr, struct acl_test *test)
7897{
7898 struct http_txn *txn = l7;
7899
Willy Tarreaub6866442008-07-14 23:54:42 +02007900 if (!txn)
7901 return 0;
7902
Willy Tarreau655dce92009-11-08 13:10:58 +01007903 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007904 return 0;
7905
Willy Tarreau3a215be2012-03-09 21:39:51 +01007906 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007907}
7908
Willy Tarreau33a7e692007-06-10 19:45:56 +02007909/* 7. Check on HTTP header's integer value. The integer value is returned.
7910 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007911 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007912 */
7913static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007914acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007915 struct acl_expr *expr, struct acl_test *test)
7916{
7917 struct http_txn *txn = l7;
7918 struct hdr_idx *idx = &txn->hdr_idx;
7919 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007920
Willy Tarreaub6866442008-07-14 23:54:42 +02007921 if (!txn)
7922 return 0;
7923
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7925 /* search for header from the beginning */
7926 ctx->idx = 0;
7927
Willy Tarreau33a7e692007-06-10 19:45:56 +02007928 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7929 test->flags |= ACL_TEST_F_FETCH_MORE;
7930 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007931 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007932 return 1;
7933 }
7934
7935 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7936 test->flags |= ACL_TEST_F_VOL_HDR;
7937 return 0;
7938}
7939
Willy Tarreauc11416f2007-06-17 16:58:38 +02007940static int
7941acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7942 struct acl_expr *expr, struct acl_test *test)
7943{
7944 struct http_txn *txn = l7;
7945
Willy Tarreaub6866442008-07-14 23:54:42 +02007946 if (!txn)
7947 return 0;
7948
Willy Tarreau655dce92009-11-08 13:10:58 +01007949 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007950 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007951
Willy Tarreauc11416f2007-06-17 16:58:38 +02007952 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7953 /* ensure the indexes are not affected */
7954 return 0;
7955
Willy Tarreau3a215be2012-03-09 21:39:51 +01007956 return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007957}
7958
7959static int
7960acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7961 struct acl_expr *expr, struct acl_test *test)
7962{
7963 struct http_txn *txn = l7;
7964
Willy Tarreaub6866442008-07-14 23:54:42 +02007965 if (!txn)
7966 return 0;
7967
Willy Tarreau655dce92009-11-08 13:10:58 +01007968 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007969 return 0;
7970
Willy Tarreau3a215be2012-03-09 21:39:51 +01007971 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007972}
7973
Willy Tarreau106f9792009-09-19 07:54:16 +02007974/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7975 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7976 */
7977static int
7978acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7979 struct acl_expr *expr, struct acl_test *test)
7980{
7981 struct http_txn *txn = l7;
7982 struct hdr_idx *idx = &txn->hdr_idx;
7983 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7984
7985 if (!txn)
7986 return 0;
7987
7988 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7989 /* search for header from the beginning */
7990 ctx->idx = 0;
7991
Willy Tarreauf4362b32011-12-16 17:49:52 +01007992 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007993 test->flags |= ACL_TEST_F_FETCH_MORE;
7994 test->flags |= ACL_TEST_F_VOL_HDR;
7995 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01007996 temp_pattern.type = PATTERN_TYPE_IP;
7997 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
7998 return 1;
7999 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008000 }
8001
8002 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8003 test->flags |= ACL_TEST_F_VOL_HDR;
8004 return 0;
8005}
8006
8007static int
8008acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8009 struct acl_expr *expr, struct acl_test *test)
8010{
8011 struct http_txn *txn = l7;
8012
8013 if (!txn)
8014 return 0;
8015
Willy Tarreau655dce92009-11-08 13:10:58 +01008016 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008017 return 0;
8018
8019 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8020 /* ensure the indexes are not affected */
8021 return 0;
8022
Willy Tarreau3a215be2012-03-09 21:39:51 +01008023 return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008024}
8025
8026static int
8027acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8028 struct acl_expr *expr, struct acl_test *test)
8029{
8030 struct http_txn *txn = l7;
8031
8032 if (!txn)
8033 return 0;
8034
Willy Tarreau655dce92009-11-08 13:10:58 +01008035 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008036 return 0;
8037
Willy Tarreau3a215be2012-03-09 21:39:51 +01008038 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008039}
8040
Willy Tarreau737b0c12007-06-10 21:28:46 +02008041/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8042 * the first '/' after the possible hostname, and ends before the possible '?'.
8043 */
8044static int
8045acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8046 struct acl_expr *expr, struct acl_test *test)
8047{
8048 struct http_txn *txn = l7;
8049 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008050
Willy Tarreaub6866442008-07-14 23:54:42 +02008051 if (!txn)
8052 return 0;
8053
Willy Tarreau655dce92009-11-08 13:10:58 +01008054 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008055 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008056
Willy Tarreauc11416f2007-06-17 16:58:38 +02008057 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8058 /* ensure the indexes are not affected */
8059 return 0;
8060
Willy Tarreau3a215be2012-03-09 21:39:51 +01008061 end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008062 ptr = http_get_path(txn);
8063 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008064 return 0;
8065
8066 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008067 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008068
8069 while (ptr < end && *ptr != '?')
8070 ptr++;
8071
Willy Tarreau664092c2011-12-16 19:11:42 +01008072 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008073
8074 /* we do not need to set READ_ONLY because the data is in a buffer */
8075 test->flags = ACL_TEST_F_VOL_1ST;
8076 return 1;
8077}
8078
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008079static int
8080acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8081 struct acl_expr *expr, struct acl_test *test)
8082{
8083 struct buffer *req = s->req;
8084 struct http_txn *txn = &s->txn;
8085 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008086
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008087 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8088 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8089 */
8090
8091 if (!s || !req)
8092 return 0;
8093
Willy Tarreau655dce92009-11-08 13:10:58 +01008094 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008095 /* Already decoded as OK */
8096 test->flags |= ACL_TEST_F_SET_RES_PASS;
8097 return 1;
8098 }
8099
8100 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008101 if (likely(msg->next < req->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01008102 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008103
Willy Tarreau655dce92009-11-08 13:10:58 +01008104 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008105 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8106 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8107 return 1;
8108 }
8109 /* wait for final state */
8110 test->flags |= ACL_TEST_F_MAY_CHANGE;
8111 return 0;
8112 }
8113
8114 /* OK we got a valid HTTP request. We have some minor preparation to
8115 * perform so that further checks can rely on HTTP tests.
8116 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008117 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008118 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8119 s->flags |= SN_REDIRECTABLE;
8120
Willy Tarreau418bfcc2012-03-09 13:56:20 +01008121 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008122 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8123 return 1;
8124 }
8125
8126 test->flags |= ACL_TEST_F_SET_RES_PASS;
8127 return 1;
8128}
8129
Willy Tarreau7f18e522010-10-22 20:04:13 +02008130/* return a valid test if the current request is the first one on the connection */
8131static int
8132acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8133 struct acl_expr *expr, struct acl_test *test)
8134{
8135 if (!s)
8136 return 0;
8137
8138 if (s->txn.flags & TX_NOT_FIRST)
8139 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8140 else
8141 test->flags |= ACL_TEST_F_SET_RES_PASS;
8142
8143 return 1;
8144}
8145
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008146static int
8147acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8148 struct acl_expr *expr, struct acl_test *test)
8149{
8150
8151 if (!s)
8152 return 0;
8153
8154 if (!get_http_auth(s))
8155 return 0;
8156
8157 test->ctx.a[0] = expr->arg.ul;
8158 test->ctx.a[1] = s->txn.auth.user;
8159 test->ctx.a[2] = s->txn.auth.pass;
8160
8161 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8162
8163 return 1;
8164}
Willy Tarreau8797c062007-05-07 00:55:35 +02008165
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008166/* Try to find the next occurrence of a cookie name in a cookie header value.
8167 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8168 * the cookie value is returned into *value and *value_l, and the function
8169 * returns a pointer to the next pointer to search from if the value was found.
8170 * Otherwise if the cookie was not found, NULL is returned and neither value
8171 * nor value_l are touched. The input <hdr> string should first point to the
8172 * header's value, and the <hdr_end> pointer must point to the first character
8173 * not part of the value. <list> must be non-zero if value may represent a list
8174 * of values (cookie headers). This makes it faster to abort parsing when no
8175 * list is expected.
8176 */
8177static char *
8178extract_cookie_value(char *hdr, const char *hdr_end,
8179 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008180 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008181{
8182 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8183 char *next;
8184
8185 /* we search at least a cookie name followed by an equal, and more
8186 * generally something like this :
8187 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8188 */
8189 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8190 /* Iterate through all cookies on this line */
8191
8192 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8193 att_beg++;
8194
8195 /* find att_end : this is the first character after the last non
8196 * space before the equal. It may be equal to hdr_end.
8197 */
8198 equal = att_end = att_beg;
8199
8200 while (equal < hdr_end) {
8201 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8202 break;
8203 if (http_is_spht[(unsigned char)*equal++])
8204 continue;
8205 att_end = equal;
8206 }
8207
8208 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8209 * is between <att_beg> and <equal>, both may be identical.
8210 */
8211
8212 /* look for end of cookie if there is an equal sign */
8213 if (equal < hdr_end && *equal == '=') {
8214 /* look for the beginning of the value */
8215 val_beg = equal + 1;
8216 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8217 val_beg++;
8218
8219 /* find the end of the value, respecting quotes */
8220 next = find_cookie_value_end(val_beg, hdr_end);
8221
8222 /* make val_end point to the first white space or delimitor after the value */
8223 val_end = next;
8224 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8225 val_end--;
8226 } else {
8227 val_beg = val_end = next = equal;
8228 }
8229
8230 /* We have nothing to do with attributes beginning with '$'. However,
8231 * they will automatically be removed if a header before them is removed,
8232 * since they're supposed to be linked together.
8233 */
8234 if (*att_beg == '$')
8235 continue;
8236
8237 /* Ignore cookies with no equal sign */
8238 if (equal == next)
8239 continue;
8240
8241 /* Now we have the cookie name between att_beg and att_end, and
8242 * its value between val_beg and val_end.
8243 */
8244
8245 if (att_end - att_beg == cookie_name_l &&
8246 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8247 /* let's return this value and indicate where to go on from */
8248 *value = val_beg;
8249 *value_l = val_end - val_beg;
8250 return next + 1;
8251 }
8252
8253 /* Set-Cookie headers only have the name in the first attr=value part */
8254 if (!list)
8255 break;
8256 }
8257
8258 return NULL;
8259}
8260
8261/* Iterate over all cookies present in a request. The context is stored in
8262 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8263 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8264 * non-null, then multiple cookies may be parsed on the same line.
8265 * The cookie name is in expr->arg and the name length in expr->arg_len.
8266 */
8267static int
8268acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8269 const char *hdr_name, int hdr_name_len, int multi,
8270 struct acl_expr *expr, struct acl_test *test)
8271{
8272 struct http_txn *txn = l7;
8273 struct hdr_idx *idx = &txn->hdr_idx;
8274 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8275
8276 if (!txn)
8277 return 0;
8278
8279 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8280 /* search for the header from the beginning, we must first initialize
8281 * the search parameters.
8282 */
8283 test->ctx.a[0] = NULL;
8284 ctx->idx = 0;
8285 }
8286
8287 while (1) {
8288 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8289 if (!test->ctx.a[0]) {
8290 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8291 goto out;
8292
8293 if (ctx->vlen < expr->arg_len + 1)
8294 continue;
8295
8296 test->ctx.a[0] = ctx->line + ctx->val;
8297 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8298 }
8299
8300 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8301 expr->arg.str, expr->arg_len, multi,
8302 &temp_pattern.data.str.str,
8303 &temp_pattern.data.str.len);
8304 if (test->ctx.a[0]) {
8305 /* one value was returned into temp_pattern.data.str.{str,len} */
8306 test->flags |= ACL_TEST_F_FETCH_MORE;
8307 test->flags |= ACL_TEST_F_VOL_HDR;
8308 return 1;
8309 }
8310 }
8311
8312 out:
8313 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8314 test->flags |= ACL_TEST_F_VOL_HDR;
8315 return 0;
8316}
8317
8318static int
8319acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8320 struct acl_expr *expr, struct acl_test *test)
8321{
8322 struct http_txn *txn = l7;
8323
8324 if (!txn)
8325 return 0;
8326
8327 if (txn->req.msg_state < HTTP_MSG_BODY)
8328 return 0;
8329
8330 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8331 /* ensure the indexes are not affected */
8332 return 0;
8333
8334 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008335 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008336}
8337
8338static int
8339acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8340 struct acl_expr *expr, struct acl_test *test)
8341{
8342 struct http_txn *txn = l7;
8343
8344 if (!txn)
8345 return 0;
8346
8347 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8348 return 0;
8349
8350 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008351 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008352}
8353
8354/* Iterate over all cookies present in a request to count how many occurrences
8355 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8356 * multiple cookies may be parsed on the same line.
8357 */
8358static int
8359acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8360 const char *hdr_name, int hdr_name_len, int multi,
8361 struct acl_expr *expr, struct acl_test *test)
8362{
8363 struct http_txn *txn = l7;
8364 struct hdr_idx *idx = &txn->hdr_idx;
8365 struct hdr_ctx ctx;
8366 int cnt;
8367 char *val_beg, *val_end;
8368
8369 if (!txn)
8370 return 0;
8371
Willy Tarreau46787ed2012-04-11 17:28:40 +02008372 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008373 ctx.idx = 0;
8374 cnt = 0;
8375
8376 while (1) {
8377 /* Note: val_beg == NULL every time we need to fetch a new header */
8378 if (!val_beg) {
8379 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8380 break;
8381
8382 if (ctx.vlen < expr->arg_len + 1)
8383 continue;
8384
8385 val_beg = ctx.line + ctx.val;
8386 val_end = val_beg + ctx.vlen;
8387 }
8388
8389 while ((val_beg = extract_cookie_value(val_beg, val_end,
8390 expr->arg.str, expr->arg_len, multi,
8391 &temp_pattern.data.str.str,
8392 &temp_pattern.data.str.len))) {
8393 cnt++;
8394 }
8395 }
8396
8397 temp_pattern.data.integer = cnt;
8398 test->flags |= ACL_TEST_F_VOL_HDR;
8399 return 1;
8400}
8401
8402static int
8403acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8404 struct acl_expr *expr, struct acl_test *test)
8405{
8406 struct http_txn *txn = l7;
8407
8408 if (!txn)
8409 return 0;
8410
8411 if (txn->req.msg_state < HTTP_MSG_BODY)
8412 return 0;
8413
8414 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8415 /* ensure the indexes are not affected */
8416 return 0;
8417
8418 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008419 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008420}
8421
8422static int
8423acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8424 struct acl_expr *expr, struct acl_test *test)
8425{
8426 struct http_txn *txn = l7;
8427
8428 if (!txn)
8429 return 0;
8430
8431 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8432 return 0;
8433
8434 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008435 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008436}
8437
Willy Tarreau8797c062007-05-07 00:55:35 +02008438/************************************************************************/
8439/* All supported keywords must be declared here. */
8440/************************************************************************/
8441
8442/* Note: must not be declared <const> as its list will be overwritten */
8443static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008444 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8445
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008446 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008447 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8448 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008449 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008450
Willy Tarreauc4262962010-05-10 23:42:40 +02008451 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008452 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8453 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8454 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8455 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8456 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8457 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008458 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008459 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008460 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008461
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008462 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008463 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008464 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8465 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8466 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8467 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8468 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8469 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8470 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008471 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008472 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008473 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008474
Willy Tarreauc4262962010-05-10 23:42:40 +02008475 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008476 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8477 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8478 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8479 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8480 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8481 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8482 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008483 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008484 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008485 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008486
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008487 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8488 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8489 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8490 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8491 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8492 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8493 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8494 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8495 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8496
8497 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8498 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8499 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8500 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8501 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8502 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8503 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8504 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8505 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8506
Willy Tarreauc4262962010-05-10 23:42:40 +02008507 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008508 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8509 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8510 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8511 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8512 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8513 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008514 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008515
Willy Tarreau7f18e522010-10-22 20:04:13 +02008516 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8517 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8518 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008519 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008520}};
8521
Willy Tarreau4a568972010-05-12 08:08:50 +02008522/************************************************************************/
8523/* The code below is dedicated to pattern fetching and matching */
8524/************************************************************************/
8525
Willy Tarreaue428fb72011-12-16 21:50:30 +01008526/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008527static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008528pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8529 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008530{
8531 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008532
Willy Tarreaue428fb72011-12-16 21:50:30 +01008533 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8534 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008535}
8536
David Cournapeau16023ee2010-12-23 20:55:41 +09008537/*
8538 * Given a path string and its length, find the position of beginning of the
8539 * query string. Returns NULL if no query string is found in the path.
8540 *
8541 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8542 *
8543 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8544 */
8545static inline char *find_query_string(char *path, size_t path_l)
8546{
8547 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008548
David Cournapeau16023ee2010-12-23 20:55:41 +09008549 p = memchr(path, '?', path_l);
8550 return p ? p + 1 : NULL;
8551}
8552
8553static inline int is_param_delimiter(char c)
8554{
8555 return c == '&' || c == ';';
8556}
8557
8558/*
8559 * Given a url parameter, find the starting position of the first occurence,
8560 * or NULL if the parameter is not found.
8561 *
8562 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8563 * the function will return query_string+8.
8564 */
8565static char*
8566find_url_param_pos(char* query_string, size_t query_string_l,
8567 char* url_param_name, size_t url_param_name_l)
8568{
8569 char *pos, *last;
8570
8571 pos = query_string;
8572 last = query_string + query_string_l - url_param_name_l - 1;
8573
8574 while (pos <= last) {
8575 if (pos[url_param_name_l] == '=') {
8576 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8577 return pos;
8578 pos += url_param_name_l + 1;
8579 }
8580 while (pos <= last && !is_param_delimiter(*pos))
8581 pos++;
8582 pos++;
8583 }
8584 return NULL;
8585}
8586
8587/*
8588 * Given a url parameter name, returns its value and size into *value and
8589 * *value_l respectively, and returns non-zero. If the parameter is not found,
8590 * zero is returned and value/value_l are not touched.
8591 */
8592static int
8593find_url_param_value(char* path, size_t path_l,
8594 char* url_param_name, size_t url_param_name_l,
8595 char** value, size_t* value_l)
8596{
8597 char *query_string, *qs_end;
8598 char *arg_start;
8599 char *value_start, *value_end;
8600
8601 query_string = find_query_string(path, path_l);
8602 if (!query_string)
8603 return 0;
8604
8605 qs_end = path + path_l;
8606 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8607 url_param_name, url_param_name_l);
8608 if (!arg_start)
8609 return 0;
8610
8611 value_start = arg_start + url_param_name_l + 1;
8612 value_end = value_start;
8613
8614 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8615 value_end++;
8616
8617 *value = value_start;
8618 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008619 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008620}
8621
8622static int
8623pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8624 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8625{
8626 struct http_txn *txn = l7;
8627 struct http_msg *msg = &txn->req;
8628 char *url_param_value;
8629 size_t url_param_value_l;
8630
Willy Tarreau3a215be2012-03-09 21:39:51 +01008631 if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
David Cournapeau16023ee2010-12-23 20:55:41 +09008632 arg_p->data.str.str, arg_p->data.str.len,
8633 &url_param_value, &url_param_value_l))
8634 return 0;
8635
8636 data->str.str = url_param_value;
8637 data->str.len = url_param_value_l;
8638 return 1;
8639}
8640
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008641/* Try to find in request or response message is in <msg> and whose transaction
8642 * is in <txn> the last occurrence of a cookie name in all cookie header values
8643 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8644 * pointer and size of the last occurrence of the cookie value is returned into
8645 * <value> and <value_l>, and the function returns non-zero if the value was
8646 * found. Otherwise if the cookie was not found, zero is returned and neither
8647 * value nor value_l are touched. The input hdr string should begin at the
8648 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8649 * value may represent a list of values (cookie headers).
8650 */
8651
8652static int
8653find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8654 const char *hdr_name, int hdr_name_len,
8655 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008656 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008657{
8658 struct hdr_ctx ctx;
8659 int found = 0;
8660
8661 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008662 while (http_find_header2(hdr_name, hdr_name_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008663 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008664 if (ctx.vlen < cookie_name_l + 1)
8665 continue;
8666
Willy Tarreau4573af92012-04-06 18:20:06 +02008667 hdr = ctx.line + ctx.val;
8668 end = hdr + ctx.vlen;
8669 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8670 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008671 }
8672 return found;
8673}
8674
8675static int
8676pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8677 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8678{
8679 struct http_txn *txn = l7;
8680 struct http_msg *msg = &txn->req;
8681 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008682 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008683 int found = 0;
8684
8685 found = find_cookie_value(msg, txn, "Cookie", 6,
8686 arg_p->data.str.str, arg_p->data.str.len, 1,
8687 &cookie_value, &cookie_value_l);
8688 if (found) {
8689 data->str.str = cookie_value;
8690 data->str.len = cookie_value_l;
8691 }
8692
8693 return found;
8694}
8695
8696
8697static int
8698pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8699 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8700{
8701 struct http_txn *txn = l7;
8702 struct http_msg *msg = &txn->rsp;
8703 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008704 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008705 int found = 0;
8706
8707 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8708 arg_p->data.str.str, arg_p->data.str.len, 1,
8709 &cookie_value, &cookie_value_l);
8710 if (found) {
8711 data->str.str = cookie_value;
8712 data->str.len = cookie_value_l;
8713 }
8714
8715 return found;
8716}
8717
Emeric Brun485479d2010-09-23 18:02:19 +02008718
Willy Tarreau4a568972010-05-12 08:08:50 +02008719/************************************************************************/
8720/* All supported keywords must be declared here. */
8721/************************************************************************/
8722/* Note: must not be declared <const> as its list will be overwritten */
8723static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008724 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008725 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008726 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8727 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008728 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008729}};
8730
Willy Tarreau8797c062007-05-07 00:55:35 +02008731
8732__attribute__((constructor))
8733static void __http_protocol_init(void)
8734{
8735 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008736 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008737}
8738
8739
Willy Tarreau58f10d72006-12-04 02:26:12 +01008740/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008741 * Local variables:
8742 * c-indent-level: 8
8743 * c-basic-offset: 8
8744 * End:
8745 */