blob: 4521cce46bed8a395e70a168ee5c2f55d36f1c9f [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 Tarreau8d5d7f22007-01-21 19:16:41 +01001277void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1278{
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 Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 state = msg->msg_state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001283 ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001284 end = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 if (unlikely(ptr >= end))
1287 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 /*
1291 * First, states that are specific to the response only.
1292 * We check them first so that request and headers are
1293 * closer to each other (accessed more often).
1294 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001296 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001298 /* we have a start of message, but we have to check
1299 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001300 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001301 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001302 char *beg = buf->p;
1303
Willy Tarreau15de77e2010-01-02 21:59:16 +01001304 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001305 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001306 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001307 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001310 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 hdr_idx_init(idx);
1312 state = HTTP_MSG_RPVER;
1313 goto http_msg_rpver;
1314 }
1315
1316 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1317 goto http_msg_invalid;
1318
1319 if (unlikely(*ptr == '\n'))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1322 /* stop here */
1323
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001325 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 EXPECT_LF_HERE(ptr, http_msg_invalid);
1327 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1328 /* stop here */
1329
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001331 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPVER_SP:
1333 case HTTP_MSG_RPCODE:
1334 case HTTP_MSG_RPCODE_SP:
1335 case HTTP_MSG_RPREASON:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001336 ptr = (char *)http_parse_stsline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001337 state, ptr, end,
1338 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 if (unlikely(!ptr))
1340 return;
1341
1342 /* we have a full response and we know that we have either a CR
1343 * or an LF at <ptr>.
1344 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1346
Willy Tarreau3a215be2012-03-09 21:39:51 +01001347 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(*ptr == '\r'))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1350 goto http_msg_rpline_end;
1351
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001353 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 /* msg->sol must point to the first of CR or LF. */
1355 EXPECT_LF_HERE(ptr, http_msg_invalid);
1356 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1357 /* stop here */
1358
1359 /*
1360 * Second, states that are specific to the request only
1361 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001363 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001365 /* we have a start of message, but we have to check
1366 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001367 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001368 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001369 char *beg = buf->p;
1370
Willy Tarreau15de77e2010-01-02 21:59:16 +01001371 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001372 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001373 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001374 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001375 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001377 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001378 /* we will need this when keep-alive will be supported
1379 hdr_idx_init(idx);
1380 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 state = HTTP_MSG_RQMETH;
1382 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1386 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(*ptr == '\n'))
1389 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001394 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 EXPECT_LF_HERE(ptr, http_msg_invalid);
1396 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001400 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH_SP:
1402 case HTTP_MSG_RQURI:
1403 case HTTP_MSG_RQURI_SP:
1404 case HTTP_MSG_RQVER:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001405 ptr = (char *)http_parse_reqline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001406 state, ptr, end,
1407 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (unlikely(!ptr))
1409 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 /* we have a full request and we know that we have either a CR
1412 * or an LF at <ptr>.
1413 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau3a215be2012-03-09 21:39:51 +01001416 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(*ptr == '\r'))
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 /* check for HTTP/0.9 request : no version information available.
1424 * msg->sol must point to the first of CR or LF.
1425 */
1426 if (unlikely(msg->sl.rq.v_l == 0))
1427 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 EXPECT_LF_HERE(ptr, http_msg_invalid);
1430 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /*
1434 * Common states below
1435 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001437 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001438 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_CRLF(*ptr))) {
1440 goto http_msg_hdr_name;
1441 }
1442
1443 if (likely(*ptr == '\r'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1445 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001448 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 /* assumes msg->sol points to the first char */
1450 if (likely(HTTP_IS_TOKEN(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001453 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001456 if (likely(msg->err_pos < -1) || *ptr == '\n')
1457 goto http_msg_invalid;
1458
1459 if (msg->err_pos == -1) /* capture error pointer */
1460 msg->err_pos = ptr - buf->data; /* >= 0 now */
1461
1462 /* and we still accept this non-token character */
1463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001467 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 if (likely(HTTP_IS_SPHT(*ptr)))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001472 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (likely(!HTTP_IS_CRLF(*ptr))) {
1475 goto http_msg_hdr_val;
1476 }
1477
1478 if (likely(*ptr == '\r'))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1480 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 if (likely(HTTP_IS_SPHT(*ptr))) {
1490 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001491 for (; buf->p + msg->sov < ptr; msg->sov++)
1492 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 goto http_msg_hdr_l1_sp;
1494 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001495 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001496 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_complete_header;
1498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001501 /* assumes msg->sol points to the first char, and msg->sov
1502 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 */
1504 if (likely(!HTTP_IS_CRLF(*ptr)))
1505 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau12e48b32012-03-05 16:57:34 +01001507 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 /* Note: we could also copy eol into ->eoh so that we have the
1509 * real header end in case it ends with lots of LWS, but is this
1510 * really needed ?
1511 */
1512 if (likely(*ptr == '\r'))
1513 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1514 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 EXPECT_LF_HERE(ptr, http_msg_invalid);
1519 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1524 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001525 for (; buf->p + msg->eol < ptr; msg->eol++)
1526 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 goto http_msg_hdr_val;
1528 }
1529 http_msg_complete_header:
1530 /*
1531 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001532 * Assumes msg->sol points to the first char, msg->sov points
1533 * to the first character of the value and msg->eol to the
1534 * first CR or LF so we know how the line ends. We insert last
1535 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001537 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 idx, idx->tail) < 0))
1539 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001540
Willy Tarreau3a215be2012-03-09 21:39:51 +01001541 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (likely(!HTTP_IS_CRLF(*ptr))) {
1543 goto http_msg_hdr_name;
1544 }
1545
1546 if (likely(*ptr == '\r'))
1547 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1548 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001551 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 /* Assumes msg->sol points to the first of either CR or LF */
1553 EXPECT_LF_HERE(ptr, http_msg_invalid);
1554 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001555 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001556 msg->eoh = msg->sol;
1557 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001558 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001560
1561 case HTTP_MSG_ERROR:
1562 /* this may only happen if we call http_msg_analyser() twice with an error */
1563 break;
1564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565#ifdef DEBUG_FULL
1566 default:
1567 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1568 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001569#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 }
1571 http_msg_ood:
1572 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001573 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001574 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 http_msg_invalid:
1578 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001579 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001580 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 return;
1582}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001583
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001584/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1585 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1586 * nothing is done and 1 is returned.
1587 */
1588static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1589{
1590 int delta;
1591 char *cur_end;
1592
1593 if (msg->sl.rq.v_l != 0)
1594 return 1;
1595
Willy Tarreau3a215be2012-03-09 21:39:51 +01001596 cur_end = req->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597 delta = 0;
1598
1599 if (msg->sl.rq.u_l == 0) {
1600 /* if no URI was set, add "/" */
1601 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1602 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001603 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001604 }
1605 /* add HTTP version */
1606 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001607 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001609 cur_end = (char *)http_parse_reqline(msg, req->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 HTTP_MSG_RQMETH,
Willy Tarreau3a215be2012-03-09 21:39:51 +01001611 req->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 NULL, NULL);
1613 if (unlikely(!cur_end))
1614 return 0;
1615
1616 /* we have a full HTTP/1.0 request now and we know that
1617 * we have either a CR or an LF at <ptr>.
1618 */
1619 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1620 return 1;
1621}
1622
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001623/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001624 * and "keep-alive" values. If we already know that some headers may safely
1625 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001626 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1627 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1628 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1629 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1630 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001631 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001632 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001634{
Willy Tarreau5b154472009-12-21 20:11:07 +01001635 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001636 const char *hdr_val = "Connection";
1637 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001638
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001639 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001640 return;
1641
Willy Tarreau88d349d2010-01-25 12:15:43 +01001642 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1643 hdr_val = "Proxy-Connection";
1644 hdr_len = 16;
1645 }
1646
Willy Tarreau5b154472009-12-21 20:11:07 +01001647 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001648 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001649 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001650 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1651 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001652 if (to_del & 2)
1653 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001654 else
1655 txn->flags |= TX_CON_KAL_SET;
1656 }
1657 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1658 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001659 if (to_del & 1)
1660 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 else
1662 txn->flags |= TX_CON_CLO_SET;
1663 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001664 }
1665
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001666 txn->flags |= TX_HDR_CONN_PRS;
1667 return;
1668}
Willy Tarreau5b154472009-12-21 20:11:07 +01001669
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001670/* Apply desired changes on the Connection: header. Values may be removed and/or
1671 * added depending on the <wanted> flags, which are exclusively composed of
1672 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1673 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1674 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001675void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676{
1677 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001678 const char *hdr_val = "Connection";
1679 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680
1681 ctx.idx = 0;
1682
Willy Tarreau88d349d2010-01-25 12:15:43 +01001683
1684 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1685 hdr_val = "Proxy-Connection";
1686 hdr_len = 16;
1687 }
1688
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001690 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1692 if (wanted & TX_CON_KAL_SET)
1693 txn->flags |= TX_CON_KAL_SET;
1694 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001695 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001696 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001697 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1698 if (wanted & TX_CON_CLO_SET)
1699 txn->flags |= TX_CON_CLO_SET;
1700 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001701 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001702 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001703 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704
1705 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1706 return;
1707
1708 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1709 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001710 hdr_val = "Connection: close";
1711 hdr_len = 17;
1712 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1713 hdr_val = "Proxy-Connection: close";
1714 hdr_len = 23;
1715 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001716 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001717 }
1718
1719 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1720 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001721 hdr_val = "Connection: keep-alive";
1722 hdr_len = 22;
1723 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1724 hdr_val = "Proxy-Connection: keep-alive";
1725 hdr_len = 28;
1726 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001727 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001728 }
1729 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001730}
1731
Willy Tarreaua458b672012-03-05 11:17:50 +01001732/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001733 * first byte of body, and increments msg->sov by the number of bytes parsed,
1734 * so that we know we can forward between ->som and ->sov. Note that due to
1735 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1736 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001737 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001738 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001741{
Willy Tarreaua458b672012-03-05 11:17:50 +01001742 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1743 char *ptr_old = ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001744 char *end = buf->data + buf->size;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001745 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 unsigned int chunk = 0;
1747
1748 /* The chunk size is in the following form, though we are only
1749 * interested in the size and CRLF :
1750 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1751 */
1752 while (1) {
1753 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001754 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001756 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001757 if (c < 0) /* not a hex digit anymore */
1758 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 if (++ptr >= end)
1760 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001761 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001762 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 chunk = (chunk << 4) + c;
1764 }
1765
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001767 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001768 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769
1770 while (http_is_spht[(unsigned char)*ptr]) {
1771 if (++ptr >= end)
1772 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001773 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001774 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001775 }
1776
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777 /* Up to there, we know that at least one byte is present at *ptr. Check
1778 * for the end of chunk size.
1779 */
1780 while (1) {
1781 if (likely(HTTP_IS_CRLF(*ptr))) {
1782 /* we now have a CR or an LF at ptr */
1783 if (likely(*ptr == '\r')) {
1784 if (++ptr >= end)
1785 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001786 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001787 return 0;
1788 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001789
Willy Tarreaud98cf932009-12-27 22:54:55 +01001790 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001791 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 if (++ptr >= end)
1793 ptr = buf->data;
1794 /* done */
1795 break;
1796 }
1797 else if (*ptr == ';') {
1798 /* chunk extension, ends at next CRLF */
1799 if (++ptr >= end)
1800 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001801 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001802 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803
1804 while (!HTTP_IS_CRLF(*ptr)) {
1805 if (++ptr >= end)
1806 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001807 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808 return 0;
1809 }
1810 /* we have a CRLF now, loop above */
1811 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001812 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001814 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001815 }
1816
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001818 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001821 msg->sov += ptr - ptr_old;
1822 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001823 msg->chunk_len = chunk;
1824 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001826 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001827 error:
1828 msg->err_pos = ptr - buf->data;
1829 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001830}
1831
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832/* This function skips trailers in the buffer <buf> associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001833 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834 * the trailers is found, it is automatically scheduled to be forwarded,
1835 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1836 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001837 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001838 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001840 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1841 * which implies that all non-trailers data have already been scheduled for
1842 * forwarding, and that the difference between msg->som and msg->sov exactly
1843 * matches the length of trailers already parsed and not forwarded. It is also
1844 * important to note that this function is designed to be able to parse wrapped
1845 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001846 */
1847int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1848{
Willy Tarreaua458b672012-03-05 11:17:50 +01001849 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 while (1) {
1851 char *p1 = NULL, *p2 = NULL;
Willy Tarreaua458b672012-03-05 11:17:50 +01001852 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001853 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001854 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855
1856 /* scan current line and stop at LF or CRLF */
1857 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001858 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 return 0;
1860
1861 if (*ptr == '\n') {
1862 if (!p1)
1863 p1 = ptr;
1864 p2 = ptr;
1865 break;
1866 }
1867
1868 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001869 if (p1) {
1870 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001872 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 p1 = ptr;
1874 }
1875
1876 ptr++;
1877 if (ptr >= buf->data + buf->size)
1878 ptr = buf->data;
1879 }
1880
1881 /* after LF; point to beginning of next line */
1882 p2++;
1883 if (p2 >= buf->data + buf->size)
1884 p2 = buf->data;
1885
Willy Tarreaua458b672012-03-05 11:17:50 +01001886 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001887 if (bytes < 0)
1888 bytes += buf->size;
1889
1890 /* schedule this line for forwarding */
1891 msg->sov += bytes;
1892 if (msg->sov >= buf->size)
1893 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001894
Willy Tarreaua458b672012-03-05 11:17:50 +01001895 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001896 /* LF/CRLF at beginning of line => end of trailers at p2.
1897 * Everything was scheduled for forwarding, there's nothing
1898 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001899 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001900 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901 msg->msg_state = HTTP_MSG_DONE;
1902 return 1;
1903 }
1904 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001905 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 }
1907}
1908
1909/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1910 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001911 * ->som, ->next in order to include this part into the next forwarding phase.
1912 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1914 * not enough data are available, the function does not change anything and
1915 * returns zero. If a parse error is encountered, the function returns < 0 and
1916 * does not change anything. Note: this function is designed to parse wrapped
1917 * CRLF at the end of the buffer.
1918 */
1919int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1920{
1921 char *ptr;
1922 int bytes;
1923
1924 /* NB: we'll check data availabilty at the end. It's not a
1925 * problem because whatever we match first will be checked
1926 * against the correct length.
1927 */
1928 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001929 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 if (*ptr == '\r') {
1931 bytes++;
1932 ptr++;
1933 if (ptr >= buf->data + buf->size)
1934 ptr = buf->data;
1935 }
1936
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001937 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 return 0;
1939
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001940 if (*ptr != '\n') {
1941 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001943 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944
1945 ptr++;
1946 if (ptr >= buf->data + buf->size)
1947 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001948 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1949 msg->som = 0;
1950 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1952 return 1;
1953}
Willy Tarreau5b154472009-12-21 20:11:07 +01001954
Willy Tarreau89fa7062012-03-02 16:13:16 +01001955/* This function may only be used when the buffer's o is empty */
Willy Tarreau83e3af02009-12-28 17:39:57 +01001956void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1957{
Willy Tarreau89fa7062012-03-02 16:13:16 +01001958 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001959
1960 /* two possible cases :
1961 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001962 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001963 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001964 if (buf->i) {
1965 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001966 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001967 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001968 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001969 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001971 }
1972 if (block2)
1973 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001974 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001975 if (block2)
1976 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001977 }
1978
1979 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001980 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001981
Willy Tarreau83e3af02009-12-28 17:39:57 +01001982 if (msg->err_pos >= 0) {
1983 msg->err_pos += off;
1984 if (msg->err_pos >= buf->size)
1985 msg->err_pos -= buf->size;
1986 }
1987
1988 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001989 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01001990 buf->flags |= BF_FULL;
1991}
1992
Willy Tarreaud787e662009-07-07 10:14:51 +02001993/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1994 * processing can continue on next analysers, or zero if it either needs more
1995 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1996 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1997 * when it has nothing left to do, and may remove any analyser when it wants to
1998 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002000int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001{
Willy Tarreau59234e92008-11-30 23:51:27 +01002002 /*
2003 * We will parse the partial (or complete) lines.
2004 * We will check the request syntax, and also join multi-line
2005 * headers. An index of all the lines will be elaborated while
2006 * parsing.
2007 *
2008 * For the parsing, we use a 28 states FSM.
2009 *
2010 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002011 * req->p + msg->som = beginning of request
2012 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002013 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002014 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002015 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002016 *
2017 * At end of parsing, we may perform a capture of the error (if any), and
2018 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2019 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2020 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002021 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002022
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002024 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 struct http_txn *txn = &s->txn;
2026 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002027 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002028
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002029 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 +01002030 now_ms, __FUNCTION__,
2031 s,
2032 req,
2033 req->rex, req->wex,
2034 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002035 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002036 req->analysers);
2037
Willy Tarreau52a0c602009-08-16 22:45:38 +02002038 /* we're speaking HTTP here, so let's speak HTTP to the client */
2039 s->srv_error = http_return_srv_error;
2040
Willy Tarreau83e3af02009-12-28 17:39:57 +01002041 /* There's a protected area at the end of the buffer for rewriting
2042 * purposes. We don't want to start to parse the request if the
2043 * protected area is affected, because we may have to move processed
2044 * data later, which is much more complicated.
2045 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002046 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002047 if ((txn->flags & TX_NOT_FIRST) &&
2048 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002049 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002050 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002051 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002052 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2053 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002054 /* some data has still not left the buffer, wake us once that's done */
2055 buffer_dont_connect(req);
2056 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2057 return 0;
2058 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002059 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002060 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002061 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002062 }
2063
Willy Tarreau065e8332010-01-08 00:30:20 +01002064 /* Note that we have the same problem with the response ; we
2065 * may want to send a redirect, error or anything which requires
2066 * some spare space. So we'll ensure that we have at least
2067 * maxrewrite bytes available in the response buffer before
2068 * processing that one. This will only affect pipelined
2069 * keep-alive requests.
2070 */
2071 if ((txn->flags & TX_NOT_FIRST) &&
2072 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002073 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 +01002074 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 +01002075 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002076 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2077 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002078 /* don't let a connection request be initiated */
2079 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002080 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002081 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002082 return 0;
2083 }
2084 }
2085
Willy Tarreaua458b672012-03-05 11:17:50 +01002086 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002087 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002088 }
2089
Willy Tarreau59234e92008-11-30 23:51:27 +01002090 /* 1: we might have to print this header in debug mode */
2091 if (unlikely((global.mode & MODE_DEBUG) &&
2092 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002093 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002094 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002096
Willy Tarreauea1175a2012-03-05 15:52:30 +01002097 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002098 eol = sol + msg->sl.rq.l;
2099 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002100
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 sol += hdr_idx_first_pos(&txn->hdr_idx);
2102 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 while (cur_idx) {
2105 eol = sol + txn->hdr_idx.v[cur_idx].len;
2106 debug_hdr("clihdr", s, sol, eol);
2107 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2108 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002109 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 }
2111
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 /*
2114 * Now we quickly check if we have found a full valid request.
2115 * If not so, we check the FD and buffer states before leaving.
2116 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002117 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002118 * requests are checked first. When waiting for a second request
2119 * on a keep-alive session, if we encounter and error, close, t/o,
2120 * we note the error in the session flags but don't set any state.
2121 * Since the error will be noted there, it will not be counted by
2122 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002123 * Last, we may increase some tracked counters' http request errors on
2124 * the cases that are deliberately the client's fault. For instance,
2125 * a timeout or connection reset is not counted as an error. However
2126 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002128
Willy Tarreau655dce92009-11-08 13:10:58 +01002129 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002130 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002131 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002132 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002133 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002134 session_inc_http_req_ctr(s);
2135 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002136 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002138 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002139
Willy Tarreau59234e92008-11-30 23:51:27 +01002140 /* 1: Since we are in header mode, if there's no space
2141 * left for headers, we won't be able to free more
2142 * later, so the session will never terminate. We
2143 * must terminate it now.
2144 */
2145 if (unlikely(req->flags & BF_FULL)) {
2146 /* FIXME: check if URI is set and return Status
2147 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002148 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002149 session_inc_http_req_ctr(s);
2150 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002151 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002152 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002153 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002154 goto return_bad_req;
2155 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002156
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 /* 2: have we encountered a read error ? */
2158 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002159 if (!(s->flags & SN_ERR_MASK))
2160 s->flags |= SN_ERR_CLICL;
2161
Willy Tarreaufcffa692010-01-10 14:21:19 +01002162 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002163 goto failed_keep_alive;
2164
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002166 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002167 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002168 session_inc_http_err_ctr(s);
2169 }
2170
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 msg->msg_state = HTTP_MSG_ERROR;
2172 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002173
Willy Tarreauda7ff642010-06-23 11:44:09 +02002174 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002175 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002176 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002177 if (s->listener->counters)
2178 s->listener->counters->failed_req++;
2179
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 if (!(s->flags & SN_FINST_MASK))
2181 s->flags |= SN_FINST_R;
2182 return 0;
2183 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 /* 3: has the read timeout expired ? */
2186 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002187 if (!(s->flags & SN_ERR_MASK))
2188 s->flags |= SN_ERR_CLITO;
2189
Willy Tarreaufcffa692010-01-10 14:21:19 +01002190 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002191 goto failed_keep_alive;
2192
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002194 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002195 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002196 session_inc_http_err_ctr(s);
2197 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 txn->status = 408;
2199 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2200 msg->msg_state = HTTP_MSG_ERROR;
2201 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002202
Willy Tarreauda7ff642010-06-23 11:44:09 +02002203 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002204 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002205 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002206 if (s->listener->counters)
2207 s->listener->counters->failed_req++;
2208
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 if (!(s->flags & SN_FINST_MASK))
2210 s->flags |= SN_FINST_R;
2211 return 0;
2212 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002213
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 /* 4: have we encountered a close ? */
2215 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002216 if (!(s->flags & SN_ERR_MASK))
2217 s->flags |= SN_ERR_CLICL;
2218
Willy Tarreaufcffa692010-01-10 14:21:19 +01002219 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002220 goto failed_keep_alive;
2221
Willy Tarreau4076a152009-04-02 15:18:36 +02002222 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002223 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002224 txn->status = 400;
2225 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2226 msg->msg_state = HTTP_MSG_ERROR;
2227 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002228
Willy Tarreauda7ff642010-06-23 11:44:09 +02002229 session_inc_http_err_ctr(s);
2230 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002231 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002232 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002233 if (s->listener->counters)
2234 s->listener->counters->failed_req++;
2235
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 if (!(s->flags & SN_FINST_MASK))
2237 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002238 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002239 }
2240
Willy Tarreau520d95e2009-09-19 21:04:57 +02002241 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002242 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002243 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002244#ifdef TCP_QUICKACK
2245 if (s->listener->options & LI_O_NOQUICKACK) {
2246 /* We need more data, we have to re-enable quick-ack in case we
2247 * previously disabled it, otherwise we might cause the client
2248 * to delay next data.
2249 */
2250 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2251 }
2252#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002253
Willy Tarreaufcffa692010-01-10 14:21:19 +01002254 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2255 /* If the client starts to talk, let's fall back to
2256 * request timeout processing.
2257 */
2258 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002259 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002260 }
2261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002263 if (!tick_isset(req->analyse_exp)) {
2264 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2265 (txn->flags & TX_WAIT_NEXT_RQ) &&
2266 tick_isset(s->be->timeout.httpka))
2267 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2268 else
2269 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2270 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 /* we're not ready yet */
2273 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002274
2275 failed_keep_alive:
2276 /* Here we process low-level errors for keep-alive requests. In
2277 * short, if the request is not the first one and it experiences
2278 * a timeout, read error or shutdown, we just silently close so
2279 * that the client can try again.
2280 */
2281 txn->status = 0;
2282 msg->msg_state = HTTP_MSG_RQBEFORE;
2283 req->analysers = 0;
2284 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002285 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002286 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002287 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002289
Willy Tarreaud787e662009-07-07 10:14:51 +02002290 /* OK now we have a complete HTTP request with indexed headers. Let's
2291 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002292 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2293 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002294 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002295 * byte after the last LF. msg->sov points to the first byte of data.
2296 * msg->eol cannot be trusted because it may have been left uninitialized
2297 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002298 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002299
Willy Tarreauda7ff642010-06-23 11:44:09 +02002300 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002301 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2302
Willy Tarreaub16a5742010-01-10 14:46:16 +01002303 if (txn->flags & TX_WAIT_NEXT_RQ) {
2304 /* kill the pending keep-alive timeout */
2305 txn->flags &= ~TX_WAIT_NEXT_RQ;
2306 req->analyse_exp = TICK_ETERNITY;
2307 }
2308
2309
Willy Tarreaud787e662009-07-07 10:14:51 +02002310 /* Maybe we found in invalid header name while we were configured not
2311 * to block on that, so we have to capture it now.
2312 */
2313 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002314 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 /*
2317 * 1: identify the method
2318 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002319 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002320
2321 /* we can make use of server redirect on GET and HEAD */
2322 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2323 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 /*
2326 * 2: check if the URI matches the monitor_uri.
2327 * We have to do this for every request which gets in, because
2328 * the monitor-uri is defined by the frontend.
2329 */
2330 if (unlikely((s->fe->monitor_uri_len != 0) &&
2331 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002332 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 s->fe->monitor_uri,
2334 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002335 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002337 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002339
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002341 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002344 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2345 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 ret = acl_pass(ret);
2348 if (cond->pol == ACL_COND_UNLESS)
2349 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 if (ret) {
2352 /* we fail this request, let's return 503 service unavail */
2353 txn->status = 503;
2354 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2355 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002356 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002357 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /* nothing to fail, let's reply normaly */
2360 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002361 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 goto return_prx_cond;
2363 }
2364
2365 /*
2366 * 3: Maybe we have to copy the original REQURI for the logs ?
2367 * Note: we cannot log anymore if the request has been
2368 * classified as invalid.
2369 */
2370 if (unlikely(s->logs.logwait & LW_REQ)) {
2371 /* we have a complete HTTP request that we must log */
2372 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2373 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002374
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 if (urilen >= REQURI_LEN)
2376 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002377 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002379
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 if (!(s->logs.logwait &= ~LW_REQ))
2381 s->do_log(s);
2382 } else {
2383 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 }
Willy Tarreau06619262006-12-17 08:37:22 +01002386
William Lallemanda73203e2012-03-12 12:48:57 +01002387 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2388 s->unique_id = pool_alloc2(pool2_uniqueid);
2389 }
2390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002392 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2393 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002394
Willy Tarreau5b154472009-12-21 20:11:07 +01002395 /* ... and check if the request is HTTP/1.1 or above */
2396 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002397 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2398 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2399 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002400 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002401
2402 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002403 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002404
Willy Tarreau88d349d2010-01-25 12:15:43 +01002405 /* if the frontend has "option http-use-proxy-header", we'll check if
2406 * we have what looks like a proxied connection instead of a connection,
2407 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2408 * Note that this is *not* RFC-compliant, however browsers and proxies
2409 * happen to do that despite being non-standard :-(
2410 * We consider that a request not beginning with either '/' or '*' is
2411 * a proxied connection, which covers both "scheme://location" and
2412 * CONNECT ip:port.
2413 */
2414 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002415 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002416 txn->flags |= TX_USE_PX_CONN;
2417
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002418 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002419 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002420
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002422 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002423 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002424 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002425
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426 /* 6: determine the transfer-length.
2427 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2428 * the presence of a message-body in a REQUEST and its transfer length
2429 * must be determined that way (in order of precedence) :
2430 * 1. The presence of a message-body in a request is signaled by the
2431 * inclusion of a Content-Length or Transfer-Encoding header field
2432 * in the request's header fields. When a request message contains
2433 * both a message-body of non-zero length and a method that does
2434 * not define any semantics for that request message-body, then an
2435 * origin server SHOULD either ignore the message-body or respond
2436 * with an appropriate error message (e.g., 413). A proxy or
2437 * gateway, when presented the same request, SHOULD either forward
2438 * the request inbound with the message- body or ignore the
2439 * message-body when determining a response.
2440 *
2441 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2442 * and the "chunked" transfer-coding (Section 6.2) is used, the
2443 * transfer-length is defined by the use of this transfer-coding.
2444 * If a Transfer-Encoding header field is present and the "chunked"
2445 * transfer-coding is not present, the transfer-length is defined
2446 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002447 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 * 3. If a Content-Length header field is present, its decimal value in
2449 * OCTETs represents both the entity-length and the transfer-length.
2450 * If a message is received with both a Transfer-Encoding header
2451 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002453 * 4. By the server closing the connection. (Closing the connection
2454 * cannot be used to indicate the end of a request body, since that
2455 * would leave no possibility for the server to send back a response.)
2456 *
2457 * Whenever a transfer-coding is applied to a message-body, the set of
2458 * transfer-codings MUST include "chunked", unless the message indicates
2459 * it is terminated by closing the connection. When the "chunked"
2460 * transfer-coding is used, it MUST be the last transfer-coding applied
2461 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 */
2463
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002464 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002465 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002467 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002468 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002469 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002470 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2471 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002472 /* bad transfer-encoding (chunked followed by something else) */
2473 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002474 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002475 break;
2476 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002477 }
2478
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002480 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002481 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482 signed long long cl;
2483
Willy Tarreauad14f752011-09-02 20:33:27 +02002484 if (!ctx.vlen) {
2485 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002486 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002487 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488
Willy Tarreauad14f752011-09-02 20:33:27 +02002489 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2490 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002491 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002492 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493
Willy Tarreauad14f752011-09-02 20:33:27 +02002494 if (cl < 0) {
2495 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002496 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002497 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002499 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002500 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002502 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002504 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002505 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002506 }
2507
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002508 /* bodyless requests have a known length */
2509 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002510 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002511
Willy Tarreaud787e662009-07-07 10:14:51 +02002512 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002513 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002514 req->analyse_exp = TICK_ETERNITY;
2515 return 1;
2516
2517 return_bad_req:
2518 /* We centralize bad requests processing here */
2519 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2520 /* we detected a parsing error. We want to archive this request
2521 * in the dedicated proxy area for later troubleshooting.
2522 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002523 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 }
2525
2526 txn->req.msg_state = HTTP_MSG_ERROR;
2527 txn->status = 400;
2528 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002529
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002530 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002531 if (s->listener->counters)
2532 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002533
2534 return_prx_cond:
2535 if (!(s->flags & SN_ERR_MASK))
2536 s->flags |= SN_ERR_PRXCOND;
2537 if (!(s->flags & SN_FINST_MASK))
2538 s->flags |= SN_FINST_R;
2539
2540 req->analysers = 0;
2541 req->analyse_exp = TICK_ETERNITY;
2542 return 0;
2543}
2544
Cyril Bonté70be45d2010-10-12 00:14:35 +02002545/* We reached the stats page through a POST request.
2546 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002547 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002548 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002549int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002551 struct proxy *px = NULL;
2552 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002553
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002554 char key[LINESIZE];
2555 int action = ST_ADM_ACTION_NONE;
2556 int reprocess = 0;
2557
2558 int total_servers = 0;
2559 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560
2561 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002562 char *st_cur_param = NULL;
2563 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002564
Willy Tarreauea1175a2012-03-05 15:52:30 +01002565 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002566 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002567
2568 cur_param = next_param = end_params;
2569
Cyril Bonté23b39d92011-02-10 22:54:44 +01002570 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002571 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002572 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002573 return 1;
2574 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002575 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002576 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002577 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002578 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579 }
2580
2581 *end_params = '\0';
2582
Willy Tarreau295a8372011-03-10 11:25:07 +01002583 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584
2585 /*
2586 * Parse the parameters in reverse order to only store the last value.
2587 * From the html form, the backend and the action are at the end.
2588 */
2589 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002590 char *value;
2591 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002592
2593 cur_param--;
2594 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002595 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002597 poffset = (cur_param != first_param ? 1 : 0);
2598 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2599 if ((plen > 0) && (plen <= sizeof(key))) {
2600 strncpy(key, cur_param + poffset, plen);
2601 key[plen - 1] = '\0';
2602 } else {
2603 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2604 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002605 }
2606
2607 /* Parse the value */
2608 value = key;
2609 while (*value != '\0' && *value != '=') {
2610 value++;
2611 }
2612 if (*value == '=') {
2613 /* Ok, a value is found, we can mark the end of the key */
2614 *value++ = '\0';
2615 }
2616
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002617 if (!url_decode(key) || !url_decode(value))
2618 break;
2619
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002621 if (!px && (strcmp(key, "b") == 0)) {
2622 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2623 /* the backend name is unknown or ambiguous (duplicate names) */
2624 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2625 goto out;
2626 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002627 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002628 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002629 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002630 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002631 }
2632 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002633 action = ST_ADM_ACTION_ENABLE;
2634 }
2635 else {
2636 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2637 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002638 }
2639 }
2640 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002641 if (!(px && action)) {
2642 /*
2643 * Indicates that we'll need to reprocess the parameters
2644 * as soon as backend and action are known
2645 */
2646 if (!reprocess) {
2647 st_cur_param = cur_param;
2648 st_next_param = next_param;
2649 }
2650 reprocess = 1;
2651 }
2652 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002653 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002654 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002655 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002656 /* Not already in maintenance, we can change the server state */
2657 sv->state |= SRV_MAINTAIN;
2658 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002659 altered_servers++;
2660 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002661 }
2662 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002663 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002664 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002665 /* Already in maintenance, we can change the server state */
2666 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002667 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002668 altered_servers++;
2669 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002670 }
2671 break;
2672 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002673 } else {
2674 /* the server name is unknown or ambiguous (duplicate names) */
2675 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002676 }
2677 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002678 if (reprocess && px && action) {
2679 /* Now, we know the backend and the action chosen by the user.
2680 * We can safely restart from the first server parameter
2681 * to reprocess them
2682 */
2683 cur_param = st_cur_param;
2684 next_param = st_next_param;
2685 reprocess = 0;
2686 goto reprocess_servers;
2687 }
2688
Cyril Bonté70be45d2010-10-12 00:14:35 +02002689 next_param = cur_param;
2690 }
2691 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002692
2693 if (total_servers == 0) {
2694 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2695 }
2696 else if (altered_servers == 0) {
2697 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2698 }
2699 else if (altered_servers == total_servers) {
2700 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2701 }
2702 else {
2703 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2704 }
2705 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002706 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002707}
2708
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002709/* returns a pointer to the first rule which forbids access (deny or http_auth),
2710 * or NULL if everything's OK.
2711 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002712static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002713http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2714{
Willy Tarreauff011f22011-01-06 17:51:27 +01002715 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002716
Willy Tarreauff011f22011-01-06 17:51:27 +01002717 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002718 int ret = 1;
2719
Willy Tarreauff011f22011-01-06 17:51:27 +01002720 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002721 continue;
2722
2723 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002724 if (rule->cond) {
2725 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002726 ret = acl_pass(ret);
2727
Willy Tarreauff011f22011-01-06 17:51:27 +01002728 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002729 ret = !ret;
2730 }
2731
2732 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002733 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734 return NULL; /* no problem */
2735 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002736 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002737 }
2738 }
2739 return NULL;
2740}
2741
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002742/* This stream analyser runs all HTTP request processing which is common to
2743 * frontends and backends, which means blocking ACLs, filters, connection-close,
2744 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002745 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002746 * either needs more data or wants to immediately abort the request (eg: deny,
2747 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002748 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002750{
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 struct http_txn *txn = &s->txn;
2752 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002753 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002754 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002756 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002757 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002758
Willy Tarreau655dce92009-11-08 13:10:58 +01002759 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002760 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002761 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002762 return 0;
2763 }
2764
Willy Tarreau3a816292009-07-07 10:55:49 +02002765 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002766 req->analyse_exp = TICK_ETERNITY;
2767
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002768 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 +02002769 now_ms, __FUNCTION__,
2770 s,
2771 req,
2772 req->rex, req->wex,
2773 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002774 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002775 req->analysers);
2776
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777 /* first check whether we have some ACLs set to block this request */
2778 list_for_each_entry(cond, &px->block_cond, list) {
2779 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002780
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 ret = acl_pass(ret);
2782 if (cond->pol == ACL_COND_UNLESS)
2783 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002784
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002785 if (ret) {
2786 txn->status = 403;
2787 /* let's log the request time */
2788 s->logs.tv_request = now;
2789 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002790 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002794
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002795 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002796 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002797
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002798 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002799 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002800 do_stats = stats_check_uri(s->rep->prod, txn, px);
2801 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002802 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 +01002803 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002804 else
2805 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002806
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002807 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002808 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002809 txn->status = 403;
2810 s->logs.tv_request = now;
2811 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002812 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002813 s->fe->fe_counters.denied_req++;
2814 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2815 s->be->be_counters.denied_req++;
2816 if (s->listener->counters)
2817 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002818 goto return_prx_cond;
2819 }
2820
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821 /* try headers filters */
2822 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002823 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002825
Willy Tarreau59234e92008-11-30 23:51:27 +01002826 /* has the request been denied ? */
2827 if (txn->flags & TX_CLDENY) {
2828 /* no need to go further */
2829 txn->status = 403;
2830 /* let's log the request time */
2831 s->logs.tv_request = now;
2832 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002833 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002834 goto return_prx_cond;
2835 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002836
2837 /* When a connection is tarpitted, we use the tarpit timeout,
2838 * which may be the same as the connect timeout if unspecified.
2839 * If unset, then set it to zero because we really want it to
2840 * eventually expire. We build the tarpit as an analyser.
2841 */
2842 if (txn->flags & TX_CLTARPIT) {
2843 buffer_erase(s->req);
2844 /* wipe the request out so that we can drop the connection early
2845 * if the client closes first.
2846 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002847 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002848 req->analysers = 0; /* remove switching rules etc... */
2849 req->analysers |= AN_REQ_HTTP_TARPIT;
2850 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2851 if (!req->analyse_exp)
2852 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002853 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002854 return 1;
2855 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002856 }
Willy Tarreau06619262006-12-17 08:37:22 +01002857
Willy Tarreau5b154472009-12-21 20:11:07 +01002858 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2859 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002860 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2861 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002862 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2863 * avoid to redo the same work if FE and BE have the same settings (common).
2864 * The method consists in checking if options changed between the two calls
2865 * (implying that either one is non-null, or one of them is non-null and we
2866 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002867 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002868
Willy Tarreaudc008c52010-02-01 16:20:08 +01002869 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2870 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2871 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2872 (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 +01002873 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002874
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002875 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2876 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002877 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002878 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2879 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002880 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002881 tmp = TX_CON_WANT_CLO;
2882
Willy Tarreau5b154472009-12-21 20:11:07 +01002883 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2884 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002885
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002886 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2887 /* parse the Connection header and possibly clean it */
2888 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002889 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002890 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2891 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002892 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002893 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002895 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002896 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002897
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002898 /* check if client or config asks for explicit close in KAL/SCL */
2899 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2900 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2901 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002902 (!(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 +01002903 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002904 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002905 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2907 }
Willy Tarreau78599912009-10-17 20:12:21 +02002908
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002909 /* we can be blocked here because the request needs to be authenticated,
2910 * either to pass or to access stats.
2911 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002912 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002913 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002914 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002915
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002916 if (!realm)
2917 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2918
Willy Tarreau844a7e72010-01-31 21:46:18 +01002919 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002920 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2921 txn->status = 401;
2922 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002923 /* on 401 we still count one error, because normal browsing
2924 * won't significantly increase the counter but brute force
2925 * attempts will.
2926 */
2927 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002928 goto return_prx_cond;
2929 }
2930
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002931 /* add request headers from the rule sets in the same order */
2932 list_for_each_entry(wl, &px->req_add, list) {
2933 if (wl->cond) {
2934 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2935 ret = acl_pass(ret);
2936 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2937 ret = !ret;
2938 if (!ret)
2939 continue;
2940 }
2941
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002942 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002943 goto return_bad_req;
2944 }
2945
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002946 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002947 struct stats_admin_rule *stats_admin_rule;
2948
2949 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002950 * FIXME!!! that one is rather dangerous, we want to
2951 * make it follow standard rules (eg: clear req->analysers).
2952 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002953
Cyril Bonté474be412010-10-12 00:14:36 +02002954 /* now check whether we have some admin rules for this request */
2955 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2956 int ret = 1;
2957
2958 if (stats_admin_rule->cond) {
2959 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2960 ret = acl_pass(ret);
2961 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2962 ret = !ret;
2963 }
2964
2965 if (ret) {
2966 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002967 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002968 break;
2969 }
2970 }
2971
Cyril Bonté70be45d2010-10-12 00:14:35 +02002972 /* Was the status page requested with a POST ? */
2973 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002974 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002975 if (msg->msg_state < HTTP_MSG_100_SENT) {
2976 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2977 * send an HTTP/1.1 100 Continue intermediate response.
2978 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002979 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002980 struct hdr_ctx ctx;
2981 ctx.idx = 0;
2982 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002983 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002984 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2985 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2986 }
2987 }
2988 msg->msg_state = HTTP_MSG_100_SENT;
2989 s->logs.tv_request = now; /* update the request timer to reflect full request */
2990 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002991 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002992 /* we need more data */
2993 req->analysers |= an_bit;
2994 buffer_dont_connect(req);
2995 return 0;
2996 }
Cyril Bonté474be412010-10-12 00:14:36 +02002997 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002998 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002999 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003000 }
3001
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003002 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003003 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003004 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003005 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003006 s->rep->prod->applet.private = s;
3007 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003008 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003009 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3010 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003011
3012 return 0;
3013
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003015
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 /* check whether we have some ACLs set to redirect this request */
3017 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003018 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003019
Willy Tarreauf285f542010-01-03 20:03:03 +01003020 if (rule->cond) {
3021 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3022 ret = acl_pass(ret);
3023 if (rule->cond->pol == ACL_COND_UNLESS)
3024 ret = !ret;
3025 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003028 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003030
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 /* build redirect message */
3032 switch(rule->code) {
3033 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 msg_fmt = HTTP_303;
3035 break;
3036 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 msg_fmt = HTTP_301;
3038 break;
3039 case 302:
3040 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 msg_fmt = HTTP_302;
3042 break;
3043 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003044
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003045 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 switch(rule->type) {
3049 case REDIRECT_TYPE_PREFIX: {
3050 const char *path;
3051 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 path = http_get_path(txn);
3054 /* build message using path */
3055 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003056 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 +02003057 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3058 int qs = 0;
3059 while (qs < pathlen) {
3060 if (path[qs] == '?') {
3061 pathlen = qs;
3062 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003063 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003065 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 } else {
3068 path = "/";
3069 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003070 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003072 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 goto return_bad_req;
3074
3075 /* add prefix. Note that if prefix == "/", we don't want to
3076 * add anything, otherwise it makes it hard for the user to
3077 * configure a self-redirection.
3078 */
3079 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003080 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3081 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082 }
3083
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 /* add path */
3085 memcpy(rdr.str + rdr.len, path, pathlen);
3086 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003087
3088 /* append a slash at the end of the location is needed and missing */
3089 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3090 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3091 if (rdr.len > rdr.size - 5)
3092 goto return_bad_req;
3093 rdr.str[rdr.len] = '/';
3094 rdr.len++;
3095 }
3096
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003097 break;
3098 }
3099 case REDIRECT_TYPE_LOCATION:
3100 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003101 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003102 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 /* add location */
3105 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3106 rdr.len += rule->rdr_len;
3107 break;
3108 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003109
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 if (rule->cookie_len) {
3111 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3112 rdr.len += 14;
3113 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3114 rdr.len += rule->cookie_len;
3115 memcpy(rdr.str + rdr.len, "\r\n", 2);
3116 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003117 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003118
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003119 /* add end of headers and the keep-alive/close status.
3120 * We may choose to set keep-alive if the Location begins
3121 * with a slash, because the client will come back to the
3122 * same server.
3123 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 txn->status = rule->code;
3125 /* let's log the request time */
3126 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003127
3128 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003129 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3130 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003131 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3132 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3133 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003134 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003135 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3136 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3137 rdr.len += 30;
3138 } else {
3139 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3140 rdr.len += 24;
3141 }
Willy Tarreau75661452010-01-10 10:35:01 +01003142 }
3143 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3144 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003145 buffer_write(req->prod->ob, rdr.str, rdr.len);
3146 /* "eat" the request */
3147 buffer_ignore(req, msg->sov - msg->som);
3148 msg->som = msg->sov;
3149 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003150 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3151 txn->req.msg_state = HTTP_MSG_CLOSED;
3152 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003153 break;
3154 } else {
3155 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003156 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3157 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3158 rdr.len += 29;
3159 } else {
3160 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3161 rdr.len += 23;
3162 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003163 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003164 goto return_prx_cond;
3165 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003166 }
3167 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003168
Willy Tarreau2be39392010-01-03 17:24:51 +01003169 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3170 * If this happens, then the data will not come immediately, so we must
3171 * send all what we have without waiting. Note that due to the small gain
3172 * in waiting for the body of the request, it's easier to simply put the
3173 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3174 * itself once used.
3175 */
3176 req->flags |= BF_SEND_DONTWAIT;
3177
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003178 /* that's OK for us now, let's move on to next analysers */
3179 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003181 return_bad_req:
3182 /* We centralize bad requests processing here */
3183 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3184 /* we detected a parsing error. We want to archive this request
3185 * in the dedicated proxy area for later troubleshooting.
3186 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003187 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003188 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003190 txn->req.msg_state = HTTP_MSG_ERROR;
3191 txn->status = 400;
3192 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003193
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003194 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003195 if (s->listener->counters)
3196 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 return_prx_cond:
3199 if (!(s->flags & SN_ERR_MASK))
3200 s->flags |= SN_ERR_PRXCOND;
3201 if (!(s->flags & SN_FINST_MASK))
3202 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 req->analysers = 0;
3205 req->analyse_exp = TICK_ETERNITY;
3206 return 0;
3207}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003208
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003209/* This function performs all the processing enabled for the current request.
3210 * It returns 1 if the processing can continue on next analysers, or zero if it
3211 * needs more data, encounters an error, or wants to immediately abort the
3212 * request. It relies on buffers flags, and updates s->req->analysers.
3213 */
3214int http_process_request(struct session *s, struct buffer *req, int an_bit)
3215{
3216 struct http_txn *txn = &s->txn;
3217 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003218
Willy Tarreau655dce92009-11-08 13:10:58 +01003219 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003220 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003221 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003222 return 0;
3223 }
3224
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003225 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 +02003226 now_ms, __FUNCTION__,
3227 s,
3228 req,
3229 req->rex, req->wex,
3230 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003231 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003232 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003233
Willy Tarreau59234e92008-11-30 23:51:27 +01003234 /*
3235 * Right now, we know that we have processed the entire headers
3236 * and that unwanted requests have been filtered out. We can do
3237 * whatever we want with the remaining request. Also, now we
3238 * may have separate values for ->fe, ->be.
3239 */
Willy Tarreau06619262006-12-17 08:37:22 +01003240
Willy Tarreau59234e92008-11-30 23:51:27 +01003241 /*
3242 * If HTTP PROXY is set we simply get remote server address
3243 * parsing incoming request.
3244 */
3245 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003246 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 +01003247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003248
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003250 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 * Note that doing so might move headers in the request, but
3252 * the fields will stay coherent and the URI will not move.
3253 * This should only be performed in the backend.
3254 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003255 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3257 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003258
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003260 * 8: the appsession cookie was looked up very early in 1.2,
3261 * so let's do the same now.
3262 */
3263
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003264 /* It needs to look into the URI unless persistence must be ignored */
3265 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003266 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 +01003267 }
3268
William Lallemanda73203e2012-03-12 12:48:57 +01003269 /* add unique-id if "header-unique-id" is specified */
3270
3271 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3272 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3273
3274 if (s->fe->header_unique_id && s->unique_id) {
3275 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3276 if (ret < 0 || ret > global.tune.bufsize)
3277 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003278 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003279 goto return_bad_req;
3280 }
3281
Cyril Bontéb21570a2009-11-29 20:04:48 +01003282 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003283 * 9: add X-Forwarded-For if either the frontend or the backend
3284 * asks for it.
3285 */
3286 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003287 struct hdr_ctx ctx = { .idx = 0 };
3288
3289 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003290 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 +02003291 /* The header is set to be added only if none is present
3292 * and we found it, so don't do anything.
3293 */
3294 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003295 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 /* Add an X-Forwarded-For header unless the source IP is
3297 * in the 'except' network range.
3298 */
3299 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003300 (((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 +01003301 != s->fe->except_net.s_addr) &&
3302 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003303 (((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 +01003304 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003305 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003307 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003308
3309 /* Note: we rely on the backend to get the header name to be used for
3310 * x-forwarded-for, because the header is really meant for the backends.
3311 * However, if the backend did not specify any option, we have to rely
3312 * on the frontend's header name.
3313 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003314 if (s->be->fwdfor_hdr_len) {
3315 len = s->be->fwdfor_hdr_len;
3316 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003317 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003318 len = s->fe->fwdfor_hdr_len;
3319 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003320 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003321 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003322
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003323 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003324 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003325 }
3326 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003327 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 /* FIXME: for the sake of completeness, we should also support
3329 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003330 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 int len;
3332 char pn[INET6_ADDRSTRLEN];
3333 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003334 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003335 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003336
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 /* Note: we rely on the backend to get the header name to be used for
3338 * x-forwarded-for, because the header is really meant for the backends.
3339 * However, if the backend did not specify any option, we have to rely
3340 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003341 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003342 if (s->be->fwdfor_hdr_len) {
3343 len = s->be->fwdfor_hdr_len;
3344 memcpy(trash, s->be->fwdfor_hdr_name, len);
3345 } else {
3346 len = s->fe->fwdfor_hdr_len;
3347 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003348 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003350
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003351 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 goto return_bad_req;
3353 }
3354 }
3355
3356 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003357 * 10: add X-Original-To if either the frontend or the backend
3358 * asks for it.
3359 */
3360 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3361
3362 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003363 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003364 /* Add an X-Original-To header unless the destination IP is
3365 * in the 'except' network range.
3366 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003367 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003368
Willy Tarreau6471afb2011-09-23 10:54:59 +02003369 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003370 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003371 (((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 +02003372 != s->fe->except_to.s_addr) &&
3373 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003374 (((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 +02003375 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003376 int len;
3377 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003378 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003379
3380 /* Note: we rely on the backend to get the header name to be used for
3381 * x-original-to, because the header is really meant for the backends.
3382 * However, if the backend did not specify any option, we have to rely
3383 * on the frontend's header name.
3384 */
3385 if (s->be->orgto_hdr_len) {
3386 len = s->be->orgto_hdr_len;
3387 memcpy(trash, s->be->orgto_hdr_name, len);
3388 } else {
3389 len = s->fe->orgto_hdr_len;
3390 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003391 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003392 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3393
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003394 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003395 goto return_bad_req;
3396 }
3397 }
3398 }
3399
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003400 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3401 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003402 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003403 unsigned int want_flags = 0;
3404
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003405 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003406 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3407 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3408 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003409 want_flags |= TX_CON_CLO_SET;
3410 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003411 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3412 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3413 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003414 want_flags |= TX_CON_KAL_SET;
3415 }
3416
3417 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003418 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003419 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003420
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003421
Willy Tarreau522d6c02009-12-06 18:49:18 +01003422 /* If we have no server assigned yet and we're balancing on url_param
3423 * with a POST request, we may be interested in checking the body for
3424 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003425 */
3426 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3427 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003428 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003429 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430 buffer_dont_connect(req);
3431 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003434 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003435 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003436#ifdef TCP_QUICKACK
3437 /* We expect some data from the client. Unless we know for sure
3438 * we already have a full request, we have to re-enable quick-ack
3439 * in case we previously disabled it, otherwise we might cause
3440 * the client to delay further data.
3441 */
3442 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003443 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003444 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003445 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3446#endif
3447 }
Willy Tarreau03945942009-12-22 16:50:27 +01003448
Willy Tarreau59234e92008-11-30 23:51:27 +01003449 /*************************************************************
3450 * OK, that's finished for the headers. We have done what we *
3451 * could. Let's switch to the DATA state. *
3452 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003453 req->analyse_exp = TICK_ETERNITY;
3454 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003455
Willy Tarreau59234e92008-11-30 23:51:27 +01003456 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 /* OK let's go on with the BODY now */
3458 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003459
Willy Tarreau59234e92008-11-30 23:51:27 +01003460 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003461 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003462 /* we detected a parsing error. We want to archive this request
3463 * in the dedicated proxy area for later troubleshooting.
3464 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003465 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003466 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003467
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 txn->req.msg_state = HTTP_MSG_ERROR;
3469 txn->status = 400;
3470 req->analysers = 0;
3471 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003472
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003473 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003474 if (s->listener->counters)
3475 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003476
Willy Tarreau59234e92008-11-30 23:51:27 +01003477 if (!(s->flags & SN_ERR_MASK))
3478 s->flags |= SN_ERR_PRXCOND;
3479 if (!(s->flags & SN_FINST_MASK))
3480 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003481 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003482}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003483
Willy Tarreau60b85b02008-11-30 23:28:40 +01003484/* This function is an analyser which processes the HTTP tarpit. It always
3485 * returns zero, at the beginning because it prevents any other processing
3486 * from occurring, and at the end because it terminates the request.
3487 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003488int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489{
3490 struct http_txn *txn = &s->txn;
3491
3492 /* This connection is being tarpitted. The CLIENT side has
3493 * already set the connect expiration date to the right
3494 * timeout. We just have to check that the client is still
3495 * there and that the timeout has not expired.
3496 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003497 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003498 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3499 !tick_is_expired(req->analyse_exp, now_ms))
3500 return 0;
3501
3502 /* We will set the queue timer to the time spent, just for
3503 * logging purposes. We fake a 500 server error, so that the
3504 * attacker will not suspect his connection has been tarpitted.
3505 * It will not cause trouble to the logs because we can exclude
3506 * the tarpitted connections by filtering on the 'PT' status flags.
3507 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003508 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3509
3510 txn->status = 500;
3511 if (req->flags != BF_READ_ERROR)
3512 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3513
3514 req->analysers = 0;
3515 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003516
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003517 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003518 if (s->listener->counters)
3519 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003520
Willy Tarreau60b85b02008-11-30 23:28:40 +01003521 if (!(s->flags & SN_ERR_MASK))
3522 s->flags |= SN_ERR_PRXCOND;
3523 if (!(s->flags & SN_FINST_MASK))
3524 s->flags |= SN_FINST_T;
3525 return 0;
3526}
3527
Willy Tarreaud34af782008-11-30 23:36:37 +01003528/* This function is an analyser which processes the HTTP request body. It looks
3529 * for parameters to be used for the load balancing algorithm (url_param). It
3530 * must only be called after the standard HTTP request processing has occurred,
3531 * because it expects the request to be parsed. It returns zero if it needs to
3532 * read more data, or 1 once it has completed its analysis.
3533 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003534int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003535{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003536 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003537 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003538 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003539
3540 /* We have to parse the HTTP request body to find any required data.
3541 * "balance url_param check_post" should have been the only way to get
3542 * into this. We were brought here after HTTP header analysis, so all
3543 * related structures are ready.
3544 */
3545
Willy Tarreau522d6c02009-12-06 18:49:18 +01003546 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3547 goto missing_data;
3548
3549 if (msg->msg_state < HTTP_MSG_100_SENT) {
3550 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3551 * send an HTTP/1.1 100 Continue intermediate response.
3552 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003553 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003554 struct hdr_ctx ctx;
3555 ctx.idx = 0;
3556 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003557 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003558 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3559 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3560 }
3561 }
3562 msg->msg_state = HTTP_MSG_100_SENT;
3563 }
3564
3565 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003566 /* we have msg->sov which points to the first byte of message body.
3567 * msg->som still points to the beginning of the message. We must
3568 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003569 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003570 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003571
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003572 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003573 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3574 else
3575 msg->msg_state = HTTP_MSG_DATA;
3576 }
3577
3578 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003579 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003580 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003581 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003582 */
3583 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003584
Willy Tarreau115acb92009-12-26 13:56:06 +01003585 if (!ret)
3586 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003587 else if (ret < 0) {
3588 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003590 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003591 }
3592
Willy Tarreaud98cf932009-12-27 22:54:55 +01003593 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003594 * We have the first data byte is in msg->sov. We're waiting for at
3595 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003596 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003597
Willy Tarreau124d9912011-03-01 20:30:48 +01003598 if (msg->body_len < limit)
3599 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003600
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003601 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602 goto http_end;
3603
3604 missing_data:
3605 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003606 if (req->flags & BF_FULL) {
3607 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003608 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003609 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003610
Willy Tarreau522d6c02009-12-06 18:49:18 +01003611 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3612 txn->status = 408;
3613 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003614
3615 if (!(s->flags & SN_ERR_MASK))
3616 s->flags |= SN_ERR_CLITO;
3617 if (!(s->flags & SN_FINST_MASK))
3618 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003619 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003620 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621
3622 /* we get here if we need to wait for more data */
3623 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003624 /* Not enough data. We'll re-use the http-request
3625 * timeout here. Ideally, we should set the timeout
3626 * relative to the accept() date. We just set the
3627 * request timeout once at the beginning of the
3628 * request.
3629 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003630 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003631 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003632 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003633 return 0;
3634 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003635
3636 http_end:
3637 /* The situation will not evolve, so let's give up on the analysis. */
3638 s->logs.tv_request = now; /* update the request timer to reflect full request */
3639 req->analysers &= ~an_bit;
3640 req->analyse_exp = TICK_ETERNITY;
3641 return 1;
3642
3643 return_bad_req: /* let's centralize all bad requests */
3644 txn->req.msg_state = HTTP_MSG_ERROR;
3645 txn->status = 400;
3646 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3647
Willy Tarreau79ebac62010-06-07 13:47:49 +02003648 if (!(s->flags & SN_ERR_MASK))
3649 s->flags |= SN_ERR_PRXCOND;
3650 if (!(s->flags & SN_FINST_MASK))
3651 s->flags |= SN_FINST_R;
3652
Willy Tarreau522d6c02009-12-06 18:49:18 +01003653 return_err_msg:
3654 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003655 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003656 if (s->listener->counters)
3657 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003658 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003659}
3660
Willy Tarreau45c0d982012-03-09 12:11:57 +01003661/* send a server's name with an outgoing request over an established connection */
3662int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003663
3664 struct hdr_ctx ctx;
3665
Mark Lamourinec2247f02012-01-04 13:02:01 -05003666 char *hdr_name = be->server_id_hdr_name;
3667 int hdr_name_len = be->server_id_hdr_len;
3668
3669 char *hdr_val;
3670
William Lallemandd9e90662012-01-30 17:27:17 +01003671 ctx.idx = 0;
3672
Willy Tarreau45c0d982012-03-09 12:11:57 +01003673 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 -05003674 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003675 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003676 }
3677
3678 /* Add the new header requested with the server value */
3679 hdr_val = trash;
3680 memcpy(hdr_val, hdr_name, hdr_name_len);
3681 hdr_val += hdr_name_len;
3682 *hdr_val++ = ':';
3683 *hdr_val++ = ' ';
3684 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003685 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003686
3687 return 0;
3688}
3689
Willy Tarreau610ecce2010-01-04 21:15:02 +01003690/* Terminate current transaction and prepare a new one. This is very tricky
3691 * right now but it works.
3692 */
3693void http_end_txn_clean_session(struct session *s)
3694{
3695 /* FIXME: We need a more portable way of releasing a backend's and a
3696 * server's connections. We need a safer way to reinitialize buffer
3697 * flags. We also need a more accurate method for computing per-request
3698 * data.
3699 */
3700 http_silent_debug(__LINE__, s);
3701
3702 s->req->cons->flags |= SI_FL_NOLINGER;
3703 s->req->cons->shutr(s->req->cons);
3704 s->req->cons->shutw(s->req->cons);
3705
3706 http_silent_debug(__LINE__, s);
3707
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003708 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003710 if (unlikely(s->srv_conn))
3711 sess_change_server(s, NULL);
3712 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003713
3714 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3715 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003716 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717
3718 if (s->txn.status) {
3719 int n;
3720
3721 n = s->txn.status / 100;
3722 if (n < 1 || n > 5)
3723 n = 0;
3724
3725 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003726 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
Willy Tarreau24657792010-02-26 10:30:28 +01003728 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003730 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 }
3732
3733 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003734 s->logs.bytes_in -= s->req->i;
3735 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
3737 /* let's do a final log if we need it */
3738 if (s->logs.logwait &&
3739 !(s->flags & SN_MONITOR) &&
3740 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3741 s->do_log(s);
3742 }
3743
3744 s->logs.accept_date = date; /* user-visible date for logging */
3745 s->logs.tv_accept = now; /* corrected date for internal use */
3746 tv_zero(&s->logs.tv_request);
3747 s->logs.t_queue = -1;
3748 s->logs.t_connect = -1;
3749 s->logs.t_data = -1;
3750 s->logs.t_close = 0;
3751 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3752 s->logs.srv_queue_size = 0; /* we will get this number soon */
3753
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003754 s->logs.bytes_in = s->req->total = s->req->i;
3755 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
3757 if (s->pend_pos)
3758 pendconn_free(s->pend_pos);
3759
Willy Tarreau827aee92011-03-10 16:55:02 +01003760 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761 if (s->flags & SN_CURR_SESS) {
3762 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003763 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003765 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3766 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 }
3768
Willy Tarreau9e000c62011-03-10 14:03:36 +01003769 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770
3771 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3772 s->req->cons->fd = -1; /* just to help with debugging */
3773 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003774 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775 s->req->cons->err_loc = NULL;
3776 s->req->cons->exp = TICK_ETERNITY;
3777 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003778 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3779 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 +02003780 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 +01003781 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3782 s->txn.meth = 0;
3783 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003784 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003785 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 s->req->cons->flags |= SI_FL_INDEP_STR;
3787
Willy Tarreau96e31212011-05-30 18:10:30 +02003788 if (s->fe->options2 & PR_O2_NODELAY) {
3789 s->req->flags |= BF_NEVER_WAIT;
3790 s->rep->flags |= BF_NEVER_WAIT;
3791 }
3792
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 /* if the request buffer is not empty, it means we're
3794 * about to process another request, so send pending
3795 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003796 * Just don't do this if the buffer is close to be full,
3797 * because the request will wait for it to flush a little
3798 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003800 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003801 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003802 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003803 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 +01003804 s->rep->flags |= BF_EXPECT_MORE;
3805 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003806
3807 /* we're removing the analysers, we MUST re-enable events detection */
3808 buffer_auto_read(s->req);
3809 buffer_auto_close(s->req);
3810 buffer_auto_read(s->rep);
3811 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812
Willy Tarreau342b11c2010-11-24 16:22:09 +01003813 s->req->analysers = s->listener->analysers;
3814 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 s->rep->analysers = 0;
3816
3817 http_silent_debug(__LINE__, s);
3818}
3819
3820
3821/* This function updates the request state machine according to the response
3822 * state machine and buffer flags. It returns 1 if it changes anything (flag
3823 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3824 * it is only used to find when a request/response couple is complete. Both
3825 * this function and its equivalent should loop until both return zero. It
3826 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3827 */
3828int http_sync_req_state(struct session *s)
3829{
3830 struct buffer *buf = s->req;
3831 struct http_txn *txn = &s->txn;
3832 unsigned int old_flags = buf->flags;
3833 unsigned int old_state = txn->req.msg_state;
3834
3835 http_silent_debug(__LINE__, s);
3836 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3837 return 0;
3838
3839 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003840 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003841 * We can shut the read side unless we want to abort_on_close,
3842 * or we have a POST request. The issue with POST requests is
3843 * that some browsers still send a CRLF after the request, and
3844 * this CRLF must be read so that it does not remain in the kernel
3845 * buffers, otherwise a close could cause an RST on some systems
3846 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003847 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003848 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003849 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850
3851 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3852 goto wait_other_side;
3853
3854 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3855 /* The server has not finished to respond, so we
3856 * don't want to move in order not to upset it.
3857 */
3858 goto wait_other_side;
3859 }
3860
3861 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3862 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003863 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 txn->req.msg_state = HTTP_MSG_TUNNEL;
3865 goto wait_other_side;
3866 }
3867
3868 /* When we get here, it means that both the request and the
3869 * response have finished receiving. Depending on the connection
3870 * mode, we'll have to wait for the last bytes to leave in either
3871 * direction, and sometimes for a close to be effective.
3872 */
3873
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003874 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3875 /* Server-close mode : queue a connection close to the server */
3876 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003878 }
3879 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3880 /* Option forceclose is set, or either side wants to close,
3881 * let's enforce it now that we're not expecting any new
3882 * data to come. The caller knows the session is complete
3883 * once both states are CLOSED.
3884 */
3885 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 buffer_shutr_now(buf);
3887 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003889 }
3890 else {
3891 /* The last possible modes are keep-alive and tunnel. Since tunnel
3892 * mode does not set the body analyser, we can't reach this place
3893 * in tunnel mode, so we're left with keep-alive only.
3894 * This mode is currently not implemented, we switch to tunnel mode.
3895 */
3896 buffer_auto_read(buf);
3897 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 }
3899
3900 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3901 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003902 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3903
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 if (!(buf->flags & BF_OUT_EMPTY)) {
3905 txn->req.msg_state = HTTP_MSG_CLOSING;
3906 goto http_msg_closing;
3907 }
3908 else {
3909 txn->req.msg_state = HTTP_MSG_CLOSED;
3910 goto http_msg_closed;
3911 }
3912 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003913 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003914 }
3915
3916 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3917 http_msg_closing:
3918 /* nothing else to forward, just waiting for the output buffer
3919 * to be empty and for the shutw_now to take effect.
3920 */
3921 if (buf->flags & BF_OUT_EMPTY) {
3922 txn->req.msg_state = HTTP_MSG_CLOSED;
3923 goto http_msg_closed;
3924 }
3925 else if (buf->flags & BF_SHUTW) {
3926 txn->req.msg_state = HTTP_MSG_ERROR;
3927 goto wait_other_side;
3928 }
3929 }
3930
3931 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3932 http_msg_closed:
3933 goto wait_other_side;
3934 }
3935
3936 wait_other_side:
3937 http_silent_debug(__LINE__, s);
3938 return txn->req.msg_state != old_state || buf->flags != old_flags;
3939}
3940
3941
3942/* This function updates the response state machine according to the request
3943 * state machine and buffer flags. It returns 1 if it changes anything (flag
3944 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3945 * it is only used to find when a request/response couple is complete. Both
3946 * this function and its equivalent should loop until both return zero. It
3947 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3948 */
3949int http_sync_res_state(struct session *s)
3950{
3951 struct buffer *buf = s->rep;
3952 struct http_txn *txn = &s->txn;
3953 unsigned int old_flags = buf->flags;
3954 unsigned int old_state = txn->rsp.msg_state;
3955
3956 http_silent_debug(__LINE__, s);
3957 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3958 return 0;
3959
3960 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3961 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003962 * still monitor the server connection for a possible close
3963 * while the request is being uploaded, so we don't disable
3964 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003965 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003966 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967
3968 if (txn->req.msg_state == HTTP_MSG_ERROR)
3969 goto wait_other_side;
3970
3971 if (txn->req.msg_state < HTTP_MSG_DONE) {
3972 /* The client seems to still be sending data, probably
3973 * because we got an error response during an upload.
3974 * We have the choice of either breaking the connection
3975 * or letting it pass through. Let's do the later.
3976 */
3977 goto wait_other_side;
3978 }
3979
3980 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3981 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003982 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3984 goto wait_other_side;
3985 }
3986
3987 /* When we get here, it means that both the request and the
3988 * response have finished receiving. Depending on the connection
3989 * mode, we'll have to wait for the last bytes to leave in either
3990 * direction, and sometimes for a close to be effective.
3991 */
3992
3993 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3994 /* Server-close mode : shut read and wait for the request
3995 * side to close its output buffer. The caller will detect
3996 * when we're in DONE and the other is in CLOSED and will
3997 * catch that for the final cleanup.
3998 */
3999 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4000 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004002 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4003 /* Option forceclose is set, or either side wants to close,
4004 * let's enforce it now that we're not expecting any new
4005 * data to come. The caller knows the session is complete
4006 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004008 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4009 buffer_shutr_now(buf);
4010 buffer_shutw_now(buf);
4011 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 /* The last possible modes are keep-alive and tunnel. Since tunnel
4015 * mode does not set the body analyser, we can't reach this place
4016 * in tunnel mode, so we're left with keep-alive only.
4017 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004019 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021 }
4022
4023 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4024 /* if we've just closed an output, let's switch */
4025 if (!(buf->flags & BF_OUT_EMPTY)) {
4026 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4027 goto http_msg_closing;
4028 }
4029 else {
4030 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4031 goto http_msg_closed;
4032 }
4033 }
4034 goto wait_other_side;
4035 }
4036
4037 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4038 http_msg_closing:
4039 /* nothing else to forward, just waiting for the output buffer
4040 * to be empty and for the shutw_now to take effect.
4041 */
4042 if (buf->flags & BF_OUT_EMPTY) {
4043 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4044 goto http_msg_closed;
4045 }
4046 else if (buf->flags & BF_SHUTW) {
4047 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004048 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004049 if (target_srv(&s->target))
4050 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 goto wait_other_side;
4052 }
4053 }
4054
4055 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4056 http_msg_closed:
4057 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004058 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004060 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 goto wait_other_side;
4062 }
4063
4064 wait_other_side:
4065 http_silent_debug(__LINE__, s);
4066 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4067}
4068
4069
4070/* Resync the request and response state machines. Return 1 if either state
4071 * changes.
4072 */
4073int http_resync_states(struct session *s)
4074{
4075 struct http_txn *txn = &s->txn;
4076 int old_req_state = txn->req.msg_state;
4077 int old_res_state = txn->rsp.msg_state;
4078
4079 http_silent_debug(__LINE__, s);
4080 http_sync_req_state(s);
4081 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004082 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 if (!http_sync_res_state(s))
4084 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004085 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086 if (!http_sync_req_state(s))
4087 break;
4088 }
4089 http_silent_debug(__LINE__, s);
4090 /* OK, both state machines agree on a compatible state.
4091 * There are a few cases we're interested in :
4092 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4093 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4094 * directions, so let's simply disable both analysers.
4095 * - HTTP_MSG_CLOSED on the response only means we must abort the
4096 * request.
4097 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4098 * with server-close mode means we've completed one request and we
4099 * must re-initialize the server connection.
4100 */
4101
4102 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4103 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4104 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4105 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4106 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004107 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004110 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004111 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004113 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4114 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004115 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004116 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004117 s->rep->analysers = 0;
4118 buffer_auto_close(s->rep);
4119 buffer_auto_read(s->rep);
4120 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 buffer_abort(s->req);
4122 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004124 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 }
4126 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4127 txn->rsp.msg_state == HTTP_MSG_DONE &&
4128 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4129 /* server-close: terminate this server connection and
4130 * reinitialize a fresh-new transaction.
4131 */
4132 http_end_txn_clean_session(s);
4133 }
4134
4135 http_silent_debug(__LINE__, s);
4136 return txn->req.msg_state != old_req_state ||
4137 txn->rsp.msg_state != old_res_state;
4138}
4139
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140/* This function is an analyser which forwards request body (including chunk
4141 * sizes if any). It is called as soon as we must forward, even if we forward
4142 * zero byte. The only situation where it must not be called is when we're in
4143 * tunnel mode and we want to forward till the close. It's used both to forward
4144 * remaining data and to resync after end of body. It expects the msg_state to
4145 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4146 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004147 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004148 * bytes of pending data + the headers if not already done (between som and sov).
4149 * It eventually adjusts som to match sov after the data in between have been sent.
4150 */
4151int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4152{
4153 struct http_txn *txn = &s->txn;
4154 struct http_msg *msg = &s->txn.req;
4155
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004156 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4157 return 0;
4158
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004159 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004160 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004161 /* Output closed while we were sending data. We must abort and
4162 * wake the other side up.
4163 */
4164 msg->msg_state = HTTP_MSG_ERROR;
4165 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004166 return 1;
4167 }
4168
Willy Tarreau4fe41902010-06-07 22:27:41 +02004169 /* in most states, we should abort in case of early close */
4170 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004171
4172 /* Note that we don't have to send 100-continue back because we don't
4173 * need the data to complete our job, and it's up to the server to
4174 * decide whether to return 100, 417 or anything else in return of
4175 * an "Expect: 100-continue" header.
4176 */
4177
4178 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004179 /* we have msg->sov which points to the first byte of message body.
4180 * msg->som still points to the beginning of the message. We must
4181 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004182 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004183 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004184
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004185 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004186 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4187 else {
4188 msg->msg_state = HTTP_MSG_DATA;
4189 }
4190 }
4191
Willy Tarreaud98cf932009-12-27 22:54:55 +01004192 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004193 int bytes;
4194
Willy Tarreau610ecce2010-01-04 21:15:02 +01004195 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004196 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004197 bytes = msg->sov - msg->som;
4198 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004199 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004200 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4201 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004202 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004203 msg->chunk_len += (unsigned int)bytes;
4204 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004205 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004206
Willy Tarreaucaabe412010-01-03 23:08:28 +01004207 if (msg->msg_state == HTTP_MSG_DATA) {
4208 /* must still forward */
4209 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004210 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004211
4212 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004213 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004214 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004216 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004217 }
4218 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004219 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004220 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004221 * TRAILERS state.
4222 */
4223 int ret = http_parse_chunk_size(req, msg);
4224
4225 if (!ret)
4226 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004227 else if (ret < 0) {
4228 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004229 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004230 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004231 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004232 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4236 /* we want the CRLF after the data */
4237 int ret;
4238
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 ret = http_skip_chunk_crlf(req, msg);
4240
4241 if (ret == 0)
4242 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004243 else if (ret < 0) {
4244 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004245 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004246 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004247 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004248 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 /* we're in MSG_CHUNK_SIZE now */
4250 }
4251 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4252 int ret = http_forward_trailers(req, msg);
4253
4254 if (ret == 0)
4255 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004256 else if (ret < 0) {
4257 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004258 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004259 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004260 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004261 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004262 /* we're in HTTP_MSG_DONE now */
4263 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004265 int old_state = msg->msg_state;
4266
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004268 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004269 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4270 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4271 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 if (http_resync_states(s)) {
4273 /* some state changes occurred, maybe the analyser
4274 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004275 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004276 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4277 if (req->flags & BF_SHUTW) {
4278 /* request errors are most likely due to
4279 * the server aborting the transfer.
4280 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004281 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004282 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004283 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004284 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004286 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004288 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004289
4290 /* If "option abortonclose" is set on the backend, we
4291 * want to monitor the client's connection and forward
4292 * any shutdown notification to the server, which will
4293 * decide whether to close or to go on processing the
4294 * request.
4295 */
4296 if (s->be->options & PR_O_ABRT_CLOSE) {
4297 buffer_auto_read(req);
4298 buffer_auto_close(req);
4299 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004300 else if (s->txn.meth == HTTP_METH_POST) {
4301 /* POST requests may require to read extra CRLF
4302 * sent by broken browsers and which could cause
4303 * an RST to be sent upon close on some systems
4304 * (eg: Linux).
4305 */
4306 buffer_auto_read(req);
4307 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004308
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004310 }
4311 }
4312
Willy Tarreaud98cf932009-12-27 22:54:55 +01004313 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004314 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004315 if (req->flags & BF_SHUTR) {
4316 if (!(s->flags & SN_ERR_MASK))
4317 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004318 if (!(s->flags & SN_FINST_MASK)) {
4319 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4320 s->flags |= SN_FINST_H;
4321 else
4322 s->flags |= SN_FINST_D;
4323 }
4324
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004325 s->fe->fe_counters.cli_aborts++;
4326 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004327 if (target_srv(&s->target))
4328 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004329
4330 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004331 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004332
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004333 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004334 if (req->flags & BF_SHUTW)
4335 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004337 /* When TE: chunked is used, we need to get there again to parse remaining
4338 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4339 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004340 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004341 buffer_dont_close(req);
4342
Willy Tarreau5c620922011-05-11 19:56:11 +02004343 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004344 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4345 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004346 * modes are already handled by the stream sock layer. We must not do
4347 * this in content-length mode because it could present the MSG_MORE
4348 * flag with the last block of forwarded data, which would cause an
4349 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004350 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004351 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004352 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004353
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004355 return 0;
4356
Willy Tarreaud98cf932009-12-27 22:54:55 +01004357 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004358 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004359 if (s->listener->counters)
4360 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004361 return_bad_req_stats_ok:
4362 txn->req.msg_state = HTTP_MSG_ERROR;
4363 if (txn->status) {
4364 /* Note: we don't send any error if some data were already sent */
4365 stream_int_retnclose(req->prod, NULL);
4366 } else {
4367 txn->status = 400;
4368 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4369 }
4370 req->analysers = 0;
4371 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004372
4373 if (!(s->flags & SN_ERR_MASK))
4374 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004375 if (!(s->flags & SN_FINST_MASK)) {
4376 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4377 s->flags |= SN_FINST_H;
4378 else
4379 s->flags |= SN_FINST_D;
4380 }
4381 return 0;
4382
4383 aborted_xfer:
4384 txn->req.msg_state = HTTP_MSG_ERROR;
4385 if (txn->status) {
4386 /* Note: we don't send any error if some data were already sent */
4387 stream_int_retnclose(req->prod, NULL);
4388 } else {
4389 txn->status = 502;
4390 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4391 }
4392 req->analysers = 0;
4393 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4394
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004395 s->fe->fe_counters.srv_aborts++;
4396 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004397 if (target_srv(&s->target))
4398 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004399
4400 if (!(s->flags & SN_ERR_MASK))
4401 s->flags |= SN_ERR_SRVCL;
4402 if (!(s->flags & SN_FINST_MASK)) {
4403 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4404 s->flags |= SN_FINST_H;
4405 else
4406 s->flags |= SN_FINST_D;
4407 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004408 return 0;
4409}
4410
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004411/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4412 * processing can continue on next analysers, or zero if it either needs more
4413 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4414 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4415 * when it has nothing left to do, and may remove any analyser when it wants to
4416 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004417 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004418int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004419{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004420 struct http_txn *txn = &s->txn;
4421 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004422 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004423 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004424 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004425 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004426
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004427 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 +02004428 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004430 rep,
4431 rep->rex, rep->wex,
4432 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004433 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004434 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004435
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004436 /*
4437 * Now parse the partial (or complete) lines.
4438 * We will check the response syntax, and also join multi-line
4439 * headers. An index of all the lines will be elaborated while
4440 * parsing.
4441 *
4442 * For the parsing, we use a 28 states FSM.
4443 *
4444 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004445 * rep->data + msg->som = beginning of response
4446 * rep->data + msg->eoh = end of processed headers / start of current one
4447 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004448 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004450 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004451 */
4452
Willy Tarreau83e3af02009-12-28 17:39:57 +01004453 /* There's a protected area at the end of the buffer for rewriting
4454 * purposes. We don't want to start to parse the request if the
4455 * protected area is affected, because we may have to move processed
4456 * data later, which is much more complicated.
4457 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004458 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004459 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004460 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004461 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004462 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004463 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004464 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4465 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004466 buffer_dont_close(rep);
4467 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4468 return 0;
4469 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004470 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004471 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004472 }
4473
Willy Tarreaua458b672012-03-05 11:17:50 +01004474 if (likely(msg->next < rep->i))
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004475 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004476 }
4477
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 /* 1: we might have to print this header in debug mode */
4479 if (unlikely((global.mode & MODE_DEBUG) &&
4480 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004481 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004482 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004483 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004484
Willy Tarreauea1175a2012-03-05 15:52:30 +01004485 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004486 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 sol += hdr_idx_first_pos(&txn->hdr_idx);
4490 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004491
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004492 while (cur_idx) {
4493 eol = sol + txn->hdr_idx.v[cur_idx].len;
4494 debug_hdr("srvhdr", s, sol, eol);
4495 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4496 cur_idx = txn->hdr_idx.v[cur_idx].next;
4497 }
4498 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500 /*
4501 * Now we quickly check if we have found a full valid response.
4502 * If not so, we check the FD and buffer states before leaving.
4503 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004504 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 * responses are checked first.
4506 *
4507 * Depending on whether the client is still there or not, we
4508 * may send an error response back or not. Note that normally
4509 * we should only check for HTTP status there, and check I/O
4510 * errors somewhere else.
4511 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004512
Willy Tarreau655dce92009-11-08 13:10:58 +01004513 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004514 /* Invalid response */
4515 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4516 /* we detected a parsing error. We want to archive this response
4517 * in the dedicated proxy area for later troubleshooting.
4518 */
4519 hdr_response_bad:
4520 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004521 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004523 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004524 if (target_srv(&s->target)) {
4525 target_srv(&s->target)->counters.failed_resp++;
4526 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004527 }
Willy Tarreau64648412010-03-05 10:41:54 +01004528 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004529 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 rep->analysers = 0;
4531 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004532 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004533 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4535
4536 if (!(s->flags & SN_ERR_MASK))
4537 s->flags |= SN_ERR_PRXCOND;
4538 if (!(s->flags & SN_FINST_MASK))
4539 s->flags |= SN_FINST_H;
4540
4541 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004542 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 /* too large response does not fit in buffer. */
4545 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004546 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004547 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004548 goto hdr_response_bad;
4549 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 /* read error */
4552 else if (rep->flags & BF_READ_ERROR) {
4553 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004554 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004555
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004556 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004557 if (target_srv(&s->target)) {
4558 target_srv(&s->target)->counters.failed_resp++;
4559 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004560 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004561
Willy Tarreau90deb182010-01-07 00:20:41 +01004562 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004563 rep->analysers = 0;
4564 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004565 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004566 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004567 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004568
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 if (!(s->flags & SN_ERR_MASK))
4570 s->flags |= SN_ERR_SRVCL;
4571 if (!(s->flags & SN_FINST_MASK))
4572 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004573 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004574 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004575
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576 /* read timeout : return a 504 to the client. */
4577 else if (rep->flags & BF_READ_TIMEOUT) {
4578 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004579 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004580
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004581 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004582 if (target_srv(&s->target)) {
4583 target_srv(&s->target)->counters.failed_resp++;
4584 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004585 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004586
Willy Tarreau90deb182010-01-07 00:20:41 +01004587 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004588 rep->analysers = 0;
4589 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004590 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004591 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004592 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004593
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004594 if (!(s->flags & SN_ERR_MASK))
4595 s->flags |= SN_ERR_SRVTO;
4596 if (!(s->flags & SN_FINST_MASK))
4597 s->flags |= SN_FINST_H;
4598 return 0;
4599 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004600
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004601 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004602 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004603 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004604 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004605
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004606 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004607 if (target_srv(&s->target)) {
4608 target_srv(&s->target)->counters.failed_resp++;
4609 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004610 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004611
Willy Tarreau90deb182010-01-07 00:20:41 +01004612 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 rep->analysers = 0;
4614 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004615 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004616 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004618
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 if (!(s->flags & SN_ERR_MASK))
4620 s->flags |= SN_ERR_SRVCL;
4621 if (!(s->flags & SN_FINST_MASK))
4622 s->flags |= SN_FINST_H;
4623 return 0;
4624 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004625
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 /* write error to client (we don't send any message then) */
4627 else if (rep->flags & BF_WRITE_ERROR) {
4628 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004629 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004630
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004631 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004633 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634
4635 if (!(s->flags & SN_ERR_MASK))
4636 s->flags |= SN_ERR_CLICL;
4637 if (!(s->flags & SN_FINST_MASK))
4638 s->flags |= SN_FINST_H;
4639
4640 /* process_session() will take care of the error */
4641 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004642 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004643
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 buffer_dont_close(rep);
4645 return 0;
4646 }
4647
4648 /* More interesting part now : we know that we have a complete
4649 * response which at least looks like HTTP. We have an indicator
4650 * of each header's length, so we can parse them quickly.
4651 */
4652
4653 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004654 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 /*
4657 * 1: get the status code
4658 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004659 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 if (n < 1 || n > 5)
4661 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004662 /* when the client triggers a 4xx from the server, it's most often due
4663 * to a missing object or permission. These events should be tracked
4664 * because if they happen often, it may indicate a brute force or a
4665 * vulnerability scan.
4666 */
4667 if (n == 4)
4668 session_inc_http_err_ctr(s);
4669
Willy Tarreau827aee92011-03-10 16:55:02 +01004670 if (target_srv(&s->target))
4671 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672
Willy Tarreau5b154472009-12-21 20:11:07 +01004673 /* check if the response is HTTP/1.1 or above */
4674 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004675 ((rep->p[msg->sol + 5] > '1') ||
4676 ((rep->p[msg->sol + 5] == '1') &&
4677 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004678 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004679
4680 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004681 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 +01004682
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004683 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004684 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004685
Willy Tarreau3a215be2012-03-09 21:39:51 +01004686 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687
Willy Tarreau39650402010-03-15 19:44:39 +01004688 /* Adjust server's health based on status code. Note: status codes 501
4689 * and 505 are triggered on demand by client request, so we must not
4690 * count them as server failures.
4691 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004692 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004693 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004694 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004695 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004696 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004697 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004698
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 /*
4700 * 2: check for cacheability.
4701 */
4702
4703 switch (txn->status) {
4704 case 200:
4705 case 203:
4706 case 206:
4707 case 300:
4708 case 301:
4709 case 410:
4710 /* RFC2616 @13.4:
4711 * "A response received with a status code of
4712 * 200, 203, 206, 300, 301 or 410 MAY be stored
4713 * by a cache (...) unless a cache-control
4714 * directive prohibits caching."
4715 *
4716 * RFC2616 @9.5: POST method :
4717 * "Responses to this method are not cacheable,
4718 * unless the response includes appropriate
4719 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004720 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 if (likely(txn->meth != HTTP_METH_POST) &&
4722 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4723 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4724 break;
4725 default:
4726 break;
4727 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004728
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 /*
4730 * 3: we may need to capture headers
4731 */
4732 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004733 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004734 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004735 txn->rsp.cap, s->fe->rsp_cap);
4736
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004737 /* 4: determine the transfer-length.
4738 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4739 * the presence of a message-body in a RESPONSE and its transfer length
4740 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004741 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004742 * All responses to the HEAD request method MUST NOT include a
4743 * message-body, even though the presence of entity-header fields
4744 * might lead one to believe they do. All 1xx (informational), 204
4745 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4746 * message-body. All other responses do include a message-body,
4747 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004748 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004749 * 1. Any response which "MUST NOT" include a message-body (such as the
4750 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4751 * always terminated by the first empty line after the header fields,
4752 * regardless of the entity-header fields present in the message.
4753 *
4754 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4755 * the "chunked" transfer-coding (Section 6.2) is used, the
4756 * transfer-length is defined by the use of this transfer-coding.
4757 * If a Transfer-Encoding header field is present and the "chunked"
4758 * transfer-coding is not present, the transfer-length is defined by
4759 * the sender closing the connection.
4760 *
4761 * 3. If a Content-Length header field is present, its decimal value in
4762 * OCTETs represents both the entity-length and the transfer-length.
4763 * If a message is received with both a Transfer-Encoding header
4764 * field and a Content-Length header field, the latter MUST be ignored.
4765 *
4766 * 4. If the message uses the media type "multipart/byteranges", and
4767 * the transfer-length is not otherwise specified, then this self-
4768 * delimiting media type defines the transfer-length. This media
4769 * type MUST NOT be used unless the sender knows that the recipient
4770 * can parse it; the presence in a request of a Range header with
4771 * multiple byte-range specifiers from a 1.1 client implies that the
4772 * client can parse multipart/byteranges responses.
4773 *
4774 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004775 */
4776
4777 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004778 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004779 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004780 * FIXME: should we parse anyway and return an error on chunked encoding ?
4781 */
4782 if (txn->meth == HTTP_METH_HEAD ||
4783 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004784 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004785 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004786 goto skip_content_length;
4787 }
4788
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004789 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004790 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004791 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004792 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004793 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004794 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4795 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004796 /* bad transfer-encoding (chunked followed by something else) */
4797 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004798 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004799 break;
4800 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004801 }
4802
4803 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4804 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004805 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004806 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004807 signed long long cl;
4808
Willy Tarreauad14f752011-09-02 20:33:27 +02004809 if (!ctx.vlen) {
4810 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004811 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004812 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004813
Willy Tarreauad14f752011-09-02 20:33:27 +02004814 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4815 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004816 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004817 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004818
Willy Tarreauad14f752011-09-02 20:33:27 +02004819 if (cl < 0) {
4820 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004821 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004822 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004824 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004825 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004826 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004827 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004830 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004831 }
4832
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004833 /* FIXME: we should also implement the multipart/byterange method.
4834 * For now on, we resort to close mode in this case (unknown length).
4835 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004837
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004838 /* end of job, return OK */
4839 rep->analysers &= ~an_bit;
4840 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004841 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842 return 1;
4843}
4844
4845/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004846 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4847 * and updates t->rep->analysers. It might make sense to explode it into several
4848 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004849 */
4850int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4851{
4852 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004853 struct http_msg *msg = &txn->rsp;
4854 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004855 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004857 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 +02004858 now_ms, __FUNCTION__,
4859 t,
4860 rep,
4861 rep->rex, rep->wex,
4862 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004863 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 rep->analysers);
4865
Willy Tarreau655dce92009-11-08 13:10:58 +01004866 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 return 0;
4868
4869 rep->analysers &= ~an_bit;
4870 rep->analyse_exp = TICK_ETERNITY;
4871
Willy Tarreau5b154472009-12-21 20:11:07 +01004872 /* Now we have to check if we need to modify the Connection header.
4873 * This is more difficult on the response than it is on the request,
4874 * because we can have two different HTTP versions and we don't know
4875 * how the client will interprete a response. For instance, let's say
4876 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4877 * HTTP/1.1 response without any header. Maybe it will bound itself to
4878 * HTTP/1.0 because it only knows about it, and will consider the lack
4879 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4880 * the lack of header as a keep-alive. Thus we will use two flags
4881 * indicating how a request MAY be understood by the client. In case
4882 * of multiple possibilities, we'll fix the header to be explicit. If
4883 * ambiguous cases such as both close and keepalive are seen, then we
4884 * will fall back to explicit close. Note that we won't take risks with
4885 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004886 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004887 */
4888
Willy Tarreaudc008c52010-02-01 16:20:08 +01004889 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4890 txn->status == 101)) {
4891 /* Either we've established an explicit tunnel, or we're
4892 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004893 * to understand the next protocols. We have to switch to tunnel
4894 * mode, so that we transfer the request and responses then let
4895 * this protocol pass unmodified. When we later implement specific
4896 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004897 * header which contains information about that protocol for
4898 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004899 */
4900 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4901 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004902 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4903 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4904 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004905 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004906
Willy Tarreau60466522010-01-18 19:08:45 +01004907 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004908 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004909 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4910 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004911
Willy Tarreau60466522010-01-18 19:08:45 +01004912 /* now adjust header transformations depending on current state */
4913 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4914 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4915 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004916 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004917 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004918 }
Willy Tarreau60466522010-01-18 19:08:45 +01004919 else { /* SCL / KAL */
4920 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004921 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004922 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004923 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004924
Willy Tarreau60466522010-01-18 19:08:45 +01004925 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004926 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004927
Willy Tarreau60466522010-01-18 19:08:45 +01004928 /* Some keep-alive responses are converted to Server-close if
4929 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004930 */
Willy Tarreau60466522010-01-18 19:08:45 +01004931 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4932 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004933 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004934 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004935 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004936 }
4937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004939 /*
4940 * 3: we will have to evaluate the filters.
4941 * As opposed to version 1.2, now they will be evaluated in the
4942 * filters order and not in the header order. This means that
4943 * each filter has to be validated among all headers.
4944 *
4945 * Filters are tried with ->be first, then with ->fe if it is
4946 * different from ->be.
4947 */
4948
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004949 cur_proxy = t->be;
4950 while (1) {
4951 struct proxy *rule_set = cur_proxy;
4952
4953 /* try headers filters */
4954 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004955 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004956 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004957 if (target_srv(&t->target)) {
4958 target_srv(&t->target)->counters.failed_resp++;
4959 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004960 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004961 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004962 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004963 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004964 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004965 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004966 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004967 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 if (!(t->flags & SN_ERR_MASK))
4969 t->flags |= SN_ERR_PRXCOND;
4970 if (!(t->flags & SN_FINST_MASK))
4971 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004972 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004973 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004975
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004976 /* has the response been denied ? */
4977 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004978 if (target_srv(&t->target))
4979 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004980
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004981 t->be->be_counters.denied_resp++;
4982 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004983 if (t->listener->counters)
4984 t->listener->counters->denied_resp++;
4985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004987 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004988
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004989 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004990 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004991 if (txn->status < 200)
4992 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004993 if (wl->cond) {
4994 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4995 ret = acl_pass(ret);
4996 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4997 ret = !ret;
4998 if (!ret)
4999 continue;
5000 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005001 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005002 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005003 }
5004
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005005 /* check whether we're already working on the frontend */
5006 if (cur_proxy == t->fe)
5007 break;
5008 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005009 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005011 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005012 * We may be facing a 100-continue response, in which case this
5013 * is not the right response, and we're waiting for the next one.
5014 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005015 * next one.
5016 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005017 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005018 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005019 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005020 msg->msg_state = HTTP_MSG_RPBEFORE;
5021 txn->status = 0;
5022 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5023 return 1;
5024 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005025 else if (unlikely(txn->status < 200))
5026 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005027
5028 /* we don't have any 1xx status code now */
5029
5030 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005031 * 4: check for server cookie.
5032 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005033 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5034 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005035 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005036
Willy Tarreaubaaee002006-06-26 02:48:02 +02005037
Willy Tarreaua15645d2007-03-18 16:22:39 +01005038 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005039 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005040 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005041 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005042 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005043
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005044 /*
5045 * 6: add server cookie in the response if needed
5046 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005047 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005048 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005049 (!(t->flags & SN_DIRECT) ||
5050 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5051 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5052 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5053 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005054 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5055 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005057 /* the server is known, it's not the one the client requested, or the
5058 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059 * insert a set-cookie here, except if we want to insert only on POST
5060 * requests and this one isn't. Note that servers which don't have cookies
5061 * (eg: some backup servers) will return a full cookie removal request.
5062 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005063 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005064 len = sprintf(trash,
5065 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5066 t->be->cookie_name);
5067 }
5068 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005069 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005070
5071 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5072 /* emit last_date, which is mandatory */
5073 trash[len++] = COOKIE_DELIM_DATE;
5074 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5075 if (t->be->cookie_maxlife) {
5076 /* emit first_date, which is either the original one or
5077 * the current date.
5078 */
5079 trash[len++] = COOKIE_DELIM_DATE;
5080 s30tob64(txn->cookie_first_date ?
5081 txn->cookie_first_date >> 2 :
5082 (date.tv_sec+3) >> 2, trash + len);
5083 len += 5;
5084 }
5085 }
5086 len += sprintf(trash + len, "; path=/");
5087 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005088
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089 if (t->be->cookie_domain)
5090 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005091
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005092 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005094
Willy Tarreauf1348312010-10-07 15:54:11 +02005095 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005096 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005097 /* the server did not change, only the date was updated */
5098 txn->flags |= TX_SCK_UPDATED;
5099 else
5100 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005101
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005102 /* Here, we will tell an eventual cache on the client side that we don't
5103 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5104 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5105 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5106 */
5107 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005108
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005109 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5110
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005111 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005112 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005113 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005114 }
5115 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117 /*
5118 * 7: check if result will be cacheable with a cookie.
5119 * We'll block the response if security checks have caught
5120 * nasty things such as a cacheable cookie.
5121 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005122 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5123 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005124 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125
5126 /* we're in presence of a cacheable response containing
5127 * a set-cookie header. We'll block it as requested by
5128 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005129 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005130 if (target_srv(&t->target))
5131 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005133 t->be->be_counters.denied_resp++;
5134 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005135 if (t->listener->counters)
5136 t->listener->counters->denied_resp++;
5137
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005138 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005139 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 send_log(t->be, LOG_ALERT,
5141 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005142 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143 goto return_srv_prx_502;
5144 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005145
5146 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005147 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005148 */
Willy Tarreau60466522010-01-18 19:08:45 +01005149 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5150 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5151 unsigned int want_flags = 0;
5152
5153 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5154 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5155 /* we want a keep-alive response here. Keep-alive header
5156 * required if either side is not 1.1.
5157 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005158 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005159 want_flags |= TX_CON_KAL_SET;
5160 }
5161 else {
5162 /* we want a close response here. Close header required if
5163 * the server is 1.1, regardless of the client.
5164 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005165 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005166 want_flags |= TX_CON_CLO_SET;
5167 }
5168
5169 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005170 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005172
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005173 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005174 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005175 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005176 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005177
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178 /*************************************************************
5179 * OK, that's finished for the headers. We have done what we *
5180 * could. Let's switch to the DATA state. *
5181 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005182
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 /* if the user wants to log as soon as possible, without counting
5186 * bytes from the server, then this is the right moment. We have
5187 * to temporarily assign bytes_out to log what we currently have.
5188 */
5189 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5190 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5191 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005192 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 t->logs.bytes_out = 0;
5194 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005195
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005196 /* Note: we must not try to cheat by jumping directly to DATA,
5197 * otherwise we would not let the client side wake up.
5198 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005199
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005200 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005202 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005204
Willy Tarreaud98cf932009-12-27 22:54:55 +01005205/* This function is an analyser which forwards response body (including chunk
5206 * sizes if any). It is called as soon as we must forward, even if we forward
5207 * zero byte. The only situation where it must not be called is when we're in
5208 * tunnel mode and we want to forward till the close. It's used both to forward
5209 * remaining data and to resync after end of body. It expects the msg_state to
5210 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5211 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005212 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005213 * bytes of pending data + the headers if not already done (between som and sov).
5214 * It eventually adjusts som to match sov after the data in between have been sent.
5215 */
5216int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5217{
5218 struct http_txn *txn = &s->txn;
5219 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005220 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005221
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005222 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5223 return 0;
5224
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005225 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005226 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005227 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005228 /* Output closed while we were sending data. We must abort and
5229 * wake the other side up.
5230 */
5231 msg->msg_state = HTTP_MSG_ERROR;
5232 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005233 return 1;
5234 }
5235
Willy Tarreau4fe41902010-06-07 22:27:41 +02005236 /* in most states, we should abort in case of early close */
5237 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005238
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005240 /* we have msg->sov which points to the first byte of message body.
5241 * msg->som still points to the beginning of the message. We must
5242 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005243 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005244 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005245
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005246 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005247 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5248 else {
5249 msg->msg_state = HTTP_MSG_DATA;
5250 }
5251 }
5252
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005254 int bytes;
5255
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005257 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005258 bytes = msg->sov - msg->som;
5259 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005260 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005261 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5262 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005263 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005264 msg->chunk_len += (unsigned int)bytes;
5265 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005266 }
5267
Willy Tarreaucaabe412010-01-03 23:08:28 +01005268 if (msg->msg_state == HTTP_MSG_DATA) {
5269 /* must still forward */
5270 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005271 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005272
5273 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005274 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005275 msg->msg_state = HTTP_MSG_DATA_CRLF;
5276 else
5277 msg->msg_state = HTTP_MSG_DONE;
5278 }
5279 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005280 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005281 * set ->sov and ->next to point to the body and switch to DATA or
5282 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 */
5284 int ret = http_parse_chunk_size(res, msg);
5285
5286 if (!ret)
5287 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005288 else if (ret < 0) {
5289 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005290 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005291 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005292 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005294 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5296 /* we want the CRLF after the data */
5297 int ret;
5298
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 ret = http_skip_chunk_crlf(res, msg);
5300
5301 if (!ret)
5302 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005303 else if (ret < 0) {
5304 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005305 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005306 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005307 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005308 /* we're in MSG_CHUNK_SIZE now */
5309 }
5310 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5311 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005312
Willy Tarreaud98cf932009-12-27 22:54:55 +01005313 if (ret == 0)
5314 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005315 else if (ret < 0) {
5316 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005317 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005318 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005319 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005320 /* we're in HTTP_MSG_DONE now */
5321 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005323 int old_state = msg->msg_state;
5324
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005326 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005327 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5328 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5329 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330 if (http_resync_states(s)) {
5331 http_silent_debug(__LINE__, s);
5332 /* some state changes occurred, maybe the analyser
5333 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005334 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005335 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5336 if (res->flags & BF_SHUTW) {
5337 /* response errors are most likely due to
5338 * the client aborting the transfer.
5339 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005340 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005341 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005342 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005343 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005345 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005347 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349 }
5350 }
5351
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005353 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005354 if (res->flags & BF_SHUTR) {
5355 if (!(s->flags & SN_ERR_MASK))
5356 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005357 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005358 if (target_srv(&s->target))
5359 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005360 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005361 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005362
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005363 if (res->flags & BF_SHUTW)
5364 goto aborted_xfer;
5365
Willy Tarreau40dba092010-03-04 18:14:51 +01005366 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 if (!s->req->analysers)
5368 goto return_bad_res;
5369
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005370 /* forward any pending data */
5371 bytes = msg->sov - msg->som;
5372 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005373 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005374 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5375 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005376 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005377 msg->chunk_len += (unsigned int)bytes;
5378 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005379 }
5380
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005381 /* When TE: chunked is used, we need to get there again to parse remaining
5382 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5383 * Similarly, with keep-alive on the client side, we don't want to forward a
5384 * close.
5385 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005387 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5388 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5389 buffer_dont_close(res);
5390
Willy Tarreau5c620922011-05-11 19:56:11 +02005391 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005392 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5393 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005394 * modes are already handled by the stream sock layer. We must not do
5395 * this in content-length mode because it could present the MSG_MORE
5396 * flag with the last block of forwarded data, which would cause an
5397 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005398 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005399 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005400 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005401
Willy Tarreaud98cf932009-12-27 22:54:55 +01005402 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 return 0;
5405
Willy Tarreau40dba092010-03-04 18:14:51 +01005406 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005407 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005408 if (target_srv(&s->target))
5409 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005410
5411 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005413 /* don't send any error message as we're in the body */
5414 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005416 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005417 if (target_srv(&s->target))
5418 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005419
5420 if (!(s->flags & SN_ERR_MASK))
5421 s->flags |= SN_ERR_PRXCOND;
5422 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005423 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005424 return 0;
5425
5426 aborted_xfer:
5427 txn->rsp.msg_state = HTTP_MSG_ERROR;
5428 /* don't send any error message as we're in the body */
5429 stream_int_retnclose(res->cons, NULL);
5430 res->analysers = 0;
5431 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5432
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005433 s->fe->fe_counters.cli_aborts++;
5434 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005435 if (target_srv(&s->target))
5436 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005437
5438 if (!(s->flags & SN_ERR_MASK))
5439 s->flags |= SN_ERR_CLICL;
5440 if (!(s->flags & SN_FINST_MASK))
5441 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005442 return 0;
5443}
5444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005445/* Iterate the same filter through all request headers.
5446 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005447 * Since it can manage the switch to another backend, it updates the per-proxy
5448 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005450int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005452 char term;
5453 char *cur_ptr, *cur_end, *cur_next;
5454 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005455 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005456 struct hdr_idx_elem *cur_hdr;
5457 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005459 last_hdr = 0;
5460
Willy Tarreau3a215be2012-03-09 21:39:51 +01005461 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462 old_idx = 0;
5463
5464 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005465 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005466 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005467 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 (exp->action == ACT_ALLOW ||
5469 exp->action == ACT_DENY ||
5470 exp->action == ACT_TARPIT))
5471 return 0;
5472
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005473 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 if (!cur_idx)
5475 break;
5476
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005477 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005478 cur_ptr = cur_next;
5479 cur_end = cur_ptr + cur_hdr->len;
5480 cur_next = cur_end + cur_hdr->cr + 1;
5481
5482 /* Now we have one header between cur_ptr and cur_end,
5483 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005484 */
5485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005486 /* The annoying part is that pattern matching needs
5487 * that we modify the contents to null-terminate all
5488 * strings before testing them.
5489 */
5490
5491 term = *cur_end;
5492 *cur_end = '\0';
5493
5494 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5495 switch (exp->action) {
5496 case ACT_SETBE:
5497 /* It is not possible to jump a second time.
5498 * FIXME: should we return an HTTP/500 here so that
5499 * the admin knows there's a problem ?
5500 */
5501 if (t->be != t->fe)
5502 break;
5503
5504 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005505 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005506 last_hdr = 1;
5507 break;
5508
5509 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005510 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005511 last_hdr = 1;
5512 break;
5513
5514 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005515 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005517
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005518 t->fe->fe_counters.denied_req++;
5519 if (t->fe != t->be)
5520 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005521 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005522 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005524 break;
5525
5526 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005527 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005529
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005530 t->fe->fe_counters.denied_req++;
5531 if (t->fe != t->be)
5532 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005533 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005534 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005536 break;
5537
5538 case ACT_REPLACE:
5539 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5540 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5541 /* FIXME: if the user adds a newline in the replacement, the
5542 * index will not be recalculated for now, and the new line
5543 * will not be counted as a new header.
5544 */
5545
5546 cur_end += delta;
5547 cur_next += delta;
5548 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005549 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005550 break;
5551
5552 case ACT_REMOVE:
5553 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5554 cur_next += delta;
5555
Willy Tarreaufa355d42009-11-29 18:12:29 +01005556 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005557 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5558 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005559 cur_hdr->len = 0;
5560 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005561 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005562 break;
5563
5564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005565 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005566 if (cur_end)
5567 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005569 /* keep the link from this header to next one in case of later
5570 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005572 old_idx = cur_idx;
5573 }
5574 return 0;
5575}
5576
5577
5578/* Apply the filter to the request line.
5579 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5580 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005581 * Since it can manage the switch to another backend, it updates the per-proxy
5582 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 */
5584int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5585{
5586 char term;
5587 char *cur_ptr, *cur_end;
5588 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005589 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005590 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005591
Willy Tarreau58f10d72006-12-04 02:26:12 +01005592
Willy Tarreau3d300592007-03-18 18:34:41 +01005593 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005594 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 (exp->action == ACT_ALLOW ||
5597 exp->action == ACT_DENY ||
5598 exp->action == ACT_TARPIT))
5599 return 0;
5600 else if (exp->action == ACT_REMOVE)
5601 return 0;
5602
5603 done = 0;
5604
Willy Tarreau3a215be2012-03-09 21:39:51 +01005605 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005606 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005607
5608 /* Now we have the request line between cur_ptr and cur_end */
5609
5610 /* The annoying part is that pattern matching needs
5611 * that we modify the contents to null-terminate all
5612 * strings before testing them.
5613 */
5614
5615 term = *cur_end;
5616 *cur_end = '\0';
5617
5618 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5619 switch (exp->action) {
5620 case ACT_SETBE:
5621 /* It is not possible to jump a second time.
5622 * FIXME: should we return an HTTP/500 here so that
5623 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005625 if (t->be != t->fe)
5626 break;
5627
5628 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005629 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005630 done = 1;
5631 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005633 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005634 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 done = 1;
5636 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005638 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005639 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005641 t->fe->fe_counters.denied_req++;
5642 if (t->fe != t->be)
5643 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005644 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005645 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 done = 1;
5648 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005651 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005652
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005653 t->fe->fe_counters.denied_req++;
5654 if (t->fe != t->be)
5655 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005656 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005657 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 done = 1;
5660 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 case ACT_REPLACE:
5663 *cur_end = term; /* restore the string terminator */
5664 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5665 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5666 /* FIXME: if the user adds a newline in the replacement, the
5667 * index will not be recalculated for now, and the new line
5668 * will not be counted as a new header.
5669 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005670
Willy Tarreaufa355d42009-11-29 18:12:29 +01005671 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005673 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 HTTP_MSG_RQMETH,
5675 cur_ptr, cur_end + 1,
5676 NULL, NULL);
5677 if (unlikely(!cur_end))
5678 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 /* we have a full request and we know that we have either a CR
5681 * or an LF at <ptr>.
5682 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005683 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5684 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 /* there is no point trying this regex on headers */
5686 return 1;
5687 }
5688 }
5689 *cur_end = term; /* restore the string terminator */
5690 return done;
5691}
Willy Tarreau97de6242006-12-27 17:18:38 +01005692
Willy Tarreau58f10d72006-12-04 02:26:12 +01005693
Willy Tarreau58f10d72006-12-04 02:26:12 +01005694
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005696 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005698 * unparsable request. Since it can manage the switch to another backend, it
5699 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005701int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005703 struct http_txn *txn = &s->txn;
5704 struct hdr_exp *exp;
5705
5706 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 /*
5710 * The interleaving of transformations and verdicts
5711 * makes it difficult to decide to continue or stop
5712 * the evaluation.
5713 */
5714
Willy Tarreau6c123b12010-01-28 20:22:06 +01005715 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5716 break;
5717
Willy Tarreau3d300592007-03-18 18:34:41 +01005718 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005720 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005722
5723 /* if this filter had a condition, evaluate it now and skip to
5724 * next filter if the condition does not match.
5725 */
5726 if (exp->cond) {
5727 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5728 ret = acl_pass(ret);
5729 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5730 ret = !ret;
5731
5732 if (!ret)
5733 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734 }
5735
5736 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005737 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005738 if (unlikely(ret < 0))
5739 return -1;
5740
5741 if (likely(ret == 0)) {
5742 /* The filter did not match the request, it can be
5743 * iterated through all headers.
5744 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005745 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005746 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005747 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005749}
5750
5751
Willy Tarreaua15645d2007-03-18 16:22:39 +01005752
Willy Tarreau58f10d72006-12-04 02:26:12 +01005753/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005754 * Try to retrieve the server associated to the appsession.
5755 * If the server is found, it's assigned to the session.
5756 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005757void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005758 struct http_txn *txn = &t->txn;
5759 appsess *asession = NULL;
5760 char *sessid_temp = NULL;
5761
Cyril Bontéb21570a2009-11-29 20:04:48 +01005762 if (len > t->be->appsession_len) {
5763 len = t->be->appsession_len;
5764 }
5765
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005766 if (t->be->options2 & PR_O2_AS_REQL) {
5767 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005768 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005769 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005770 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005771 }
5772
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005773 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005774 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5775 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5776 return;
5777 }
5778
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005779 memcpy(txn->sessid, buf, len);
5780 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005781 }
5782
5783 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5784 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5785 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5786 return;
5787 }
5788
Cyril Bontéb21570a2009-11-29 20:04:48 +01005789 memcpy(sessid_temp, buf, len);
5790 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005791
5792 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5793 /* free previously allocated memory */
5794 pool_free2(apools.sessid, sessid_temp);
5795
5796 if (asession != NULL) {
5797 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5798 if (!(t->be->options2 & PR_O2_AS_REQL))
5799 asession->request_count++;
5800
5801 if (asession->serverid != NULL) {
5802 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005803
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005804 while (srv) {
5805 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005806 if ((srv->state & SRV_RUNNING) ||
5807 (t->be->options & PR_O_PERSIST) ||
5808 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005809 /* we found the server and it's usable */
5810 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005811 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005812 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005813 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005814
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005815 break;
5816 } else {
5817 txn->flags &= ~TX_CK_MASK;
5818 txn->flags |= TX_CK_DOWN;
5819 }
5820 }
5821 srv = srv->next;
5822 }
5823 }
5824 }
5825}
5826
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005827/* Find the end of a cookie value contained between <s> and <e>. It works the
5828 * same way as with headers above except that the semi-colon also ends a token.
5829 * See RFC2965 for more information. Note that it requires a valid header to
5830 * return a valid result.
5831 */
5832char *find_cookie_value_end(char *s, const char *e)
5833{
5834 int quoted, qdpair;
5835
5836 quoted = qdpair = 0;
5837 for (; s < e; s++) {
5838 if (qdpair) qdpair = 0;
5839 else if (quoted) {
5840 if (*s == '\\') qdpair = 1;
5841 else if (*s == '"') quoted = 0;
5842 }
5843 else if (*s == '"') quoted = 1;
5844 else if (*s == ',' || *s == ';') return s;
5845 }
5846 return s;
5847}
5848
5849/* Delete a value in a header between delimiters <from> and <next> in buffer
5850 * <buf>. The number of characters displaced is returned, and the pointer to
5851 * the first delimiter is updated if required. The function tries as much as
5852 * possible to respect the following principles :
5853 * - replace <from> delimiter by the <next> one unless <from> points to a
5854 * colon, in which case <next> is simply removed
5855 * - set exactly one space character after the new first delimiter, unless
5856 * there are not enough characters in the block being moved to do so.
5857 * - remove unneeded spaces before the previous delimiter and after the new
5858 * one.
5859 *
5860 * It is the caller's responsibility to ensure that :
5861 * - <from> points to a valid delimiter or the colon ;
5862 * - <next> points to a valid delimiter or the final CR/LF ;
5863 * - there are non-space chars before <from> ;
5864 * - there is a CR/LF at or after <next>.
5865 */
5866int del_hdr_value(struct buffer *buf, char **from, char *next)
5867{
5868 char *prev = *from;
5869
5870 if (*prev == ':') {
5871 /* We're removing the first value, preserve the colon and add a
5872 * space if possible.
5873 */
5874 if (!http_is_crlf[(unsigned char)*next])
5875 next++;
5876 prev++;
5877 if (prev < next)
5878 *prev++ = ' ';
5879
5880 while (http_is_spht[(unsigned char)*next])
5881 next++;
5882 } else {
5883 /* Remove useless spaces before the old delimiter. */
5884 while (http_is_spht[(unsigned char)*(prev-1)])
5885 prev--;
5886 *from = prev;
5887
5888 /* copy the delimiter and if possible a space if we're
5889 * not at the end of the line.
5890 */
5891 if (!http_is_crlf[(unsigned char)*next]) {
5892 *prev++ = *next++;
5893 if (prev + 1 < next)
5894 *prev++ = ' ';
5895 while (http_is_spht[(unsigned char)*next])
5896 next++;
5897 }
5898 }
5899 return buffer_replace2(buf, prev, next, NULL, 0);
5900}
5901
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005902/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005903 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005904 * desirable to call it only when needed. This code is quite complex because
5905 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5906 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907 */
5908void manage_client_side_cookies(struct session *t, struct buffer *req)
5909{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005910 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005911 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5914 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005917 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005918 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005920 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005922 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005923
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005924 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005925 hdr_beg = hdr_next;
5926 hdr_end = hdr_beg + cur_hdr->len;
5927 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005928
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005929 /* We have one full header between hdr_beg and hdr_end, and the
5930 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 * "Cookie:" headers.
5932 */
5933
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005934 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005935 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005936 old_idx = cur_idx;
5937 continue;
5938 }
5939
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005940 del_from = NULL; /* nothing to be deleted */
5941 preserve_hdr = 0; /* assume we may kill the whole header */
5942
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943 /* Now look for cookies. Conforming to RFC2109, we have to support
5944 * attributes whose name begin with a '$', and associate them with
5945 * the right cookie, if we want to delete this cookie.
5946 * So there are 3 cases for each cookie read :
5947 * 1) it's a special attribute, beginning with a '$' : ignore it.
5948 * 2) it's a server id cookie that we *MAY* want to delete : save
5949 * some pointers on it (last semi-colon, beginning of cookie...)
5950 * 3) it's an application cookie : we *MAY* have to delete a previous
5951 * "special" cookie.
5952 * At the end of loop, if a "special" cookie remains, we may have to
5953 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005954 * *MUST* delete it.
5955 *
5956 * Note: RFC2965 is unclear about the processing of spaces around
5957 * the equal sign in the ATTR=VALUE form. A careful inspection of
5958 * the RFC explicitly allows spaces before it, and not within the
5959 * tokens (attrs or values). An inspection of RFC2109 allows that
5960 * too but section 10.1.3 lets one think that spaces may be allowed
5961 * after the equal sign too, resulting in some (rare) buggy
5962 * implementations trying to do that. So let's do what servers do.
5963 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5964 * allowed quoted strings in values, with any possible character
5965 * after a backslash, including control chars and delimitors, which
5966 * causes parsing to become ambiguous. Browsers also allow spaces
5967 * within values even without quotes.
5968 *
5969 * We have to keep multiple pointers in order to support cookie
5970 * removal at the beginning, middle or end of header without
5971 * corrupting the header. All of these headers are valid :
5972 *
5973 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5974 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5975 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5976 * | | | | | | | | |
5977 * | | | | | | | | hdr_end <--+
5978 * | | | | | | | +--> next
5979 * | | | | | | +----> val_end
5980 * | | | | | +-----------> val_beg
5981 * | | | | +--------------> equal
5982 * | | | +----------------> att_end
5983 * | | +---------------------> att_beg
5984 * | +--------------------------> prev
5985 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986 */
5987
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005988 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5989 /* Iterate through all cookies on this line */
5990
5991 /* find att_beg */
5992 att_beg = prev + 1;
5993 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5994 att_beg++;
5995
5996 /* find att_end : this is the first character after the last non
5997 * space before the equal. It may be equal to hdr_end.
5998 */
5999 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006000
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006001 while (equal < hdr_end) {
6002 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006003 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006004 if (http_is_spht[(unsigned char)*equal++])
6005 continue;
6006 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006007 }
6008
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006009 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6010 * is between <att_beg> and <equal>, both may be identical.
6011 */
6012
6013 /* look for end of cookie if there is an equal sign */
6014 if (equal < hdr_end && *equal == '=') {
6015 /* look for the beginning of the value */
6016 val_beg = equal + 1;
6017 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6018 val_beg++;
6019
6020 /* find the end of the value, respecting quotes */
6021 next = find_cookie_value_end(val_beg, hdr_end);
6022
6023 /* make val_end point to the first white space or delimitor after the value */
6024 val_end = next;
6025 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6026 val_end--;
6027 } else {
6028 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006029 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006030
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006031 /* We have nothing to do with attributes beginning with '$'. However,
6032 * they will automatically be removed if a header before them is removed,
6033 * since they're supposed to be linked together.
6034 */
6035 if (*att_beg == '$')
6036 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006038 /* Ignore cookies with no equal sign */
6039 if (equal == next) {
6040 /* This is not our cookie, so we must preserve it. But if we already
6041 * scheduled another cookie for removal, we cannot remove the
6042 * complete header, but we can remove the previous block itself.
6043 */
6044 preserve_hdr = 1;
6045 if (del_from != NULL) {
6046 int delta = del_hdr_value(req, &del_from, prev);
6047 val_end += delta;
6048 next += delta;
6049 hdr_end += delta;
6050 hdr_next += delta;
6051 cur_hdr->len += delta;
6052 http_msg_move_end(&txn->req, delta);
6053 prev = del_from;
6054 del_from = NULL;
6055 }
6056 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006057 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 /* if there are spaces around the equal sign, we need to
6060 * strip them otherwise we'll get trouble for cookie captures,
6061 * or even for rewrites. Since this happens extremely rarely,
6062 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6065 int stripped_before = 0;
6066 int stripped_after = 0;
6067
6068 if (att_end != equal) {
6069 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6070 equal += stripped_before;
6071 val_beg += stripped_before;
6072 }
6073
6074 if (val_beg > equal + 1) {
6075 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6076 val_beg += stripped_after;
6077 stripped_before += stripped_after;
6078 }
6079
6080 val_end += stripped_before;
6081 next += stripped_before;
6082 hdr_end += stripped_before;
6083 hdr_next += stripped_before;
6084 cur_hdr->len += stripped_before;
6085 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006086 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006087 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 /* First, let's see if we want to capture this cookie. We check
6090 * that we don't already have a client side cookie, because we
6091 * can only capture one. Also as an optimisation, we ignore
6092 * cookies shorter than the declared name.
6093 */
6094 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6095 (val_end - att_beg >= t->fe->capture_namelen) &&
6096 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6097 int log_len = val_end - att_beg;
6098
6099 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6100 Alert("HTTP logging : out of memory.\n");
6101 } else {
6102 if (log_len > t->fe->capture_len)
6103 log_len = t->fe->capture_len;
6104 memcpy(txn->cli_cookie, att_beg, log_len);
6105 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006106 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108
Willy Tarreaubca99692010-10-06 19:25:55 +02006109 /* Persistence cookies in passive, rewrite or insert mode have the
6110 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006111 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006112 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006114 * For cookies in prefix mode, the form is :
6115 *
6116 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 */
6118 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6119 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6120 struct server *srv = t->be->srv;
6121 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6124 * have the server ID between val_beg and delim, and the original cookie between
6125 * delim+1 and val_end. Otherwise, delim==val_end :
6126 *
6127 * Cookie: NAME=SRV; # in all but prefix modes
6128 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6129 * | || || | |+-> next
6130 * | || || | +--> val_end
6131 * | || || +---------> delim
6132 * | || |+------------> val_beg
6133 * | || +-------------> att_end = equal
6134 * | |+-----------------> att_beg
6135 * | +------------------> prev
6136 * +-------------------------> hdr_beg
6137 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 if (t->be->options & PR_O_COOK_PFX) {
6140 for (delim = val_beg; delim < val_end; delim++)
6141 if (*delim == COOKIE_DELIM)
6142 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006143 } else {
6144 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006145 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006146 /* Now check if the cookie contains a date field, which would
6147 * appear after a vertical bar ('|') just after the server name
6148 * and before the delimiter.
6149 */
6150 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6151 if (vbar1) {
6152 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006153 * right is the last seen date. It is a base64 encoded
6154 * 30-bit value representing the UNIX date since the
6155 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006156 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006157 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006158 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006159 if (val_end - vbar1 >= 5) {
6160 val = b64tos30(vbar1);
6161 if (val > 0)
6162 txn->cookie_last_date = val << 2;
6163 }
6164 /* look for a second vertical bar */
6165 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6166 if (vbar1 && (val_end - vbar1 > 5)) {
6167 val = b64tos30(vbar1 + 1);
6168 if (val > 0)
6169 txn->cookie_first_date = val << 2;
6170 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006171 }
6172 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006173
Willy Tarreauf64d1412010-10-07 20:06:11 +02006174 /* if the cookie has an expiration date and the proxy wants to check
6175 * it, then we do that now. We first check if the cookie is too old,
6176 * then only if it has expired. We detect strict overflow because the
6177 * time resolution here is not great (4 seconds). Cookies with dates
6178 * in the future are ignored if their offset is beyond one day. This
6179 * allows an admin to fix timezone issues without expiring everyone
6180 * and at the same time avoids keeping unwanted side effects for too
6181 * long.
6182 */
6183 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006184 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6185 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006186 txn->flags &= ~TX_CK_MASK;
6187 txn->flags |= TX_CK_OLD;
6188 delim = val_beg; // let's pretend we have not found the cookie
6189 txn->cookie_first_date = 0;
6190 txn->cookie_last_date = 0;
6191 }
6192 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006193 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6194 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006195 txn->flags &= ~TX_CK_MASK;
6196 txn->flags |= TX_CK_EXPIRED;
6197 delim = val_beg; // let's pretend we have not found the cookie
6198 txn->cookie_first_date = 0;
6199 txn->cookie_last_date = 0;
6200 }
6201
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 /* Here, we'll look for the first running server which supports the cookie.
6203 * This allows to share a same cookie between several servers, for example
6204 * to dedicate backup servers to specific servers only.
6205 * However, to prevent clients from sticking to cookie-less backup server
6206 * when they have incidentely learned an empty cookie, we simply ignore
6207 * empty cookies and mark them as invalid.
6208 * The same behaviour is applied when persistence must be ignored.
6209 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006210 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006211 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006212
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 while (srv) {
6214 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6215 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6216 if ((srv->state & SRV_RUNNING) ||
6217 (t->be->options & PR_O_PERSIST) ||
6218 (t->flags & SN_FORCE_PRST)) {
6219 /* we found the server and we can use it */
6220 txn->flags &= ~TX_CK_MASK;
6221 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6222 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006223 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 break;
6225 } else {
6226 /* we found a server, but it's down,
6227 * mark it as such and go on in case
6228 * another one is available.
6229 */
6230 txn->flags &= ~TX_CK_MASK;
6231 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 srv = srv->next;
6235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreauf64d1412010-10-07 20:06:11 +02006237 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006238 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006240 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6241 txn->flags |= TX_CK_UNUSED;
6242 else
6243 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 /* depending on the cookie mode, we may have to either :
6247 * - delete the complete cookie if we're in insert+indirect mode, so that
6248 * the server never sees it ;
6249 * - remove the server id from the cookie value, and tag the cookie as an
6250 * application cookie so that it does not get accidentely removed later,
6251 * if we're in cookie prefix mode
6252 */
6253 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6254 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6257 val_end += delta;
6258 next += delta;
6259 hdr_end += delta;
6260 hdr_next += delta;
6261 cur_hdr->len += delta;
6262 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 del_from = NULL;
6265 preserve_hdr = 1; /* we want to keep this cookie */
6266 }
6267 else if (del_from == NULL &&
6268 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6269 del_from = prev;
6270 }
6271 } else {
6272 /* This is not our cookie, so we must preserve it. But if we already
6273 * scheduled another cookie for removal, we cannot remove the
6274 * complete header, but we can remove the previous block itself.
6275 */
6276 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 if (del_from != NULL) {
6279 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006280 if (att_beg >= del_from)
6281 att_beg += delta;
6282 if (att_end >= del_from)
6283 att_end += delta;
6284 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 val_end += delta;
6286 next += delta;
6287 hdr_end += delta;
6288 hdr_next += delta;
6289 cur_hdr->len += delta;
6290 http_msg_move_end(&txn->req, delta);
6291 prev = del_from;
6292 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006293 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006294 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 /* Look for the appsession cookie unless persistence must be ignored */
6297 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6298 int cmp_len, value_len;
6299 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006301 if (t->be->options2 & PR_O2_AS_PFX) {
6302 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6303 value_begin = att_beg + t->be->appsession_name_len;
6304 value_len = val_end - att_beg - t->be->appsession_name_len;
6305 } else {
6306 cmp_len = att_end - att_beg;
6307 value_begin = val_beg;
6308 value_len = val_end - val_beg;
6309 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006310
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 /* let's see if the cookie is our appcookie */
6312 if (cmp_len == t->be->appsession_name_len &&
6313 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6314 manage_client_side_appsession(t, value_begin, value_len);
6315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316 }
6317
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006318 /* continue with next cookie on this header line */
6319 att_beg = next;
6320 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 /* There are no more cookies on this line.
6323 * We may still have one (or several) marked for deletion at the
6324 * end of the line. We must do this now in two ways :
6325 * - if some cookies must be preserved, we only delete from the
6326 * mark to the end of line ;
6327 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 if (preserve_hdr) {
6332 delta = del_hdr_value(req, &del_from, hdr_end);
6333 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006334 cur_hdr->len += delta;
6335 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337
6338 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006339 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6340 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006342 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006345 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 }
6347
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006348 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351}
6352
6353
Willy Tarreaua15645d2007-03-18 16:22:39 +01006354/* Iterate the same filter through all response headers contained in <rtr>.
6355 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6356 */
6357int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6358{
6359 char term;
6360 char *cur_ptr, *cur_end, *cur_next;
6361 int cur_idx, old_idx, last_hdr;
6362 struct http_txn *txn = &t->txn;
6363 struct hdr_idx_elem *cur_hdr;
6364 int len, delta;
6365
6366 last_hdr = 0;
6367
Willy Tarreau3a215be2012-03-09 21:39:51 +01006368 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 old_idx = 0;
6370
6371 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006372 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006374 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006375 (exp->action == ACT_ALLOW ||
6376 exp->action == ACT_DENY))
6377 return 0;
6378
6379 cur_idx = txn->hdr_idx.v[old_idx].next;
6380 if (!cur_idx)
6381 break;
6382
6383 cur_hdr = &txn->hdr_idx.v[cur_idx];
6384 cur_ptr = cur_next;
6385 cur_end = cur_ptr + cur_hdr->len;
6386 cur_next = cur_end + cur_hdr->cr + 1;
6387
6388 /* Now we have one header between cur_ptr and cur_end,
6389 * and the next header starts at cur_next.
6390 */
6391
6392 /* The annoying part is that pattern matching needs
6393 * that we modify the contents to null-terminate all
6394 * strings before testing them.
6395 */
6396
6397 term = *cur_end;
6398 *cur_end = '\0';
6399
6400 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6401 switch (exp->action) {
6402 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006403 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006404 last_hdr = 1;
6405 break;
6406
6407 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006408 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006409 last_hdr = 1;
6410 break;
6411
6412 case ACT_REPLACE:
6413 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6414 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6415 /* FIXME: if the user adds a newline in the replacement, the
6416 * index will not be recalculated for now, and the new line
6417 * will not be counted as a new header.
6418 */
6419
6420 cur_end += delta;
6421 cur_next += delta;
6422 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006423 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006424 break;
6425
6426 case ACT_REMOVE:
6427 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6428 cur_next += delta;
6429
Willy Tarreaufa355d42009-11-29 18:12:29 +01006430 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006431 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6432 txn->hdr_idx.used--;
6433 cur_hdr->len = 0;
6434 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006435 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006436 break;
6437
6438 }
6439 }
6440 if (cur_end)
6441 *cur_end = term; /* restore the string terminator */
6442
6443 /* keep the link from this header to next one in case of later
6444 * removal of next header.
6445 */
6446 old_idx = cur_idx;
6447 }
6448 return 0;
6449}
6450
6451
6452/* Apply the filter to the status line in the response buffer <rtr>.
6453 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6454 * or -1 if a replacement resulted in an invalid status line.
6455 */
6456int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6457{
6458 char term;
6459 char *cur_ptr, *cur_end;
6460 int done;
6461 struct http_txn *txn = &t->txn;
6462 int len, delta;
6463
6464
Willy Tarreau3d300592007-03-18 18:34:41 +01006465 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006467 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468 (exp->action == ACT_ALLOW ||
6469 exp->action == ACT_DENY))
6470 return 0;
6471 else if (exp->action == ACT_REMOVE)
6472 return 0;
6473
6474 done = 0;
6475
Willy Tarreau3a215be2012-03-09 21:39:51 +01006476 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006477 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478
6479 /* Now we have the status line between cur_ptr and cur_end */
6480
6481 /* The annoying part is that pattern matching needs
6482 * that we modify the contents to null-terminate all
6483 * strings before testing them.
6484 */
6485
6486 term = *cur_end;
6487 *cur_end = '\0';
6488
6489 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6490 switch (exp->action) {
6491 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006492 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006493 done = 1;
6494 break;
6495
6496 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006497 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 done = 1;
6499 break;
6500
6501 case ACT_REPLACE:
6502 *cur_end = term; /* restore the string terminator */
6503 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6504 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6505 /* FIXME: if the user adds a newline in the replacement, the
6506 * index will not be recalculated for now, and the new line
6507 * will not be counted as a new header.
6508 */
6509
Willy Tarreaufa355d42009-11-29 18:12:29 +01006510 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006511 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006512 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006513 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006514 cur_ptr, cur_end + 1,
6515 NULL, NULL);
6516 if (unlikely(!cur_end))
6517 return -1;
6518
6519 /* we have a full respnse and we know that we have either a CR
6520 * or an LF at <ptr>.
6521 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006522 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 +02006523 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006524 /* there is no point trying this regex on headers */
6525 return 1;
6526 }
6527 }
6528 *cur_end = term; /* restore the string terminator */
6529 return done;
6530}
6531
6532
6533
6534/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006535 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6537 * unparsable response.
6538 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006539int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006540{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006541 struct http_txn *txn = &s->txn;
6542 struct hdr_exp *exp;
6543
6544 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 int ret;
6546
6547 /*
6548 * The interleaving of transformations and verdicts
6549 * makes it difficult to decide to continue or stop
6550 * the evaluation.
6551 */
6552
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006553 if (txn->flags & TX_SVDENY)
6554 break;
6555
Willy Tarreau3d300592007-03-18 18:34:41 +01006556 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6558 exp->action == ACT_PASS)) {
6559 exp = exp->next;
6560 continue;
6561 }
6562
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006563 /* if this filter had a condition, evaluate it now and skip to
6564 * next filter if the condition does not match.
6565 */
6566 if (exp->cond) {
6567 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6568 ret = acl_pass(ret);
6569 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6570 ret = !ret;
6571 if (!ret)
6572 continue;
6573 }
6574
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006576 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 if (unlikely(ret < 0))
6578 return -1;
6579
6580 if (likely(ret == 0)) {
6581 /* The filter did not match the response, it can be
6582 * iterated through all headers.
6583 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006584 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 }
6587 return 0;
6588}
6589
6590
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006592 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006593 * desirable to call it only when needed. This function is also used when we
6594 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006596void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597{
6598 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006599 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006600 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006602 char *hdr_beg, *hdr_end, *hdr_next;
6603 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 /* Iterate through the headers.
6606 * we start with the start line.
6607 */
6608 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006609 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610
6611 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6612 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006613 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
6615 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006616 hdr_beg = hdr_next;
6617 hdr_end = hdr_beg + cur_hdr->len;
6618 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619
Willy Tarreau24581ba2010-08-31 22:39:35 +02006620 /* We have one full header between hdr_beg and hdr_end, and the
6621 * next header starts at hdr_next. We're only interested in
6622 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 */
6624
Willy Tarreau24581ba2010-08-31 22:39:35 +02006625 is_cookie2 = 0;
6626 prev = hdr_beg + 10;
6627 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006628 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006629 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6630 if (!val) {
6631 old_idx = cur_idx;
6632 continue;
6633 }
6634 is_cookie2 = 1;
6635 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 }
6637
Willy Tarreau24581ba2010-08-31 22:39:35 +02006638 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6639 * <prev> points to the colon.
6640 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006641 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642
Willy Tarreau24581ba2010-08-31 22:39:35 +02006643 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6644 * check-cache is enabled) and we are not interested in checking
6645 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006646 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006647 if (t->be->cookie_name == NULL &&
6648 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006649 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 return;
6651
Willy Tarreau24581ba2010-08-31 22:39:35 +02006652 /* OK so now we know we have to process this response cookie.
6653 * The format of the Set-Cookie header is slightly different
6654 * from the format of the Cookie header in that it does not
6655 * support the comma as a cookie delimiter (thus the header
6656 * cannot be folded) because the Expires attribute described in
6657 * the original Netscape's spec may contain an unquoted date
6658 * with a comma inside. We have to live with this because
6659 * many browsers don't support Max-Age and some browsers don't
6660 * support quoted strings. However the Set-Cookie2 header is
6661 * clean.
6662 *
6663 * We have to keep multiple pointers in order to support cookie
6664 * removal at the beginning, middle or end of header without
6665 * corrupting the header (in case of set-cookie2). A special
6666 * pointer, <scav> points to the beginning of the set-cookie-av
6667 * fields after the first semi-colon. The <next> pointer points
6668 * either to the end of line (set-cookie) or next unquoted comma
6669 * (set-cookie2). All of these headers are valid :
6670 *
6671 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6672 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6673 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6674 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6675 * | | | | | | | | | |
6676 * | | | | | | | | +-> next hdr_end <--+
6677 * | | | | | | | +------------> scav
6678 * | | | | | | +--------------> val_end
6679 * | | | | | +--------------------> val_beg
6680 * | | | | +----------------------> equal
6681 * | | | +------------------------> att_end
6682 * | | +----------------------------> att_beg
6683 * | +------------------------------> prev
6684 * +-----------------------------------------> hdr_beg
6685 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686
Willy Tarreau24581ba2010-08-31 22:39:35 +02006687 for (; prev < hdr_end; prev = next) {
6688 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689
Willy Tarreau24581ba2010-08-31 22:39:35 +02006690 /* find att_beg */
6691 att_beg = prev + 1;
6692 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6693 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694
Willy Tarreau24581ba2010-08-31 22:39:35 +02006695 /* find att_end : this is the first character after the last non
6696 * space before the equal. It may be equal to hdr_end.
6697 */
6698 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 while (equal < hdr_end) {
6701 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6702 break;
6703 if (http_is_spht[(unsigned char)*equal++])
6704 continue;
6705 att_end = equal;
6706 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707
Willy Tarreau24581ba2010-08-31 22:39:35 +02006708 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6709 * is between <att_beg> and <equal>, both may be identical.
6710 */
6711
6712 /* look for end of cookie if there is an equal sign */
6713 if (equal < hdr_end && *equal == '=') {
6714 /* look for the beginning of the value */
6715 val_beg = equal + 1;
6716 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6717 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718
Willy Tarreau24581ba2010-08-31 22:39:35 +02006719 /* find the end of the value, respecting quotes */
6720 next = find_cookie_value_end(val_beg, hdr_end);
6721
6722 /* make val_end point to the first white space or delimitor after the value */
6723 val_end = next;
6724 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6725 val_end--;
6726 } else {
6727 /* <equal> points to next comma, semi-colon or EOL */
6728 val_beg = val_end = next = equal;
6729 }
6730
6731 if (next < hdr_end) {
6732 /* Set-Cookie2 supports multiple cookies, and <next> points to
6733 * a colon or semi-colon before the end. So skip all attr-value
6734 * pairs and look for the next comma. For Set-Cookie, since
6735 * commas are permitted in values, skip to the end.
6736 */
6737 if (is_cookie2)
6738 next = find_hdr_value_end(next, hdr_end);
6739 else
6740 next = hdr_end;
6741 }
6742
6743 /* Now everything is as on the diagram above */
6744
6745 /* Ignore cookies with no equal sign */
6746 if (equal == val_end)
6747 continue;
6748
6749 /* If there are spaces around the equal sign, we need to
6750 * strip them otherwise we'll get trouble for cookie captures,
6751 * or even for rewrites. Since this happens extremely rarely,
6752 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006754 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6755 int stripped_before = 0;
6756 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006757
Willy Tarreau24581ba2010-08-31 22:39:35 +02006758 if (att_end != equal) {
6759 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6760 equal += stripped_before;
6761 val_beg += stripped_before;
6762 }
6763
6764 if (val_beg > equal + 1) {
6765 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6766 val_beg += stripped_after;
6767 stripped_before += stripped_after;
6768 }
6769
6770 val_end += stripped_before;
6771 next += stripped_before;
6772 hdr_end += stripped_before;
6773 hdr_next += stripped_before;
6774 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006775 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006776 }
6777
6778 /* First, let's see if we want to capture this cookie. We check
6779 * that we don't already have a server side cookie, because we
6780 * can only capture one. Also as an optimisation, we ignore
6781 * cookies shorter than the declared name.
6782 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006783 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006784 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006785 (val_end - att_beg >= t->fe->capture_namelen) &&
6786 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6787 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006788 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789 Alert("HTTP logging : out of memory.\n");
6790 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006791 else {
6792 if (log_len > t->fe->capture_len)
6793 log_len = t->fe->capture_len;
6794 memcpy(txn->srv_cookie, att_beg, log_len);
6795 txn->srv_cookie[log_len] = 0;
6796 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 }
6798
Willy Tarreau827aee92011-03-10 16:55:02 +01006799 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006801 if (!(t->flags & SN_IGNORE_PRST) &&
6802 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6803 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006804 /* assume passive cookie by default */
6805 txn->flags &= ~TX_SCK_MASK;
6806 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807
6808 /* If the cookie is in insert mode on a known server, we'll delete
6809 * this occurrence because we'll insert another one later.
6810 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 * a direct access.
6812 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006813 if (t->be->options2 & PR_O2_COOK_PSV) {
6814 /* The "preserve" flag was set, we don't want to touch the
6815 * server's cookie.
6816 */
6817 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006818 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006819 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 /* this cookie must be deleted */
6821 if (*prev == ':' && next == hdr_end) {
6822 /* whole header */
6823 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6824 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6825 txn->hdr_idx.used--;
6826 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006827 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 hdr_next += delta;
6829 http_msg_move_end(&txn->rsp, delta);
6830 /* note: while both invalid now, <next> and <hdr_end>
6831 * are still equal, so the for() will stop as expected.
6832 */
6833 } else {
6834 /* just remove the value */
6835 int delta = del_hdr_value(res, &prev, next);
6836 next = prev;
6837 hdr_end += delta;
6838 hdr_next += delta;
6839 cur_hdr->len += delta;
6840 http_msg_move_end(&txn->rsp, delta);
6841 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006842 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006843 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006844 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006846 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006847 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 * with this server since we know it.
6849 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006850 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 next += delta;
6852 hdr_end += delta;
6853 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006855 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856
Willy Tarreauf1348312010-10-07 15:54:11 +02006857 txn->flags &= ~TX_SCK_MASK;
6858 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006860 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006862 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006864 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006865 next += delta;
6866 hdr_end += delta;
6867 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006869 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870
Willy Tarreau827aee92011-03-10 16:55:02 +01006871 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006872 txn->flags &= ~TX_SCK_MASK;
6873 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874 }
6875 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006876 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6877 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006878 int cmp_len, value_len;
6879 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Cyril Bontéb21570a2009-11-29 20:04:48 +01006881 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006882 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6883 value_begin = att_beg + t->be->appsession_name_len;
6884 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006885 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 cmp_len = att_end - att_beg;
6887 value_begin = val_beg;
6888 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006889 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006890
Cyril Bonté17530c32010-04-06 21:11:10 +02006891 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6893 /* free a possibly previously allocated memory */
6894 pool_free2(apools.sessid, txn->sessid);
6895
Cyril Bontéb21570a2009-11-29 20:04:48 +01006896 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006897 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006898 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6899 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6900 return;
6901 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006902 memcpy(txn->sessid, value_begin, value_len);
6903 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 }
6906 /* that's done for this cookie, check the next one on the same
6907 * line when next != hdr_end (only if is_cookie2).
6908 */
6909 }
6910 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006912 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006913
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006914 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006915 appsess *asession = NULL;
6916 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006917 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006918 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006919 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006920 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6921 Alert("Not enough Memory process_srv():asession:calloc().\n");
6922 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6923 return;
6924 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006925 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6926
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006927 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6928 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6929 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006930 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006931 return;
6932 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006933 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006934 asession->sessid[t->be->appsession_len] = 0;
6935
Willy Tarreau827aee92011-03-10 16:55:02 +01006936 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006938 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006940 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 return;
6942 }
6943 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006944 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945
6946 asession->request_count = 0;
6947 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6948 }
6949
6950 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6951 asession->request_count++;
6952 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006953}
6954
6955
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956/*
6957 * Check if response is cacheable or not. Updates t->flags.
6958 */
6959void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6960{
6961 struct http_txn *txn = &t->txn;
6962 char *p1, *p2;
6963
6964 char *cur_ptr, *cur_end, *cur_next;
6965 int cur_idx;
6966
Willy Tarreau5df51872007-11-25 16:20:08 +01006967 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968 return;
6969
6970 /* Iterate through the headers.
6971 * we start with the start line.
6972 */
6973 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006974 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975
6976 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6977 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006978 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979
6980 cur_hdr = &txn->hdr_idx.v[cur_idx];
6981 cur_ptr = cur_next;
6982 cur_end = cur_ptr + cur_hdr->len;
6983 cur_next = cur_end + cur_hdr->cr + 1;
6984
6985 /* We have one full header between cur_ptr and cur_end, and the
6986 * next header starts at cur_next. We're only interested in
6987 * "Cookie:" headers.
6988 */
6989
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006990 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6991 if (val) {
6992 if ((cur_end - (cur_ptr + val) >= 8) &&
6993 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6994 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6995 return;
6996 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 }
6998
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006999 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7000 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 continue;
7002
7003 /* OK, right now we know we have a cache-control header at cur_ptr */
7004
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007005 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006
7007 if (p1 >= cur_end) /* no more info */
7008 continue;
7009
7010 /* p1 is at the beginning of the value */
7011 p2 = p1;
7012
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007013 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 p2++;
7015
7016 /* we have a complete value between p1 and p2 */
7017 if (p2 < cur_end && *p2 == '=') {
7018 /* we have something of the form no-cache="set-cookie" */
7019 if ((cur_end - p1 >= 21) &&
7020 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7021 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007022 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023 continue;
7024 }
7025
7026 /* OK, so we know that either p2 points to the end of string or to a comma */
7027 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7028 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7029 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7030 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007031 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032 return;
7033 }
7034
7035 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007036 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 continue;
7038 }
7039 }
7040}
7041
7042
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043/*
7044 * Try to retrieve a known appsession in the URI, then the associated server.
7045 * If the server is found, it's assigned to the session.
7046 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007047void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007049 char *end_params, *first_param, *cur_param, *next_param;
7050 char separator;
7051 int value_len;
7052
7053 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007055 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007056 (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 +01007057 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007058 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007059
Cyril Bontéb21570a2009-11-29 20:04:48 +01007060 first_param = NULL;
7061 switch (mode) {
7062 case PR_O2_AS_M_PP:
7063 first_param = memchr(begin, ';', len);
7064 break;
7065 case PR_O2_AS_M_QS:
7066 first_param = memchr(begin, '?', len);
7067 break;
7068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Cyril Bontéb21570a2009-11-29 20:04:48 +01007070 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073
Cyril Bontéb21570a2009-11-29 20:04:48 +01007074 switch (mode) {
7075 case PR_O2_AS_M_PP:
7076 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7077 end_params = (char *) begin + len;
7078 }
7079 separator = ';';
7080 break;
7081 case PR_O2_AS_M_QS:
7082 end_params = (char *) begin + len;
7083 separator = '&';
7084 break;
7085 default:
7086 /* unknown mode, shouldn't happen */
7087 return;
7088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089
Cyril Bontéb21570a2009-11-29 20:04:48 +01007090 cur_param = next_param = end_params;
7091 while (cur_param > first_param) {
7092 cur_param--;
7093 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7094 /* let's see if this is the appsession parameter */
7095 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7096 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7097 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7098 /* Cool... it's the right one */
7099 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7100 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7101 if (value_len > 0) {
7102 manage_client_side_appsession(t, cur_param, value_len);
7103 }
7104 break;
7105 }
7106 next_param = cur_param;
7107 }
7108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007110 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007111 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113}
7114
Willy Tarreaub2513902006-12-17 14:52:38 +01007115/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007116 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007117 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007118 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007119 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007120 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007121 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007122 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007123 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007124int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007125{
7126 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007127 struct http_msg *msg = &txn->req;
7128 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7129 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007130
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007131 if (!uri_auth)
7132 return 0;
7133
Cyril Bonté70be45d2010-10-12 00:14:35 +02007134 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007135 return 0;
7136
Willy Tarreau295a8372011-03-10 11:25:07 +01007137 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007138 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007140 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007141 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007142 return 0;
7143
Willy Tarreau3a215be2012-03-09 21:39:51 +01007144 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007145 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007146 return 0;
7147
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007148 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007149 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007150 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007151 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007152 break;
7153 }
7154 h++;
7155 }
7156
7157 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007158 h = uri + uri_auth->uri_len;
7159 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007160 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007161 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007162 break;
7163 }
7164 h++;
7165 }
7166 }
7167
Willy Tarreau3a215be2012-03-09 21:39:51 +01007168 h = uri + uri_auth->uri_len;
7169 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007170 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007171 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007172 break;
7173 }
7174 h++;
7175 }
7176
Willy Tarreau3a215be2012-03-09 21:39:51 +01007177 h = uri + uri_auth->uri_len;
7178 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007179 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007180 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007181 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007182 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7183 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7184 si->applet.ctx.stats.st_code = i;
7185 break;
7186 }
7187 }
7188 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007189 break;
7190 }
7191 h++;
7192 }
7193
Willy Tarreau295a8372011-03-10 11:25:07 +01007194 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007195
Willy Tarreaub2513902006-12-17 14:52:38 +01007196 return 1;
7197}
7198
Willy Tarreau4076a152009-04-02 15:18:36 +02007199/*
7200 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007201 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007202 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7203 * or response messages cannot wrap, this function may also be used with chunks
7204 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007205 */
7206void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007207 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007208 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007209{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007210 struct buffer *buf = msg->buf;
7211
Willy Tarreauea1175a2012-03-05 15:52:30 +01007212 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7213 int len1 = buf->size - msg->som - (buf->p - buf->data);
7214 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7215 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007216 if (es->len > len1 && len1 < sizeof(es->buf))
7217 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7218 }
7219 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007220 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7221 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007222 }
7223
Willy Tarreau4076a152009-04-02 15:18:36 +02007224 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007225 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7226 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7227 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007228 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007229 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007230
Willy Tarreau4076a152009-04-02 15:18:36 +02007231 es->when = date; // user-visible date
7232 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007233 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007234 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007235 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007236 es->state = state;
7237 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007238 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007239}
Willy Tarreaub2513902006-12-17 14:52:38 +01007240
Willy Tarreau294c4732011-12-16 21:35:50 +01007241/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7242 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7243 * performed over the whole headers. Otherwise it must contain a valid header
7244 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7245 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7246 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7247 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7248 * -1.
7249 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007250 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007251unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7252 struct hdr_idx *idx, int occ,
7253 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007254{
Willy Tarreau294c4732011-12-16 21:35:50 +01007255 struct hdr_ctx local_ctx;
7256 char *ptr_hist[MAX_HDR_HISTORY];
7257 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007258 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007259 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007260
Willy Tarreau294c4732011-12-16 21:35:50 +01007261 if (!ctx) {
7262 local_ctx.idx = 0;
7263 ctx = &local_ctx;
7264 }
7265
Willy Tarreaubce70882009-09-07 11:51:47 +02007266 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007267 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007268 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007269 occ--;
7270 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007271 *vptr = ctx->line + ctx->val;
7272 *vlen = ctx->vlen;
7273 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007274 }
7275 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007276 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007277 }
7278
7279 /* negative occurrence, we scan all the list then walk back */
7280 if (-occ > MAX_HDR_HISTORY)
7281 return 0;
7282
Willy Tarreau294c4732011-12-16 21:35:50 +01007283 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007284 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007285 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7286 len_hist[hist_ptr] = ctx->vlen;
7287 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007288 hist_ptr = 0;
7289 found++;
7290 }
7291 if (-occ > found)
7292 return 0;
7293 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7294 * find occurrence -occ, so we have to check [hist_ptr+occ].
7295 */
7296 hist_ptr += occ;
7297 if (hist_ptr >= MAX_HDR_HISTORY)
7298 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007299 *vptr = ptr_hist[hist_ptr];
7300 *vlen = len_hist[hist_ptr];
7301 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007302}
7303
Willy Tarreaubaaee002006-06-26 02:48:02 +02007304/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007305 * Print a debug line with a header
7306 */
7307void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7308{
7309 int len, max;
7310 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007311 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312 max = end - start;
7313 UBOUND(max, sizeof(trash) - len - 1);
7314 len += strlcpy2(trash + len, start, max + 1);
7315 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007316 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317}
7318
Willy Tarreau0937bc42009-12-22 15:03:09 +01007319/*
7320 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7321 * the required fields are properly allocated and that we only need to (re)init
7322 * them. This should be used before processing any new request.
7323 */
7324void http_init_txn(struct session *s)
7325{
7326 struct http_txn *txn = &s->txn;
7327 struct proxy *fe = s->fe;
7328
7329 txn->flags = 0;
7330 txn->status = -1;
7331
William Lallemand5f232402012-04-05 18:02:55 +02007332 global.req_count++;
7333
Willy Tarreauf64d1412010-10-07 20:06:11 +02007334 txn->cookie_first_date = 0;
7335 txn->cookie_last_date = 0;
7336
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007337 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007338 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007339 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007340 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007341 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007342 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007343 txn->req.chunk_len = 0LL;
7344 txn->req.body_len = 0LL;
7345 txn->rsp.chunk_len = 0LL;
7346 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007347 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7348 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007349 txn->req.buf = s->req;
7350 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007351
7352 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007353
7354 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7355 if (fe->options2 & PR_O2_REQBUG_OK)
7356 txn->req.err_pos = -1; /* let buggy requests pass */
7357
Willy Tarreau46023632010-01-07 22:51:47 +01007358 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007359 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7360
Willy Tarreau46023632010-01-07 22:51:47 +01007361 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007362 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7363
7364 if (txn->hdr_idx.v)
7365 hdr_idx_init(&txn->hdr_idx);
7366}
7367
7368/* to be used at the end of a transaction */
7369void http_end_txn(struct session *s)
7370{
7371 struct http_txn *txn = &s->txn;
7372
7373 /* these ones will have been dynamically allocated */
7374 pool_free2(pool2_requri, txn->uri);
7375 pool_free2(pool2_capture, txn->cli_cookie);
7376 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007377 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007378 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007379
William Lallemanda73203e2012-03-12 12:48:57 +01007380 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007381 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007382 txn->uri = NULL;
7383 txn->srv_cookie = NULL;
7384 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007385
7386 if (txn->req.cap) {
7387 struct cap_hdr *h;
7388 for (h = s->fe->req_cap; h; h = h->next)
7389 pool_free2(h->pool, txn->req.cap[h->index]);
7390 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7391 }
7392
7393 if (txn->rsp.cap) {
7394 struct cap_hdr *h;
7395 for (h = s->fe->rsp_cap; h; h = h->next)
7396 pool_free2(h->pool, txn->rsp.cap[h->index]);
7397 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7398 }
7399
Willy Tarreau0937bc42009-12-22 15:03:09 +01007400}
7401
7402/* to be used at the end of a transaction to prepare a new one */
7403void http_reset_txn(struct session *s)
7404{
7405 http_end_txn(s);
7406 http_init_txn(s);
7407
7408 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007409 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007410 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007411 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007412 /* re-init store persistence */
7413 s->store_count = 0;
7414
Willy Tarreau0937bc42009-12-22 15:03:09 +01007415 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007416
7417 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7418
Willy Tarreau739cfba2010-01-25 23:11:14 +01007419 /* We must trim any excess data from the response buffer, because we
7420 * may have blocked an invalid response from a server that we don't
7421 * want to accidentely forward once we disable the analysers, nor do
7422 * we want those data to come along with next response. A typical
7423 * example of such data would be from a buggy server responding to
7424 * a HEAD with some data, or sending more than the advertised
7425 * content-length.
7426 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007427 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007428 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007429
Willy Tarreau0937bc42009-12-22 15:03:09 +01007430 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007431 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007432
Willy Tarreaud04e8582010-05-31 12:31:35 +02007433 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007434 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007435
7436 s->req->rex = TICK_ETERNITY;
7437 s->req->wex = TICK_ETERNITY;
7438 s->req->analyse_exp = TICK_ETERNITY;
7439 s->rep->rex = TICK_ETERNITY;
7440 s->rep->wex = TICK_ETERNITY;
7441 s->rep->analyse_exp = TICK_ETERNITY;
7442}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007443
Willy Tarreauff011f22011-01-06 17:51:27 +01007444void free_http_req_rules(struct list *r) {
7445 struct http_req_rule *tr, *pr;
7446
7447 list_for_each_entry_safe(pr, tr, r, list) {
7448 LIST_DEL(&pr->list);
7449 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7450 free(pr->http_auth.realm);
7451
7452 free(pr);
7453 }
7454}
7455
7456struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7457{
7458 struct http_req_rule *rule;
7459 int cur_arg;
7460
7461 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7462 if (!rule) {
7463 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7464 return NULL;
7465 }
7466
7467 if (!*args[0]) {
7468 goto req_error_parsing;
7469 } else if (!strcmp(args[0], "allow")) {
7470 rule->action = HTTP_REQ_ACT_ALLOW;
7471 cur_arg = 1;
7472 } else if (!strcmp(args[0], "deny")) {
7473 rule->action = HTTP_REQ_ACT_DENY;
7474 cur_arg = 1;
7475 } else if (!strcmp(args[0], "auth")) {
7476 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7477 cur_arg = 1;
7478
7479 while(*args[cur_arg]) {
7480 if (!strcmp(args[cur_arg], "realm")) {
7481 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7482 cur_arg+=2;
7483 continue;
7484 } else
7485 break;
7486 }
7487 } else {
7488req_error_parsing:
7489 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7490 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7491 return NULL;
7492 }
7493
7494 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7495 struct acl_cond *cond;
7496
7497 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7498 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7499 file, linenum, args[0]);
7500 return NULL;
7501 }
7502 rule->cond = cond;
7503 }
7504 else if (*args[cur_arg]) {
7505 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7506 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7507 file, linenum, args[0], args[cur_arg]);
7508 return NULL;
7509 }
7510
7511 return rule;
7512}
7513
Willy Tarreau8797c062007-05-07 00:55:35 +02007514/************************************************************************/
7515/* The code below is dedicated to ACL parsing and matching */
7516/************************************************************************/
7517
7518
7519
7520
7521/* 1. Check on METHOD
7522 * We use the pre-parsed method if it is known, and store its number as an
7523 * integer. If it is unknown, we use the pointer and the length.
7524 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007525static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007526{
7527 int len, meth;
7528
Willy Tarreauae8b7962007-06-09 23:10:04 +02007529 len = strlen(*text);
7530 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007531
7532 pattern->val.i = meth;
7533 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007534 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007535 if (!pattern->ptr.str)
7536 return 0;
7537 pattern->len = len;
7538 }
7539 return 1;
7540}
7541
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007542/* This function fetches the method of current HTTP request and stores
7543 * it in the global pattern struct as a chunk. There are two possibilities :
7544 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7545 * in <len> and <ptr> is NULL ;
7546 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7547 * <len> to its length.
7548 * This is intended to be used with acl_match_meth() only.
7549 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007550static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007551acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7552 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007553{
7554 int meth;
7555 struct http_txn *txn = l7;
7556
Willy Tarreaub6866442008-07-14 23:54:42 +02007557 if (!txn)
7558 return 0;
7559
Willy Tarreau655dce92009-11-08 13:10:58 +01007560 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007561 return 0;
7562
Willy Tarreau8797c062007-05-07 00:55:35 +02007563 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007564 temp_pattern.data.str.len = meth;
7565 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007566 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007567 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7568 /* ensure the indexes are not affected */
7569 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007570 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007571 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007572 }
7573 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7574 return 1;
7575}
7576
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007577/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007578static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7579{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007580 int icase;
7581
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007582
7583 if (temp_pattern.data.str.str == NULL) {
7584 /* well-known method */
7585 if (temp_pattern.data.str.len == pattern->val.i)
7586 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007587 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007588 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007589
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007590 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7591 if (pattern->val.i != HTTP_METH_OTHER)
7592 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007593
7594 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007595 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007596 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007597
7598 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007599 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7600 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007601 return ACL_PAT_FAIL;
7602 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007603}
7604
7605/* 2. Check on Request/Status Version
7606 * We simply compare strings here.
7607 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007608static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007609{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007610 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007611 if (!pattern->ptr.str)
7612 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007613 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007614 return 1;
7615}
7616
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007617static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007618acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7619 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007620{
7621 struct http_txn *txn = l7;
7622 char *ptr;
7623 int len;
7624
Willy Tarreaub6866442008-07-14 23:54:42 +02007625 if (!txn)
7626 return 0;
7627
Willy Tarreau655dce92009-11-08 13:10:58 +01007628 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007629 return 0;
7630
Willy Tarreau8797c062007-05-07 00:55:35 +02007631 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007632 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007633
7634 while ((len-- > 0) && (*ptr++ != '/'));
7635 if (len <= 0)
7636 return 0;
7637
Willy Tarreau664092c2011-12-16 19:11:42 +01007638 temp_pattern.data.str.str = ptr;
7639 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007640
7641 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7642 return 1;
7643}
7644
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007645static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007646acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7647 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007648{
7649 struct http_txn *txn = l7;
7650 char *ptr;
7651 int len;
7652
Willy Tarreaub6866442008-07-14 23:54:42 +02007653 if (!txn)
7654 return 0;
7655
Willy Tarreau655dce92009-11-08 13:10:58 +01007656 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007657 return 0;
7658
Willy Tarreau8797c062007-05-07 00:55:35 +02007659 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007660 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007661
7662 while ((len-- > 0) && (*ptr++ != '/'));
7663 if (len <= 0)
7664 return 0;
7665
Willy Tarreau664092c2011-12-16 19:11:42 +01007666 temp_pattern.data.str.str = ptr;
7667 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007668
7669 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7670 return 1;
7671}
7672
7673/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007674static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007675acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7676 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007677{
7678 struct http_txn *txn = l7;
7679 char *ptr;
7680 int len;
7681
Willy Tarreaub6866442008-07-14 23:54:42 +02007682 if (!txn)
7683 return 0;
7684
Willy Tarreau655dce92009-11-08 13:10:58 +01007685 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007686 return 0;
7687
Willy Tarreau8797c062007-05-07 00:55:35 +02007688 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007689 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007690
Willy Tarreaua5e37562011-12-16 17:06:15 +01007691 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007692 test->flags = ACL_TEST_F_VOL_1ST;
7693 return 1;
7694}
7695
7696/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007697static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007698acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7699 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007700{
7701 struct http_txn *txn = l7;
7702
Willy Tarreaub6866442008-07-14 23:54:42 +02007703 if (!txn)
7704 return 0;
7705
Willy Tarreau655dce92009-11-08 13:10:58 +01007706 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007707 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007708
Willy Tarreauc11416f2007-06-17 16:58:38 +02007709 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7710 /* ensure the indexes are not affected */
7711 return 0;
7712
Willy Tarreau664092c2011-12-16 19:11:42 +01007713 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007714 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007715
Willy Tarreauf3d25982007-05-08 22:45:09 +02007716 /* we do not need to set READ_ONLY because the data is in a buffer */
7717 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007718 return 1;
7719}
7720
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007721static int
7722acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7723 struct acl_expr *expr, struct acl_test *test)
7724{
7725 struct http_txn *txn = l7;
7726
Willy Tarreaub6866442008-07-14 23:54:42 +02007727 if (!txn)
7728 return 0;
7729
Willy Tarreau655dce92009-11-08 13:10:58 +01007730 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007731 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007732
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007733 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7734 /* ensure the indexes are not affected */
7735 return 0;
7736
7737 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007738 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 +01007739 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7740 return 0;
7741 temp_pattern.type = PATTERN_TYPE_IP;
7742 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007743
7744 /*
7745 * If we are parsing url in frontend space, we prepare backend stage
7746 * to not parse again the same url ! optimization lazyness...
7747 */
7748 if (px->options & PR_O_HTTP_PROXY)
7749 l4->flags |= SN_ADDR_SET;
7750
Willy Tarreauf4362b32011-12-16 17:49:52 +01007751 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007752 return 1;
7753}
7754
7755static int
7756acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7757 struct acl_expr *expr, struct acl_test *test)
7758{
7759 struct http_txn *txn = l7;
7760
Willy Tarreaub6866442008-07-14 23:54:42 +02007761 if (!txn)
7762 return 0;
7763
Willy Tarreau655dce92009-11-08 13:10:58 +01007764 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007765 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007766
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007767 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7768 /* ensure the indexes are not affected */
7769 return 0;
7770
7771 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007772 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 +01007773 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007774
7775 if (px->options & PR_O_HTTP_PROXY)
7776 l4->flags |= SN_ADDR_SET;
7777
7778 test->flags = ACL_TEST_F_READ_ONLY;
7779 return 1;
7780}
7781
Willy Tarreauc11416f2007-06-17 16:58:38 +02007782/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7783 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7784 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007785static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007786acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007787 struct acl_expr *expr, struct acl_test *test)
7788{
7789 struct http_txn *txn = l7;
7790 struct hdr_idx *idx = &txn->hdr_idx;
7791 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007792
Willy Tarreaub6866442008-07-14 23:54:42 +02007793 if (!txn)
7794 return 0;
7795
Willy Tarreau33a7e692007-06-10 19:45:56 +02007796 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7797 /* search for header from the beginning */
7798 ctx->idx = 0;
7799
Willy Tarreau33a7e692007-06-10 19:45:56 +02007800 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7801 test->flags |= ACL_TEST_F_FETCH_MORE;
7802 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007803 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7804 temp_pattern.data.str.len = ctx->vlen;
7805
Willy Tarreau33a7e692007-06-10 19:45:56 +02007806 return 1;
7807 }
7808
7809 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7810 test->flags |= ACL_TEST_F_VOL_HDR;
7811 return 0;
7812}
7813
Willy Tarreau33a7e692007-06-10 19:45:56 +02007814static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007815acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7816 struct acl_expr *expr, struct acl_test *test)
7817{
7818 struct http_txn *txn = l7;
7819
Willy Tarreaub6866442008-07-14 23:54:42 +02007820 if (!txn)
7821 return 0;
7822
Willy Tarreau655dce92009-11-08 13:10:58 +01007823 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007824 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007825
Willy Tarreauc11416f2007-06-17 16:58:38 +02007826 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7827 /* ensure the indexes are not affected */
7828 return 0;
7829
Willy Tarreau3a215be2012-03-09 21:39:51 +01007830 return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007831}
7832
7833static int
7834acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7835 struct acl_expr *expr, struct acl_test *test)
7836{
7837 struct http_txn *txn = l7;
7838
Willy Tarreaub6866442008-07-14 23:54:42 +02007839 if (!txn)
7840 return 0;
7841
Willy Tarreau655dce92009-11-08 13:10:58 +01007842 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007843 return 0;
7844
Willy Tarreau3a215be2012-03-09 21:39:51 +01007845 return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007846}
7847
7848/* 6. Check on HTTP header count. The number of occurrences is returned.
7849 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7850 */
7851static int
7852acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007853 struct acl_expr *expr, struct acl_test *test)
7854{
7855 struct http_txn *txn = l7;
7856 struct hdr_idx *idx = &txn->hdr_idx;
7857 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007858 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007859
Willy Tarreaub6866442008-07-14 23:54:42 +02007860 if (!txn)
7861 return 0;
7862
Willy Tarreau33a7e692007-06-10 19:45:56 +02007863 ctx.idx = 0;
7864 cnt = 0;
7865 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7866 cnt++;
7867
Willy Tarreaua5e37562011-12-16 17:06:15 +01007868 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007869 test->flags = ACL_TEST_F_VOL_HDR;
7870 return 1;
7871}
7872
Willy Tarreauc11416f2007-06-17 16:58:38 +02007873static int
7874acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7875 struct acl_expr *expr, struct acl_test *test)
7876{
7877 struct http_txn *txn = l7;
7878
Willy Tarreaub6866442008-07-14 23:54:42 +02007879 if (!txn)
7880 return 0;
7881
Willy Tarreau655dce92009-11-08 13:10:58 +01007882 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007883 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007884
Willy Tarreauc11416f2007-06-17 16:58:38 +02007885 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7886 /* ensure the indexes are not affected */
7887 return 0;
7888
Willy Tarreau3a215be2012-03-09 21:39:51 +01007889 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007890}
7891
7892static int
7893acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7894 struct acl_expr *expr, struct acl_test *test)
7895{
7896 struct http_txn *txn = l7;
7897
Willy Tarreaub6866442008-07-14 23:54:42 +02007898 if (!txn)
7899 return 0;
7900
Willy Tarreau655dce92009-11-08 13:10:58 +01007901 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007902 return 0;
7903
Willy Tarreau3a215be2012-03-09 21:39:51 +01007904 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007905}
7906
Willy Tarreau33a7e692007-06-10 19:45:56 +02007907/* 7. Check on HTTP header's integer value. The integer value is returned.
7908 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007909 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007910 */
7911static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007912acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007913 struct acl_expr *expr, struct acl_test *test)
7914{
7915 struct http_txn *txn = l7;
7916 struct hdr_idx *idx = &txn->hdr_idx;
7917 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007918
Willy Tarreaub6866442008-07-14 23:54:42 +02007919 if (!txn)
7920 return 0;
7921
Willy Tarreau33a7e692007-06-10 19:45:56 +02007922 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7923 /* search for header from the beginning */
7924 ctx->idx = 0;
7925
Willy Tarreau33a7e692007-06-10 19:45:56 +02007926 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7927 test->flags |= ACL_TEST_F_FETCH_MORE;
7928 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007929 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007930 return 1;
7931 }
7932
7933 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7934 test->flags |= ACL_TEST_F_VOL_HDR;
7935 return 0;
7936}
7937
Willy Tarreauc11416f2007-06-17 16:58:38 +02007938static int
7939acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7940 struct acl_expr *expr, struct acl_test *test)
7941{
7942 struct http_txn *txn = l7;
7943
Willy Tarreaub6866442008-07-14 23:54:42 +02007944 if (!txn)
7945 return 0;
7946
Willy Tarreau655dce92009-11-08 13:10:58 +01007947 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007949
Willy Tarreauc11416f2007-06-17 16:58:38 +02007950 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7951 /* ensure the indexes are not affected */
7952 return 0;
7953
Willy Tarreau3a215be2012-03-09 21:39:51 +01007954 return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007955}
7956
7957static int
7958acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7959 struct acl_expr *expr, struct acl_test *test)
7960{
7961 struct http_txn *txn = l7;
7962
Willy Tarreaub6866442008-07-14 23:54:42 +02007963 if (!txn)
7964 return 0;
7965
Willy Tarreau655dce92009-11-08 13:10:58 +01007966 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007967 return 0;
7968
Willy Tarreau3a215be2012-03-09 21:39:51 +01007969 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007970}
7971
Willy Tarreau106f9792009-09-19 07:54:16 +02007972/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7973 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7974 */
7975static int
7976acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7977 struct acl_expr *expr, struct acl_test *test)
7978{
7979 struct http_txn *txn = l7;
7980 struct hdr_idx *idx = &txn->hdr_idx;
7981 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7982
7983 if (!txn)
7984 return 0;
7985
7986 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7987 /* search for header from the beginning */
7988 ctx->idx = 0;
7989
Willy Tarreauf4362b32011-12-16 17:49:52 +01007990 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007991 test->flags |= ACL_TEST_F_FETCH_MORE;
7992 test->flags |= ACL_TEST_F_VOL_HDR;
7993 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01007994 temp_pattern.type = PATTERN_TYPE_IP;
7995 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
7996 return 1;
7997 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02007998 }
7999
8000 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8001 test->flags |= ACL_TEST_F_VOL_HDR;
8002 return 0;
8003}
8004
8005static int
8006acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8007 struct acl_expr *expr, struct acl_test *test)
8008{
8009 struct http_txn *txn = l7;
8010
8011 if (!txn)
8012 return 0;
8013
Willy Tarreau655dce92009-11-08 13:10:58 +01008014 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008015 return 0;
8016
8017 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8018 /* ensure the indexes are not affected */
8019 return 0;
8020
Willy Tarreau3a215be2012-03-09 21:39:51 +01008021 return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008022}
8023
8024static int
8025acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8026 struct acl_expr *expr, struct acl_test *test)
8027{
8028 struct http_txn *txn = l7;
8029
8030 if (!txn)
8031 return 0;
8032
Willy Tarreau655dce92009-11-08 13:10:58 +01008033 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008034 return 0;
8035
Willy Tarreau3a215be2012-03-09 21:39:51 +01008036 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008037}
8038
Willy Tarreau737b0c12007-06-10 21:28:46 +02008039/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8040 * the first '/' after the possible hostname, and ends before the possible '?'.
8041 */
8042static int
8043acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8044 struct acl_expr *expr, struct acl_test *test)
8045{
8046 struct http_txn *txn = l7;
8047 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008048
Willy Tarreaub6866442008-07-14 23:54:42 +02008049 if (!txn)
8050 return 0;
8051
Willy Tarreau655dce92009-11-08 13:10:58 +01008052 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008053 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008054
Willy Tarreauc11416f2007-06-17 16:58:38 +02008055 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8056 /* ensure the indexes are not affected */
8057 return 0;
8058
Willy Tarreau3a215be2012-03-09 21:39:51 +01008059 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 +01008060 ptr = http_get_path(txn);
8061 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008062 return 0;
8063
8064 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008065 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008066
8067 while (ptr < end && *ptr != '?')
8068 ptr++;
8069
Willy Tarreau664092c2011-12-16 19:11:42 +01008070 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008071
8072 /* we do not need to set READ_ONLY because the data is in a buffer */
8073 test->flags = ACL_TEST_F_VOL_1ST;
8074 return 1;
8075}
8076
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008077static int
8078acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8079 struct acl_expr *expr, struct acl_test *test)
8080{
8081 struct buffer *req = s->req;
8082 struct http_txn *txn = &s->txn;
8083 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008084
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008085 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8086 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8087 */
8088
8089 if (!s || !req)
8090 return 0;
8091
Willy Tarreau655dce92009-11-08 13:10:58 +01008092 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008093 /* Already decoded as OK */
8094 test->flags |= ACL_TEST_F_SET_RES_PASS;
8095 return 1;
8096 }
8097
8098 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008099 if (likely(msg->next < req->i))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008100 http_msg_analyzer(req, msg, &txn->hdr_idx);
8101
Willy Tarreau655dce92009-11-08 13:10:58 +01008102 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008103 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8104 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8105 return 1;
8106 }
8107 /* wait for final state */
8108 test->flags |= ACL_TEST_F_MAY_CHANGE;
8109 return 0;
8110 }
8111
8112 /* OK we got a valid HTTP request. We have some minor preparation to
8113 * perform so that further checks can rely on HTTP tests.
8114 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008115 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008116 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8117 s->flags |= SN_REDIRECTABLE;
8118
8119 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8120 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8121 return 1;
8122 }
8123
8124 test->flags |= ACL_TEST_F_SET_RES_PASS;
8125 return 1;
8126}
8127
Willy Tarreau7f18e522010-10-22 20:04:13 +02008128/* return a valid test if the current request is the first one on the connection */
8129static int
8130acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8131 struct acl_expr *expr, struct acl_test *test)
8132{
8133 if (!s)
8134 return 0;
8135
8136 if (s->txn.flags & TX_NOT_FIRST)
8137 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8138 else
8139 test->flags |= ACL_TEST_F_SET_RES_PASS;
8140
8141 return 1;
8142}
8143
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008144static int
8145acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8146 struct acl_expr *expr, struct acl_test *test)
8147{
8148
8149 if (!s)
8150 return 0;
8151
8152 if (!get_http_auth(s))
8153 return 0;
8154
8155 test->ctx.a[0] = expr->arg.ul;
8156 test->ctx.a[1] = s->txn.auth.user;
8157 test->ctx.a[2] = s->txn.auth.pass;
8158
8159 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8160
8161 return 1;
8162}
Willy Tarreau8797c062007-05-07 00:55:35 +02008163
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008164/* Try to find the next occurrence of a cookie name in a cookie header value.
8165 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8166 * the cookie value is returned into *value and *value_l, and the function
8167 * returns a pointer to the next pointer to search from if the value was found.
8168 * Otherwise if the cookie was not found, NULL is returned and neither value
8169 * nor value_l are touched. The input <hdr> string should first point to the
8170 * header's value, and the <hdr_end> pointer must point to the first character
8171 * not part of the value. <list> must be non-zero if value may represent a list
8172 * of values (cookie headers). This makes it faster to abort parsing when no
8173 * list is expected.
8174 */
8175static char *
8176extract_cookie_value(char *hdr, const char *hdr_end,
8177 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008178 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008179{
8180 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8181 char *next;
8182
8183 /* we search at least a cookie name followed by an equal, and more
8184 * generally something like this :
8185 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8186 */
8187 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8188 /* Iterate through all cookies on this line */
8189
8190 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8191 att_beg++;
8192
8193 /* find att_end : this is the first character after the last non
8194 * space before the equal. It may be equal to hdr_end.
8195 */
8196 equal = att_end = att_beg;
8197
8198 while (equal < hdr_end) {
8199 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8200 break;
8201 if (http_is_spht[(unsigned char)*equal++])
8202 continue;
8203 att_end = equal;
8204 }
8205
8206 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8207 * is between <att_beg> and <equal>, both may be identical.
8208 */
8209
8210 /* look for end of cookie if there is an equal sign */
8211 if (equal < hdr_end && *equal == '=') {
8212 /* look for the beginning of the value */
8213 val_beg = equal + 1;
8214 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8215 val_beg++;
8216
8217 /* find the end of the value, respecting quotes */
8218 next = find_cookie_value_end(val_beg, hdr_end);
8219
8220 /* make val_end point to the first white space or delimitor after the value */
8221 val_end = next;
8222 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8223 val_end--;
8224 } else {
8225 val_beg = val_end = next = equal;
8226 }
8227
8228 /* We have nothing to do with attributes beginning with '$'. However,
8229 * they will automatically be removed if a header before them is removed,
8230 * since they're supposed to be linked together.
8231 */
8232 if (*att_beg == '$')
8233 continue;
8234
8235 /* Ignore cookies with no equal sign */
8236 if (equal == next)
8237 continue;
8238
8239 /* Now we have the cookie name between att_beg and att_end, and
8240 * its value between val_beg and val_end.
8241 */
8242
8243 if (att_end - att_beg == cookie_name_l &&
8244 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8245 /* let's return this value and indicate where to go on from */
8246 *value = val_beg;
8247 *value_l = val_end - val_beg;
8248 return next + 1;
8249 }
8250
8251 /* Set-Cookie headers only have the name in the first attr=value part */
8252 if (!list)
8253 break;
8254 }
8255
8256 return NULL;
8257}
8258
8259/* Iterate over all cookies present in a request. The context is stored in
8260 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8261 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8262 * non-null, then multiple cookies may be parsed on the same line.
8263 * The cookie name is in expr->arg and the name length in expr->arg_len.
8264 */
8265static int
8266acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8267 const char *hdr_name, int hdr_name_len, int multi,
8268 struct acl_expr *expr, struct acl_test *test)
8269{
8270 struct http_txn *txn = l7;
8271 struct hdr_idx *idx = &txn->hdr_idx;
8272 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8273
8274 if (!txn)
8275 return 0;
8276
8277 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8278 /* search for the header from the beginning, we must first initialize
8279 * the search parameters.
8280 */
8281 test->ctx.a[0] = NULL;
8282 ctx->idx = 0;
8283 }
8284
8285 while (1) {
8286 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8287 if (!test->ctx.a[0]) {
8288 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8289 goto out;
8290
8291 if (ctx->vlen < expr->arg_len + 1)
8292 continue;
8293
8294 test->ctx.a[0] = ctx->line + ctx->val;
8295 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8296 }
8297
8298 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8299 expr->arg.str, expr->arg_len, multi,
8300 &temp_pattern.data.str.str,
8301 &temp_pattern.data.str.len);
8302 if (test->ctx.a[0]) {
8303 /* one value was returned into temp_pattern.data.str.{str,len} */
8304 test->flags |= ACL_TEST_F_FETCH_MORE;
8305 test->flags |= ACL_TEST_F_VOL_HDR;
8306 return 1;
8307 }
8308 }
8309
8310 out:
8311 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8312 test->flags |= ACL_TEST_F_VOL_HDR;
8313 return 0;
8314}
8315
8316static int
8317acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8318 struct acl_expr *expr, struct acl_test *test)
8319{
8320 struct http_txn *txn = l7;
8321
8322 if (!txn)
8323 return 0;
8324
8325 if (txn->req.msg_state < HTTP_MSG_BODY)
8326 return 0;
8327
8328 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8329 /* ensure the indexes are not affected */
8330 return 0;
8331
8332 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008333 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 +02008334}
8335
8336static int
8337acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8338 struct acl_expr *expr, struct acl_test *test)
8339{
8340 struct http_txn *txn = l7;
8341
8342 if (!txn)
8343 return 0;
8344
8345 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8346 return 0;
8347
8348 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008349 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 +02008350}
8351
8352/* Iterate over all cookies present in a request to count how many occurrences
8353 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8354 * multiple cookies may be parsed on the same line.
8355 */
8356static int
8357acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8358 const char *hdr_name, int hdr_name_len, int multi,
8359 struct acl_expr *expr, struct acl_test *test)
8360{
8361 struct http_txn *txn = l7;
8362 struct hdr_idx *idx = &txn->hdr_idx;
8363 struct hdr_ctx ctx;
8364 int cnt;
8365 char *val_beg, *val_end;
8366
8367 if (!txn)
8368 return 0;
8369
Willy Tarreau46787ed2012-04-11 17:28:40 +02008370 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008371 ctx.idx = 0;
8372 cnt = 0;
8373
8374 while (1) {
8375 /* Note: val_beg == NULL every time we need to fetch a new header */
8376 if (!val_beg) {
8377 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8378 break;
8379
8380 if (ctx.vlen < expr->arg_len + 1)
8381 continue;
8382
8383 val_beg = ctx.line + ctx.val;
8384 val_end = val_beg + ctx.vlen;
8385 }
8386
8387 while ((val_beg = extract_cookie_value(val_beg, val_end,
8388 expr->arg.str, expr->arg_len, multi,
8389 &temp_pattern.data.str.str,
8390 &temp_pattern.data.str.len))) {
8391 cnt++;
8392 }
8393 }
8394
8395 temp_pattern.data.integer = cnt;
8396 test->flags |= ACL_TEST_F_VOL_HDR;
8397 return 1;
8398}
8399
8400static int
8401acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8402 struct acl_expr *expr, struct acl_test *test)
8403{
8404 struct http_txn *txn = l7;
8405
8406 if (!txn)
8407 return 0;
8408
8409 if (txn->req.msg_state < HTTP_MSG_BODY)
8410 return 0;
8411
8412 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8413 /* ensure the indexes are not affected */
8414 return 0;
8415
8416 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008417 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 +02008418}
8419
8420static int
8421acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8422 struct acl_expr *expr, struct acl_test *test)
8423{
8424 struct http_txn *txn = l7;
8425
8426 if (!txn)
8427 return 0;
8428
8429 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8430 return 0;
8431
8432 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008433 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 +02008434}
8435
Willy Tarreau8797c062007-05-07 00:55:35 +02008436/************************************************************************/
8437/* All supported keywords must be declared here. */
8438/************************************************************************/
8439
8440/* Note: must not be declared <const> as its list will be overwritten */
8441static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008442 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8443
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008444 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008445 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8446 { "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 +02008447 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008448
Willy Tarreauc4262962010-05-10 23:42:40 +02008449 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008450 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8451 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8452 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8453 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8454 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8455 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008456 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008457 { "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 +02008458 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008459
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008460 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008461 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008462 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8463 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8464 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8465 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8466 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8467 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8468 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008469 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008470 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008471 { "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 +02008472
Willy Tarreauc4262962010-05-10 23:42:40 +02008473 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008474 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8475 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8476 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8477 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8478 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8479 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8480 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008481 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008482 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008483 { "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 +02008484
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008485 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8486 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8487 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8488 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8489 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8490 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8491 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8492 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8493 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8494
8495 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8496 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8497 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8498 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8499 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8500 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8501 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8502 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8503 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8504
Willy Tarreauc4262962010-05-10 23:42:40 +02008505 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008506 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8507 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8508 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8509 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8510 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8511 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008512 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008513
Willy Tarreau7f18e522010-10-22 20:04:13 +02008514 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8515 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8516 { "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 +02008517 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008518}};
8519
Willy Tarreau4a568972010-05-12 08:08:50 +02008520/************************************************************************/
8521/* The code below is dedicated to pattern fetching and matching */
8522/************************************************************************/
8523
Willy Tarreaue428fb72011-12-16 21:50:30 +01008524/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008525static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008526pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8527 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008528{
8529 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008530
Willy Tarreaue428fb72011-12-16 21:50:30 +01008531 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8532 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008533}
8534
David Cournapeau16023ee2010-12-23 20:55:41 +09008535/*
8536 * Given a path string and its length, find the position of beginning of the
8537 * query string. Returns NULL if no query string is found in the path.
8538 *
8539 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8540 *
8541 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8542 */
8543static inline char *find_query_string(char *path, size_t path_l)
8544{
8545 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008546
David Cournapeau16023ee2010-12-23 20:55:41 +09008547 p = memchr(path, '?', path_l);
8548 return p ? p + 1 : NULL;
8549}
8550
8551static inline int is_param_delimiter(char c)
8552{
8553 return c == '&' || c == ';';
8554}
8555
8556/*
8557 * Given a url parameter, find the starting position of the first occurence,
8558 * or NULL if the parameter is not found.
8559 *
8560 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8561 * the function will return query_string+8.
8562 */
8563static char*
8564find_url_param_pos(char* query_string, size_t query_string_l,
8565 char* url_param_name, size_t url_param_name_l)
8566{
8567 char *pos, *last;
8568
8569 pos = query_string;
8570 last = query_string + query_string_l - url_param_name_l - 1;
8571
8572 while (pos <= last) {
8573 if (pos[url_param_name_l] == '=') {
8574 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8575 return pos;
8576 pos += url_param_name_l + 1;
8577 }
8578 while (pos <= last && !is_param_delimiter(*pos))
8579 pos++;
8580 pos++;
8581 }
8582 return NULL;
8583}
8584
8585/*
8586 * Given a url parameter name, returns its value and size into *value and
8587 * *value_l respectively, and returns non-zero. If the parameter is not found,
8588 * zero is returned and value/value_l are not touched.
8589 */
8590static int
8591find_url_param_value(char* path, size_t path_l,
8592 char* url_param_name, size_t url_param_name_l,
8593 char** value, size_t* value_l)
8594{
8595 char *query_string, *qs_end;
8596 char *arg_start;
8597 char *value_start, *value_end;
8598
8599 query_string = find_query_string(path, path_l);
8600 if (!query_string)
8601 return 0;
8602
8603 qs_end = path + path_l;
8604 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8605 url_param_name, url_param_name_l);
8606 if (!arg_start)
8607 return 0;
8608
8609 value_start = arg_start + url_param_name_l + 1;
8610 value_end = value_start;
8611
8612 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8613 value_end++;
8614
8615 *value = value_start;
8616 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008617 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008618}
8619
8620static int
8621pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8622 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8623{
8624 struct http_txn *txn = l7;
8625 struct http_msg *msg = &txn->req;
8626 char *url_param_value;
8627 size_t url_param_value_l;
8628
Willy Tarreau3a215be2012-03-09 21:39:51 +01008629 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 +09008630 arg_p->data.str.str, arg_p->data.str.len,
8631 &url_param_value, &url_param_value_l))
8632 return 0;
8633
8634 data->str.str = url_param_value;
8635 data->str.len = url_param_value_l;
8636 return 1;
8637}
8638
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008639/* Try to find in request or response message is in <msg> and whose transaction
8640 * is in <txn> the last occurrence of a cookie name in all cookie header values
8641 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8642 * pointer and size of the last occurrence of the cookie value is returned into
8643 * <value> and <value_l>, and the function returns non-zero if the value was
8644 * found. Otherwise if the cookie was not found, zero is returned and neither
8645 * value nor value_l are touched. The input hdr string should begin at the
8646 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8647 * value may represent a list of values (cookie headers).
8648 */
8649
8650static int
8651find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8652 const char *hdr_name, int hdr_name_len,
8653 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008654 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008655{
8656 struct hdr_ctx ctx;
8657 int found = 0;
8658
8659 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008660 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 +02008661 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008662 if (ctx.vlen < cookie_name_l + 1)
8663 continue;
8664
Willy Tarreau4573af92012-04-06 18:20:06 +02008665 hdr = ctx.line + ctx.val;
8666 end = hdr + ctx.vlen;
8667 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8668 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008669 }
8670 return found;
8671}
8672
8673static int
8674pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8675 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8676{
8677 struct http_txn *txn = l7;
8678 struct http_msg *msg = &txn->req;
8679 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008680 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008681 int found = 0;
8682
8683 found = find_cookie_value(msg, txn, "Cookie", 6,
8684 arg_p->data.str.str, arg_p->data.str.len, 1,
8685 &cookie_value, &cookie_value_l);
8686 if (found) {
8687 data->str.str = cookie_value;
8688 data->str.len = cookie_value_l;
8689 }
8690
8691 return found;
8692}
8693
8694
8695static int
8696pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8697 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8698{
8699 struct http_txn *txn = l7;
8700 struct http_msg *msg = &txn->rsp;
8701 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008702 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008703 int found = 0;
8704
8705 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8706 arg_p->data.str.str, arg_p->data.str.len, 1,
8707 &cookie_value, &cookie_value_l);
8708 if (found) {
8709 data->str.str = cookie_value;
8710 data->str.len = cookie_value_l;
8711 }
8712
8713 return found;
8714}
8715
Emeric Brun485479d2010-09-23 18:02:19 +02008716
Willy Tarreau4a568972010-05-12 08:08:50 +02008717/************************************************************************/
8718/* All supported keywords must be declared here. */
8719/************************************************************************/
8720/* Note: must not be declared <const> as its list will be overwritten */
8721static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008722 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008723 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008724 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8725 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008726 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008727}};
8728
Willy Tarreau8797c062007-05-07 00:55:35 +02008729
8730__attribute__((constructor))
8731static void __http_protocol_init(void)
8732{
8733 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008734 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008735}
8736
8737
Willy Tarreau58f10d72006-12-04 02:26:12 +01008738/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008739 * Local variables:
8740 * c-indent-level: 8
8741 * c-basic-offset: 8
8742 * End:
8743 */