blob: 637c856ba02809ee6b9ffc7c5a79ef4268ae35f2 [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>
Willy Tarreau61612d42012-04-19 18:42:05 +020045#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010046#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010049#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020050#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020052#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010054#include <proto/hdr_idx.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 Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010062#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020063#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/task.h>
65
Willy Tarreau522d6c02009-12-06 18:49:18 +010066const char HTTP_100[] =
67 "HTTP/1.1 100 Continue\r\n\r\n";
68
69const struct chunk http_100_chunk = {
70 .str = (char *)&HTTP_100,
71 .len = sizeof(HTTP_100)-1
72};
73
Willy Tarreaua9679ac2010-01-03 17:32:57 +010074/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020075const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010076 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
95const char *HTTP_401_fmt =
96 "HTTP/1.0 401 Unauthorized\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020099 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
101 "\r\n"
102 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
103
Willy Tarreau844a7e72010-01-31 21:46:18 +0100104const char *HTTP_407_fmt =
105 "HTTP/1.0 407 Unauthorized\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Connection: close\r\n"
108 "Content-Type: text/html\r\n"
109 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
110 "\r\n"
111 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
112
Willy Tarreau0f772532006-12-23 20:51:41 +0100113
114const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200115 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200126 [HTTP_ERR_200] =
127 "HTTP/1.0 200 OK\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
133
Willy Tarreau0f772532006-12-23 20:51:41 +0100134 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100135 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
141
142 [HTTP_ERR_403] =
143 "HTTP/1.0 403 Forbidden\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
149
150 [HTTP_ERR_408] =
151 "HTTP/1.0 408 Request Time-out\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
157
158 [HTTP_ERR_500] =
159 "HTTP/1.0 500 Server Error\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
165
166 [HTTP_ERR_502] =
167 "HTTP/1.0 502 Bad Gateway\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
173
174 [HTTP_ERR_503] =
175 "HTTP/1.0 503 Service Unavailable\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
181
182 [HTTP_ERR_504] =
183 "HTTP/1.0 504 Gateway Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
189
190};
191
Cyril Bonté19979e12012-04-04 12:57:21 +0200192/* status codes available for the stats admin page (strictly 4 chars length) */
193const char *stat_status_codes[STAT_STATUS_SIZE] = {
194 [STAT_STATUS_DENY] = "DENY",
195 [STAT_STATUS_DONE] = "DONE",
196 [STAT_STATUS_ERRP] = "ERRP",
197 [STAT_STATUS_EXCD] = "EXCD",
198 [STAT_STATUS_NONE] = "NONE",
199 [STAT_STATUS_PART] = "PART",
200 [STAT_STATUS_UNKN] = "UNKN",
201};
202
203
Willy Tarreau80587432006-12-24 17:47:20 +0100204/* We must put the messages here since GCC cannot initialize consts depending
205 * on strlen().
206 */
207struct chunk http_err_chunks[HTTP_ERR_SIZE];
208
Willy Tarreau42250582007-04-01 01:30:43 +0200209#define FD_SETS_ARE_BITFIELDS
210#ifdef FD_SETS_ARE_BITFIELDS
211/*
212 * This map is used with all the FD_* macros to check whether a particular bit
213 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
214 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
215 * byte should be encoded. Be careful to always pass bytes from 0 to 255
216 * exclusively to the macros.
217 */
218fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220
221#else
222#error "Check if your OS uses bitfields for fd_sets"
223#endif
224
Willy Tarreau80587432006-12-24 17:47:20 +0100225void init_proto_http()
226{
Willy Tarreau42250582007-04-01 01:30:43 +0200227 int i;
228 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100229 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200230
Willy Tarreau80587432006-12-24 17:47:20 +0100231 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
232 if (!http_err_msgs[msg]) {
233 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
234 abort();
235 }
236
237 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
238 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
239 }
Willy Tarreau42250582007-04-01 01:30:43 +0200240
241 /* initialize the log header encoding map : '{|}"#' should be encoded with
242 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
243 * URL encoding only requires '"', '#' to be encoded as well as non-
244 * printable characters above.
245 */
246 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
247 memset(url_encode_map, 0, sizeof(url_encode_map));
248 for (i = 0; i < 32; i++) {
249 FD_SET(i, hdr_encode_map);
250 FD_SET(i, url_encode_map);
251 }
252 for (i = 127; i < 256; i++) {
253 FD_SET(i, hdr_encode_map);
254 FD_SET(i, url_encode_map);
255 }
256
257 tmp = "\"#{|}";
258 while (*tmp) {
259 FD_SET(*tmp, hdr_encode_map);
260 tmp++;
261 }
262
263 tmp = "\"#";
264 while (*tmp) {
265 FD_SET(*tmp, url_encode_map);
266 tmp++;
267 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200268
269 /* memory allocations */
270 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200271 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
392 int size = 0;
393 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100394 "[%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 +0100395 line,
396 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 +0100397 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 +0100398 write(-1, trash, size);
399 size = 0;
400 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100401 " %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 +0100402 line,
403 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 +0100404 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 +0100405
406 write(-1, trash, size);
407}
408#else
409#define http_silent_debug(l,s) do { } while (0)
410#endif
411
412/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100413 * Adds a header and its CRLF at the tail of the message's buffer, just before
414 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100415 * The header is also automatically added to the index <hdr_idx>, and the end
416 * of headers is automatically adjusted. The number of bytes added is returned
417 * on success, otherwise <0 is returned indicating an error.
418 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100419int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100420{
421 int bytes, len;
422
423 len = strlen(text);
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100424 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100425 if (!bytes)
426 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100427 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100428 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
429}
430
431/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100432 * Adds a header and its CRLF at the tail of the message's buffer, just before
433 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434 * the buffer is only opened and the space reserved, but nothing is copied.
435 * The header is also automatically added to the index <hdr_idx>, and the end
436 * of headers is automatically adjusted. The number of bytes added is returned
437 * on success, otherwise <0 is returned indicating an error.
438 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100439int http_header_add_tail2(struct http_msg *msg,
440 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100441{
442 int bytes;
443
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100444 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100445 if (!bytes)
446 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100447 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
449}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
451/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100452 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
453 * If so, returns the position of the first non-space character relative to
454 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
455 * to return a pointer to the place after the first space. Returns 0 if the
456 * header name does not match. Checks are case-insensitive.
457 */
458int http_header_match2(const char *hdr, const char *end,
459 const char *name, int len)
460{
461 const char *val;
462
463 if (hdr + len >= end)
464 return 0;
465 if (hdr[len] != ':')
466 return 0;
467 if (strncasecmp(hdr, name, len) != 0)
468 return 0;
469 val = hdr + len + 1;
470 while (val < end && HTTP_IS_SPHT(*val))
471 val++;
472 if ((val >= end) && (len + 2 <= end - hdr))
473 return len + 2; /* we may replace starting from second space */
474 return val - hdr;
475}
476
Willy Tarreau68085d82010-01-18 14:54:04 +0100477/* Find the end of the header value contained between <s> and <e>. See RFC2616,
478 * par 2.2 for more information. Note that it requires a valid header to return
479 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100481char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482{
483 int quoted, qdpair;
484
485 quoted = qdpair = 0;
486 for (; s < e; s++) {
487 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200488 else if (quoted) {
489 if (*s == '\\') qdpair = 1;
490 else if (*s == '"') quoted = 0;
491 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 else if (*s == '"') quoted = 1;
493 else if (*s == ',') return s;
494 }
495 return s;
496}
497
498/* Find the first or next occurrence of header <name> in message buffer <sol>
499 * using headers index <idx>, and return it in the <ctx> structure. This
500 * structure holds everything necessary to use the header and find next
501 * occurrence. If its <idx> member is 0, the header is searched from the
502 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 * 1 when it finds a value, and 0 when there is no more. It is designed to work
504 * with headers defined as comma-separated lists. As a special case, if ctx->val
505 * is NULL when searching for a new values of a header, the current header is
506 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 */
508int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
Willy Tarreau68085d82010-01-18 14:54:04 +0100512 char *eol, *sov;
513 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515 cur_idx = ctx->idx;
516 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 /* We have previously returned a value, let's search
518 * another one on the same line.
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200521 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100522 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200523 eol = sol + idx->v[cur_idx].len;
524
525 if (sov >= eol)
526 /* no more values in this header */
527 goto next_hdr;
528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 /* values remaining for this header, skip the comma but save it
530 * for later use (eg: for header deletion).
531 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 sov++;
533 while (sov < eol && http_is_lws[(unsigned char)*sov])
534 sov++;
535
536 goto return_hdr;
537 }
538
539 /* first request for this header */
540 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100541 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 while (cur_idx) {
544 eol = sol + idx->v[cur_idx].len;
545
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200546 if (len == 0) {
547 /* No argument was passed, we want any header.
548 * To achieve this, we simply build a fake request. */
549 while (sol + len < eol && sol[len] != ':')
550 len++;
551 name = sol;
552 }
553
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 if ((len < eol - sol) &&
555 (sol[len] == ':') &&
556 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 sov = sol + len + 1;
559 while (sov < eol && http_is_lws[(unsigned char)*sov])
560 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 ctx->prev = old_idx;
564 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 ctx->idx = cur_idx;
566 ctx->val = sov - sol;
567
568 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200570 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200571 eol--;
572 ctx->tws++;
573 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 ctx->vlen = eol - sov;
575 return 1;
576 }
577 next_hdr:
578 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100579 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 cur_idx = idx->v[cur_idx].next;
581 }
582 return 0;
583}
584
585int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200587 struct hdr_ctx *ctx)
588{
589 return http_find_header2(name, strlen(name), sol, idx, ctx);
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Remove one value of a header. This only works on a <ctx> returned by one of
593 * the http_find_header functions. The value is removed, as well as surrounding
594 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100595 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 * message <msg>. The new index is returned. If it is zero, it means there is
597 * no more header, so any processing may stop. The ctx is always left in a form
598 * that can be handled by http_find_header2() to find next occurrence.
599 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100600int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100601{
602 int cur_idx = ctx->idx;
603 char *sol = ctx->line;
604 struct hdr_idx_elem *hdr;
605 int delta, skip_comma;
606
607 if (!cur_idx)
608 return 0;
609
610 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100613 delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100614 http_msg_move_end(msg, delta);
615 idx->used--;
616 hdr->len = 0; /* unused entry */
617 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100618 if (idx->tail == ctx->idx)
619 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 ctx->idx = ctx->prev; /* walk back to the end of previous header */
621 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
622 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200623 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 return ctx->idx;
625 }
626
627 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200628 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
629 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 */
631
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100633 delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 NULL, 0);
636 hdr->len += delta;
637 http_msg_move_end(msg, delta);
638 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200639 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 return ctx->idx;
641}
642
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100643/* This function handles a server error at the stream interface level. The
644 * stream interface is assumed to be already in a closed state. An optional
645 * message is copied into the input buffer, and an HTTP status code stored.
646 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100647 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100649static void http_server_error(struct session *t, struct stream_interface *si,
650 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100652 buffer_auto_read(si->ob);
653 buffer_abort(si->ob);
654 buffer_auto_close(si->ob);
655 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200656 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100657 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100658 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100659 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200660 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661 }
662 if (!(t->flags & SN_ERR_MASK))
663 t->flags |= err;
664 if (!(t->flags & SN_FINST_MASK))
665 t->flags |= finst;
666}
667
Willy Tarreau80587432006-12-24 17:47:20 +0100668/* This function returns the appropriate error location for the given session
669 * and message.
670 */
671
672struct chunk *error_message(struct session *s, int msgnum)
673{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200674 if (s->be->errmsg[msgnum].str)
675 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100676 else if (s->fe->errmsg[msgnum].str)
677 return &s->fe->errmsg[msgnum];
678 else
679 return &http_err_chunks[msgnum];
680}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682/*
683 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
684 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
685 */
686static http_meth_t find_http_meth(const char *str, const int len)
687{
688 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100689 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100690
691 m = ((unsigned)*str - 'A');
692
693 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100694 for (h = http_methods[m]; h->len > 0; h++) {
695 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 };
700 return HTTP_METH_OTHER;
701 }
702 return HTTP_METH_NONE;
703
704}
705
Willy Tarreau21d2af32008-02-14 20:25:24 +0100706/* Parse the URI from the given transaction (which is assumed to be in request
707 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
708 * It is returned otherwise.
709 */
710static char *
711http_get_path(struct http_txn *txn)
712{
713 char *ptr, *end;
714
Willy Tarreau3a215be2012-03-09 21:39:51 +0100715 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100716 end = ptr + txn->req.sl.rq.u_l;
717
718 if (ptr >= end)
719 return NULL;
720
721 /* RFC2616, par. 5.1.2 :
722 * Request-URI = "*" | absuri | abspath | authority
723 */
724
725 if (*ptr == '*')
726 return NULL;
727
728 if (isalpha((unsigned char)*ptr)) {
729 /* this is a scheme as described by RFC3986, par. 3.1 */
730 ptr++;
731 while (ptr < end &&
732 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
733 ptr++;
734 /* skip '://' */
735 if (ptr == end || *ptr++ != ':')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 if (ptr == end || *ptr++ != '/')
740 return NULL;
741 }
742 /* skip [user[:passwd]@]host[:[port]] */
743
744 while (ptr < end && *ptr != '/')
745 ptr++;
746
747 if (ptr == end)
748 return NULL;
749
750 /* OK, we got the '/' ! */
751 return ptr;
752}
753
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754/* Returns a 302 for a redirectable request. This may only be called just after
755 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
756 * left unchanged and will follow normal proxy processing.
757 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
761 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 char *path;
764 int len;
765
766 /* 1: create the response header */
767 rdr.len = strlen(HTTP_302);
768 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100769 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 memcpy(rdr.str, HTTP_302, rdr.len);
771
Willy Tarreau827aee92011-03-10 16:55:02 +0100772 srv = target_srv(&s->target);
773
Willy Tarreauefb453c2008-10-26 20:49:47 +0100774 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 return;
777
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100778 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
780 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
781 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100782 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783
784 /* 3: add the request URI */
785 txn = &s->txn;
786 path = http_get_path(txn);
787 if (!path)
788 return;
789
Willy Tarreau3a215be2012-03-09 21:39:51 +0100790 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 +0200791 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 return;
793
794 memcpy(rdr.str + rdr.len, path, len);
795 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100796
797 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
798 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
799 rdr.len += 29;
800 } else {
801 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
802 rdr.len += 23;
803 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804
805 /* prepare to return without error. */
Willy Tarreau060781f2012-05-07 16:50:03 +0200806 si->sock.shutr(si);
807 si->sock.shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 si->err_type = SI_ET_NONE;
809 si->err_loc = NULL;
810 si->state = SI_ST_CLO;
811
812 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100813 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814
815 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (srv)
817 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818}
819
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100820/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821 * that the server side is closed. Note that err_type is actually a
822 * bitmask, where almost only aborts may be cumulated with other
823 * values. We consider that aborted operations are more important
824 * than timeouts or errors due to the fact that nobody else in the
825 * logs might explain incomplete retries. All others should avoid
826 * being cumulated. It should normally not be possible to have multiple
827 * aborts at once, but just in case, the first one in sequence is reported.
828 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100831 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832
833 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100834 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
835 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
838 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
841 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
844 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
847 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
850 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
853 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854}
855
Willy Tarreau42250582007-04-01 01:30:43 +0200856extern const char sess_term_cond[8];
857extern const char sess_fin_state[8];
858extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200859struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200860struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100861struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100862
Willy Tarreau117f59e2007-03-04 18:17:17 +0100863/*
864 * Capture headers from message starting at <som> according to header list
865 * <cap_hdr>, and fill the <idx> structure appropriately.
866 */
867void capture_headers(char *som, struct hdr_idx *idx,
868 char **cap, struct cap_hdr *cap_hdr)
869{
870 char *eol, *sol, *col, *sov;
871 int cur_idx;
872 struct cap_hdr *h;
873 int len;
874
875 sol = som + hdr_idx_first_pos(idx);
876 cur_idx = hdr_idx_first_idx(idx);
877
878 while (cur_idx) {
879 eol = sol + idx->v[cur_idx].len;
880
881 col = sol;
882 while (col < eol && *col != ':')
883 col++;
884
885 sov = col + 1;
886 while (sov < eol && http_is_lws[(unsigned char)*sov])
887 sov++;
888
889 for (h = cap_hdr; h; h = h->next) {
890 if ((h->namelen == col - sol) &&
891 (strncasecmp(sol, h->name, h->namelen) == 0)) {
892 if (cap[h->index] == NULL)
893 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200894 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100895
896 if (cap[h->index] == NULL) {
897 Alert("HTTP capture : out of memory.\n");
898 continue;
899 }
900
901 len = eol - sov;
902 if (len > h->len)
903 len = h->len;
904
905 memcpy(cap[h->index], sov, len);
906 cap[h->index][len]=0;
907 }
908 }
909 sol = eol + idx->v[cur_idx].cr + 1;
910 cur_idx = idx->v[cur_idx].next;
911 }
912}
913
914
Willy Tarreau42250582007-04-01 01:30:43 +0200915/* either we find an LF at <ptr> or we jump to <bad>.
916 */
917#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
918
919/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
920 * otherwise to <http_msg_ood> with <state> set to <st>.
921 */
922#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
923 ptr++; \
924 if (likely(ptr < end)) \
925 goto good; \
926 else { \
927 state = (st); \
928 goto http_msg_ood; \
929 } \
930 } while (0)
931
932
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100934 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
936 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
937 * will give undefined results.
938 * Note that it is upon the caller's responsibility to ensure that ptr < end,
939 * and that msg->sol points to the beginning of the response.
940 * If a complete line is found (which implies that at least one CR or LF is
941 * found before <end>, the updated <ptr> is returned, otherwise NULL is
942 * returned indicating an incomplete line (which does not mean that parts have
943 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
944 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
945 * upon next call.
946 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200947 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100948 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
949 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200952const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100953 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100954 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100955{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100956 const char *msg_start = msg->buf->p;
957
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200960 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100961 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
963
964 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100965 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
967 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100968 state = HTTP_MSG_ERROR;
969 break;
970
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200972 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100973 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 goto http_msg_rpcode;
976 }
977 if (likely(HTTP_IS_SPHT(*ptr)))
978 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
979 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100980 state = HTTP_MSG_ERROR;
981 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100982
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200984 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 if (likely(!HTTP_IS_LWS(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
987
988 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100989 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
991 }
992
993 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100994 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 http_msg_rsp_reason:
996 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100997 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 msg->sl.st.r_l = 0;
999 goto http_msg_rpline_eol;
1000
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001002 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001004 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 goto http_msg_rpreason;
1006 }
1007 if (likely(HTTP_IS_SPHT(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1009 /* so it's a CR/LF, so there is no reason phrase */
1010 goto http_msg_rsp_reason;
1011
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001013 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 if (likely(!HTTP_IS_CRLF(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 http_msg_rpline_eol:
1018 /* We have seen the end of line. Note that we do not
1019 * necessarily have the \n yet, but at least we know that we
1020 * have EITHER \r OR \n, otherwise the response would not be
1021 * complete. We can then record the response length and return
1022 * to the caller which will be able to register it.
1023 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001024 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001025 return ptr;
1026
1027#ifdef DEBUG_FULL
1028 default:
1029 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1030 exit(1);
1031#endif
1032 }
1033
1034 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001035 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 if (ret_state)
1037 *ret_state = state;
1038 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001039 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001041}
1042
Willy Tarreau8973c702007-01-21 23:58:29 +01001043/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 * This function parses a request line between <ptr> and <end>, starting with
1045 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1046 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1047 * will give undefined results.
1048 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1049 * and that msg->sol points to the beginning of the request.
1050 * If a complete line is found (which implies that at least one CR or LF is
1051 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1052 * returned indicating an incomplete line (which does not mean that parts have
1053 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1054 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1055 * upon next call.
1056 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001058 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1059 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001060 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001062const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001063 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001064 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001066 const char *msg_start = msg->buf->p;
1067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001070 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_TOKEN(*ptr)))
1072 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001075 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_CRLF(*ptr))) {
1080 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001081 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001083 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001087 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 msg->sl.rq.v_l = 0;
1089 goto http_msg_rqline_eol;
1090 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001091 state = HTTP_MSG_ERROR;
1092 break;
1093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001095 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001097 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 goto http_msg_rquri;
1099 }
1100 if (likely(HTTP_IS_SPHT(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1102 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1103 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001106 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001107 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001111 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001115 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001116 /* non-ASCII chars are forbidden unless option
1117 * accept-invalid-http-request is enabled in the frontend.
1118 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001120 if (msg->err_pos < -1)
1121 goto invalid_char;
1122 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001123 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1125 }
1126
1127 if (likely(HTTP_IS_CRLF(*ptr))) {
1128 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1129 goto http_msg_req09_uri_e;
1130 }
1131
1132 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001133 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001134 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001135 state = HTTP_MSG_ERROR;
1136 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001138 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 goto http_msg_rqver;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1146 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1147 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001151 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001153
1154 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001155 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001156 http_msg_rqline_eol:
1157 /* We have seen the end of line. Note that we do not
1158 * necessarily have the \n yet, but at least we know that we
1159 * have EITHER \r OR \n, otherwise the request would not be
1160 * complete. We can then record the request length and return
1161 * to the caller which will be able to register it.
1162 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001163 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001164 return ptr;
1165 }
1166
1167 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001168 state = HTTP_MSG_ERROR;
1169 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171#ifdef DEBUG_FULL
1172 default:
1173 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1174 exit(1);
1175#endif
1176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180 if (ret_state)
1181 *ret_state = state;
1182 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001183 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001187/*
1188 * Returns the data from Authorization header. Function may be called more
1189 * than once so data is stored in txn->auth_data. When no header is found
1190 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1191 * searching again for something we are unable to find anyway.
1192 */
1193
1194char get_http_auth_buff[BUFSIZE];
1195
1196int
1197get_http_auth(struct session *s)
1198{
1199
1200 struct http_txn *txn = &s->txn;
1201 struct chunk auth_method;
1202 struct hdr_ctx ctx;
1203 char *h, *p;
1204 int len;
1205
1206#ifdef DEBUG_AUTH
1207 printf("Auth for session %p: %d\n", s, txn->auth.method);
1208#endif
1209
1210 if (txn->auth.method == HTTP_AUTH_WRONG)
1211 return 0;
1212
1213 if (txn->auth.method)
1214 return 1;
1215
1216 txn->auth.method = HTTP_AUTH_WRONG;
1217
1218 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001219
1220 if (txn->flags & TX_USE_PX_CONN) {
1221 h = "Proxy-Authorization";
1222 len = strlen(h);
1223 } else {
1224 h = "Authorization";
1225 len = strlen(h);
1226 }
1227
Willy Tarreau3a215be2012-03-09 21:39:51 +01001228 if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001229 return 0;
1230
1231 h = ctx.line + ctx.val;
1232
1233 p = memchr(h, ' ', ctx.vlen);
1234 if (!p || p == h)
1235 return 0;
1236
1237 chunk_initlen(&auth_method, h, 0, p-h);
1238 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1239
1240 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1241
1242 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1243 get_http_auth_buff, BUFSIZE - 1);
1244
1245 if (len < 0)
1246 return 0;
1247
1248
1249 get_http_auth_buff[len] = '\0';
1250
1251 p = strchr(get_http_auth_buff, ':');
1252
1253 if (!p)
1254 return 0;
1255
1256 txn->auth.user = get_http_auth_buff;
1257 *p = '\0';
1258 txn->auth.pass = p+1;
1259
1260 txn->auth.method = HTTP_AUTH_BASIC;
1261 return 1;
1262 }
1263
1264 return 0;
1265}
1266
Willy Tarreau58f10d72006-12-04 02:26:12 +01001267
Willy Tarreau8973c702007-01-21 23:58:29 +01001268/*
1269 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001270 * depending on the initial msg->msg_state. The caller is responsible for
1271 * ensuring that the message does not wrap. The function can be preempted
1272 * everywhere when data are missing and recalled at the exact same location
1273 * with no information loss. The message may even be realigned between two
1274 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001275 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001276 * fields. Note that msg->som and msg->sol will be initialized after completing
1277 * the first state, so that none of the msg pointers has to be initialized
1278 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001280void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001282 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001284 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 state = msg->msg_state;
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001287 ptr = buf->p + msg->next;
1288 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 if (unlikely(ptr >= end))
1291 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001292
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 /*
1295 * First, states that are specific to the response only.
1296 * We check them first so that request and headers are
1297 * closer to each other (accessed more often).
1298 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001300 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001302 /* we have a start of message, but we have to check
1303 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001304 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001306 char *beg = buf->p;
1307
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001309 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001311 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001312 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001314 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 hdr_idx_init(idx);
1316 state = HTTP_MSG_RPVER;
1317 goto http_msg_rpver;
1318 }
1319
1320 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1321 goto http_msg_invalid;
1322
1323 if (unlikely(*ptr == '\n'))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1326 /* stop here */
1327
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 EXPECT_LF_HERE(ptr, http_msg_invalid);
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1332 /* stop here */
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 case HTTP_MSG_RPVER_SP:
1337 case HTTP_MSG_RPCODE:
1338 case HTTP_MSG_RPCODE_SP:
1339 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001340 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001341 state, ptr, end,
1342 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 if (unlikely(!ptr))
1344 return;
1345
1346 /* we have a full response and we know that we have either a CR
1347 * or an LF at <ptr>.
1348 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1350
Willy Tarreau3a215be2012-03-09 21:39:51 +01001351 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (likely(*ptr == '\r'))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1354 goto http_msg_rpline_end;
1355
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001357 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 /* msg->sol must point to the first of CR or LF. */
1359 EXPECT_LF_HERE(ptr, http_msg_invalid);
1360 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1361 /* stop here */
1362
1363 /*
1364 * Second, states that are specific to the request only
1365 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001367 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 /* we have a start of message, but we have to check
1370 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001371 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001373 char *beg = buf->p;
1374
Willy Tarreau15de77e2010-01-02 21:59:16 +01001375 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001376 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001377 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001378 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001379 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001381 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001382 /* we will need this when keep-alive will be supported
1383 hdr_idx_init(idx);
1384 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 state = HTTP_MSG_RQMETH;
1386 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1390 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (unlikely(*ptr == '\n'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH_SP:
1406 case HTTP_MSG_RQURI:
1407 case HTTP_MSG_RQURI_SP:
1408 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001409 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001410 state, ptr, end,
1411 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (unlikely(!ptr))
1413 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 /* we have a full request and we know that we have either a CR
1416 * or an LF at <ptr>.
1417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreau3a215be2012-03-09 21:39:51 +01001420 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* check for HTTP/0.9 request : no version information available.
1428 * msg->sol must point to the first of CR or LF.
1429 */
1430 if (unlikely(msg->sl.rq.v_l == 0))
1431 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 EXPECT_LF_HERE(ptr, http_msg_invalid);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 /*
1438 * Common states below
1439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001442 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_CRLF(*ptr))) {
1444 goto http_msg_hdr_name;
1445 }
1446
1447 if (likely(*ptr == '\r'))
1448 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1449 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001452 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* assumes msg->sol points to the first char */
1454 if (likely(HTTP_IS_TOKEN(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001457 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001464 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001471 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001500 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001505 /* assumes msg->sol points to the first char, and msg->sov
1506 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
1508 if (likely(!HTTP_IS_CRLF(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau12e48b32012-03-05 16:57:34 +01001511 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* Note: we could also copy eol into ->eoh so that we have the
1513 * real header end in case it ends with lots of LWS, but is this
1514 * really needed ?
1515 */
1516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1518 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1528 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001529 for (; buf->p + msg->eol < ptr; msg->eol++)
1530 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_hdr_val;
1532 }
1533 http_msg_complete_header:
1534 /*
1535 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001536 * Assumes msg->sol points to the first char, msg->sov points
1537 * to the first character of the value and msg->eol to the
1538 * first CR or LF so we know how the line ends. We insert last
1539 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001541 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 idx, idx->tail) < 0))
1543 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001544
Willy Tarreau3a215be2012-03-09 21:39:51 +01001545 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (likely(!HTTP_IS_CRLF(*ptr))) {
1547 goto http_msg_hdr_name;
1548 }
1549
1550 if (likely(*ptr == '\r'))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1552 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /* Assumes msg->sol points to the first of either CR or LF */
1557 EXPECT_LF_HERE(ptr, http_msg_invalid);
1558 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001559 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001560 msg->eoh = msg->sol;
1561 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001562 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001564
1565 case HTTP_MSG_ERROR:
1566 /* this may only happen if we call http_msg_analyser() twice with an error */
1567 break;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569#ifdef DEBUG_FULL
1570 default:
1571 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1572 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
1575 http_msg_ood:
1576 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001578 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 http_msg_invalid:
1582 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
1586}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001587
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001588/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1589 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1590 * nothing is done and 1 is returned.
1591 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593{
1594 int delta;
1595 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001601 cur_end = msg->buf->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001606 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001614 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 HTTP_MSG_RQMETH,
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001616 msg->buf->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001629 * and "keep-alive" values. If we already know that some headers may safely
1630 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001631 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1632 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1633 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1634 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1635 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001636 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001638void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001639{
Willy Tarreau5b154472009-12-21 20:11:07 +01001640 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001641 const char *hdr_val = "Connection";
1642 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001643
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001644 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 return;
1646
Willy Tarreau88d349d2010-01-25 12:15:43 +01001647 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1648 hdr_val = "Proxy-Connection";
1649 hdr_len = 16;
1650 }
1651
Willy Tarreau5b154472009-12-21 20:11:07 +01001652 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001653 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1656 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001657 if (to_del & 2)
1658 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 else
1660 txn->flags |= TX_CON_KAL_SET;
1661 }
1662 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1663 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001664 if (to_del & 1)
1665 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001666 else
1667 txn->flags |= TX_CON_CLO_SET;
1668 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001669 }
1670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 txn->flags |= TX_HDR_CONN_PRS;
1672 return;
1673}
Willy Tarreau5b154472009-12-21 20:11:07 +01001674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675/* Apply desired changes on the Connection: header. Values may be removed and/or
1676 * added depending on the <wanted> flags, which are exclusively composed of
1677 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1678 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1679 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001680void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001681{
1682 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001683 const char *hdr_val = "Connection";
1684 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685
1686 ctx.idx = 0;
1687
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688
1689 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1690 hdr_val = "Proxy-Connection";
1691 hdr_len = 16;
1692 }
1693
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001695 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001696 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1697 if (wanted & TX_CON_KAL_SET)
1698 txn->flags |= TX_CON_KAL_SET;
1699 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001700 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001701 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001702 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1703 if (wanted & TX_CON_CLO_SET)
1704 txn->flags |= TX_CON_CLO_SET;
1705 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001707 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001708 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001709
1710 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1711 return;
1712
1713 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1714 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001715 hdr_val = "Connection: close";
1716 hdr_len = 17;
1717 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1718 hdr_val = "Proxy-Connection: close";
1719 hdr_len = 23;
1720 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001721 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001722 }
1723
1724 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1725 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001726 hdr_val = "Connection: keep-alive";
1727 hdr_len = 22;
1728 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1729 hdr_val = "Proxy-Connection: keep-alive";
1730 hdr_len = 28;
1731 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001732 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001733 }
1734 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001735}
1736
Willy Tarreaua458b672012-03-05 11:17:50 +01001737/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001738 * first byte of body, and increments msg->sov by the number of bytes parsed,
1739 * so that we know we can forward between ->som and ->sov. Note that due to
1740 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1741 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001742 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001745int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001746{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001747 const struct buffer *buf = msg->buf;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001748 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001749 const char *ptr_old = ptr;
1750 const char *end = buf->data + buf->size;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001751 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001752 unsigned int chunk = 0;
1753
1754 /* The chunk size is in the following form, though we are only
1755 * interested in the size and CRLF :
1756 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1757 */
1758 while (1) {
1759 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001760 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001761 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 if (c < 0) /* not a hex digit anymore */
1764 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 if (++ptr >= end)
1766 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001767 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001768 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 chunk = (chunk << 4) + c;
1770 }
1771
Willy Tarreaud98cf932009-12-27 22:54:55 +01001772 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001773 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001774 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775
1776 while (http_is_spht[(unsigned char)*ptr]) {
1777 if (++ptr >= end)
1778 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001779 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001781 }
1782
Willy Tarreaud98cf932009-12-27 22:54:55 +01001783 /* Up to there, we know that at least one byte is present at *ptr. Check
1784 * for the end of chunk size.
1785 */
1786 while (1) {
1787 if (likely(HTTP_IS_CRLF(*ptr))) {
1788 /* we now have a CR or an LF at ptr */
1789 if (likely(*ptr == '\r')) {
1790 if (++ptr >= end)
1791 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001792 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001793 return 0;
1794 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001795
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001797 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (++ptr >= end)
1799 ptr = buf->data;
1800 /* done */
1801 break;
1802 }
1803 else if (*ptr == ';') {
1804 /* chunk extension, ends at next CRLF */
1805 if (++ptr >= end)
1806 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001807 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001808 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001809
1810 while (!HTTP_IS_CRLF(*ptr)) {
1811 if (++ptr >= end)
1812 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001813 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 return 0;
1815 }
1816 /* we have a CRLF now, loop above */
1817 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001818 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001820 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
1822
Willy Tarreaud98cf932009-12-27 22:54:55 +01001823 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001824 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001826 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 msg->sov += ptr - ptr_old;
1828 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001829 msg->chunk_len = chunk;
1830 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001832 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001833 error:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001834 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001835 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001836}
1837
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001838/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 * the trailers is found, it is automatically scheduled to be forwarded,
1841 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1842 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001844 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001845 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1847 * which implies that all non-trailers data have already been scheduled for
1848 * forwarding, and that the difference between msg->som and msg->sov exactly
1849 * matches the length of trailers already parsed and not forwarded. It is also
1850 * important to note that this function is designed to be able to parse wrapped
1851 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001852 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001853int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001855 const struct buffer *buf = msg->buf;
1856
Willy Tarreaua458b672012-03-05 11:17:50 +01001857 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001859 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001860 const char *ptr = b_ptr(buf, msg->next);
1861 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001862 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863
1864 /* scan current line and stop at LF or CRLF */
1865 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001866 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867 return 0;
1868
1869 if (*ptr == '\n') {
1870 if (!p1)
1871 p1 = ptr;
1872 p2 = ptr;
1873 break;
1874 }
1875
1876 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001877 if (p1) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001878 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001880 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 p1 = ptr;
1882 }
1883
1884 ptr++;
1885 if (ptr >= buf->data + buf->size)
1886 ptr = buf->data;
1887 }
1888
1889 /* after LF; point to beginning of next line */
1890 p2++;
1891 if (p2 >= buf->data + buf->size)
1892 p2 = buf->data;
1893
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001894 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001895 if (bytes < 0)
1896 bytes += buf->size;
1897
1898 /* schedule this line for forwarding */
1899 msg->sov += bytes;
1900 if (msg->sov >= buf->size)
1901 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001902
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001903 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001904 /* LF/CRLF at beginning of line => end of trailers at p2.
1905 * Everything was scheduled for forwarding, there's nothing
1906 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001907 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001908 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909 msg->msg_state = HTTP_MSG_DONE;
1910 return 1;
1911 }
1912 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001913 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 }
1915}
1916
1917/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1918 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001919 * ->som, ->next in order to include this part into the next forwarding phase.
1920 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1922 * not enough data are available, the function does not change anything and
1923 * returns zero. If a parse error is encountered, the function returns < 0 and
1924 * does not change anything. Note: this function is designed to parse wrapped
1925 * CRLF at the end of the buffer.
1926 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001929 const struct buffer *buf = msg->buf;
1930 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 int bytes;
1932
1933 /* NB: we'll check data availabilty at the end. It's not a
1934 * problem because whatever we match first will be checked
1935 * against the correct length.
1936 */
1937 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001938 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001939 if (*ptr == '\r') {
1940 bytes++;
1941 ptr++;
1942 if (ptr >= buf->data + buf->size)
1943 ptr = buf->data;
1944 }
1945
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001946 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 return 0;
1948
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001949 if (*ptr != '\n') {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001950 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 ptr++;
1955 if (ptr >= buf->data + buf->size)
1956 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001957 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1958 msg->som = 0;
1959 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1961 return 1;
1962}
Willy Tarreau5b154472009-12-21 20:11:07 +01001963
Willy Tarreau21710ff2012-03-09 13:58:04 +01001964/* This function realigns a possibly wrapping http message at the beginning
1965 * of its buffer. The function may only be used when the buffer's tail is
1966 * empty.
1967 */
1968void http_message_realign(struct http_msg *msg)
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969{
Willy Tarreau21710ff2012-03-09 13:58:04 +01001970 struct buffer *buf = msg->buf;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001971
1972 /* two possible cases :
1973 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001974 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001975 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001976 if (buf->i) {
1977 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001978 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001979 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001980 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001981 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001982 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001983 }
1984 if (block2)
1985 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001986 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001987 if (block2)
1988 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001989 }
1990
1991 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001992 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001993 buf->flags &= ~BF_FULL;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001994 if (bi_full(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01001995 buf->flags |= BF_FULL;
1996}
1997
Willy Tarreaud787e662009-07-07 10:14:51 +02001998/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1999 * processing can continue on next analysers, or zero if it either needs more
2000 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2001 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2002 * when it has nothing left to do, and may remove any analyser when it wants to
2003 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002005int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006{
Willy Tarreau59234e92008-11-30 23:51:27 +01002007 /*
2008 * We will parse the partial (or complete) lines.
2009 * We will check the request syntax, and also join multi-line
2010 * headers. An index of all the lines will be elaborated while
2011 * parsing.
2012 *
2013 * For the parsing, we use a 28 states FSM.
2014 *
2015 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002016 * req->p + msg->som = beginning of request
2017 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002018 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002020 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002021 *
2022 * At end of parsing, we may perform a capture of the error (if any), and
2023 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2024 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2025 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002026 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002027
Willy Tarreau59234e92008-11-30 23:51:27 +01002028 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002029 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002030 struct http_txn *txn = &s->txn;
2031 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002032 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002033
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002034 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 +01002035 now_ms, __FUNCTION__,
2036 s,
2037 req,
2038 req->rex, req->wex,
2039 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002040 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002041 req->analysers);
2042
Willy Tarreau52a0c602009-08-16 22:45:38 +02002043 /* we're speaking HTTP here, so let's speak HTTP to the client */
2044 s->srv_error = http_return_srv_error;
2045
Willy Tarreau83e3af02009-12-28 17:39:57 +01002046 /* There's a protected area at the end of the buffer for rewriting
2047 * purposes. We don't want to start to parse the request if the
2048 * protected area is affected, because we may have to move processed
2049 * data later, which is much more complicated.
2050 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002051 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002052 if ((txn->flags & TX_NOT_FIRST) &&
2053 unlikely((req->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002054 bi_end(req) < b_ptr(req, msg->next) ||
2055 bi_end(req) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002056 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002057 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2058 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002059 /* some data has still not left the buffer, wake us once that's done */
2060 buffer_dont_connect(req);
2061 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2062 return 0;
2063 }
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002064 if (bi_end(req) < b_ptr(req, msg->next) ||
2065 bi_end(req) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01002066 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002067 }
2068
Willy Tarreau065e8332010-01-08 00:30:20 +01002069 /* Note that we have the same problem with the response ; we
2070 * may want to send a redirect, error or anything which requires
2071 * some spare space. So we'll ensure that we have at least
2072 * maxrewrite bytes available in the response buffer before
2073 * processing that one. This will only affect pipelined
2074 * keep-alive requests.
2075 */
2076 if ((txn->flags & TX_NOT_FIRST) &&
2077 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002078 bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) ||
2079 bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002080 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002081 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2082 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002083 /* don't let a connection request be initiated */
2084 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002085 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002086 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002087 return 0;
2088 }
2089 }
2090
Willy Tarreaua458b672012-03-05 11:17:50 +01002091 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002092 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002093 }
2094
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 /* 1: we might have to print this header in debug mode */
2096 if (unlikely((global.mode & MODE_DEBUG) &&
2097 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002098 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002099 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002101
Willy Tarreauea1175a2012-03-05 15:52:30 +01002102 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 eol = sol + msg->sl.rq.l;
2104 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002105
Willy Tarreau59234e92008-11-30 23:51:27 +01002106 sol += hdr_idx_first_pos(&txn->hdr_idx);
2107 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002108
Willy Tarreau59234e92008-11-30 23:51:27 +01002109 while (cur_idx) {
2110 eol = sol + txn->hdr_idx.v[cur_idx].len;
2111 debug_hdr("clihdr", s, sol, eol);
2112 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2113 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002114 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 }
2116
Willy Tarreau58f10d72006-12-04 02:26:12 +01002117
Willy Tarreau59234e92008-11-30 23:51:27 +01002118 /*
2119 * Now we quickly check if we have found a full valid request.
2120 * If not so, we check the FD and buffer states before leaving.
2121 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002123 * requests are checked first. When waiting for a second request
2124 * on a keep-alive session, if we encounter and error, close, t/o,
2125 * we note the error in the session flags but don't set any state.
2126 * Since the error will be noted there, it will not be counted by
2127 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002128 * Last, we may increase some tracked counters' http request errors on
2129 * the cases that are deliberately the client's fault. For instance,
2130 * a timeout or connection reset is not counted as an error. However
2131 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133
Willy Tarreau655dce92009-11-08 13:10:58 +01002134 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002135 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002136 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002137 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002138 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002139 session_inc_http_req_ctr(s);
2140 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002141 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002143 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002144
Willy Tarreau59234e92008-11-30 23:51:27 +01002145 /* 1: Since we are in header mode, if there's no space
2146 * left for headers, we won't be able to free more
2147 * later, so the session will never terminate. We
2148 * must terminate it now.
2149 */
2150 if (unlikely(req->flags & BF_FULL)) {
2151 /* FIXME: check if URI is set and return Status
2152 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002153 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002154 session_inc_http_req_ctr(s);
2155 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002156 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002157 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002158 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002159 goto return_bad_req;
2160 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002161
Willy Tarreau59234e92008-11-30 23:51:27 +01002162 /* 2: have we encountered a read error ? */
2163 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002164 if (!(s->flags & SN_ERR_MASK))
2165 s->flags |= SN_ERR_CLICL;
2166
Willy Tarreaufcffa692010-01-10 14:21:19 +01002167 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002168 goto failed_keep_alive;
2169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002171 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002172 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002173 session_inc_http_err_ctr(s);
2174 }
2175
Willy Tarreau59234e92008-11-30 23:51:27 +01002176 msg->msg_state = HTTP_MSG_ERROR;
2177 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178
Willy Tarreauda7ff642010-06-23 11:44:09 +02002179 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002180 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002181 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002182 if (s->listener->counters)
2183 s->listener->counters->failed_req++;
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 if (!(s->flags & SN_FINST_MASK))
2186 s->flags |= SN_FINST_R;
2187 return 0;
2188 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002189
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 /* 3: has the read timeout expired ? */
2191 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002192 if (!(s->flags & SN_ERR_MASK))
2193 s->flags |= SN_ERR_CLITO;
2194
Willy Tarreaufcffa692010-01-10 14:21:19 +01002195 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002196 goto failed_keep_alive;
2197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002199 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002200 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002201 session_inc_http_err_ctr(s);
2202 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 txn->status = 408;
2204 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2205 msg->msg_state = HTTP_MSG_ERROR;
2206 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207
Willy Tarreauda7ff642010-06-23 11:44:09 +02002208 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002209 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002210 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002211 if (s->listener->counters)
2212 s->listener->counters->failed_req++;
2213
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 if (!(s->flags & SN_FINST_MASK))
2215 s->flags |= SN_FINST_R;
2216 return 0;
2217 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002218
Willy Tarreau59234e92008-11-30 23:51:27 +01002219 /* 4: have we encountered a close ? */
2220 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002221 if (!(s->flags & SN_ERR_MASK))
2222 s->flags |= SN_ERR_CLICL;
2223
Willy Tarreaufcffa692010-01-10 14:21:19 +01002224 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002225 goto failed_keep_alive;
2226
Willy Tarreau4076a152009-04-02 15:18:36 +02002227 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002228 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 txn->status = 400;
2230 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2231 msg->msg_state = HTTP_MSG_ERROR;
2232 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002233
Willy Tarreauda7ff642010-06-23 11:44:09 +02002234 session_inc_http_err_ctr(s);
2235 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002236 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002237 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002238 if (s->listener->counters)
2239 s->listener->counters->failed_req++;
2240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 if (!(s->flags & SN_FINST_MASK))
2242 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002243 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002244 }
2245
Willy Tarreau520d95e2009-09-19 21:04:57 +02002246 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002247 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002248 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002249#ifdef TCP_QUICKACK
2250 if (s->listener->options & LI_O_NOQUICKACK) {
2251 /* We need more data, we have to re-enable quick-ack in case we
2252 * previously disabled it, otherwise we might cause the client
2253 * to delay next data.
2254 */
2255 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2256 }
2257#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002258
Willy Tarreaufcffa692010-01-10 14:21:19 +01002259 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2260 /* If the client starts to talk, let's fall back to
2261 * request timeout processing.
2262 */
2263 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002264 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002265 }
2266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002268 if (!tick_isset(req->analyse_exp)) {
2269 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2270 (txn->flags & TX_WAIT_NEXT_RQ) &&
2271 tick_isset(s->be->timeout.httpka))
2272 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2273 else
2274 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2275 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002276
Willy Tarreau59234e92008-11-30 23:51:27 +01002277 /* we're not ready yet */
2278 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002279
2280 failed_keep_alive:
2281 /* Here we process low-level errors for keep-alive requests. In
2282 * short, if the request is not the first one and it experiences
2283 * a timeout, read error or shutdown, we just silently close so
2284 * that the client can try again.
2285 */
2286 txn->status = 0;
2287 msg->msg_state = HTTP_MSG_RQBEFORE;
2288 req->analysers = 0;
2289 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002290 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002291 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002292 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002294
Willy Tarreaud787e662009-07-07 10:14:51 +02002295 /* OK now we have a complete HTTP request with indexed headers. Let's
2296 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002297 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2298 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002299 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002300 * byte after the last LF. msg->sov points to the first byte of data.
2301 * msg->eol cannot be trusted because it may have been left uninitialized
2302 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002303 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002304
Willy Tarreauda7ff642010-06-23 11:44:09 +02002305 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002306 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2307
Willy Tarreaub16a5742010-01-10 14:46:16 +01002308 if (txn->flags & TX_WAIT_NEXT_RQ) {
2309 /* kill the pending keep-alive timeout */
2310 txn->flags &= ~TX_WAIT_NEXT_RQ;
2311 req->analyse_exp = TICK_ETERNITY;
2312 }
2313
2314
Willy Tarreaud787e662009-07-07 10:14:51 +02002315 /* Maybe we found in invalid header name while we were configured not
2316 * to block on that, so we have to capture it now.
2317 */
2318 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002319 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 /*
2322 * 1: identify the method
2323 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002324 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002325
2326 /* we can make use of server redirect on GET and HEAD */
2327 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2328 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002329
Willy Tarreau59234e92008-11-30 23:51:27 +01002330 /*
2331 * 2: check if the URI matches the monitor_uri.
2332 * We have to do this for every request which gets in, because
2333 * the monitor-uri is defined by the frontend.
2334 */
2335 if (unlikely((s->fe->monitor_uri_len != 0) &&
2336 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002337 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 s->fe->monitor_uri,
2339 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002340 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002346 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002349 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002350 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 ret = acl_pass(ret);
2353 if (cond->pol == ACL_COND_UNLESS)
2354 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 if (ret) {
2357 /* we fail this request, let's return 503 service unavail */
2358 txn->status = 503;
2359 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2360 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002361 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002362 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 /* nothing to fail, let's reply normaly */
2365 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002366 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 goto return_prx_cond;
2368 }
2369
2370 /*
2371 * 3: Maybe we have to copy the original REQURI for the logs ?
2372 * Note: we cannot log anymore if the request has been
2373 * classified as invalid.
2374 */
2375 if (unlikely(s->logs.logwait & LW_REQ)) {
2376 /* we have a complete HTTP request that we must log */
2377 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2378 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002379
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 if (urilen >= REQURI_LEN)
2381 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002382 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 if (!(s->logs.logwait &= ~LW_REQ))
2386 s->do_log(s);
2387 } else {
2388 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002389 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 }
Willy Tarreau06619262006-12-17 08:37:22 +01002391
William Lallemanda73203e2012-03-12 12:48:57 +01002392 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2393 s->unique_id = pool_alloc2(pool2_uniqueid);
2394 }
2395
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002397 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002398 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002399
Willy Tarreau5b154472009-12-21 20:11:07 +01002400 /* ... and check if the request is HTTP/1.1 or above */
2401 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002402 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2403 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2404 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002405 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002406
2407 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002408 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002409
Willy Tarreau88d349d2010-01-25 12:15:43 +01002410 /* if the frontend has "option http-use-proxy-header", we'll check if
2411 * we have what looks like a proxied connection instead of a connection,
2412 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2413 * Note that this is *not* RFC-compliant, however browsers and proxies
2414 * happen to do that despite being non-standard :-(
2415 * We consider that a request not beginning with either '/' or '*' is
2416 * a proxied connection, which covers both "scheme://location" and
2417 * CONNECT ip:port.
2418 */
2419 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002420 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002421 txn->flags |= TX_USE_PX_CONN;
2422
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002423 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002424 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002427 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002428 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002430
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002431 /* 6: determine the transfer-length.
2432 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2433 * the presence of a message-body in a REQUEST and its transfer length
2434 * must be determined that way (in order of precedence) :
2435 * 1. The presence of a message-body in a request is signaled by the
2436 * inclusion of a Content-Length or Transfer-Encoding header field
2437 * in the request's header fields. When a request message contains
2438 * both a message-body of non-zero length and a method that does
2439 * not define any semantics for that request message-body, then an
2440 * origin server SHOULD either ignore the message-body or respond
2441 * with an appropriate error message (e.g., 413). A proxy or
2442 * gateway, when presented the same request, SHOULD either forward
2443 * the request inbound with the message- body or ignore the
2444 * message-body when determining a response.
2445 *
2446 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2447 * and the "chunked" transfer-coding (Section 6.2) is used, the
2448 * transfer-length is defined by the use of this transfer-coding.
2449 * If a Transfer-Encoding header field is present and the "chunked"
2450 * transfer-coding is not present, the transfer-length is defined
2451 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002453 * 3. If a Content-Length header field is present, its decimal value in
2454 * OCTETs represents both the entity-length and the transfer-length.
2455 * If a message is received with both a Transfer-Encoding header
2456 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002457 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002458 * 4. By the server closing the connection. (Closing the connection
2459 * cannot be used to indicate the end of a request body, since that
2460 * would leave no possibility for the server to send back a response.)
2461 *
2462 * Whenever a transfer-coding is applied to a message-body, the set of
2463 * transfer-codings MUST include "chunked", unless the message indicates
2464 * it is terminated by closing the connection. When the "chunked"
2465 * transfer-coding is used, it MUST be the last transfer-coding applied
2466 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467 */
2468
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002469 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002471 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002472 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002473 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002474 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002475 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2476 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 /* bad transfer-encoding (chunked followed by something else) */
2478 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002479 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 break;
2481 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482 }
2483
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002485 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002486 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002487 signed long long cl;
2488
Willy Tarreauad14f752011-09-02 20:33:27 +02002489 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002490 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002491 goto return_bad_req;
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 (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002495 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002496 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002497 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002500 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002502 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002504 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002505 msg->err_pos = ctx.line + ctx.val - req->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002506 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002507 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002509 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002510 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 }
2512
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002513 /* bodyless requests have a known length */
2514 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002515 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002516
Willy Tarreaud787e662009-07-07 10:14:51 +02002517 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002518 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002519 req->analyse_exp = TICK_ETERNITY;
2520 return 1;
2521
2522 return_bad_req:
2523 /* We centralize bad requests processing here */
2524 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2525 /* we detected a parsing error. We want to archive this request
2526 * in the dedicated proxy area for later troubleshooting.
2527 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002528 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002529 }
2530
2531 txn->req.msg_state = HTTP_MSG_ERROR;
2532 txn->status = 400;
2533 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002535 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002536 if (s->listener->counters)
2537 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002538
2539 return_prx_cond:
2540 if (!(s->flags & SN_ERR_MASK))
2541 s->flags |= SN_ERR_PRXCOND;
2542 if (!(s->flags & SN_FINST_MASK))
2543 s->flags |= SN_FINST_R;
2544
2545 req->analysers = 0;
2546 req->analyse_exp = TICK_ETERNITY;
2547 return 0;
2548}
2549
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550/* We reached the stats page through a POST request.
2551 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002552 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002553 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002554int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002555{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002556 struct proxy *px = NULL;
2557 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002558
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002559 char key[LINESIZE];
2560 int action = ST_ADM_ACTION_NONE;
2561 int reprocess = 0;
2562
2563 int total_servers = 0;
2564 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565
2566 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002567 char *st_cur_param = NULL;
2568 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569
Willy Tarreauea1175a2012-03-05 15:52:30 +01002570 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002571 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002572
2573 cur_param = next_param = end_params;
2574
Cyril Bonté23b39d92011-02-10 22:54:44 +01002575 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002576 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002577 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002578 return 1;
2579 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002580 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002581 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002582 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002583 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584 }
2585
2586 *end_params = '\0';
2587
Willy Tarreau295a8372011-03-10 11:25:07 +01002588 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002589
2590 /*
2591 * Parse the parameters in reverse order to only store the last value.
2592 * From the html form, the backend and the action are at the end.
2593 */
2594 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002595 char *value;
2596 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597
2598 cur_param--;
2599 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002600 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002601 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002602 poffset = (cur_param != first_param ? 1 : 0);
2603 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2604 if ((plen > 0) && (plen <= sizeof(key))) {
2605 strncpy(key, cur_param + poffset, plen);
2606 key[plen - 1] = '\0';
2607 } else {
2608 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2609 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002610 }
2611
2612 /* Parse the value */
2613 value = key;
2614 while (*value != '\0' && *value != '=') {
2615 value++;
2616 }
2617 if (*value == '=') {
2618 /* Ok, a value is found, we can mark the end of the key */
2619 *value++ = '\0';
2620 }
2621
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002622 if (!url_decode(key) || !url_decode(value))
2623 break;
2624
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002626 if (!px && (strcmp(key, "b") == 0)) {
2627 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2628 /* the backend name is unknown or ambiguous (duplicate names) */
2629 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2630 goto out;
2631 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002632 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002633 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002635 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002636 }
2637 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002638 action = ST_ADM_ACTION_ENABLE;
2639 }
2640 else {
2641 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2642 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 }
2644 }
2645 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002646 if (!(px && action)) {
2647 /*
2648 * Indicates that we'll need to reprocess the parameters
2649 * as soon as backend and action are known
2650 */
2651 if (!reprocess) {
2652 st_cur_param = cur_param;
2653 st_next_param = next_param;
2654 }
2655 reprocess = 1;
2656 }
2657 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002658 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002659 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002660 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002661 /* Not already in maintenance, we can change the server state */
2662 sv->state |= SRV_MAINTAIN;
2663 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002664 altered_servers++;
2665 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002666 }
2667 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002668 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002669 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002670 /* Already in maintenance, we can change the server state */
2671 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002672 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002673 altered_servers++;
2674 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002675 }
2676 break;
2677 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002678 } else {
2679 /* the server name is unknown or ambiguous (duplicate names) */
2680 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 }
2682 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002683 if (reprocess && px && action) {
2684 /* Now, we know the backend and the action chosen by the user.
2685 * We can safely restart from the first server parameter
2686 * to reprocess them
2687 */
2688 cur_param = st_cur_param;
2689 next_param = st_next_param;
2690 reprocess = 0;
2691 goto reprocess_servers;
2692 }
2693
Cyril Bonté70be45d2010-10-12 00:14:35 +02002694 next_param = cur_param;
2695 }
2696 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002697
2698 if (total_servers == 0) {
2699 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2700 }
2701 else if (altered_servers == 0) {
2702 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2703 }
2704 else if (altered_servers == total_servers) {
2705 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2706 }
2707 else {
2708 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2709 }
2710 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002711 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712}
2713
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002714/* returns a pointer to the first rule which forbids access (deny or http_auth),
2715 * or NULL if everything's OK.
2716 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002717static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002718http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2719{
Willy Tarreauff011f22011-01-06 17:51:27 +01002720 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002721
Willy Tarreauff011f22011-01-06 17:51:27 +01002722 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002723 int ret = 1;
2724
Willy Tarreauff011f22011-01-06 17:51:27 +01002725 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002726 continue;
2727
2728 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002729 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002730 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002731 ret = acl_pass(ret);
2732
Willy Tarreauff011f22011-01-06 17:51:27 +01002733 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734 ret = !ret;
2735 }
2736
2737 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002738 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002739 return NULL; /* no problem */
2740 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002741 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002742 }
2743 }
2744 return NULL;
2745}
2746
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747/* This stream analyser runs all HTTP request processing which is common to
2748 * frontends and backends, which means blocking ACLs, filters, connection-close,
2749 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002750 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002751 * either needs more data or wants to immediately abort the request (eg: deny,
2752 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002755{
Willy Tarreaud787e662009-07-07 10:14:51 +02002756 struct http_txn *txn = &s->txn;
2757 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002758 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002759 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002761 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002762 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002763
Willy Tarreau655dce92009-11-08 13:10:58 +01002764 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002765 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002766 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002767 return 0;
2768 }
2769
Willy Tarreau3a816292009-07-07 10:55:49 +02002770 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 req->analyse_exp = TICK_ETERNITY;
2772
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002773 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 +02002774 now_ms, __FUNCTION__,
2775 s,
2776 req,
2777 req->rex, req->wex,
2778 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002779 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 req->analysers);
2781
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002782 /* first check whether we have some ACLs set to block this request */
2783 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002784 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 ret = acl_pass(ret);
2787 if (cond->pol == ACL_COND_UNLESS)
2788 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002789
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 if (ret) {
2791 txn->status = 403;
2792 /* let's log the request time */
2793 s->logs.tv_request = now;
2794 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002795 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002796 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002799
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002800 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002801 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002802
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002803 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002804 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002805 do_stats = stats_check_uri(s->rep->prod, txn, px);
2806 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002807 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 +01002808 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002809 else
2810 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002811
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002812 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002813 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002814 txn->status = 403;
2815 s->logs.tv_request = now;
2816 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002817 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002818 s->fe->fe_counters.denied_req++;
2819 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2820 s->be->be_counters.denied_req++;
2821 if (s->listener->counters)
2822 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002823 goto return_prx_cond;
2824 }
2825
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002826 /* try headers filters */
2827 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002828 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002829 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002830
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 /* has the request been denied ? */
2832 if (txn->flags & TX_CLDENY) {
2833 /* no need to go further */
2834 txn->status = 403;
2835 /* let's log the request time */
2836 s->logs.tv_request = now;
2837 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 goto return_prx_cond;
2840 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002841
2842 /* When a connection is tarpitted, we use the tarpit timeout,
2843 * which may be the same as the connect timeout if unspecified.
2844 * If unset, then set it to zero because we really want it to
2845 * eventually expire. We build the tarpit as an analyser.
2846 */
2847 if (txn->flags & TX_CLTARPIT) {
2848 buffer_erase(s->req);
2849 /* wipe the request out so that we can drop the connection early
2850 * if the client closes first.
2851 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002852 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002853 req->analysers = 0; /* remove switching rules etc... */
2854 req->analysers |= AN_REQ_HTTP_TARPIT;
2855 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2856 if (!req->analyse_exp)
2857 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002858 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002859 return 1;
2860 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 }
Willy Tarreau06619262006-12-17 08:37:22 +01002862
Willy Tarreau5b154472009-12-21 20:11:07 +01002863 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2864 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002865 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2866 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002867 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2868 * avoid to redo the same work if FE and BE have the same settings (common).
2869 * The method consists in checking if options changed between the two calls
2870 * (implying that either one is non-null, or one of them is non-null and we
2871 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002872 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002873
Willy Tarreaudc008c52010-02-01 16:20:08 +01002874 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2875 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2876 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2877 (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 +01002878 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002879
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002880 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2881 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002882 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002883 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2884 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002885 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002886 tmp = TX_CON_WANT_CLO;
2887
Willy Tarreau5b154472009-12-21 20:11:07 +01002888 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2889 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002890
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002891 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2892 /* parse the Connection header and possibly clean it */
2893 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002894 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002895 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2896 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002897 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002898 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002899 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002900 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002901 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002902
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002903 /* check if client or config asks for explicit close in KAL/SCL */
2904 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2905 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2906 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002907 (!(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 +01002908 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002910 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002911 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2912 }
Willy Tarreau78599912009-10-17 20:12:21 +02002913
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002914 /* we can be blocked here because the request needs to be authenticated,
2915 * either to pass or to access stats.
2916 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002917 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002918 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002919 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002920
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002921 if (!realm)
2922 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2923
Willy Tarreau844a7e72010-01-31 21:46:18 +01002924 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002925 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2926 txn->status = 401;
2927 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002928 /* on 401 we still count one error, because normal browsing
2929 * won't significantly increase the counter but brute force
2930 * attempts will.
2931 */
2932 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002933 goto return_prx_cond;
2934 }
2935
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002936 /* add request headers from the rule sets in the same order */
2937 list_for_each_entry(wl, &px->req_add, list) {
2938 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002939 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002940 ret = acl_pass(ret);
2941 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2942 ret = !ret;
2943 if (!ret)
2944 continue;
2945 }
2946
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002947 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002948 goto return_bad_req;
2949 }
2950
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002951 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002952 struct stats_admin_rule *stats_admin_rule;
2953
2954 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002955 * FIXME!!! that one is rather dangerous, we want to
2956 * make it follow standard rules (eg: clear req->analysers).
2957 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002958
Cyril Bonté474be412010-10-12 00:14:36 +02002959 /* now check whether we have some admin rules for this request */
2960 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2961 int ret = 1;
2962
2963 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002964 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Cyril Bonté474be412010-10-12 00:14:36 +02002965 ret = acl_pass(ret);
2966 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2967 ret = !ret;
2968 }
2969
2970 if (ret) {
2971 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002972 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002973 break;
2974 }
2975 }
2976
Cyril Bonté70be45d2010-10-12 00:14:35 +02002977 /* Was the status page requested with a POST ? */
2978 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002979 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002980 if (msg->msg_state < HTTP_MSG_100_SENT) {
2981 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2982 * send an HTTP/1.1 100 Continue intermediate response.
2983 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002984 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002985 struct hdr_ctx ctx;
2986 ctx.idx = 0;
2987 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002988 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002989 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002990 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01002991 }
2992 }
2993 msg->msg_state = HTTP_MSG_100_SENT;
2994 s->logs.tv_request = now; /* update the request timer to reflect full request */
2995 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002996 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002997 /* we need more data */
2998 req->analysers |= an_bit;
2999 buffer_dont_connect(req);
3000 return 0;
3001 }
Cyril Bonté474be412010-10-12 00:14:36 +02003002 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003003 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003004 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003005 }
3006
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003007 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003008 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003009 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003010 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003011 s->rep->prod->applet.private = s;
3012 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003013 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003014 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3015 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003016
3017 return 0;
3018
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003020
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 /* check whether we have some ACLs set to redirect this request */
3022 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003023 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003024
Willy Tarreauf285f542010-01-03 20:03:03 +01003025 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003026 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003027 ret = acl_pass(ret);
3028 if (rule->cond->pol == ACL_COND_UNLESS)
3029 ret = !ret;
3030 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003032 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003033 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 /* build redirect message */
3037 switch(rule->code) {
3038 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 msg_fmt = HTTP_303;
3040 break;
3041 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 msg_fmt = HTTP_301;
3043 break;
3044 case 302:
3045 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 msg_fmt = HTTP_302;
3047 break;
3048 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003049
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003050 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 switch(rule->type) {
3054 case REDIRECT_TYPE_PREFIX: {
3055 const char *path;
3056 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003057
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 path = http_get_path(txn);
3059 /* build message using path */
3060 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003061 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 +02003062 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3063 int qs = 0;
3064 while (qs < pathlen) {
3065 if (path[qs] == '?') {
3066 pathlen = qs;
3067 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003068 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003070 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003072 } else {
3073 path = "/";
3074 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003075 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003077 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 goto return_bad_req;
3079
3080 /* add prefix. Note that if prefix == "/", we don't want to
3081 * add anything, otherwise it makes it hard for the user to
3082 * configure a self-redirection.
3083 */
3084 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003085 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3086 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003087 }
3088
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 /* add path */
3090 memcpy(rdr.str + rdr.len, path, pathlen);
3091 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003092
3093 /* append a slash at the end of the location is needed and missing */
3094 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3095 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3096 if (rdr.len > rdr.size - 5)
3097 goto return_bad_req;
3098 rdr.str[rdr.len] = '/';
3099 rdr.len++;
3100 }
3101
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003102 break;
3103 }
3104 case REDIRECT_TYPE_LOCATION:
3105 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003106 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003107 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003108
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003109 /* add location */
3110 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3111 rdr.len += rule->rdr_len;
3112 break;
3113 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003114
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003115 if (rule->cookie_len) {
3116 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3117 rdr.len += 14;
3118 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3119 rdr.len += rule->cookie_len;
3120 memcpy(rdr.str + rdr.len, "\r\n", 2);
3121 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003122 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003124 /* add end of headers and the keep-alive/close status.
3125 * We may choose to set keep-alive if the Location begins
3126 * with a slash, because the client will come back to the
3127 * same server.
3128 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003129 txn->status = rule->code;
3130 /* let's log the request time */
3131 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003132
3133 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003134 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3135 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003136 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3137 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3138 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003139 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003140 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3141 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3142 rdr.len += 30;
3143 } else {
3144 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3145 rdr.len += 24;
3146 }
Willy Tarreau75661452010-01-10 10:35:01 +01003147 }
3148 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3149 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003150 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003151 /* "eat" the request */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003152 bi_fast_delete(req, msg->sov - msg->som);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003153 msg->som = msg->sov;
3154 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003155 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3156 txn->req.msg_state = HTTP_MSG_CLOSED;
3157 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003158 break;
3159 } else {
3160 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003161 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3162 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3163 rdr.len += 29;
3164 } else {
3165 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3166 rdr.len += 23;
3167 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003168 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003169 goto return_prx_cond;
3170 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003171 }
3172 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003173
Willy Tarreau2be39392010-01-03 17:24:51 +01003174 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3175 * If this happens, then the data will not come immediately, so we must
3176 * send all what we have without waiting. Note that due to the small gain
3177 * in waiting for the body of the request, it's easier to simply put the
3178 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3179 * itself once used.
3180 */
3181 req->flags |= BF_SEND_DONTWAIT;
3182
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003183 /* that's OK for us now, let's move on to next analysers */
3184 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003185
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003186 return_bad_req:
3187 /* We centralize bad requests processing here */
3188 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3189 /* we detected a parsing error. We want to archive this request
3190 * in the dedicated proxy area for later troubleshooting.
3191 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003192 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003193 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003194
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003195 txn->req.msg_state = HTTP_MSG_ERROR;
3196 txn->status = 400;
3197 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003198
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003199 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003200 if (s->listener->counters)
3201 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003203 return_prx_cond:
3204 if (!(s->flags & SN_ERR_MASK))
3205 s->flags |= SN_ERR_PRXCOND;
3206 if (!(s->flags & SN_FINST_MASK))
3207 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003208
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003209 req->analysers = 0;
3210 req->analyse_exp = TICK_ETERNITY;
3211 return 0;
3212}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003213
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003214/* This function performs all the processing enabled for the current request.
3215 * It returns 1 if the processing can continue on next analysers, or zero if it
3216 * needs more data, encounters an error, or wants to immediately abort the
3217 * request. It relies on buffers flags, and updates s->req->analysers.
3218 */
3219int http_process_request(struct session *s, struct buffer *req, int an_bit)
3220{
3221 struct http_txn *txn = &s->txn;
3222 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003223
Willy Tarreau655dce92009-11-08 13:10:58 +01003224 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003225 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003226 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003227 return 0;
3228 }
3229
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003230 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 +02003231 now_ms, __FUNCTION__,
3232 s,
3233 req,
3234 req->rex, req->wex,
3235 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003236 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003237 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003238
Willy Tarreau59234e92008-11-30 23:51:27 +01003239 /*
3240 * Right now, we know that we have processed the entire headers
3241 * and that unwanted requests have been filtered out. We can do
3242 * whatever we want with the remaining request. Also, now we
3243 * may have separate values for ->fe, ->be.
3244 */
Willy Tarreau06619262006-12-17 08:37:22 +01003245
Willy Tarreau59234e92008-11-30 23:51:27 +01003246 /*
3247 * If HTTP PROXY is set we simply get remote server address
3248 * parsing incoming request.
3249 */
3250 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003251 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 +01003252 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003253
Willy Tarreau59234e92008-11-30 23:51:27 +01003254 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003255 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 * Note that doing so might move headers in the request, but
3257 * the fields will stay coherent and the URI will not move.
3258 * This should only be performed in the backend.
3259 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003260 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3262 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003263
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003265 * 8: the appsession cookie was looked up very early in 1.2,
3266 * so let's do the same now.
3267 */
3268
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003269 /* It needs to look into the URI unless persistence must be ignored */
3270 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003271 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 +01003272 }
3273
William Lallemanda73203e2012-03-12 12:48:57 +01003274 /* add unique-id if "header-unique-id" is specified */
3275
3276 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3277 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3278
3279 if (s->fe->header_unique_id && s->unique_id) {
3280 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3281 if (ret < 0 || ret > global.tune.bufsize)
3282 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003283 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003284 goto return_bad_req;
3285 }
3286
Cyril Bontéb21570a2009-11-29 20:04:48 +01003287 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 * 9: add X-Forwarded-For if either the frontend or the backend
3289 * asks for it.
3290 */
3291 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003292 struct hdr_ctx ctx = { .idx = 0 };
3293
3294 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003295 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 +02003296 /* The header is set to be added only if none is present
3297 * and we found it, so don't do anything.
3298 */
3299 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003300 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 /* Add an X-Forwarded-For header unless the source IP is
3302 * in the 'except' network range.
3303 */
3304 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003305 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 != s->fe->except_net.s_addr) &&
3307 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003308 (((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 +01003309 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003310 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003312 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003313
3314 /* Note: we rely on the backend to get the header name to be used for
3315 * x-forwarded-for, because the header is really meant for the backends.
3316 * However, if the backend did not specify any option, we have to rely
3317 * on the frontend's header name.
3318 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 if (s->be->fwdfor_hdr_len) {
3320 len = s->be->fwdfor_hdr_len;
3321 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003322 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003323 len = s->fe->fwdfor_hdr_len;
3324 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003325 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003326 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003327
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003328 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003329 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003330 }
3331 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003332 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 /* FIXME: for the sake of completeness, we should also support
3334 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003335 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003336 int len;
3337 char pn[INET6_ADDRSTRLEN];
3338 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003339 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003340 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003341
Willy Tarreau59234e92008-11-30 23:51:27 +01003342 /* Note: we rely on the backend to get the header name to be used for
3343 * x-forwarded-for, because the header is really meant for the backends.
3344 * However, if the backend did not specify any option, we have to rely
3345 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003346 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 if (s->be->fwdfor_hdr_len) {
3348 len = s->be->fwdfor_hdr_len;
3349 memcpy(trash, s->be->fwdfor_hdr_name, len);
3350 } else {
3351 len = s->fe->fwdfor_hdr_len;
3352 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003353 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003355
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003356 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003357 goto return_bad_req;
3358 }
3359 }
3360
3361 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003362 * 10: add X-Original-To if either the frontend or the backend
3363 * asks for it.
3364 */
3365 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3366
3367 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003368 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003369 /* Add an X-Original-To header unless the destination IP is
3370 * in the 'except' network range.
3371 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003372 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003373
Willy Tarreau6471afb2011-09-23 10:54:59 +02003374 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003375 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003376 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003377 != s->fe->except_to.s_addr) &&
3378 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003379 (((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 +02003380 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003381 int len;
3382 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003383 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003384
3385 /* Note: we rely on the backend to get the header name to be used for
3386 * x-original-to, because the header is really meant for the backends.
3387 * However, if the backend did not specify any option, we have to rely
3388 * on the frontend's header name.
3389 */
3390 if (s->be->orgto_hdr_len) {
3391 len = s->be->orgto_hdr_len;
3392 memcpy(trash, s->be->orgto_hdr_name, len);
3393 } else {
3394 len = s->fe->orgto_hdr_len;
3395 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003396 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003397 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3398
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003399 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003400 goto return_bad_req;
3401 }
3402 }
3403 }
3404
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003405 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3406 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003407 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003408 unsigned int want_flags = 0;
3409
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003410 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003411 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
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_CLO_SET;
3415 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003416 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3417 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3418 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003419 want_flags |= TX_CON_KAL_SET;
3420 }
3421
3422 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003423 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003425
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003426
Willy Tarreau522d6c02009-12-06 18:49:18 +01003427 /* If we have no server assigned yet and we're balancing on url_param
3428 * with a POST request, we may be interested in checking the body for
3429 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003430 */
3431 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3432 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003433 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003434 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003435 buffer_dont_connect(req);
3436 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003438
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003439 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003440 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003441#ifdef TCP_QUICKACK
3442 /* We expect some data from the client. Unless we know for sure
3443 * we already have a full request, we have to re-enable quick-ack
3444 * in case we previously disabled it, otherwise we might cause
3445 * the client to delay further data.
3446 */
3447 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003448 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003449 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003450 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3451#endif
3452 }
Willy Tarreau03945942009-12-22 16:50:27 +01003453
Willy Tarreau59234e92008-11-30 23:51:27 +01003454 /*************************************************************
3455 * OK, that's finished for the headers. We have done what we *
3456 * could. Let's switch to the DATA state. *
3457 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003458 req->analyse_exp = TICK_ETERNITY;
3459 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003460
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003462 /* OK let's go on with the BODY now */
3463 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003464
Willy Tarreau59234e92008-11-30 23:51:27 +01003465 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003466 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003467 /* we detected a parsing error. We want to archive this request
3468 * in the dedicated proxy area for later troubleshooting.
3469 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003470 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003471 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003472
Willy Tarreau59234e92008-11-30 23:51:27 +01003473 txn->req.msg_state = HTTP_MSG_ERROR;
3474 txn->status = 400;
3475 req->analysers = 0;
3476 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003477
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003478 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003479 if (s->listener->counters)
3480 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003481
Willy Tarreau59234e92008-11-30 23:51:27 +01003482 if (!(s->flags & SN_ERR_MASK))
3483 s->flags |= SN_ERR_PRXCOND;
3484 if (!(s->flags & SN_FINST_MASK))
3485 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003486 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003487}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003488
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489/* This function is an analyser which processes the HTTP tarpit. It always
3490 * returns zero, at the beginning because it prevents any other processing
3491 * from occurring, and at the end because it terminates the request.
3492 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003493int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003494{
3495 struct http_txn *txn = &s->txn;
3496
3497 /* This connection is being tarpitted. The CLIENT side has
3498 * already set the connect expiration date to the right
3499 * timeout. We just have to check that the client is still
3500 * there and that the timeout has not expired.
3501 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003502 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003503 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3504 !tick_is_expired(req->analyse_exp, now_ms))
3505 return 0;
3506
3507 /* We will set the queue timer to the time spent, just for
3508 * logging purposes. We fake a 500 server error, so that the
3509 * attacker will not suspect his connection has been tarpitted.
3510 * It will not cause trouble to the logs because we can exclude
3511 * the tarpitted connections by filtering on the 'PT' status flags.
3512 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003513 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3514
3515 txn->status = 500;
3516 if (req->flags != BF_READ_ERROR)
3517 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3518
3519 req->analysers = 0;
3520 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003521
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003522 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003523 if (s->listener->counters)
3524 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003525
Willy Tarreau60b85b02008-11-30 23:28:40 +01003526 if (!(s->flags & SN_ERR_MASK))
3527 s->flags |= SN_ERR_PRXCOND;
3528 if (!(s->flags & SN_FINST_MASK))
3529 s->flags |= SN_FINST_T;
3530 return 0;
3531}
3532
Willy Tarreaud34af782008-11-30 23:36:37 +01003533/* This function is an analyser which processes the HTTP request body. It looks
3534 * for parameters to be used for the load balancing algorithm (url_param). It
3535 * must only be called after the standard HTTP request processing has occurred,
3536 * because it expects the request to be parsed. It returns zero if it needs to
3537 * read more data, or 1 once it has completed its analysis.
3538 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003539int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003540{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003541 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003542 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003543 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003544
3545 /* We have to parse the HTTP request body to find any required data.
3546 * "balance url_param check_post" should have been the only way to get
3547 * into this. We were brought here after HTTP header analysis, so all
3548 * related structures are ready.
3549 */
3550
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3552 goto missing_data;
3553
3554 if (msg->msg_state < HTTP_MSG_100_SENT) {
3555 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3556 * send an HTTP/1.1 100 Continue intermediate response.
3557 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003558 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 struct hdr_ctx ctx;
3560 ctx.idx = 0;
3561 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003562 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003563 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003564 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003565 }
3566 }
3567 msg->msg_state = HTTP_MSG_100_SENT;
3568 }
3569
3570 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003571 /* we have msg->sov which points to the first byte of message body.
3572 * msg->som still points to the beginning of the message. We must
3573 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003574 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003575 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003576
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003577 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003578 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3579 else
3580 msg->msg_state = HTTP_MSG_DATA;
3581 }
3582
3583 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003584 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003585 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003586 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003587 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003588 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003589
Willy Tarreau115acb92009-12-26 13:56:06 +01003590 if (!ret)
3591 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003592 else if (ret < 0) {
3593 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003595 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003596 }
3597
Willy Tarreaud98cf932009-12-27 22:54:55 +01003598 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003599 * We have the first data byte is in msg->sov. We're waiting for at
3600 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003601 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602
Willy Tarreau124d9912011-03-01 20:30:48 +01003603 if (msg->body_len < limit)
3604 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003605
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003606 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003607 goto http_end;
3608
3609 missing_data:
3610 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003611 if (req->flags & BF_FULL) {
3612 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003613 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003614 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003615
Willy Tarreau522d6c02009-12-06 18:49:18 +01003616 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3617 txn->status = 408;
3618 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003619
3620 if (!(s->flags & SN_ERR_MASK))
3621 s->flags |= SN_ERR_CLITO;
3622 if (!(s->flags & SN_FINST_MASK))
3623 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003625 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003626
3627 /* we get here if we need to wait for more data */
3628 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003629 /* Not enough data. We'll re-use the http-request
3630 * timeout here. Ideally, we should set the timeout
3631 * relative to the accept() date. We just set the
3632 * request timeout once at the beginning of the
3633 * request.
3634 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003635 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003636 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003637 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003638 return 0;
3639 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003640
3641 http_end:
3642 /* The situation will not evolve, so let's give up on the analysis. */
3643 s->logs.tv_request = now; /* update the request timer to reflect full request */
3644 req->analysers &= ~an_bit;
3645 req->analyse_exp = TICK_ETERNITY;
3646 return 1;
3647
3648 return_bad_req: /* let's centralize all bad requests */
3649 txn->req.msg_state = HTTP_MSG_ERROR;
3650 txn->status = 400;
3651 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3652
Willy Tarreau79ebac62010-06-07 13:47:49 +02003653 if (!(s->flags & SN_ERR_MASK))
3654 s->flags |= SN_ERR_PRXCOND;
3655 if (!(s->flags & SN_FINST_MASK))
3656 s->flags |= SN_FINST_R;
3657
Willy Tarreau522d6c02009-12-06 18:49:18 +01003658 return_err_msg:
3659 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003660 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003661 if (s->listener->counters)
3662 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003663 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003664}
3665
Willy Tarreau45c0d982012-03-09 12:11:57 +01003666/* send a server's name with an outgoing request over an established connection */
3667int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668
3669 struct hdr_ctx ctx;
3670
Mark Lamourinec2247f02012-01-04 13:02:01 -05003671 char *hdr_name = be->server_id_hdr_name;
3672 int hdr_name_len = be->server_id_hdr_len;
3673
3674 char *hdr_val;
3675
William Lallemandd9e90662012-01-30 17:27:17 +01003676 ctx.idx = 0;
3677
Willy Tarreau45c0d982012-03-09 12:11:57 +01003678 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 -05003679 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003680 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003681 }
3682
3683 /* Add the new header requested with the server value */
3684 hdr_val = trash;
3685 memcpy(hdr_val, hdr_name, hdr_name_len);
3686 hdr_val += hdr_name_len;
3687 *hdr_val++ = ':';
3688 *hdr_val++ = ' ';
3689 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003690 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003691
3692 return 0;
3693}
3694
Willy Tarreau610ecce2010-01-04 21:15:02 +01003695/* Terminate current transaction and prepare a new one. This is very tricky
3696 * right now but it works.
3697 */
3698void http_end_txn_clean_session(struct session *s)
3699{
3700 /* FIXME: We need a more portable way of releasing a backend's and a
3701 * server's connections. We need a safer way to reinitialize buffer
3702 * flags. We also need a more accurate method for computing per-request
3703 * data.
3704 */
3705 http_silent_debug(__LINE__, s);
3706
3707 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau060781f2012-05-07 16:50:03 +02003708 s->req->cons->sock.shutr(s->req->cons);
3709 s->req->cons->sock.shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710
3711 http_silent_debug(__LINE__, s);
3712
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003713 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003715 if (unlikely(s->srv_conn))
3716 sess_change_server(s, NULL);
3717 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003718
3719 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3720 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003721 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722
3723 if (s->txn.status) {
3724 int n;
3725
3726 n = s->txn.status / 100;
3727 if (n < 1 || n > 5)
3728 n = 0;
3729
3730 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003731 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732
Willy Tarreau24657792010-02-26 10:30:28 +01003733 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003735 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736 }
3737
3738 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003739 s->logs.bytes_in -= s->req->i;
3740 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741
3742 /* let's do a final log if we need it */
3743 if (s->logs.logwait &&
3744 !(s->flags & SN_MONITOR) &&
3745 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3746 s->do_log(s);
3747 }
3748
3749 s->logs.accept_date = date; /* user-visible date for logging */
3750 s->logs.tv_accept = now; /* corrected date for internal use */
3751 tv_zero(&s->logs.tv_request);
3752 s->logs.t_queue = -1;
3753 s->logs.t_connect = -1;
3754 s->logs.t_data = -1;
3755 s->logs.t_close = 0;
3756 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3757 s->logs.srv_queue_size = 0; /* we will get this number soon */
3758
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003759 s->logs.bytes_in = s->req->total = s->req->i;
3760 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761
3762 if (s->pend_pos)
3763 pendconn_free(s->pend_pos);
3764
Willy Tarreau827aee92011-03-10 16:55:02 +01003765 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003766 if (s->flags & SN_CURR_SESS) {
3767 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003768 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003770 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3771 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003772 }
3773
Willy Tarreau9e000c62011-03-10 14:03:36 +01003774 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775
3776 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3777 s->req->cons->fd = -1; /* just to help with debugging */
3778 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003779 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 s->req->cons->err_loc = NULL;
3781 s->req->cons->exp = TICK_ETERNITY;
3782 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003783 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3784 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 +02003785 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 +01003786 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3787 s->txn.meth = 0;
3788 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003789 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003790 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791 s->req->cons->flags |= SI_FL_INDEP_STR;
3792
Willy Tarreau96e31212011-05-30 18:10:30 +02003793 if (s->fe->options2 & PR_O2_NODELAY) {
3794 s->req->flags |= BF_NEVER_WAIT;
3795 s->rep->flags |= BF_NEVER_WAIT;
3796 }
3797
Willy Tarreau610ecce2010-01-04 21:15:02 +01003798 /* if the request buffer is not empty, it means we're
3799 * about to process another request, so send pending
3800 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003801 * Just don't do this if the buffer is close to be full,
3802 * because the request will wait for it to flush a little
3803 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003805 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003806 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003807 !(s->rep->flags & BF_FULL) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02003808 bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003809 s->rep->flags |= BF_EXPECT_MORE;
3810 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003811
3812 /* we're removing the analysers, we MUST re-enable events detection */
3813 buffer_auto_read(s->req);
3814 buffer_auto_close(s->req);
3815 buffer_auto_read(s->rep);
3816 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817
Willy Tarreau342b11c2010-11-24 16:22:09 +01003818 s->req->analysers = s->listener->analysers;
3819 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 s->rep->analysers = 0;
3821
3822 http_silent_debug(__LINE__, s);
3823}
3824
3825
3826/* This function updates the request state machine according to the response
3827 * state machine and buffer flags. It returns 1 if it changes anything (flag
3828 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3829 * it is only used to find when a request/response couple is complete. Both
3830 * this function and its equivalent should loop until both return zero. It
3831 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3832 */
3833int http_sync_req_state(struct session *s)
3834{
3835 struct buffer *buf = s->req;
3836 struct http_txn *txn = &s->txn;
3837 unsigned int old_flags = buf->flags;
3838 unsigned int old_state = txn->req.msg_state;
3839
3840 http_silent_debug(__LINE__, s);
3841 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3842 return 0;
3843
3844 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003845 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003846 * We can shut the read side unless we want to abort_on_close,
3847 * or we have a POST request. The issue with POST requests is
3848 * that some browsers still send a CRLF after the request, and
3849 * this CRLF must be read so that it does not remain in the kernel
3850 * buffers, otherwise a close could cause an RST on some systems
3851 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003852 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003853 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003854 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855
3856 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3857 goto wait_other_side;
3858
3859 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3860 /* The server has not finished to respond, so we
3861 * don't want to move in order not to upset it.
3862 */
3863 goto wait_other_side;
3864 }
3865
3866 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3867 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003868 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869 txn->req.msg_state = HTTP_MSG_TUNNEL;
3870 goto wait_other_side;
3871 }
3872
3873 /* When we get here, it means that both the request and the
3874 * response have finished receiving. Depending on the connection
3875 * mode, we'll have to wait for the last bytes to leave in either
3876 * direction, and sometimes for a close to be effective.
3877 */
3878
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003879 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3880 /* Server-close mode : queue a connection close to the server */
3881 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003883 }
3884 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3885 /* Option forceclose is set, or either side wants to close,
3886 * let's enforce it now that we're not expecting any new
3887 * data to come. The caller knows the session is complete
3888 * once both states are CLOSED.
3889 */
3890 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 buffer_shutr_now(buf);
3892 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003893 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003894 }
3895 else {
3896 /* The last possible modes are keep-alive and tunnel. Since tunnel
3897 * mode does not set the body analyser, we can't reach this place
3898 * in tunnel mode, so we're left with keep-alive only.
3899 * This mode is currently not implemented, we switch to tunnel mode.
3900 */
3901 buffer_auto_read(buf);
3902 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 }
3904
3905 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3906 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003907 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3908
Willy Tarreau610ecce2010-01-04 21:15:02 +01003909 if (!(buf->flags & BF_OUT_EMPTY)) {
3910 txn->req.msg_state = HTTP_MSG_CLOSING;
3911 goto http_msg_closing;
3912 }
3913 else {
3914 txn->req.msg_state = HTTP_MSG_CLOSED;
3915 goto http_msg_closed;
3916 }
3917 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003918 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003919 }
3920
3921 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3922 http_msg_closing:
3923 /* nothing else to forward, just waiting for the output buffer
3924 * to be empty and for the shutw_now to take effect.
3925 */
3926 if (buf->flags & BF_OUT_EMPTY) {
3927 txn->req.msg_state = HTTP_MSG_CLOSED;
3928 goto http_msg_closed;
3929 }
3930 else if (buf->flags & BF_SHUTW) {
3931 txn->req.msg_state = HTTP_MSG_ERROR;
3932 goto wait_other_side;
3933 }
3934 }
3935
3936 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3937 http_msg_closed:
3938 goto wait_other_side;
3939 }
3940
3941 wait_other_side:
3942 http_silent_debug(__LINE__, s);
3943 return txn->req.msg_state != old_state || buf->flags != old_flags;
3944}
3945
3946
3947/* This function updates the response state machine according to the request
3948 * state machine and buffer flags. It returns 1 if it changes anything (flag
3949 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3950 * it is only used to find when a request/response couple is complete. Both
3951 * this function and its equivalent should loop until both return zero. It
3952 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3953 */
3954int http_sync_res_state(struct session *s)
3955{
3956 struct buffer *buf = s->rep;
3957 struct http_txn *txn = &s->txn;
3958 unsigned int old_flags = buf->flags;
3959 unsigned int old_state = txn->rsp.msg_state;
3960
3961 http_silent_debug(__LINE__, s);
3962 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3963 return 0;
3964
3965 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3966 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003967 * still monitor the server connection for a possible close
3968 * while the request is being uploaded, so we don't disable
3969 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003971 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972
3973 if (txn->req.msg_state == HTTP_MSG_ERROR)
3974 goto wait_other_side;
3975
3976 if (txn->req.msg_state < HTTP_MSG_DONE) {
3977 /* The client seems to still be sending data, probably
3978 * because we got an error response during an upload.
3979 * We have the choice of either breaking the connection
3980 * or letting it pass through. Let's do the later.
3981 */
3982 goto wait_other_side;
3983 }
3984
3985 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3986 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003987 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3989 goto wait_other_side;
3990 }
3991
3992 /* When we get here, it means that both the request and the
3993 * response have finished receiving. Depending on the connection
3994 * mode, we'll have to wait for the last bytes to leave in either
3995 * direction, and sometimes for a close to be effective.
3996 */
3997
3998 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3999 /* Server-close mode : shut read and wait for the request
4000 * side to close its output buffer. The caller will detect
4001 * when we're in DONE and the other is in CLOSED and will
4002 * catch that for the final cleanup.
4003 */
4004 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4005 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004007 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4008 /* Option forceclose is set, or either side wants to close,
4009 * let's enforce it now that we're not expecting any new
4010 * data to come. The caller knows the session is complete
4011 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004013 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4014 buffer_shutr_now(buf);
4015 buffer_shutw_now(buf);
4016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 }
4018 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004019 /* The last possible modes are keep-alive and tunnel. Since tunnel
4020 * mode does not set the body analyser, we can't reach this place
4021 * in tunnel mode, so we're left with keep-alive only.
4022 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004024 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004025 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 }
4027
4028 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4029 /* if we've just closed an output, let's switch */
4030 if (!(buf->flags & BF_OUT_EMPTY)) {
4031 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4032 goto http_msg_closing;
4033 }
4034 else {
4035 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4036 goto http_msg_closed;
4037 }
4038 }
4039 goto wait_other_side;
4040 }
4041
4042 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4043 http_msg_closing:
4044 /* nothing else to forward, just waiting for the output buffer
4045 * to be empty and for the shutw_now to take effect.
4046 */
4047 if (buf->flags & BF_OUT_EMPTY) {
4048 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4049 goto http_msg_closed;
4050 }
4051 else if (buf->flags & BF_SHUTW) {
4052 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004053 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004054 if (target_srv(&s->target))
4055 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004056 goto wait_other_side;
4057 }
4058 }
4059
4060 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4061 http_msg_closed:
4062 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004063 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004065 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 goto wait_other_side;
4067 }
4068
4069 wait_other_side:
4070 http_silent_debug(__LINE__, s);
4071 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4072}
4073
4074
4075/* Resync the request and response state machines. Return 1 if either state
4076 * changes.
4077 */
4078int http_resync_states(struct session *s)
4079{
4080 struct http_txn *txn = &s->txn;
4081 int old_req_state = txn->req.msg_state;
4082 int old_res_state = txn->rsp.msg_state;
4083
4084 http_silent_debug(__LINE__, s);
4085 http_sync_req_state(s);
4086 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004087 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 if (!http_sync_res_state(s))
4089 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004090 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 if (!http_sync_req_state(s))
4092 break;
4093 }
4094 http_silent_debug(__LINE__, s);
4095 /* OK, both state machines agree on a compatible state.
4096 * There are a few cases we're interested in :
4097 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4098 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4099 * directions, so let's simply disable both analysers.
4100 * - HTTP_MSG_CLOSED on the response only means we must abort the
4101 * request.
4102 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4103 * with server-close mode means we've completed one request and we
4104 * must re-initialize the server connection.
4105 */
4106
4107 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4108 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4109 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4110 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4111 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004112 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004115 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004116 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004118 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4119 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004120 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004121 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004122 s->rep->analysers = 0;
4123 buffer_auto_close(s->rep);
4124 buffer_auto_read(s->rep);
4125 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126 buffer_abort(s->req);
4127 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004128 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004129 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 }
4131 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4132 txn->rsp.msg_state == HTTP_MSG_DONE &&
4133 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4134 /* server-close: terminate this server connection and
4135 * reinitialize a fresh-new transaction.
4136 */
4137 http_end_txn_clean_session(s);
4138 }
4139
4140 http_silent_debug(__LINE__, s);
4141 return txn->req.msg_state != old_req_state ||
4142 txn->rsp.msg_state != old_res_state;
4143}
4144
Willy Tarreaud98cf932009-12-27 22:54:55 +01004145/* This function is an analyser which forwards request body (including chunk
4146 * sizes if any). It is called as soon as we must forward, even if we forward
4147 * zero byte. The only situation where it must not be called is when we're in
4148 * tunnel mode and we want to forward till the close. It's used both to forward
4149 * remaining data and to resync after end of body. It expects the msg_state to
4150 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4151 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004152 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004153 * bytes of pending data + the headers if not already done (between som and sov).
4154 * It eventually adjusts som to match sov after the data in between have been sent.
4155 */
4156int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4157{
4158 struct http_txn *txn = &s->txn;
4159 struct http_msg *msg = &s->txn.req;
4160
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004161 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4162 return 0;
4163
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004164 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004165 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004166 /* Output closed while we were sending data. We must abort and
4167 * wake the other side up.
4168 */
4169 msg->msg_state = HTTP_MSG_ERROR;
4170 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004171 return 1;
4172 }
4173
Willy Tarreau4fe41902010-06-07 22:27:41 +02004174 /* in most states, we should abort in case of early close */
4175 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176
4177 /* Note that we don't have to send 100-continue back because we don't
4178 * need the data to complete our job, and it's up to the server to
4179 * decide whether to return 100, 417 or anything else in return of
4180 * an "Expect: 100-continue" header.
4181 */
4182
4183 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004184 /* we have msg->sov which points to the first byte of message body.
4185 * msg->som still points to the beginning of the message. We must
4186 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004188 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004189
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004190 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004191 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4192 else {
4193 msg->msg_state = HTTP_MSG_DATA;
4194 }
4195 }
4196
Willy Tarreaud98cf932009-12-27 22:54:55 +01004197 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004198 int bytes;
4199
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004201 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004202 bytes = msg->sov - msg->som;
4203 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004204 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004205 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4206 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004207 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004208 msg->chunk_len += (unsigned int)bytes;
4209 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004210 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004211
Willy Tarreaucaabe412010-01-03 23:08:28 +01004212 if (msg->msg_state == HTTP_MSG_DATA) {
4213 /* must still forward */
4214 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004215 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004216
4217 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004218 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004219 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004221 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004222 }
4223 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004224 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004225 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004226 * TRAILERS state.
4227 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004228 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004229
4230 if (!ret)
4231 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004232 else if (ret < 0) {
4233 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004234 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004235 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004237 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004240 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4241 /* we want the CRLF after the data */
4242 int ret;
4243
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004244 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245
4246 if (ret == 0)
4247 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004248 else if (ret < 0) {
4249 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004250 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004251 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004252 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004253 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004254 /* we're in MSG_CHUNK_SIZE now */
4255 }
4256 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004257 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258
4259 if (ret == 0)
4260 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004261 else if (ret < 0) {
4262 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004263 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004264 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004265 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004266 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004267 /* we're in HTTP_MSG_DONE now */
4268 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004270 int old_state = msg->msg_state;
4271
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004273 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004274 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4275 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4276 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277 if (http_resync_states(s)) {
4278 /* some state changes occurred, maybe the analyser
4279 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004280 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004281 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4282 if (req->flags & BF_SHUTW) {
4283 /* request errors are most likely due to
4284 * the server aborting the transfer.
4285 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004286 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004287 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004288 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004289 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004291 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004293 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004294
4295 /* If "option abortonclose" is set on the backend, we
4296 * want to monitor the client's connection and forward
4297 * any shutdown notification to the server, which will
4298 * decide whether to close or to go on processing the
4299 * request.
4300 */
4301 if (s->be->options & PR_O_ABRT_CLOSE) {
4302 buffer_auto_read(req);
4303 buffer_auto_close(req);
4304 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004305 else if (s->txn.meth == HTTP_METH_POST) {
4306 /* POST requests may require to read extra CRLF
4307 * sent by broken browsers and which could cause
4308 * an RST to be sent upon close on some systems
4309 * (eg: Linux).
4310 */
4311 buffer_auto_read(req);
4312 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004313
Willy Tarreau610ecce2010-01-04 21:15:02 +01004314 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004315 }
4316 }
4317
Willy Tarreaud98cf932009-12-27 22:54:55 +01004318 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004319 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004320 if (req->flags & BF_SHUTR) {
4321 if (!(s->flags & SN_ERR_MASK))
4322 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004323 if (!(s->flags & SN_FINST_MASK)) {
4324 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4325 s->flags |= SN_FINST_H;
4326 else
4327 s->flags |= SN_FINST_D;
4328 }
4329
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004330 s->fe->fe_counters.cli_aborts++;
4331 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004332 if (target_srv(&s->target))
4333 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004334
4335 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004336 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004337
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004338 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004339 if (req->flags & BF_SHUTW)
4340 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004342 /* When TE: chunked is used, we need to get there again to parse remaining
4343 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4344 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004345 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004346 buffer_dont_close(req);
4347
Willy Tarreau5c620922011-05-11 19:56:11 +02004348 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004349 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4350 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004351 * modes are already handled by the stream sock layer. We must not do
4352 * this in content-length mode because it could present the MSG_MORE
4353 * flag with the last block of forwarded data, which would cause an
4354 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004355 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004356 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004357 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004358
Willy Tarreau610ecce2010-01-04 21:15:02 +01004359 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004360 return 0;
4361
Willy Tarreaud98cf932009-12-27 22:54:55 +01004362 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004363 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004364 if (s->listener->counters)
4365 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004366 return_bad_req_stats_ok:
4367 txn->req.msg_state = HTTP_MSG_ERROR;
4368 if (txn->status) {
4369 /* Note: we don't send any error if some data were already sent */
4370 stream_int_retnclose(req->prod, NULL);
4371 } else {
4372 txn->status = 400;
4373 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4374 }
4375 req->analysers = 0;
4376 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004377
4378 if (!(s->flags & SN_ERR_MASK))
4379 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004380 if (!(s->flags & SN_FINST_MASK)) {
4381 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4382 s->flags |= SN_FINST_H;
4383 else
4384 s->flags |= SN_FINST_D;
4385 }
4386 return 0;
4387
4388 aborted_xfer:
4389 txn->req.msg_state = HTTP_MSG_ERROR;
4390 if (txn->status) {
4391 /* Note: we don't send any error if some data were already sent */
4392 stream_int_retnclose(req->prod, NULL);
4393 } else {
4394 txn->status = 502;
4395 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4396 }
4397 req->analysers = 0;
4398 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4399
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004400 s->fe->fe_counters.srv_aborts++;
4401 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004402 if (target_srv(&s->target))
4403 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004404
4405 if (!(s->flags & SN_ERR_MASK))
4406 s->flags |= SN_ERR_SRVCL;
4407 if (!(s->flags & SN_FINST_MASK)) {
4408 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4409 s->flags |= SN_FINST_H;
4410 else
4411 s->flags |= SN_FINST_D;
4412 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004413 return 0;
4414}
4415
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004416/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4417 * processing can continue on next analysers, or zero if it either needs more
4418 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4419 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4420 * when it has nothing left to do, and may remove any analyser when it wants to
4421 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004422 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004424{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 struct http_txn *txn = &s->txn;
4426 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004427 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004428 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004430 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004431
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004432 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 +02004433 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004434 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004435 rep,
4436 rep->rex, rep->wex,
4437 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004438 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004439 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004440
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441 /*
4442 * Now parse the partial (or complete) lines.
4443 * We will check the response syntax, and also join multi-line
4444 * headers. An index of all the lines will be elaborated while
4445 * parsing.
4446 *
4447 * For the parsing, we use a 28 states FSM.
4448 *
4449 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004450 * rep->data + msg->som = beginning of response
4451 * rep->data + msg->eoh = end of processed headers / start of current one
4452 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004453 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004455 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004456 */
4457
Willy Tarreau83e3af02009-12-28 17:39:57 +01004458 /* There's a protected area at the end of the buffer for rewriting
4459 * purposes. We don't want to start to parse the request if the
4460 * protected area is affected, because we may have to move processed
4461 * data later, which is much more complicated.
4462 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004463 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004464 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02004465 bi_end(rep) < b_ptr(rep, msg->next) ||
4466 bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004467 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004468 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004469 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4470 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004471 buffer_dont_close(rep);
4472 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4473 return 0;
4474 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004475 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01004476 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004477 }
4478
Willy Tarreaua458b672012-03-05 11:17:50 +01004479 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004480 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004481 }
4482
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004483 /* 1: we might have to print this header in debug mode */
4484 if (unlikely((global.mode & MODE_DEBUG) &&
4485 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004486 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004487 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004489
Willy Tarreauea1175a2012-03-05 15:52:30 +01004490 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004491 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004492 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004493
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 sol += hdr_idx_first_pos(&txn->hdr_idx);
4495 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004496
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004497 while (cur_idx) {
4498 eol = sol + txn->hdr_idx.v[cur_idx].len;
4499 debug_hdr("srvhdr", s, sol, eol);
4500 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4501 cur_idx = txn->hdr_idx.v[cur_idx].next;
4502 }
4503 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004504
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 /*
4506 * Now we quickly check if we have found a full valid response.
4507 * If not so, we check the FD and buffer states before leaving.
4508 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004509 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004510 * responses are checked first.
4511 *
4512 * Depending on whether the client is still there or not, we
4513 * may send an error response back or not. Note that normally
4514 * we should only check for HTTP status there, and check I/O
4515 * errors somewhere else.
4516 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004517
Willy Tarreau655dce92009-11-08 13:10:58 +01004518 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 /* Invalid response */
4520 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4521 /* we detected a parsing error. We want to archive this response
4522 * in the dedicated proxy area for later troubleshooting.
4523 */
4524 hdr_response_bad:
4525 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004526 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004528 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004529 if (target_srv(&s->target)) {
4530 target_srv(&s->target)->counters.failed_resp++;
4531 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004532 }
Willy Tarreau64648412010-03-05 10:41:54 +01004533 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004534 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 rep->analysers = 0;
4536 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004537 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004538 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4540
4541 if (!(s->flags & SN_ERR_MASK))
4542 s->flags |= SN_ERR_PRXCOND;
4543 if (!(s->flags & SN_FINST_MASK))
4544 s->flags |= SN_FINST_H;
4545
4546 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004548
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 /* too large response does not fit in buffer. */
4550 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004551 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004552 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004553 goto hdr_response_bad;
4554 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 /* read error */
4557 else if (rep->flags & BF_READ_ERROR) {
4558 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004559 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004560
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004561 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004562 if (target_srv(&s->target)) {
4563 target_srv(&s->target)->counters.failed_resp++;
4564 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004565 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004566
Willy Tarreau90deb182010-01-07 00:20:41 +01004567 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 rep->analysers = 0;
4569 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004570 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004571 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004572 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004573
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 if (!(s->flags & SN_ERR_MASK))
4575 s->flags |= SN_ERR_SRVCL;
4576 if (!(s->flags & SN_FINST_MASK))
4577 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004578 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004579 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 /* read timeout : return a 504 to the client. */
4582 else if (rep->flags & BF_READ_TIMEOUT) {
4583 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004584 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004585
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004586 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004587 if (target_srv(&s->target)) {
4588 target_srv(&s->target)->counters.failed_resp++;
4589 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004590 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004591
Willy Tarreau90deb182010-01-07 00:20:41 +01004592 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 rep->analysers = 0;
4594 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004595 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004596 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004598
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 if (!(s->flags & SN_ERR_MASK))
4600 s->flags |= SN_ERR_SRVTO;
4601 if (!(s->flags & SN_FINST_MASK))
4602 s->flags |= SN_FINST_H;
4603 return 0;
4604 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004605
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004606 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004608 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004609 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004610
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004611 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004612 if (target_srv(&s->target)) {
4613 target_srv(&s->target)->counters.failed_resp++;
4614 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004615 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004616
Willy Tarreau90deb182010-01-07 00:20:41 +01004617 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 rep->analysers = 0;
4619 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004620 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004621 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004622 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 if (!(s->flags & SN_ERR_MASK))
4625 s->flags |= SN_ERR_SRVCL;
4626 if (!(s->flags & SN_FINST_MASK))
4627 s->flags |= SN_FINST_H;
4628 return 0;
4629 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004630
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 /* write error to client (we don't send any message then) */
4632 else if (rep->flags & BF_WRITE_ERROR) {
4633 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004634 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004635
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004636 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004638 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639
4640 if (!(s->flags & SN_ERR_MASK))
4641 s->flags |= SN_ERR_CLICL;
4642 if (!(s->flags & SN_FINST_MASK))
4643 s->flags |= SN_FINST_H;
4644
4645 /* process_session() will take care of the error */
4646 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004647 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 buffer_dont_close(rep);
4650 return 0;
4651 }
4652
4653 /* More interesting part now : we know that we have a complete
4654 * response which at least looks like HTTP. We have an indicator
4655 * of each header's length, so we can parse them quickly.
4656 */
4657
4658 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004659 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 /*
4662 * 1: get the status code
4663 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004664 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 if (n < 1 || n > 5)
4666 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004667 /* when the client triggers a 4xx from the server, it's most often due
4668 * to a missing object or permission. These events should be tracked
4669 * because if they happen often, it may indicate a brute force or a
4670 * vulnerability scan.
4671 */
4672 if (n == 4)
4673 session_inc_http_err_ctr(s);
4674
Willy Tarreau827aee92011-03-10 16:55:02 +01004675 if (target_srv(&s->target))
4676 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677
Willy Tarreau5b154472009-12-21 20:11:07 +01004678 /* check if the response is HTTP/1.1 or above */
4679 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004680 ((rep->p[msg->sol + 5] > '1') ||
4681 ((rep->p[msg->sol + 5] == '1') &&
4682 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004683 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004684
4685 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004686 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 +01004687
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004688 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004689 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004690
Willy Tarreau3a215be2012-03-09 21:39:51 +01004691 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004692
Willy Tarreau39650402010-03-15 19:44:39 +01004693 /* Adjust server's health based on status code. Note: status codes 501
4694 * and 505 are triggered on demand by client request, so we must not
4695 * count them as server failures.
4696 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004697 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004698 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004699 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004700 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004701 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004702 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004703
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004704 /*
4705 * 2: check for cacheability.
4706 */
4707
4708 switch (txn->status) {
4709 case 200:
4710 case 203:
4711 case 206:
4712 case 300:
4713 case 301:
4714 case 410:
4715 /* RFC2616 @13.4:
4716 * "A response received with a status code of
4717 * 200, 203, 206, 300, 301 or 410 MAY be stored
4718 * by a cache (...) unless a cache-control
4719 * directive prohibits caching."
4720 *
4721 * RFC2616 @9.5: POST method :
4722 * "Responses to this method are not cacheable,
4723 * unless the response includes appropriate
4724 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004725 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004726 if (likely(txn->meth != HTTP_METH_POST) &&
4727 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4728 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4729 break;
4730 default:
4731 break;
4732 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004733
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004734 /*
4735 * 3: we may need to capture headers
4736 */
4737 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004738 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004739 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740 txn->rsp.cap, s->fe->rsp_cap);
4741
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004742 /* 4: determine the transfer-length.
4743 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4744 * the presence of a message-body in a RESPONSE and its transfer length
4745 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004746 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004747 * All responses to the HEAD request method MUST NOT include a
4748 * message-body, even though the presence of entity-header fields
4749 * might lead one to believe they do. All 1xx (informational), 204
4750 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4751 * message-body. All other responses do include a message-body,
4752 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004753 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004754 * 1. Any response which "MUST NOT" include a message-body (such as the
4755 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4756 * always terminated by the first empty line after the header fields,
4757 * regardless of the entity-header fields present in the message.
4758 *
4759 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4760 * the "chunked" transfer-coding (Section 6.2) is used, the
4761 * transfer-length is defined by the use of this transfer-coding.
4762 * If a Transfer-Encoding header field is present and the "chunked"
4763 * transfer-coding is not present, the transfer-length is defined by
4764 * the sender closing the connection.
4765 *
4766 * 3. If a Content-Length header field is present, its decimal value in
4767 * OCTETs represents both the entity-length and the transfer-length.
4768 * If a message is received with both a Transfer-Encoding header
4769 * field and a Content-Length header field, the latter MUST be ignored.
4770 *
4771 * 4. If the message uses the media type "multipart/byteranges", and
4772 * the transfer-length is not otherwise specified, then this self-
4773 * delimiting media type defines the transfer-length. This media
4774 * type MUST NOT be used unless the sender knows that the recipient
4775 * can parse it; the presence in a request of a Range header with
4776 * multiple byte-range specifiers from a 1.1 client implies that the
4777 * client can parse multipart/byteranges responses.
4778 *
4779 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004780 */
4781
4782 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004783 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004784 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004785 * FIXME: should we parse anyway and return an error on chunked encoding ?
4786 */
4787 if (txn->meth == HTTP_METH_HEAD ||
4788 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004789 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004790 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004791 goto skip_content_length;
4792 }
4793
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004794 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004795 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004796 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004797 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004798 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004799 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4800 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004801 /* bad transfer-encoding (chunked followed by something else) */
4802 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004804 break;
4805 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004806 }
4807
4808 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4809 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004810 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004811 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 signed long long cl;
4813
Willy Tarreauad14f752011-09-02 20:33:27 +02004814 if (!ctx.vlen) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004815 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004816 goto hdr_response_bad;
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 (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004820 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004821 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004822 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823
Willy Tarreauad14f752011-09-02 20:33:27 +02004824 if (cl < 0) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004825 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004826 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004827 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02004830 msg->err_pos = ctx.line + ctx.val - rep->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004831 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004832 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004834 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004835 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836 }
4837
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004838 /* FIXME: we should also implement the multipart/byterange method.
4839 * For now on, we resort to close mode in this case (unknown length).
4840 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004841skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004842
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 /* end of job, return OK */
4844 rep->analysers &= ~an_bit;
4845 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004846 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004847 return 1;
4848}
4849
4850/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004851 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4852 * and updates t->rep->analysers. It might make sense to explode it into several
4853 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004854 */
4855int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4856{
4857 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004858 struct http_msg *msg = &txn->rsp;
4859 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004860 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004861
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004862 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 +02004863 now_ms, __FUNCTION__,
4864 t,
4865 rep,
4866 rep->rex, rep->wex,
4867 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004868 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 rep->analysers);
4870
Willy Tarreau655dce92009-11-08 13:10:58 +01004871 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004872 return 0;
4873
4874 rep->analysers &= ~an_bit;
4875 rep->analyse_exp = TICK_ETERNITY;
4876
Willy Tarreau5b154472009-12-21 20:11:07 +01004877 /* Now we have to check if we need to modify the Connection header.
4878 * This is more difficult on the response than it is on the request,
4879 * because we can have two different HTTP versions and we don't know
4880 * how the client will interprete a response. For instance, let's say
4881 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4882 * HTTP/1.1 response without any header. Maybe it will bound itself to
4883 * HTTP/1.0 because it only knows about it, and will consider the lack
4884 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4885 * the lack of header as a keep-alive. Thus we will use two flags
4886 * indicating how a request MAY be understood by the client. In case
4887 * of multiple possibilities, we'll fix the header to be explicit. If
4888 * ambiguous cases such as both close and keepalive are seen, then we
4889 * will fall back to explicit close. Note that we won't take risks with
4890 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004891 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004892 */
4893
Willy Tarreaudc008c52010-02-01 16:20:08 +01004894 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4895 txn->status == 101)) {
4896 /* Either we've established an explicit tunnel, or we're
4897 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004898 * to understand the next protocols. We have to switch to tunnel
4899 * mode, so that we transfer the request and responses then let
4900 * this protocol pass unmodified. When we later implement specific
4901 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004902 * header which contains information about that protocol for
4903 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004904 */
4905 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4906 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004907 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4908 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4909 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004910 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004911
Willy Tarreau60466522010-01-18 19:08:45 +01004912 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004913 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004914 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4915 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004916
Willy Tarreau60466522010-01-18 19:08:45 +01004917 /* now adjust header transformations depending on current state */
4918 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4919 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4920 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004921 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004922 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004923 }
Willy Tarreau60466522010-01-18 19:08:45 +01004924 else { /* SCL / KAL */
4925 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004926 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004927 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004928 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004929
Willy Tarreau60466522010-01-18 19:08:45 +01004930 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004931 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004932
Willy Tarreau60466522010-01-18 19:08:45 +01004933 /* Some keep-alive responses are converted to Server-close if
4934 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004935 */
Willy Tarreau60466522010-01-18 19:08:45 +01004936 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4937 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004938 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004939 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004940 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004941 }
4942
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004943 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004944 /*
4945 * 3: we will have to evaluate the filters.
4946 * As opposed to version 1.2, now they will be evaluated in the
4947 * filters order and not in the header order. This means that
4948 * each filter has to be validated among all headers.
4949 *
4950 * Filters are tried with ->be first, then with ->fe if it is
4951 * different from ->be.
4952 */
4953
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 cur_proxy = t->be;
4955 while (1) {
4956 struct proxy *rule_set = cur_proxy;
4957
4958 /* try headers filters */
4959 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004960 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004961 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004962 if (target_srv(&t->target)) {
4963 target_srv(&t->target)->counters.failed_resp++;
4964 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004965 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004966 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004967 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004968 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004969 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004970 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004971 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004972 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 if (!(t->flags & SN_ERR_MASK))
4974 t->flags |= SN_ERR_PRXCOND;
4975 if (!(t->flags & SN_FINST_MASK))
4976 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004977 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004978 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004980
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004981 /* has the response been denied ? */
4982 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004983 if (target_srv(&t->target))
4984 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004985
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004986 t->be->be_counters.denied_resp++;
4987 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004988 if (t->listener->counters)
4989 t->listener->counters->denied_resp++;
4990
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004992 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004993
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004994 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004995 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004996 if (txn->status < 200)
4997 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004998 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004999 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005000 ret = acl_pass(ret);
5001 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5002 ret = !ret;
5003 if (!ret)
5004 continue;
5005 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005006 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005007 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005008 }
5009
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005010 /* check whether we're already working on the frontend */
5011 if (cur_proxy == t->fe)
5012 break;
5013 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005014 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005015
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005016 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005017 * We may be facing a 100-continue response, in which case this
5018 * is not the right response, and we're waiting for the next one.
5019 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005020 * next one.
5021 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005022 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005023 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005024 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005025 msg->msg_state = HTTP_MSG_RPBEFORE;
5026 txn->status = 0;
5027 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5028 return 1;
5029 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005030 else if (unlikely(txn->status < 200))
5031 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005032
5033 /* we don't have any 1xx status code now */
5034
5035 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005036 * 4: check for server cookie.
5037 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005038 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5039 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005041
Willy Tarreaubaaee002006-06-26 02:48:02 +02005042
Willy Tarreaua15645d2007-03-18 16:22:39 +01005043 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005044 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005045 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005046 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005047 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005049 /*
5050 * 6: add server cookie in the response if needed
5051 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005052 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005053 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005054 (!(t->flags & SN_DIRECT) ||
5055 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5056 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5057 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5058 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005059 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5060 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005062 /* the server is known, it's not the one the client requested, or the
5063 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 * insert a set-cookie here, except if we want to insert only on POST
5065 * requests and this one isn't. Note that servers which don't have cookies
5066 * (eg: some backup servers) will return a full cookie removal request.
5067 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005068 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005069 len = sprintf(trash,
5070 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5071 t->be->cookie_name);
5072 }
5073 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005074 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005075
5076 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5077 /* emit last_date, which is mandatory */
5078 trash[len++] = COOKIE_DELIM_DATE;
5079 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5080 if (t->be->cookie_maxlife) {
5081 /* emit first_date, which is either the original one or
5082 * the current date.
5083 */
5084 trash[len++] = COOKIE_DELIM_DATE;
5085 s30tob64(txn->cookie_first_date ?
5086 txn->cookie_first_date >> 2 :
5087 (date.tv_sec+3) >> 2, trash + len);
5088 len += 5;
5089 }
5090 }
5091 len += sprintf(trash + len, "; path=/");
5092 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 if (t->be->cookie_domain)
5095 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005096
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005097 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005098 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005099
Willy Tarreauf1348312010-10-07 15:54:11 +02005100 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005101 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005102 /* the server did not change, only the date was updated */
5103 txn->flags |= TX_SCK_UPDATED;
5104 else
5105 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005106
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005107 /* Here, we will tell an eventual cache on the client side that we don't
5108 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5109 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5110 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5111 */
5112 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005113
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5115
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005116 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005117 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005119 }
5120 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122 /*
5123 * 7: check if result will be cacheable with a cookie.
5124 * We'll block the response if security checks have caught
5125 * nasty things such as a cacheable cookie.
5126 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005127 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5128 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005129 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005130
5131 /* we're in presence of a cacheable response containing
5132 * a set-cookie header. We'll block it as requested by
5133 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005134 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005135 if (target_srv(&t->target))
5136 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005137
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005138 t->be->be_counters.denied_resp++;
5139 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005140 if (t->listener->counters)
5141 t->listener->counters->denied_resp++;
5142
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005144 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145 send_log(t->be, LOG_ALERT,
5146 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005147 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 goto return_srv_prx_502;
5149 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005150
5151 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005152 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005153 */
Willy Tarreau60466522010-01-18 19:08:45 +01005154 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5155 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5156 unsigned int want_flags = 0;
5157
5158 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5159 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5160 /* we want a keep-alive response here. Keep-alive header
5161 * required if either side is not 1.1.
5162 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005163 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005164 want_flags |= TX_CON_KAL_SET;
5165 }
5166 else {
5167 /* we want a close response here. Close header required if
5168 * the server is 1.1, regardless of the client.
5169 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005170 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005171 want_flags |= TX_CON_CLO_SET;
5172 }
5173
5174 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005175 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005176 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005177
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005178 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005179 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005180 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005181 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005182
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183 /*************************************************************
5184 * OK, that's finished for the headers. We have done what we *
5185 * could. Let's switch to the DATA state. *
5186 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005187
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005189
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /* if the user wants to log as soon as possible, without counting
5191 * bytes from the server, then this is the right moment. We have
5192 * to temporarily assign bytes_out to log what we currently have.
5193 */
5194 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5195 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5196 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005197 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005198 t->logs.bytes_out = 0;
5199 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 /* Note: we must not try to cheat by jumping directly to DATA,
5202 * otherwise we would not let the client side wake up.
5203 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005204
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005205 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005207 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005209
Willy Tarreaud98cf932009-12-27 22:54:55 +01005210/* This function is an analyser which forwards response body (including chunk
5211 * sizes if any). It is called as soon as we must forward, even if we forward
5212 * zero byte. The only situation where it must not be called is when we're in
5213 * tunnel mode and we want to forward till the close. It's used both to forward
5214 * remaining data and to resync after end of body. It expects the msg_state to
5215 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5216 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005217 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005218 * bytes of pending data + the headers if not already done (between som and sov).
5219 * It eventually adjusts som to match sov after the data in between have been sent.
5220 */
5221int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5222{
5223 struct http_txn *txn = &s->txn;
5224 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005225 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005226
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005227 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5228 return 0;
5229
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005230 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005231 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005232 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005233 /* Output closed while we were sending data. We must abort and
5234 * wake the other side up.
5235 */
5236 msg->msg_state = HTTP_MSG_ERROR;
5237 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005238 return 1;
5239 }
5240
Willy Tarreau4fe41902010-06-07 22:27:41 +02005241 /* in most states, we should abort in case of early close */
5242 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005243
Willy Tarreaud98cf932009-12-27 22:54:55 +01005244 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005245 /* we have msg->sov which points to the first byte of message body.
5246 * msg->som still points to the beginning of the message. We must
5247 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005248 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005249 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005250
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005251 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5253 else {
5254 msg->msg_state = HTTP_MSG_DATA;
5255 }
5256 }
5257
Willy Tarreaud98cf932009-12-27 22:54:55 +01005258 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005259 int bytes;
5260
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005262 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005263 bytes = msg->sov - msg->som;
5264 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005265 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005266 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5267 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005268 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005269 msg->chunk_len += (unsigned int)bytes;
5270 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005271 }
5272
Willy Tarreaucaabe412010-01-03 23:08:28 +01005273 if (msg->msg_state == HTTP_MSG_DATA) {
5274 /* must still forward */
5275 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005276 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005277
5278 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005279 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005280 msg->msg_state = HTTP_MSG_DATA_CRLF;
5281 else
5282 msg->msg_state = HTTP_MSG_DONE;
5283 }
5284 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005285 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005286 * set ->sov and ->next to point to the body and switch to DATA or
5287 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005288 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005289 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005290
5291 if (!ret)
5292 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005293 else if (ret < 0) {
5294 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005295 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005296 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005297 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005298 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5301 /* we want the CRLF after the data */
5302 int ret;
5303
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005304 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305
5306 if (!ret)
5307 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005308 else if (ret < 0) {
5309 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005310 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005312 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005313 /* we're in MSG_CHUNK_SIZE now */
5314 }
5315 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005316 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005317
Willy Tarreaud98cf932009-12-27 22:54:55 +01005318 if (ret == 0)
5319 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005320 else if (ret < 0) {
5321 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005322 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005324 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005325 /* we're in HTTP_MSG_DONE now */
5326 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005328 int old_state = msg->msg_state;
5329
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005331 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005332 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5333 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5334 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005335 if (http_resync_states(s)) {
5336 http_silent_debug(__LINE__, s);
5337 /* some state changes occurred, maybe the analyser
5338 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005339 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005340 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5341 if (res->flags & BF_SHUTW) {
5342 /* response errors are most likely due to
5343 * the client aborting the transfer.
5344 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005345 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005346 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005347 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005348 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005350 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005351 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005352 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005353 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005354 }
5355 }
5356
Willy Tarreaud98cf932009-12-27 22:54:55 +01005357 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005358 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005359 if (res->flags & BF_SHUTR) {
5360 if (!(s->flags & SN_ERR_MASK))
5361 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005362 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005363 if (target_srv(&s->target))
5364 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005365 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005366 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005367
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005368 if (res->flags & BF_SHUTW)
5369 goto aborted_xfer;
5370
Willy Tarreau40dba092010-03-04 18:14:51 +01005371 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 if (!s->req->analysers)
5373 goto return_bad_res;
5374
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005375 /* forward any pending data */
5376 bytes = msg->sov - msg->som;
5377 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005379 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5380 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005381 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005382 msg->chunk_len += (unsigned int)bytes;
5383 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005384 }
5385
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005386 /* When TE: chunked is used, we need to get there again to parse remaining
5387 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5388 * Similarly, with keep-alive on the client side, we don't want to forward a
5389 * close.
5390 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005391 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005392 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5393 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5394 buffer_dont_close(res);
5395
Willy Tarreau5c620922011-05-11 19:56:11 +02005396 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005397 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5398 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005399 * modes are already handled by the stream sock layer. We must not do
5400 * this in content-length mode because it could present the MSG_MORE
5401 * flag with the last block of forwarded data, which would cause an
5402 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005403 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005404 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005405 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005406
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005408 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005409 return 0;
5410
Willy Tarreau40dba092010-03-04 18:14:51 +01005411 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005412 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005413 if (target_srv(&s->target))
5414 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005415
5416 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005418 /* don't send any error message as we're in the body */
5419 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005420 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005421 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005422 if (target_srv(&s->target))
5423 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424
5425 if (!(s->flags & SN_ERR_MASK))
5426 s->flags |= SN_ERR_PRXCOND;
5427 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005428 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005429 return 0;
5430
5431 aborted_xfer:
5432 txn->rsp.msg_state = HTTP_MSG_ERROR;
5433 /* don't send any error message as we're in the body */
5434 stream_int_retnclose(res->cons, NULL);
5435 res->analysers = 0;
5436 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5437
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005438 s->fe->fe_counters.cli_aborts++;
5439 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005440 if (target_srv(&s->target))
5441 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005442
5443 if (!(s->flags & SN_ERR_MASK))
5444 s->flags |= SN_ERR_CLICL;
5445 if (!(s->flags & SN_FINST_MASK))
5446 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 return 0;
5448}
5449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005450/* Iterate the same filter through all request headers.
5451 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005452 * Since it can manage the switch to another backend, it updates the per-proxy
5453 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005455int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005456{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005457 char term;
5458 char *cur_ptr, *cur_end, *cur_next;
5459 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005460 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005461 struct hdr_idx_elem *cur_hdr;
5462 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464 last_hdr = 0;
5465
Willy Tarreau3a215be2012-03-09 21:39:51 +01005466 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005467 old_idx = 0;
5468
5469 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005470 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005471 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005472 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005473 (exp->action == ACT_ALLOW ||
5474 exp->action == ACT_DENY ||
5475 exp->action == ACT_TARPIT))
5476 return 0;
5477
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005478 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005479 if (!cur_idx)
5480 break;
5481
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005482 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005483 cur_ptr = cur_next;
5484 cur_end = cur_ptr + cur_hdr->len;
5485 cur_next = cur_end + cur_hdr->cr + 1;
5486
5487 /* Now we have one header between cur_ptr and cur_end,
5488 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005489 */
5490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005491 /* The annoying part is that pattern matching needs
5492 * that we modify the contents to null-terminate all
5493 * strings before testing them.
5494 */
5495
5496 term = *cur_end;
5497 *cur_end = '\0';
5498
5499 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5500 switch (exp->action) {
5501 case ACT_SETBE:
5502 /* It is not possible to jump a second time.
5503 * FIXME: should we return an HTTP/500 here so that
5504 * the admin knows there's a problem ?
5505 */
5506 if (t->be != t->fe)
5507 break;
5508
5509 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005510 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005511 last_hdr = 1;
5512 break;
5513
5514 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005515 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 last_hdr = 1;
5517 break;
5518
5519 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005520 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005521 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005522
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005523 t->fe->fe_counters.denied_req++;
5524 if (t->fe != t->be)
5525 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005526 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005527 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005529 break;
5530
5531 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005532 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005533 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005534
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005535 t->fe->fe_counters.denied_req++;
5536 if (t->fe != t->be)
5537 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005538 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005539 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005540
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005541 break;
5542
5543 case ACT_REPLACE:
5544 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5545 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5546 /* FIXME: if the user adds a newline in the replacement, the
5547 * index will not be recalculated for now, and the new line
5548 * will not be counted as a new header.
5549 */
5550
5551 cur_end += delta;
5552 cur_next += delta;
5553 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005554 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005555 break;
5556
5557 case ACT_REMOVE:
5558 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5559 cur_next += delta;
5560
Willy Tarreaufa355d42009-11-29 18:12:29 +01005561 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005562 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5563 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 cur_hdr->len = 0;
5565 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005566 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005567 break;
5568
5569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005570 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 if (cur_end)
5572 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 /* keep the link from this header to next one in case of later
5575 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005576 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005577 old_idx = cur_idx;
5578 }
5579 return 0;
5580}
5581
5582
5583/* Apply the filter to the request line.
5584 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5585 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005586 * Since it can manage the switch to another backend, it updates the per-proxy
5587 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005588 */
5589int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5590{
5591 char term;
5592 char *cur_ptr, *cur_end;
5593 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005594 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005596
Willy Tarreau58f10d72006-12-04 02:26:12 +01005597
Willy Tarreau3d300592007-03-18 18:34:41 +01005598 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005599 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005600 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005601 (exp->action == ACT_ALLOW ||
5602 exp->action == ACT_DENY ||
5603 exp->action == ACT_TARPIT))
5604 return 0;
5605 else if (exp->action == ACT_REMOVE)
5606 return 0;
5607
5608 done = 0;
5609
Willy Tarreau3a215be2012-03-09 21:39:51 +01005610 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005611 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005612
5613 /* Now we have the request line between cur_ptr and cur_end */
5614
5615 /* The annoying part is that pattern matching needs
5616 * that we modify the contents to null-terminate all
5617 * strings before testing them.
5618 */
5619
5620 term = *cur_end;
5621 *cur_end = '\0';
5622
5623 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5624 switch (exp->action) {
5625 case ACT_SETBE:
5626 /* It is not possible to jump a second time.
5627 * FIXME: should we return an HTTP/500 here so that
5628 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005629 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005630 if (t->be != t->fe)
5631 break;
5632
5633 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005634 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 done = 1;
5636 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005638 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005639 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 done = 1;
5641 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005644 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005645
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005646 t->fe->fe_counters.denied_req++;
5647 if (t->fe != t->be)
5648 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005649 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005650 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 done = 1;
5653 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005655 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005656 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005657
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005658 t->fe->fe_counters.denied_req++;
5659 if (t->fe != t->be)
5660 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005661 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005662 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 done = 1;
5665 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 case ACT_REPLACE:
5668 *cur_end = term; /* restore the string terminator */
5669 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5670 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5671 /* FIXME: if the user adds a newline in the replacement, the
5672 * index will not be recalculated for now, and the new line
5673 * will not be counted as a new header.
5674 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005675
Willy Tarreaufa355d42009-11-29 18:12:29 +01005676 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005678 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 HTTP_MSG_RQMETH,
5680 cur_ptr, cur_end + 1,
5681 NULL, NULL);
5682 if (unlikely(!cur_end))
5683 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 /* we have a full request and we know that we have either a CR
5686 * or an LF at <ptr>.
5687 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005688 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5689 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005690 /* there is no point trying this regex on headers */
5691 return 1;
5692 }
5693 }
5694 *cur_end = term; /* restore the string terminator */
5695 return done;
5696}
Willy Tarreau97de6242006-12-27 17:18:38 +01005697
Willy Tarreau58f10d72006-12-04 02:26:12 +01005698
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005701 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005703 * unparsable request. Since it can manage the switch to another backend, it
5704 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005706int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005708 struct http_txn *txn = &s->txn;
5709 struct hdr_exp *exp;
5710
5711 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714 /*
5715 * The interleaving of transformations and verdicts
5716 * makes it difficult to decide to continue or stop
5717 * the evaluation.
5718 */
5719
Willy Tarreau6c123b12010-01-28 20:22:06 +01005720 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5721 break;
5722
Willy Tarreau3d300592007-03-18 18:34:41 +01005723 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005725 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005726 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005727
5728 /* if this filter had a condition, evaluate it now and skip to
5729 * next filter if the condition does not match.
5730 */
5731 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005732 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005733 ret = acl_pass(ret);
5734 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5735 ret = !ret;
5736
5737 if (!ret)
5738 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 }
5740
5741 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005742 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 if (unlikely(ret < 0))
5744 return -1;
5745
5746 if (likely(ret == 0)) {
5747 /* The filter did not match the request, it can be
5748 * iterated through all headers.
5749 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005750 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005752 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005754}
5755
5756
Willy Tarreaua15645d2007-03-18 16:22:39 +01005757
Willy Tarreau58f10d72006-12-04 02:26:12 +01005758/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005759 * Try to retrieve the server associated to the appsession.
5760 * If the server is found, it's assigned to the session.
5761 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005762void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005763 struct http_txn *txn = &t->txn;
5764 appsess *asession = NULL;
5765 char *sessid_temp = NULL;
5766
Cyril Bontéb21570a2009-11-29 20:04:48 +01005767 if (len > t->be->appsession_len) {
5768 len = t->be->appsession_len;
5769 }
5770
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005771 if (t->be->options2 & PR_O2_AS_REQL) {
5772 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005773 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005774 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005775 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005776 }
5777
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005778 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005779 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5780 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5781 return;
5782 }
5783
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005784 memcpy(txn->sessid, buf, len);
5785 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005786 }
5787
5788 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5789 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5790 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5791 return;
5792 }
5793
Cyril Bontéb21570a2009-11-29 20:04:48 +01005794 memcpy(sessid_temp, buf, len);
5795 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005796
5797 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5798 /* free previously allocated memory */
5799 pool_free2(apools.sessid, sessid_temp);
5800
5801 if (asession != NULL) {
5802 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5803 if (!(t->be->options2 & PR_O2_AS_REQL))
5804 asession->request_count++;
5805
5806 if (asession->serverid != NULL) {
5807 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005808
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005809 while (srv) {
5810 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005811 if ((srv->state & SRV_RUNNING) ||
5812 (t->be->options & PR_O_PERSIST) ||
5813 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005814 /* we found the server and it's usable */
5815 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005816 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005817 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005818 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005819
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005820 break;
5821 } else {
5822 txn->flags &= ~TX_CK_MASK;
5823 txn->flags |= TX_CK_DOWN;
5824 }
5825 }
5826 srv = srv->next;
5827 }
5828 }
5829 }
5830}
5831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005832/* Find the end of a cookie value contained between <s> and <e>. It works the
5833 * same way as with headers above except that the semi-colon also ends a token.
5834 * See RFC2965 for more information. Note that it requires a valid header to
5835 * return a valid result.
5836 */
5837char *find_cookie_value_end(char *s, const char *e)
5838{
5839 int quoted, qdpair;
5840
5841 quoted = qdpair = 0;
5842 for (; s < e; s++) {
5843 if (qdpair) qdpair = 0;
5844 else if (quoted) {
5845 if (*s == '\\') qdpair = 1;
5846 else if (*s == '"') quoted = 0;
5847 }
5848 else if (*s == '"') quoted = 1;
5849 else if (*s == ',' || *s == ';') return s;
5850 }
5851 return s;
5852}
5853
5854/* Delete a value in a header between delimiters <from> and <next> in buffer
5855 * <buf>. The number of characters displaced is returned, and the pointer to
5856 * the first delimiter is updated if required. The function tries as much as
5857 * possible to respect the following principles :
5858 * - replace <from> delimiter by the <next> one unless <from> points to a
5859 * colon, in which case <next> is simply removed
5860 * - set exactly one space character after the new first delimiter, unless
5861 * there are not enough characters in the block being moved to do so.
5862 * - remove unneeded spaces before the previous delimiter and after the new
5863 * one.
5864 *
5865 * It is the caller's responsibility to ensure that :
5866 * - <from> points to a valid delimiter or the colon ;
5867 * - <next> points to a valid delimiter or the final CR/LF ;
5868 * - there are non-space chars before <from> ;
5869 * - there is a CR/LF at or after <next>.
5870 */
5871int del_hdr_value(struct buffer *buf, char **from, char *next)
5872{
5873 char *prev = *from;
5874
5875 if (*prev == ':') {
5876 /* We're removing the first value, preserve the colon and add a
5877 * space if possible.
5878 */
5879 if (!http_is_crlf[(unsigned char)*next])
5880 next++;
5881 prev++;
5882 if (prev < next)
5883 *prev++ = ' ';
5884
5885 while (http_is_spht[(unsigned char)*next])
5886 next++;
5887 } else {
5888 /* Remove useless spaces before the old delimiter. */
5889 while (http_is_spht[(unsigned char)*(prev-1)])
5890 prev--;
5891 *from = prev;
5892
5893 /* copy the delimiter and if possible a space if we're
5894 * not at the end of the line.
5895 */
5896 if (!http_is_crlf[(unsigned char)*next]) {
5897 *prev++ = *next++;
5898 if (prev + 1 < next)
5899 *prev++ = ' ';
5900 while (http_is_spht[(unsigned char)*next])
5901 next++;
5902 }
5903 }
5904 return buffer_replace2(buf, prev, next, NULL, 0);
5905}
5906
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005907/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005908 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005909 * desirable to call it only when needed. This code is quite complex because
5910 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5911 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005912 */
5913void manage_client_side_cookies(struct session *t, struct buffer *req)
5914{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005915 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5919 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005921 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005922 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005923 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005925 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005926 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005927 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005928
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005929 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005930 hdr_beg = hdr_next;
5931 hdr_end = hdr_beg + cur_hdr->len;
5932 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005934 /* We have one full header between hdr_beg and hdr_end, and the
5935 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005936 * "Cookie:" headers.
5937 */
5938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005939 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005940 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005941 old_idx = cur_idx;
5942 continue;
5943 }
5944
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005945 del_from = NULL; /* nothing to be deleted */
5946 preserve_hdr = 0; /* assume we may kill the whole header */
5947
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948 /* Now look for cookies. Conforming to RFC2109, we have to support
5949 * attributes whose name begin with a '$', and associate them with
5950 * the right cookie, if we want to delete this cookie.
5951 * So there are 3 cases for each cookie read :
5952 * 1) it's a special attribute, beginning with a '$' : ignore it.
5953 * 2) it's a server id cookie that we *MAY* want to delete : save
5954 * some pointers on it (last semi-colon, beginning of cookie...)
5955 * 3) it's an application cookie : we *MAY* have to delete a previous
5956 * "special" cookie.
5957 * At the end of loop, if a "special" cookie remains, we may have to
5958 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005959 * *MUST* delete it.
5960 *
5961 * Note: RFC2965 is unclear about the processing of spaces around
5962 * the equal sign in the ATTR=VALUE form. A careful inspection of
5963 * the RFC explicitly allows spaces before it, and not within the
5964 * tokens (attrs or values). An inspection of RFC2109 allows that
5965 * too but section 10.1.3 lets one think that spaces may be allowed
5966 * after the equal sign too, resulting in some (rare) buggy
5967 * implementations trying to do that. So let's do what servers do.
5968 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5969 * allowed quoted strings in values, with any possible character
5970 * after a backslash, including control chars and delimitors, which
5971 * causes parsing to become ambiguous. Browsers also allow spaces
5972 * within values even without quotes.
5973 *
5974 * We have to keep multiple pointers in order to support cookie
5975 * removal at the beginning, middle or end of header without
5976 * corrupting the header. All of these headers are valid :
5977 *
5978 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5979 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5980 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5981 * | | | | | | | | |
5982 * | | | | | | | | hdr_end <--+
5983 * | | | | | | | +--> next
5984 * | | | | | | +----> val_end
5985 * | | | | | +-----------> val_beg
5986 * | | | | +--------------> equal
5987 * | | | +----------------> att_end
5988 * | | +---------------------> att_beg
5989 * | +--------------------------> prev
5990 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991 */
5992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005993 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5994 /* Iterate through all cookies on this line */
5995
5996 /* find att_beg */
5997 att_beg = prev + 1;
5998 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5999 att_beg++;
6000
6001 /* find att_end : this is the first character after the last non
6002 * space before the equal. It may be equal to hdr_end.
6003 */
6004 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006005
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006006 while (equal < hdr_end) {
6007 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006008 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006009 if (http_is_spht[(unsigned char)*equal++])
6010 continue;
6011 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006012 }
6013
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006014 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6015 * is between <att_beg> and <equal>, both may be identical.
6016 */
6017
6018 /* look for end of cookie if there is an equal sign */
6019 if (equal < hdr_end && *equal == '=') {
6020 /* look for the beginning of the value */
6021 val_beg = equal + 1;
6022 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6023 val_beg++;
6024
6025 /* find the end of the value, respecting quotes */
6026 next = find_cookie_value_end(val_beg, hdr_end);
6027
6028 /* make val_end point to the first white space or delimitor after the value */
6029 val_end = next;
6030 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6031 val_end--;
6032 } else {
6033 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006034 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006035
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006036 /* We have nothing to do with attributes beginning with '$'. However,
6037 * they will automatically be removed if a header before them is removed,
6038 * since they're supposed to be linked together.
6039 */
6040 if (*att_beg == '$')
6041 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006042
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 /* Ignore cookies with no equal sign */
6044 if (equal == next) {
6045 /* This is not our cookie, so we must preserve it. But if we already
6046 * scheduled another cookie for removal, we cannot remove the
6047 * complete header, but we can remove the previous block itself.
6048 */
6049 preserve_hdr = 1;
6050 if (del_from != NULL) {
6051 int delta = del_hdr_value(req, &del_from, prev);
6052 val_end += delta;
6053 next += delta;
6054 hdr_end += delta;
6055 hdr_next += delta;
6056 cur_hdr->len += delta;
6057 http_msg_move_end(&txn->req, delta);
6058 prev = del_from;
6059 del_from = NULL;
6060 }
6061 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006062 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 /* if there are spaces around the equal sign, we need to
6065 * strip them otherwise we'll get trouble for cookie captures,
6066 * or even for rewrites. Since this happens extremely rarely,
6067 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006068 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006069 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6070 int stripped_before = 0;
6071 int stripped_after = 0;
6072
6073 if (att_end != equal) {
6074 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6075 equal += stripped_before;
6076 val_beg += stripped_before;
6077 }
6078
6079 if (val_beg > equal + 1) {
6080 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6081 val_beg += stripped_after;
6082 stripped_before += stripped_after;
6083 }
6084
6085 val_end += stripped_before;
6086 next += stripped_before;
6087 hdr_end += stripped_before;
6088 hdr_next += stripped_before;
6089 cur_hdr->len += stripped_before;
6090 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006091 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006094 /* First, let's see if we want to capture this cookie. We check
6095 * that we don't already have a client side cookie, because we
6096 * can only capture one. Also as an optimisation, we ignore
6097 * cookies shorter than the declared name.
6098 */
6099 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6100 (val_end - att_beg >= t->fe->capture_namelen) &&
6101 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6102 int log_len = val_end - att_beg;
6103
6104 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6105 Alert("HTTP logging : out of memory.\n");
6106 } else {
6107 if (log_len > t->fe->capture_len)
6108 log_len = t->fe->capture_len;
6109 memcpy(txn->cli_cookie, att_beg, log_len);
6110 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113
Willy Tarreaubca99692010-10-06 19:25:55 +02006114 /* Persistence cookies in passive, rewrite or insert mode have the
6115 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006116 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006117 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006119 * For cookies in prefix mode, the form is :
6120 *
6121 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006122 */
6123 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6124 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6125 struct server *srv = t->be->srv;
6126 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006128 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6129 * have the server ID between val_beg and delim, and the original cookie between
6130 * delim+1 and val_end. Otherwise, delim==val_end :
6131 *
6132 * Cookie: NAME=SRV; # in all but prefix modes
6133 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6134 * | || || | |+-> next
6135 * | || || | +--> val_end
6136 * | || || +---------> delim
6137 * | || |+------------> val_beg
6138 * | || +-------------> att_end = equal
6139 * | |+-----------------> att_beg
6140 * | +------------------> prev
6141 * +-------------------------> hdr_beg
6142 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006143
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006144 if (t->be->options & PR_O_COOK_PFX) {
6145 for (delim = val_beg; delim < val_end; delim++)
6146 if (*delim == COOKIE_DELIM)
6147 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006148 } else {
6149 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006150 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006151 /* Now check if the cookie contains a date field, which would
6152 * appear after a vertical bar ('|') just after the server name
6153 * and before the delimiter.
6154 */
6155 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6156 if (vbar1) {
6157 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006158 * right is the last seen date. It is a base64 encoded
6159 * 30-bit value representing the UNIX date since the
6160 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006161 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006162 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006163 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006164 if (val_end - vbar1 >= 5) {
6165 val = b64tos30(vbar1);
6166 if (val > 0)
6167 txn->cookie_last_date = val << 2;
6168 }
6169 /* look for a second vertical bar */
6170 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6171 if (vbar1 && (val_end - vbar1 > 5)) {
6172 val = b64tos30(vbar1 + 1);
6173 if (val > 0)
6174 txn->cookie_first_date = val << 2;
6175 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006176 }
6177 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178
Willy Tarreauf64d1412010-10-07 20:06:11 +02006179 /* if the cookie has an expiration date and the proxy wants to check
6180 * it, then we do that now. We first check if the cookie is too old,
6181 * then only if it has expired. We detect strict overflow because the
6182 * time resolution here is not great (4 seconds). Cookies with dates
6183 * in the future are ignored if their offset is beyond one day. This
6184 * allows an admin to fix timezone issues without expiring everyone
6185 * and at the same time avoids keeping unwanted side effects for too
6186 * long.
6187 */
6188 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006189 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6190 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006191 txn->flags &= ~TX_CK_MASK;
6192 txn->flags |= TX_CK_OLD;
6193 delim = val_beg; // let's pretend we have not found the cookie
6194 txn->cookie_first_date = 0;
6195 txn->cookie_last_date = 0;
6196 }
6197 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006198 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6199 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006200 txn->flags &= ~TX_CK_MASK;
6201 txn->flags |= TX_CK_EXPIRED;
6202 delim = val_beg; // let's pretend we have not found the cookie
6203 txn->cookie_first_date = 0;
6204 txn->cookie_last_date = 0;
6205 }
6206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* Here, we'll look for the first running server which supports the cookie.
6208 * This allows to share a same cookie between several servers, for example
6209 * to dedicate backup servers to specific servers only.
6210 * However, to prevent clients from sticking to cookie-less backup server
6211 * when they have incidentely learned an empty cookie, we simply ignore
6212 * empty cookies and mark them as invalid.
6213 * The same behaviour is applied when persistence must be ignored.
6214 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006215 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 while (srv) {
6219 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6220 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6221 if ((srv->state & SRV_RUNNING) ||
6222 (t->be->options & PR_O_PERSIST) ||
6223 (t->flags & SN_FORCE_PRST)) {
6224 /* we found the server and we can use it */
6225 txn->flags &= ~TX_CK_MASK;
6226 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6227 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006228 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 break;
6230 } else {
6231 /* we found a server, but it's down,
6232 * mark it as such and go on in case
6233 * another one is available.
6234 */
6235 txn->flags &= ~TX_CK_MASK;
6236 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 srv = srv->next;
6240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241
Willy Tarreauf64d1412010-10-07 20:06:11 +02006242 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006243 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006245 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6246 txn->flags |= TX_CK_UNUSED;
6247 else
6248 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* depending on the cookie mode, we may have to either :
6252 * - delete the complete cookie if we're in insert+indirect mode, so that
6253 * the server never sees it ;
6254 * - remove the server id from the cookie value, and tag the cookie as an
6255 * application cookie so that it does not get accidentely removed later,
6256 * if we're in cookie prefix mode
6257 */
6258 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6259 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006260
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6262 val_end += delta;
6263 next += delta;
6264 hdr_end += delta;
6265 hdr_next += delta;
6266 cur_hdr->len += delta;
6267 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 del_from = NULL;
6270 preserve_hdr = 1; /* we want to keep this cookie */
6271 }
6272 else if (del_from == NULL &&
6273 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6274 del_from = prev;
6275 }
6276 } else {
6277 /* This is not our cookie, so we must preserve it. But if we already
6278 * scheduled another cookie for removal, we cannot remove the
6279 * complete header, but we can remove the previous block itself.
6280 */
6281 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 if (del_from != NULL) {
6284 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006285 if (att_beg >= del_from)
6286 att_beg += delta;
6287 if (att_end >= del_from)
6288 att_end += delta;
6289 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 val_end += delta;
6291 next += delta;
6292 hdr_end += delta;
6293 hdr_next += delta;
6294 cur_hdr->len += delta;
6295 http_msg_move_end(&txn->req, delta);
6296 prev = del_from;
6297 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006301 /* Look for the appsession cookie unless persistence must be ignored */
6302 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6303 int cmp_len, value_len;
6304 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006305
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 if (t->be->options2 & PR_O2_AS_PFX) {
6307 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6308 value_begin = att_beg + t->be->appsession_name_len;
6309 value_len = val_end - att_beg - t->be->appsession_name_len;
6310 } else {
6311 cmp_len = att_end - att_beg;
6312 value_begin = val_beg;
6313 value_len = val_end - val_beg;
6314 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 /* let's see if the cookie is our appcookie */
6317 if (cmp_len == t->be->appsession_name_len &&
6318 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6319 manage_client_side_appsession(t, value_begin, value_len);
6320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321 }
6322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 /* continue with next cookie on this header line */
6324 att_beg = next;
6325 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 /* There are no more cookies on this line.
6328 * We may still have one (or several) marked for deletion at the
6329 * end of the line. We must do this now in two ways :
6330 * - if some cookies must be preserved, we only delete from the
6331 * mark to the end of line ;
6332 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 if (preserve_hdr) {
6337 delta = del_hdr_value(req, &del_from, hdr_end);
6338 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006339 cur_hdr->len += delta;
6340 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342
6343 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006344 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6345 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006347 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006350 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351 }
6352
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006355 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356}
6357
6358
Willy Tarreaua15645d2007-03-18 16:22:39 +01006359/* Iterate the same filter through all response headers contained in <rtr>.
6360 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6361 */
6362int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6363{
6364 char term;
6365 char *cur_ptr, *cur_end, *cur_next;
6366 int cur_idx, old_idx, last_hdr;
6367 struct http_txn *txn = &t->txn;
6368 struct hdr_idx_elem *cur_hdr;
6369 int len, delta;
6370
6371 last_hdr = 0;
6372
Willy Tarreau3a215be2012-03-09 21:39:51 +01006373 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 old_idx = 0;
6375
6376 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006377 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006379 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006380 (exp->action == ACT_ALLOW ||
6381 exp->action == ACT_DENY))
6382 return 0;
6383
6384 cur_idx = txn->hdr_idx.v[old_idx].next;
6385 if (!cur_idx)
6386 break;
6387
6388 cur_hdr = &txn->hdr_idx.v[cur_idx];
6389 cur_ptr = cur_next;
6390 cur_end = cur_ptr + cur_hdr->len;
6391 cur_next = cur_end + cur_hdr->cr + 1;
6392
6393 /* Now we have one header between cur_ptr and cur_end,
6394 * and the next header starts at cur_next.
6395 */
6396
6397 /* The annoying part is that pattern matching needs
6398 * that we modify the contents to null-terminate all
6399 * strings before testing them.
6400 */
6401
6402 term = *cur_end;
6403 *cur_end = '\0';
6404
6405 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6406 switch (exp->action) {
6407 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006408 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006409 last_hdr = 1;
6410 break;
6411
6412 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006413 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006414 last_hdr = 1;
6415 break;
6416
6417 case ACT_REPLACE:
6418 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6419 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6420 /* FIXME: if the user adds a newline in the replacement, the
6421 * index will not be recalculated for now, and the new line
6422 * will not be counted as a new header.
6423 */
6424
6425 cur_end += delta;
6426 cur_next += delta;
6427 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006428 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006429 break;
6430
6431 case ACT_REMOVE:
6432 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6433 cur_next += delta;
6434
Willy Tarreaufa355d42009-11-29 18:12:29 +01006435 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006436 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6437 txn->hdr_idx.used--;
6438 cur_hdr->len = 0;
6439 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006440 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 break;
6442
6443 }
6444 }
6445 if (cur_end)
6446 *cur_end = term; /* restore the string terminator */
6447
6448 /* keep the link from this header to next one in case of later
6449 * removal of next header.
6450 */
6451 old_idx = cur_idx;
6452 }
6453 return 0;
6454}
6455
6456
6457/* Apply the filter to the status line in the response buffer <rtr>.
6458 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6459 * or -1 if a replacement resulted in an invalid status line.
6460 */
6461int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6462{
6463 char term;
6464 char *cur_ptr, *cur_end;
6465 int done;
6466 struct http_txn *txn = &t->txn;
6467 int len, delta;
6468
6469
Willy Tarreau3d300592007-03-18 18:34:41 +01006470 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006471 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006472 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006473 (exp->action == ACT_ALLOW ||
6474 exp->action == ACT_DENY))
6475 return 0;
6476 else if (exp->action == ACT_REMOVE)
6477 return 0;
6478
6479 done = 0;
6480
Willy Tarreau3a215be2012-03-09 21:39:51 +01006481 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006482 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483
6484 /* Now we have the status line between cur_ptr and cur_end */
6485
6486 /* The annoying part is that pattern matching needs
6487 * that we modify the contents to null-terminate all
6488 * strings before testing them.
6489 */
6490
6491 term = *cur_end;
6492 *cur_end = '\0';
6493
6494 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6495 switch (exp->action) {
6496 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006497 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 done = 1;
6499 break;
6500
6501 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006502 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 done = 1;
6504 break;
6505
6506 case ACT_REPLACE:
6507 *cur_end = term; /* restore the string terminator */
6508 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6509 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6510 /* FIXME: if the user adds a newline in the replacement, the
6511 * index will not be recalculated for now, and the new line
6512 * will not be counted as a new header.
6513 */
6514
Willy Tarreaufa355d42009-11-29 18:12:29 +01006515 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006517 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006518 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 cur_ptr, cur_end + 1,
6520 NULL, NULL);
6521 if (unlikely(!cur_end))
6522 return -1;
6523
6524 /* we have a full respnse and we know that we have either a CR
6525 * or an LF at <ptr>.
6526 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006527 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 +02006528 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 /* there is no point trying this regex on headers */
6530 return 1;
6531 }
6532 }
6533 *cur_end = term; /* restore the string terminator */
6534 return done;
6535}
6536
6537
6538
6539/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006540 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6542 * unparsable response.
6543 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006544int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006546 struct http_txn *txn = &s->txn;
6547 struct hdr_exp *exp;
6548
6549 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550 int ret;
6551
6552 /*
6553 * The interleaving of transformations and verdicts
6554 * makes it difficult to decide to continue or stop
6555 * the evaluation.
6556 */
6557
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006558 if (txn->flags & TX_SVDENY)
6559 break;
6560
Willy Tarreau3d300592007-03-18 18:34:41 +01006561 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6563 exp->action == ACT_PASS)) {
6564 exp = exp->next;
6565 continue;
6566 }
6567
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006568 /* if this filter had a condition, evaluate it now and skip to
6569 * next filter if the condition does not match.
6570 */
6571 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006572 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006573 ret = acl_pass(ret);
6574 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6575 ret = !ret;
6576 if (!ret)
6577 continue;
6578 }
6579
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006581 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582 if (unlikely(ret < 0))
6583 return -1;
6584
6585 if (likely(ret == 0)) {
6586 /* The filter did not match the response, it can be
6587 * iterated through all headers.
6588 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006589 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591 }
6592 return 0;
6593}
6594
6595
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006597 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006598 * desirable to call it only when needed. This function is also used when we
6599 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006601void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602{
6603 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006604 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006605 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006607 char *hdr_beg, *hdr_end, *hdr_next;
6608 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 /* Iterate through the headers.
6611 * we start with the start line.
6612 */
6613 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006614 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615
6616 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6617 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006618 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619
6620 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006621 hdr_beg = hdr_next;
6622 hdr_end = hdr_beg + cur_hdr->len;
6623 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624
Willy Tarreau24581ba2010-08-31 22:39:35 +02006625 /* We have one full header between hdr_beg and hdr_end, and the
6626 * next header starts at hdr_next. We're only interested in
6627 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 */
6629
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 is_cookie2 = 0;
6631 prev = hdr_beg + 10;
6632 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006633 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6635 if (!val) {
6636 old_idx = cur_idx;
6637 continue;
6638 }
6639 is_cookie2 = 1;
6640 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 }
6642
Willy Tarreau24581ba2010-08-31 22:39:35 +02006643 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6644 * <prev> points to the colon.
6645 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006646 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647
Willy Tarreau24581ba2010-08-31 22:39:35 +02006648 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6649 * check-cache is enabled) and we are not interested in checking
6650 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006651 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006652 if (t->be->cookie_name == NULL &&
6653 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006654 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 return;
6656
Willy Tarreau24581ba2010-08-31 22:39:35 +02006657 /* OK so now we know we have to process this response cookie.
6658 * The format of the Set-Cookie header is slightly different
6659 * from the format of the Cookie header in that it does not
6660 * support the comma as a cookie delimiter (thus the header
6661 * cannot be folded) because the Expires attribute described in
6662 * the original Netscape's spec may contain an unquoted date
6663 * with a comma inside. We have to live with this because
6664 * many browsers don't support Max-Age and some browsers don't
6665 * support quoted strings. However the Set-Cookie2 header is
6666 * clean.
6667 *
6668 * We have to keep multiple pointers in order to support cookie
6669 * removal at the beginning, middle or end of header without
6670 * corrupting the header (in case of set-cookie2). A special
6671 * pointer, <scav> points to the beginning of the set-cookie-av
6672 * fields after the first semi-colon. The <next> pointer points
6673 * either to the end of line (set-cookie) or next unquoted comma
6674 * (set-cookie2). All of these headers are valid :
6675 *
6676 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6677 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6678 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6679 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6680 * | | | | | | | | | |
6681 * | | | | | | | | +-> next hdr_end <--+
6682 * | | | | | | | +------------> scav
6683 * | | | | | | +--------------> val_end
6684 * | | | | | +--------------------> val_beg
6685 * | | | | +----------------------> equal
6686 * | | | +------------------------> att_end
6687 * | | +----------------------------> att_beg
6688 * | +------------------------------> prev
6689 * +-----------------------------------------> hdr_beg
6690 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691
Willy Tarreau24581ba2010-08-31 22:39:35 +02006692 for (; prev < hdr_end; prev = next) {
6693 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694
Willy Tarreau24581ba2010-08-31 22:39:35 +02006695 /* find att_beg */
6696 att_beg = prev + 1;
6697 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6698 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 /* find att_end : this is the first character after the last non
6701 * space before the equal. It may be equal to hdr_end.
6702 */
6703 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704
Willy Tarreau24581ba2010-08-31 22:39:35 +02006705 while (equal < hdr_end) {
6706 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6707 break;
6708 if (http_is_spht[(unsigned char)*equal++])
6709 continue;
6710 att_end = equal;
6711 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712
Willy Tarreau24581ba2010-08-31 22:39:35 +02006713 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6714 * is between <att_beg> and <equal>, both may be identical.
6715 */
6716
6717 /* look for end of cookie if there is an equal sign */
6718 if (equal < hdr_end && *equal == '=') {
6719 /* look for the beginning of the value */
6720 val_beg = equal + 1;
6721 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6722 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723
Willy Tarreau24581ba2010-08-31 22:39:35 +02006724 /* find the end of the value, respecting quotes */
6725 next = find_cookie_value_end(val_beg, hdr_end);
6726
6727 /* make val_end point to the first white space or delimitor after the value */
6728 val_end = next;
6729 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6730 val_end--;
6731 } else {
6732 /* <equal> points to next comma, semi-colon or EOL */
6733 val_beg = val_end = next = equal;
6734 }
6735
6736 if (next < hdr_end) {
6737 /* Set-Cookie2 supports multiple cookies, and <next> points to
6738 * a colon or semi-colon before the end. So skip all attr-value
6739 * pairs and look for the next comma. For Set-Cookie, since
6740 * commas are permitted in values, skip to the end.
6741 */
6742 if (is_cookie2)
6743 next = find_hdr_value_end(next, hdr_end);
6744 else
6745 next = hdr_end;
6746 }
6747
6748 /* Now everything is as on the diagram above */
6749
6750 /* Ignore cookies with no equal sign */
6751 if (equal == val_end)
6752 continue;
6753
6754 /* If there are spaces around the equal sign, we need to
6755 * strip them otherwise we'll get trouble for cookie captures,
6756 * or even for rewrites. Since this happens extremely rarely,
6757 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006759 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6760 int stripped_before = 0;
6761 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762
Willy Tarreau24581ba2010-08-31 22:39:35 +02006763 if (att_end != equal) {
6764 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6765 equal += stripped_before;
6766 val_beg += stripped_before;
6767 }
6768
6769 if (val_beg > equal + 1) {
6770 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6771 val_beg += stripped_after;
6772 stripped_before += stripped_after;
6773 }
6774
6775 val_end += stripped_before;
6776 next += stripped_before;
6777 hdr_end += stripped_before;
6778 hdr_next += stripped_before;
6779 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006780 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006781 }
6782
6783 /* First, let's see if we want to capture this cookie. We check
6784 * that we don't already have a server side cookie, because we
6785 * can only capture one. Also as an optimisation, we ignore
6786 * cookies shorter than the declared name.
6787 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006788 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006789 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006790 (val_end - att_beg >= t->fe->capture_namelen) &&
6791 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6792 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006793 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 Alert("HTTP logging : out of memory.\n");
6795 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006796 else {
6797 if (log_len > t->fe->capture_len)
6798 log_len = t->fe->capture_len;
6799 memcpy(txn->srv_cookie, att_beg, log_len);
6800 txn->srv_cookie[log_len] = 0;
6801 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006802 }
6803
Willy Tarreau827aee92011-03-10 16:55:02 +01006804 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006805 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 if (!(t->flags & SN_IGNORE_PRST) &&
6807 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6808 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006809 /* assume passive cookie by default */
6810 txn->flags &= ~TX_SCK_MASK;
6811 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812
6813 /* If the cookie is in insert mode on a known server, we'll delete
6814 * this occurrence because we'll insert another one later.
6815 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006816 * a direct access.
6817 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006818 if (t->be->options2 & PR_O2_COOK_PSV) {
6819 /* The "preserve" flag was set, we don't want to touch the
6820 * server's cookie.
6821 */
6822 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006823 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006824 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 /* this cookie must be deleted */
6826 if (*prev == ':' && next == hdr_end) {
6827 /* whole header */
6828 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6829 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6830 txn->hdr_idx.used--;
6831 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006832 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006833 hdr_next += delta;
6834 http_msg_move_end(&txn->rsp, delta);
6835 /* note: while both invalid now, <next> and <hdr_end>
6836 * are still equal, so the for() will stop as expected.
6837 */
6838 } else {
6839 /* just remove the value */
6840 int delta = del_hdr_value(res, &prev, next);
6841 next = prev;
6842 hdr_end += delta;
6843 hdr_next += delta;
6844 cur_hdr->len += delta;
6845 http_msg_move_end(&txn->rsp, delta);
6846 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006847 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006848 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006851 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 * with this server since we know it.
6854 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006855 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 next += delta;
6857 hdr_end += delta;
6858 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006860 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861
Willy Tarreauf1348312010-10-07 15:54:11 +02006862 txn->flags &= ~TX_SCK_MASK;
6863 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006865 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006867 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006869 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006870 next += delta;
6871 hdr_end += delta;
6872 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006874 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875
Willy Tarreau827aee92011-03-10 16:55:02 +01006876 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006877 txn->flags &= ~TX_SCK_MASK;
6878 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879 }
6880 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006881 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6882 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006883 int cmp_len, value_len;
6884 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
Cyril Bontéb21570a2009-11-29 20:04:48 +01006886 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6888 value_begin = att_beg + t->be->appsession_name_len;
6889 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006890 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 cmp_len = att_end - att_beg;
6892 value_begin = val_beg;
6893 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006894 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006895
Cyril Bonté17530c32010-04-06 21:11:10 +02006896 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6898 /* free a possibly previously allocated memory */
6899 pool_free2(apools.sessid, txn->sessid);
6900
Cyril Bontéb21570a2009-11-29 20:04:48 +01006901 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006902 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006903 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6904 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6905 return;
6906 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006907 memcpy(txn->sessid, value_begin, value_len);
6908 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 }
6911 /* that's done for this cookie, check the next one on the same
6912 * line when next != hdr_end (only if is_cookie2).
6913 */
6914 }
6915 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006916 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006917 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006918
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006919 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006920 appsess *asession = NULL;
6921 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006922 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006923 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006924 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006925 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6926 Alert("Not enough Memory process_srv():asession:calloc().\n");
6927 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6928 return;
6929 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006930 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6931
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6933 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6934 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006935 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006936 return;
6937 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006938 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 asession->sessid[t->be->appsession_len] = 0;
6940
Willy Tarreau827aee92011-03-10 16:55:02 +01006941 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006942 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006943 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006944 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006945 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006946 return;
6947 }
6948 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006949 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006950
6951 asession->request_count = 0;
6952 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6953 }
6954
6955 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6956 asession->request_count++;
6957 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958}
6959
6960
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961/*
6962 * Check if response is cacheable or not. Updates t->flags.
6963 */
6964void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6965{
6966 struct http_txn *txn = &t->txn;
6967 char *p1, *p2;
6968
6969 char *cur_ptr, *cur_end, *cur_next;
6970 int cur_idx;
6971
Willy Tarreau5df51872007-11-25 16:20:08 +01006972 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973 return;
6974
6975 /* Iterate through the headers.
6976 * we start with the start line.
6977 */
6978 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006979 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980
6981 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6982 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006983 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984
6985 cur_hdr = &txn->hdr_idx.v[cur_idx];
6986 cur_ptr = cur_next;
6987 cur_end = cur_ptr + cur_hdr->len;
6988 cur_next = cur_end + cur_hdr->cr + 1;
6989
6990 /* We have one full header between cur_ptr and cur_end, and the
6991 * next header starts at cur_next. We're only interested in
6992 * "Cookie:" headers.
6993 */
6994
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006995 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6996 if (val) {
6997 if ((cur_end - (cur_ptr + val) >= 8) &&
6998 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6999 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7000 return;
7001 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002 }
7003
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007004 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7005 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 continue;
7007
7008 /* OK, right now we know we have a cache-control header at cur_ptr */
7009
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007010 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011
7012 if (p1 >= cur_end) /* no more info */
7013 continue;
7014
7015 /* p1 is at the beginning of the value */
7016 p2 = p1;
7017
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007018 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019 p2++;
7020
7021 /* we have a complete value between p1 and p2 */
7022 if (p2 < cur_end && *p2 == '=') {
7023 /* we have something of the form no-cache="set-cookie" */
7024 if ((cur_end - p1 >= 21) &&
7025 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7026 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007027 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007028 continue;
7029 }
7030
7031 /* OK, so we know that either p2 points to the end of string or to a comma */
7032 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7033 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7034 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7035 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007036 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 return;
7038 }
7039
7040 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007041 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 continue;
7043 }
7044 }
7045}
7046
7047
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048/*
7049 * Try to retrieve a known appsession in the URI, then the associated server.
7050 * If the server is found, it's assigned to the session.
7051 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007052void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007054 char *end_params, *first_param, *cur_param, *next_param;
7055 char separator;
7056 int value_len;
7057
7058 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007059
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007060 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007061 (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 +01007062 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007063 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Cyril Bontéb21570a2009-11-29 20:04:48 +01007065 first_param = NULL;
7066 switch (mode) {
7067 case PR_O2_AS_M_PP:
7068 first_param = memchr(begin, ';', len);
7069 break;
7070 case PR_O2_AS_M_QS:
7071 first_param = memchr(begin, '?', len);
7072 break;
7073 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007074
Cyril Bontéb21570a2009-11-29 20:04:48 +01007075 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078
Cyril Bontéb21570a2009-11-29 20:04:48 +01007079 switch (mode) {
7080 case PR_O2_AS_M_PP:
7081 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7082 end_params = (char *) begin + len;
7083 }
7084 separator = ';';
7085 break;
7086 case PR_O2_AS_M_QS:
7087 end_params = (char *) begin + len;
7088 separator = '&';
7089 break;
7090 default:
7091 /* unknown mode, shouldn't happen */
7092 return;
7093 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Cyril Bontéb21570a2009-11-29 20:04:48 +01007095 cur_param = next_param = end_params;
7096 while (cur_param > first_param) {
7097 cur_param--;
7098 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7099 /* let's see if this is the appsession parameter */
7100 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7101 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7102 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7103 /* Cool... it's the right one */
7104 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7105 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7106 if (value_len > 0) {
7107 manage_client_side_appsession(t, cur_param, value_len);
7108 }
7109 break;
7110 }
7111 next_param = cur_param;
7112 }
7113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007115 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007116 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007117#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118}
7119
Willy Tarreaub2513902006-12-17 14:52:38 +01007120/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007121 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007122 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007123 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007124 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007125 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007126 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007127 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007128 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007129int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007130{
7131 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007132 struct http_msg *msg = &txn->req;
7133 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7134 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007135
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007136 if (!uri_auth)
7137 return 0;
7138
Cyril Bonté70be45d2010-10-12 00:14:35 +02007139 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007140 return 0;
7141
Willy Tarreau295a8372011-03-10 11:25:07 +01007142 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007143 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007146 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007147 return 0;
7148
Willy Tarreau3a215be2012-03-09 21:39:51 +01007149 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007150 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007151 return 0;
7152
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007153 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007154 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007155 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007156 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007157 break;
7158 }
7159 h++;
7160 }
7161
7162 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007163 h = uri + uri_auth->uri_len;
7164 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007165 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007166 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007167 break;
7168 }
7169 h++;
7170 }
7171 }
7172
Willy Tarreau3a215be2012-03-09 21:39:51 +01007173 h = uri + uri_auth->uri_len;
7174 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007175 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007176 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007177 break;
7178 }
7179 h++;
7180 }
7181
Willy Tarreau3a215be2012-03-09 21:39:51 +01007182 h = uri + uri_auth->uri_len;
7183 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007184 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007185 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007186 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007187 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7188 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7189 si->applet.ctx.stats.st_code = i;
7190 break;
7191 }
7192 }
7193 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007194 break;
7195 }
7196 h++;
7197 }
7198
Willy Tarreau295a8372011-03-10 11:25:07 +01007199 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007200
Willy Tarreaub2513902006-12-17 14:52:38 +01007201 return 1;
7202}
7203
Willy Tarreau4076a152009-04-02 15:18:36 +02007204/*
7205 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007206 * By default it tries to report the error position as msg->err_pos. However if
7207 * this one is not set, it will then report msg->next, which is the last known
7208 * parsing point. The function is able to deal with wrapping buffers. It always
7209 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007210 */
7211void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007212 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007213 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007214{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007215 struct buffer *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007216 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007217
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007218 es->len = MIN(buf->i, sizeof(es->buf));
7219 len1 = buf->data + buf->size - buf->p;
7220 len1 = MIN(len1, es->len);
7221 len2 = es->len - len1; /* remaining data if buffer wraps */
7222
7223 memcpy(es->buf, buf->p, len1);
7224 if (len2)
7225 memcpy(es->buf + len1, buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007226
Willy Tarreau4076a152009-04-02 15:18:36 +02007227 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007228 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007229 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007230 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007231
Willy Tarreau4076a152009-04-02 15:18:36 +02007232 es->when = date; // user-visible date
7233 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007234 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007235 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007236 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007237 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007238 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007239 es->b_flags = buf->flags;
7240 es->s_flags = s->flags;
7241 es->t_flags = s->txn.flags;
7242 es->m_flags = msg->flags;
7243 es->b_out = buf->o;
7244 es->b_wrap = buf->data + buf->size - buf->p;
7245 es->b_tot = buf->total;
7246 es->m_clen = msg->chunk_len;
7247 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007248}
Willy Tarreaub2513902006-12-17 14:52:38 +01007249
Willy Tarreau294c4732011-12-16 21:35:50 +01007250/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7251 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7252 * performed over the whole headers. Otherwise it must contain a valid header
7253 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7254 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7255 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7256 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7257 * -1.
7258 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007259 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007260unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007261 struct hdr_idx *idx, int occ,
7262 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007263{
Willy Tarreau294c4732011-12-16 21:35:50 +01007264 struct hdr_ctx local_ctx;
7265 char *ptr_hist[MAX_HDR_HISTORY];
7266 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007267 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007268 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007269
Willy Tarreau294c4732011-12-16 21:35:50 +01007270 if (!ctx) {
7271 local_ctx.idx = 0;
7272 ctx = &local_ctx;
7273 }
7274
Willy Tarreaubce70882009-09-07 11:51:47 +02007275 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007276 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007277 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 occ--;
7279 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007280 *vptr = ctx->line + ctx->val;
7281 *vlen = ctx->vlen;
7282 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007283 }
7284 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007285 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007286 }
7287
7288 /* negative occurrence, we scan all the list then walk back */
7289 if (-occ > MAX_HDR_HISTORY)
7290 return 0;
7291
Willy Tarreau294c4732011-12-16 21:35:50 +01007292 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007293 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007294 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7295 len_hist[hist_ptr] = ctx->vlen;
7296 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007297 hist_ptr = 0;
7298 found++;
7299 }
7300 if (-occ > found)
7301 return 0;
7302 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7303 * find occurrence -occ, so we have to check [hist_ptr+occ].
7304 */
7305 hist_ptr += occ;
7306 if (hist_ptr >= MAX_HDR_HISTORY)
7307 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007308 *vptr = ptr_hist[hist_ptr];
7309 *vlen = len_hist[hist_ptr];
7310 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007311}
7312
Willy Tarreaubaaee002006-06-26 02:48:02 +02007313/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314 * Print a debug line with a header
7315 */
7316void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7317{
7318 int len, max;
7319 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007320 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 max = end - start;
7322 UBOUND(max, sizeof(trash) - len - 1);
7323 len += strlcpy2(trash + len, start, max + 1);
7324 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007325 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007326}
7327
Willy Tarreau0937bc42009-12-22 15:03:09 +01007328/*
7329 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7330 * the required fields are properly allocated and that we only need to (re)init
7331 * them. This should be used before processing any new request.
7332 */
7333void http_init_txn(struct session *s)
7334{
7335 struct http_txn *txn = &s->txn;
7336 struct proxy *fe = s->fe;
7337
7338 txn->flags = 0;
7339 txn->status = -1;
7340
William Lallemand5f232402012-04-05 18:02:55 +02007341 global.req_count++;
7342
Willy Tarreauf64d1412010-10-07 20:06:11 +02007343 txn->cookie_first_date = 0;
7344 txn->cookie_last_date = 0;
7345
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007346 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007347 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007348 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007349 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007350 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007351 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007352 txn->req.chunk_len = 0LL;
7353 txn->req.body_len = 0LL;
7354 txn->rsp.chunk_len = 0LL;
7355 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007356 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7357 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007358 txn->req.buf = s->req;
7359 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007360
7361 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007362
7363 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7364 if (fe->options2 & PR_O2_REQBUG_OK)
7365 txn->req.err_pos = -1; /* let buggy requests pass */
7366
Willy Tarreau46023632010-01-07 22:51:47 +01007367 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007368 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7369
Willy Tarreau46023632010-01-07 22:51:47 +01007370 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007371 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7372
7373 if (txn->hdr_idx.v)
7374 hdr_idx_init(&txn->hdr_idx);
7375}
7376
7377/* to be used at the end of a transaction */
7378void http_end_txn(struct session *s)
7379{
7380 struct http_txn *txn = &s->txn;
7381
7382 /* these ones will have been dynamically allocated */
7383 pool_free2(pool2_requri, txn->uri);
7384 pool_free2(pool2_capture, txn->cli_cookie);
7385 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007386 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007387 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007388
William Lallemanda73203e2012-03-12 12:48:57 +01007389 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007390 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007391 txn->uri = NULL;
7392 txn->srv_cookie = NULL;
7393 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007394
7395 if (txn->req.cap) {
7396 struct cap_hdr *h;
7397 for (h = s->fe->req_cap; h; h = h->next)
7398 pool_free2(h->pool, txn->req.cap[h->index]);
7399 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7400 }
7401
7402 if (txn->rsp.cap) {
7403 struct cap_hdr *h;
7404 for (h = s->fe->rsp_cap; h; h = h->next)
7405 pool_free2(h->pool, txn->rsp.cap[h->index]);
7406 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7407 }
7408
Willy Tarreau0937bc42009-12-22 15:03:09 +01007409}
7410
7411/* to be used at the end of a transaction to prepare a new one */
7412void http_reset_txn(struct session *s)
7413{
7414 http_end_txn(s);
7415 http_init_txn(s);
7416
7417 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007418 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007419 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007420 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007421 /* re-init store persistence */
7422 s->store_count = 0;
7423
Willy Tarreau0937bc42009-12-22 15:03:09 +01007424 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007425
7426 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7427
Willy Tarreau739cfba2010-01-25 23:11:14 +01007428 /* We must trim any excess data from the response buffer, because we
7429 * may have blocked an invalid response from a server that we don't
7430 * want to accidentely forward once we disable the analysers, nor do
7431 * we want those data to come along with next response. A typical
7432 * example of such data would be from a buggy server responding to
7433 * a HEAD with some data, or sending more than the advertised
7434 * content-length.
7435 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007436 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007437 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007438
Willy Tarreau0937bc42009-12-22 15:03:09 +01007439 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007440 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007441
Willy Tarreaud04e8582010-05-31 12:31:35 +02007442 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007443 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007444
7445 s->req->rex = TICK_ETERNITY;
7446 s->req->wex = TICK_ETERNITY;
7447 s->req->analyse_exp = TICK_ETERNITY;
7448 s->rep->rex = TICK_ETERNITY;
7449 s->rep->wex = TICK_ETERNITY;
7450 s->rep->analyse_exp = TICK_ETERNITY;
7451}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452
Willy Tarreauff011f22011-01-06 17:51:27 +01007453void free_http_req_rules(struct list *r) {
7454 struct http_req_rule *tr, *pr;
7455
7456 list_for_each_entry_safe(pr, tr, r, list) {
7457 LIST_DEL(&pr->list);
7458 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7459 free(pr->http_auth.realm);
7460
7461 free(pr);
7462 }
7463}
7464
7465struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7466{
7467 struct http_req_rule *rule;
7468 int cur_arg;
7469
7470 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7471 if (!rule) {
7472 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7473 return NULL;
7474 }
7475
7476 if (!*args[0]) {
7477 goto req_error_parsing;
7478 } else if (!strcmp(args[0], "allow")) {
7479 rule->action = HTTP_REQ_ACT_ALLOW;
7480 cur_arg = 1;
7481 } else if (!strcmp(args[0], "deny")) {
7482 rule->action = HTTP_REQ_ACT_DENY;
7483 cur_arg = 1;
7484 } else if (!strcmp(args[0], "auth")) {
7485 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7486 cur_arg = 1;
7487
7488 while(*args[cur_arg]) {
7489 if (!strcmp(args[cur_arg], "realm")) {
7490 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7491 cur_arg+=2;
7492 continue;
7493 } else
7494 break;
7495 }
7496 } else {
7497req_error_parsing:
7498 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7499 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7500 return NULL;
7501 }
7502
7503 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7504 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007505 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007506
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007507 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7508 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7509 file, linenum, args[0], errmsg);
7510 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007511 return NULL;
7512 }
7513 rule->cond = cond;
7514 }
7515 else if (*args[cur_arg]) {
7516 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7517 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7518 file, linenum, args[0], args[cur_arg]);
7519 return NULL;
7520 }
7521
7522 return rule;
7523}
7524
Willy Tarreau8797c062007-05-07 00:55:35 +02007525/************************************************************************/
7526/* The code below is dedicated to ACL parsing and matching */
7527/************************************************************************/
7528
7529
Willy Tarreau14174bc2012-04-16 14:34:04 +02007530/* This function ensures that the prerequisites for an L7 fetch are ready,
7531 * which means that a request or response is ready. If some data is missing,
7532 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007533 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7534 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007535 *
7536 * The function returns :
7537 * 0 if some data is missing or if the requested data cannot be fetched
7538 * -1 if it is certain that we'll never have any HTTP message there
7539 * 1 if an HTTP message is ready
7540 */
7541static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007542acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007543 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007544{
7545 struct http_txn *txn = l7;
7546 struct http_msg *msg = &txn->req;
7547
7548 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7549 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7550 */
7551
7552 if (unlikely(!s || !txn))
7553 return 0;
7554
7555 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007556 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007557
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007558 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007559 if (unlikely(!s->req))
7560 return 0;
7561
7562 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
7563 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007564 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007565 return -1;
7566 }
7567
7568 /* Try to decode HTTP request */
7569 if (likely(msg->next < s->req->i))
7570 http_msg_analyzer(msg, &txn->hdr_idx);
7571
7572 /* Still no valid request ? */
7573 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7574 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007575 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007576 return -1;
7577 }
7578 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007579 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007580 return 0;
7581 }
7582
7583 /* OK we just got a valid HTTP request. We have some minor
7584 * preparation to perform so that further checks can rely
7585 * on HTTP tests.
7586 */
7587 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
7588 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7589 s->flags |= SN_REDIRECTABLE;
7590
7591 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007592 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007593 return -1;
7594 }
7595 }
7596
Willy Tarreau24e32d82012-04-23 23:55:44 +02007597 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007598 return 0; /* data might have moved and indexes changed */
7599
7600 /* otherwise everything's ready for the request */
7601 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007602 else {
7603 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007604 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7605 return 0;
7606 }
7607
7608 /* everything's OK */
7609 return 1;
7610}
Willy Tarreau8797c062007-05-07 00:55:35 +02007611
Willy Tarreauc0239e02012-04-16 14:42:55 +02007612#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007613 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02007614
Willy Tarreau24e32d82012-04-23 23:55:44 +02007615#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007616 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02007617
Willy Tarreau8797c062007-05-07 00:55:35 +02007618
7619/* 1. Check on METHOD
7620 * We use the pre-parsed method if it is known, and store its number as an
7621 * integer. If it is unknown, we use the pointer and the length.
7622 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007623static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007624{
7625 int len, meth;
7626
Willy Tarreauae8b7962007-06-09 23:10:04 +02007627 len = strlen(*text);
7628 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007629
7630 pattern->val.i = meth;
7631 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007632 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007633 if (!pattern->ptr.str) {
7634 if (err)
7635 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007636 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007637 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007638 pattern->len = len;
7639 }
7640 return 1;
7641}
7642
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007643/* This function fetches the method of current HTTP request and stores
7644 * it in the global pattern struct as a chunk. There are two possibilities :
7645 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7646 * in <len> and <ptr> is NULL ;
7647 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7648 * <len> to its length.
7649 * This is intended to be used with acl_match_meth() only.
7650 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007651static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007652acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007653 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007654{
7655 int meth;
7656 struct http_txn *txn = l7;
7657
Willy Tarreau24e32d82012-04-23 23:55:44 +02007658 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007659
Willy Tarreau8797c062007-05-07 00:55:35 +02007660 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007661 smp->type = SMP_T_UINT;
7662 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007663 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007664 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7665 /* ensure the indexes are not affected */
7666 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007667 smp->type = SMP_T_CSTR;
7668 smp->data.str.len = txn->req.sl.rq.m_l;
7669 smp->data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007670 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007671 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007672 return 1;
7673}
7674
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007675/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007676static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007677{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007678 int icase;
7679
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007680
Willy Tarreauf853c462012-04-23 18:53:56 +02007681 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007682 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007683 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007684 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007685 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007686 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007687
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007688 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7689 if (pattern->val.i != HTTP_METH_OTHER)
7690 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007691
7692 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007693 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007694 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007695
7696 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007697 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7698 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007699 return ACL_PAT_FAIL;
7700 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007701}
7702
7703/* 2. Check on Request/Status Version
7704 * We simply compare strings here.
7705 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007706static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007707{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007708 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007709 if (!pattern->ptr.str) {
7710 if (err)
7711 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007712 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007713 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007714 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007715 return 1;
7716}
7717
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007718static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007719acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007720 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007721{
7722 struct http_txn *txn = l7;
7723 char *ptr;
7724 int len;
7725
Willy Tarreauc0239e02012-04-16 14:42:55 +02007726 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007727
Willy Tarreau8797c062007-05-07 00:55:35 +02007728 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007729 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007730
7731 while ((len-- > 0) && (*ptr++ != '/'));
7732 if (len <= 0)
7733 return 0;
7734
Willy Tarreauf853c462012-04-23 18:53:56 +02007735 smp->type = SMP_T_CSTR;
7736 smp->data.str.str = ptr;
7737 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007738
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007739 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007740 return 1;
7741}
7742
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007743static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007744acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007745 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007746{
7747 struct http_txn *txn = l7;
7748 char *ptr;
7749 int len;
7750
Willy Tarreauc0239e02012-04-16 14:42:55 +02007751 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007752
Willy Tarreau8797c062007-05-07 00:55:35 +02007753 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007754 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007755
7756 while ((len-- > 0) && (*ptr++ != '/'));
7757 if (len <= 0)
7758 return 0;
7759
Willy Tarreauf853c462012-04-23 18:53:56 +02007760 smp->type = SMP_T_CSTR;
7761 smp->data.str.str = ptr;
7762 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007763
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007764 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007765 return 1;
7766}
7767
7768/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007769static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007770acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007771 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007772{
7773 struct http_txn *txn = l7;
7774 char *ptr;
7775 int len;
7776
Willy Tarreauc0239e02012-04-16 14:42:55 +02007777 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007778
Willy Tarreau8797c062007-05-07 00:55:35 +02007779 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007780 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007781
Willy Tarreauf853c462012-04-23 18:53:56 +02007782 smp->type = SMP_T_UINT;
7783 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007784 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007785 return 1;
7786}
7787
7788/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007789static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007790acl_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007791 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007792{
7793 struct http_txn *txn = l7;
7794
Willy Tarreauc0239e02012-04-16 14:42:55 +02007795 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007796
Willy Tarreauf853c462012-04-23 18:53:56 +02007797 smp->type = SMP_T_CSTR;
7798 smp->data.str.len = txn->req.sl.rq.u_l;
7799 smp->data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007800 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007801 return 1;
7802}
7803
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007804static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007805acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007806 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007807{
7808 struct http_txn *txn = l7;
7809
Willy Tarreauc0239e02012-04-16 14:42:55 +02007810 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007811
7812 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007813 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 +01007814 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7815 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007816 smp->type = SMP_T_IPV4;
7817 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007818
7819 /*
7820 * If we are parsing url in frontend space, we prepare backend stage
7821 * to not parse again the same url ! optimization lazyness...
7822 */
7823 if (px->options & PR_O_HTTP_PROXY)
7824 l4->flags |= SN_ADDR_SET;
7825
Willy Tarreau37406352012-04-23 16:16:37 +02007826 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007827 return 1;
7828}
7829
7830static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007831acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007832 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007833{
7834 struct http_txn *txn = l7;
7835
Willy Tarreauc0239e02012-04-16 14:42:55 +02007836 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007837
7838 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007839 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 Tarreauf853c462012-04-23 18:53:56 +02007840 smp->type = SMP_T_UINT;
7841 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007842
7843 if (px->options & PR_O_HTTP_PROXY)
7844 l4->flags |= SN_ADDR_SET;
7845
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007846 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007847 return 1;
7848}
7849
Willy Tarreau185b5c42012-04-26 15:11:51 +02007850/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7851 * Accepts an optional argument of type string containing the header field name,
7852 * and an optional argument of type signed or unsigned integer to request an
7853 * explicit occurrence of the header. Note that in the event of a missing name,
7854 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007855 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007856static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007857smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007858 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007859{
7860 struct http_txn *txn = l7;
7861 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007862 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007863 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007864 int occ = 0;
7865 const char *name_str = NULL;
7866 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007867
Willy Tarreau185b5c42012-04-26 15:11:51 +02007868 if (args) {
7869 if (args[0].type != ARGT_STR)
7870 return 0;
7871 name_str = args[0].data.str.str;
7872 name_len = args[0].data.str.len;
7873
7874 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7875 occ = args[1].data.uint;
7876 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007877
Willy Tarreaue333ec92012-04-16 16:26:40 +02007878 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007879
Willy Tarreau185b5c42012-04-26 15:11:51 +02007880 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007881 /* search for header from the beginning */
7882 ctx->idx = 0;
7883
Willy Tarreau185b5c42012-04-26 15:11:51 +02007884 if (!occ && !(opt & SMP_OPT_ITERATE))
7885 /* no explicit occurrence and single fetch => last header by default */
7886 occ = -1;
7887
7888 if (!occ)
7889 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007890 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007891
Willy Tarreau185b5c42012-04-26 15:11:51 +02007892 smp->type = SMP_T_CSTR;
7893 smp->flags |= SMP_F_VOL_HDR;
7894 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007895 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007896
Willy Tarreau37406352012-04-23 16:16:37 +02007897 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007898 return 0;
7899}
7900
Willy Tarreauc11416f2007-06-17 16:58:38 +02007901/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007902 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007903 */
7904static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007905smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007906 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007907{
7908 struct http_txn *txn = l7;
7909 struct hdr_idx *idx = &txn->hdr_idx;
7910 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007911 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007912 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007913
Willy Tarreau24e32d82012-04-23 23:55:44 +02007914 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007915 return 0;
7916
Willy Tarreaue333ec92012-04-16 16:26:40 +02007917 CHECK_HTTP_MESSAGE_FIRST();
7918
Willy Tarreau33a7e692007-06-10 19:45:56 +02007919 ctx.idx = 0;
7920 cnt = 0;
Willy Tarreau24e32d82012-04-23 23:55:44 +02007921 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->p + msg->sol, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007922 cnt++;
7923
Willy Tarreauf853c462012-04-23 18:53:56 +02007924 smp->type = SMP_T_UINT;
7925 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007926 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007927 return 1;
7928}
7929
Willy Tarreau185b5c42012-04-26 15:11:51 +02007930/* Fetch an HTTP header's integer value. The integer value is returned. It
7931 * takes a mandatory argument of type string and an optional one of type int
7932 * to designate a specific occurrence. It returns an unsigned integer, which
7933 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007934 */
7935static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007936smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007937 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007938{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007939 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007940
Willy Tarreauf853c462012-04-23 18:53:56 +02007941 if (ret > 0) {
7942 smp->type = SMP_T_UINT;
7943 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7944 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007945
Willy Tarreaud53e2422012-04-16 17:21:11 +02007946 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007947}
7948
Willy Tarreau185b5c42012-04-26 15:11:51 +02007949/* Fetch an HTTP header's integer value. The integer value is returned. It
7950 * takes a mandatory argument of type string and an optional one of type int
7951 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02007952 */
7953static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007954smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007955 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02007956{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007957 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007958
Willy Tarreau185b5c42012-04-26 15:11:51 +02007959 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02007960 smp->type = SMP_T_IPV4;
7961 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02007962 break;
7963 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007964 if (!(smp->flags & SMP_F_NOT_LAST))
7965 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02007966 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02007967 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02007968}
7969
Willy Tarreau737b0c12007-06-10 21:28:46 +02007970/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7971 * the first '/' after the possible hostname, and ends before the possible '?'.
7972 */
7973static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007974acl_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007975 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007976{
7977 struct http_txn *txn = l7;
7978 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007979
Willy Tarreauc0239e02012-04-16 14:42:55 +02007980 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007981
Willy Tarreau3a215be2012-03-09 21:39:51 +01007982 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 +01007983 ptr = http_get_path(txn);
7984 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007985 return 0;
7986
7987 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02007988 smp->type = SMP_T_CSTR;
7989 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007990
7991 while (ptr < end && *ptr != '?')
7992 ptr++;
7993
Willy Tarreauf853c462012-04-23 18:53:56 +02007994 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02007995 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007996 return 1;
7997}
7998
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007999static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008000acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008001 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008002{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008003 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8004 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8005 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008006
Willy Tarreau24e32d82012-04-23 23:55:44 +02008007 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008008
Willy Tarreauf853c462012-04-23 18:53:56 +02008009 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008010 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008011 return 1;
8012}
8013
Willy Tarreau7f18e522010-10-22 20:04:13 +02008014/* return a valid test if the current request is the first one on the connection */
8015static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008016acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008017 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008018{
8019 if (!s)
8020 return 0;
8021
Willy Tarreauf853c462012-04-23 18:53:56 +02008022 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008023 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008024 return 1;
8025}
8026
Willy Tarreau34db1082012-04-19 17:16:54 +02008027/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008028static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008029acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008030 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008031{
8032
Willy Tarreau24e32d82012-04-23 23:55:44 +02008033 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008034 return 0;
8035
Willy Tarreauc0239e02012-04-16 14:42:55 +02008036 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008037
Willy Tarreauc0239e02012-04-16 14:42:55 +02008038 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008039 return 0;
8040
Willy Tarreauf853c462012-04-23 18:53:56 +02008041 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008042 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008043 return 1;
8044}
Willy Tarreau8797c062007-05-07 00:55:35 +02008045
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008046/* Try to find the next occurrence of a cookie name in a cookie header value.
8047 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8048 * the cookie value is returned into *value and *value_l, and the function
8049 * returns a pointer to the next pointer to search from if the value was found.
8050 * Otherwise if the cookie was not found, NULL is returned and neither value
8051 * nor value_l are touched. The input <hdr> string should first point to the
8052 * header's value, and the <hdr_end> pointer must point to the first character
8053 * not part of the value. <list> must be non-zero if value may represent a list
8054 * of values (cookie headers). This makes it faster to abort parsing when no
8055 * list is expected.
8056 */
8057static char *
8058extract_cookie_value(char *hdr, const char *hdr_end,
8059 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008060 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008061{
8062 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8063 char *next;
8064
8065 /* we search at least a cookie name followed by an equal, and more
8066 * generally something like this :
8067 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8068 */
8069 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8070 /* Iterate through all cookies on this line */
8071
8072 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8073 att_beg++;
8074
8075 /* find att_end : this is the first character after the last non
8076 * space before the equal. It may be equal to hdr_end.
8077 */
8078 equal = att_end = att_beg;
8079
8080 while (equal < hdr_end) {
8081 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8082 break;
8083 if (http_is_spht[(unsigned char)*equal++])
8084 continue;
8085 att_end = equal;
8086 }
8087
8088 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8089 * is between <att_beg> and <equal>, both may be identical.
8090 */
8091
8092 /* look for end of cookie if there is an equal sign */
8093 if (equal < hdr_end && *equal == '=') {
8094 /* look for the beginning of the value */
8095 val_beg = equal + 1;
8096 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8097 val_beg++;
8098
8099 /* find the end of the value, respecting quotes */
8100 next = find_cookie_value_end(val_beg, hdr_end);
8101
8102 /* make val_end point to the first white space or delimitor after the value */
8103 val_end = next;
8104 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8105 val_end--;
8106 } else {
8107 val_beg = val_end = next = equal;
8108 }
8109
8110 /* We have nothing to do with attributes beginning with '$'. However,
8111 * they will automatically be removed if a header before them is removed,
8112 * since they're supposed to be linked together.
8113 */
8114 if (*att_beg == '$')
8115 continue;
8116
8117 /* Ignore cookies with no equal sign */
8118 if (equal == next)
8119 continue;
8120
8121 /* Now we have the cookie name between att_beg and att_end, and
8122 * its value between val_beg and val_end.
8123 */
8124
8125 if (att_end - att_beg == cookie_name_l &&
8126 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8127 /* let's return this value and indicate where to go on from */
8128 *value = val_beg;
8129 *value_l = val_end - val_beg;
8130 return next + 1;
8131 }
8132
8133 /* Set-Cookie headers only have the name in the first attr=value part */
8134 if (!list)
8135 break;
8136 }
8137
8138 return NULL;
8139}
8140
Willy Tarreaue333ec92012-04-16 16:26:40 +02008141/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008142 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8143 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008144 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008145 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008146 * Accepts exactly 1 argument of type string. If the input options indicate
8147 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008148 */
8149static int
Willy Tarreau51539362012-05-08 12:46:28 +02008150smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8151 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008152{
8153 struct http_txn *txn = l7;
8154 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008155 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008156 const struct http_msg *msg;
8157 const char *hdr_name;
8158 int hdr_name_len;
8159 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008160 int occ = 0;
8161 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008162
Willy Tarreau24e32d82012-04-23 23:55:44 +02008163 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008164 return 0;
8165
Willy Tarreaue333ec92012-04-16 16:26:40 +02008166 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008167
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008168 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008169 msg = &txn->req;
8170 hdr_name = "Cookie";
8171 hdr_name_len = 6;
8172 } else {
8173 msg = &txn->rsp;
8174 hdr_name = "Set-Cookie";
8175 hdr_name_len = 10;
8176 }
8177
Willy Tarreau28376d62012-04-26 21:26:10 +02008178 if (!occ && !(opt & SMP_OPT_ITERATE))
8179 /* no explicit occurrence and single fetch => last cookie by default */
8180 occ = -1;
8181
8182 /* OK so basically here, either we want only one value and it's the
8183 * last one, or we want to iterate over all of them and we fetch the
8184 * next one.
8185 */
8186
Willy Tarreaue333ec92012-04-16 16:26:40 +02008187 sol = msg->buf->p + msg->sol;
Willy Tarreau37406352012-04-23 16:16:37 +02008188 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008189 /* search for the header from the beginning, we must first initialize
8190 * the search parameters.
8191 */
Willy Tarreau37406352012-04-23 16:16:37 +02008192 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008193 ctx->idx = 0;
8194 }
8195
Willy Tarreau28376d62012-04-26 21:26:10 +02008196 smp->flags |= SMP_F_VOL_HDR;
8197
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008198 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008199 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8200 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008201 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8202 goto out;
8203
Willy Tarreau24e32d82012-04-23 23:55:44 +02008204 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008205 continue;
8206
Willy Tarreau37406352012-04-23 16:16:37 +02008207 smp->ctx.a[0] = ctx->line + ctx->val;
8208 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008209 }
8210
Willy Tarreauf853c462012-04-23 18:53:56 +02008211 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008212 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008213 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008214 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008215 &smp->data.str.str,
8216 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008217 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008218 found = 1;
8219 if (occ >= 0) {
8220 /* one value was returned into smp->data.str.{str,len} */
8221 smp->flags |= SMP_F_NOT_LAST;
8222 return 1;
8223 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008224 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008225 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008226 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008227 /* all cookie headers and values were scanned. If we're looking for the
8228 * last occurrence, we may return it now.
8229 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008230 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008231 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008232 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008233}
8234
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008235/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008236 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008237 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008238 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008239 */
8240static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008241acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008242 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008243{
8244 struct http_txn *txn = l7;
8245 struct hdr_idx *idx = &txn->hdr_idx;
8246 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008247 const struct http_msg *msg;
8248 const char *hdr_name;
8249 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008250 int cnt;
8251 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008252 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008253
Willy Tarreau24e32d82012-04-23 23:55:44 +02008254 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008255 return 0;
8256
Willy Tarreaue333ec92012-04-16 16:26:40 +02008257 CHECK_HTTP_MESSAGE_FIRST();
8258
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008259 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008260 msg = &txn->req;
8261 hdr_name = "Cookie";
8262 hdr_name_len = 6;
8263 } else {
8264 msg = &txn->rsp;
8265 hdr_name = "Set-Cookie";
8266 hdr_name_len = 10;
8267 }
8268
8269 sol = msg->buf->p + msg->sol;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008270 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008271 ctx.idx = 0;
8272 cnt = 0;
8273
8274 while (1) {
8275 /* Note: val_beg == NULL every time we need to fetch a new header */
8276 if (!val_beg) {
8277 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8278 break;
8279
Willy Tarreau24e32d82012-04-23 23:55:44 +02008280 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008281 continue;
8282
8283 val_beg = ctx.line + ctx.val;
8284 val_end = val_beg + ctx.vlen;
8285 }
8286
Willy Tarreauf853c462012-04-23 18:53:56 +02008287 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008288 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008289 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008290 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008291 &smp->data.str.str,
8292 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008293 cnt++;
8294 }
8295 }
8296
Willy Tarreauf853c462012-04-23 18:53:56 +02008297 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008298 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008299 return 1;
8300}
8301
Willy Tarreau51539362012-05-08 12:46:28 +02008302/* Fetch an cookie's integer value. The integer value is returned. It
8303 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8304 */
8305static int
8306smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8307 const struct arg *args, struct sample *smp)
8308{
8309 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8310
8311 if (ret > 0) {
8312 smp->type = SMP_T_UINT;
8313 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8314 }
8315
8316 return ret;
8317}
8318
Willy Tarreau8797c062007-05-07 00:55:35 +02008319/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008320/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008321/************************************************************************/
8322
David Cournapeau16023ee2010-12-23 20:55:41 +09008323/*
8324 * Given a path string and its length, find the position of beginning of the
8325 * query string. Returns NULL if no query string is found in the path.
8326 *
8327 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8328 *
8329 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8330 */
8331static inline char *find_query_string(char *path, size_t path_l)
8332{
8333 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008334
David Cournapeau16023ee2010-12-23 20:55:41 +09008335 p = memchr(path, '?', path_l);
8336 return p ? p + 1 : NULL;
8337}
8338
8339static inline int is_param_delimiter(char c)
8340{
8341 return c == '&' || c == ';';
8342}
8343
8344/*
8345 * Given a url parameter, find the starting position of the first occurence,
8346 * or NULL if the parameter is not found.
8347 *
8348 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8349 * the function will return query_string+8.
8350 */
8351static char*
8352find_url_param_pos(char* query_string, size_t query_string_l,
8353 char* url_param_name, size_t url_param_name_l)
8354{
8355 char *pos, *last;
8356
8357 pos = query_string;
8358 last = query_string + query_string_l - url_param_name_l - 1;
8359
8360 while (pos <= last) {
8361 if (pos[url_param_name_l] == '=') {
8362 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8363 return pos;
8364 pos += url_param_name_l + 1;
8365 }
8366 while (pos <= last && !is_param_delimiter(*pos))
8367 pos++;
8368 pos++;
8369 }
8370 return NULL;
8371}
8372
8373/*
8374 * Given a url parameter name, returns its value and size into *value and
8375 * *value_l respectively, and returns non-zero. If the parameter is not found,
8376 * zero is returned and value/value_l are not touched.
8377 */
8378static int
8379find_url_param_value(char* path, size_t path_l,
8380 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008381 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008382{
8383 char *query_string, *qs_end;
8384 char *arg_start;
8385 char *value_start, *value_end;
8386
8387 query_string = find_query_string(path, path_l);
8388 if (!query_string)
8389 return 0;
8390
8391 qs_end = path + path_l;
8392 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8393 url_param_name, url_param_name_l);
8394 if (!arg_start)
8395 return 0;
8396
8397 value_start = arg_start + url_param_name_l + 1;
8398 value_end = value_start;
8399
8400 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8401 value_end++;
8402
8403 *value = value_start;
8404 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008405 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008406}
8407
8408static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008409smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8410 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008411{
8412 struct http_txn *txn = l7;
8413 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008414
8415 if (!args || args->type != ARGT_STR)
8416 return 0;
8417
8418 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008419
Willy Tarreau3a215be2012-03-09 21:39:51 +01008420 if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008421 args->data.str.str, args->data.str.len,
8422 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008423 return 0;
8424
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008425 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008426 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008427 return 1;
8428}
8429
Willy Tarreau185b5c42012-04-26 15:11:51 +02008430/* This function is used to validate the arguments passed to any "hdr" fetch
8431 * keyword. These keywords support an optional positive or negative occurrence
8432 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8433 * is assumed that the types are already the correct ones. Returns 0 on error,
8434 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8435 * error message in case of error, that the caller is responsible for freeing.
8436 * The initial location must either be freeable or NULL.
8437 */
8438static int val_hdr(struct arg *arg, char **err_msg)
8439{
8440 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8441 if (err_msg)
8442 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8443 return 0;
8444 }
8445 return 1;
8446}
8447
Willy Tarreau4a568972010-05-12 08:08:50 +02008448/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008449/* All supported ACL keywords must be declared here. */
8450/************************************************************************/
8451
8452/* Note: must not be declared <const> as its list will be overwritten.
8453 * Please take care of keeping this list alphabetically sorted.
8454 */
8455static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau51539362012-05-08 12:46:28 +02008456 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8457 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008458 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008459 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8460 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8461 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8462 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8463 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8464 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8465 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008466
Willy Tarreau185b5c42012-04-26 15:11:51 +02008467 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8468 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8469 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8470 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8471 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8472 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8473 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8474 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8475 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8476 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8477 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008478
8479 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
8480 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
8481 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8482
8483 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8484
8485 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8486 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8487 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8488 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8489 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8490 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8491 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8492 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8493
8494 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8495 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8496 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8497
Willy Tarreau51539362012-05-08 12:46:28 +02008498 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8499 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008500 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008501 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8502 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8503 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8504 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8505 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8506 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8507 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008508
Willy Tarreau185b5c42012-04-26 15:11:51 +02008509 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8510 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8511 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8512 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8513 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8514 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8515 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8516 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8517 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8518 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8519 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008520
8521 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8522
8523 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8524 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8525 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8526 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8527 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8528 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8529 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8530 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8531 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8532 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8533
8534 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8535 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8536 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8537 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8538 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8539 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8540 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8541 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8542 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8543
8544 { NULL, NULL, NULL, NULL },
8545}};
8546
8547/************************************************************************/
8548/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008549/************************************************************************/
8550/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008551static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008552 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008553 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008554 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8555 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008556 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008557}};
8558
Willy Tarreau8797c062007-05-07 00:55:35 +02008559
8560__attribute__((constructor))
8561static void __http_protocol_init(void)
8562{
8563 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008564 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008565}
8566
8567
Willy Tarreau58f10d72006-12-04 02:26:12 +01008568/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008569 * Local variables:
8570 * c-indent-level: 8
8571 * c-basic-offset: 8
8572 * End:
8573 */