blob: 0c812d9e86c26a3417efdcc949ad970d62f8f171 [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 Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020053#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010055#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020056#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010058#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020060#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010061#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010063#include <proto/stream_interface.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;
David du Colombier7af46052012-05-16 14:16:48 +0200393 size += snprintf(trash + size, trashlen - 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 Tarreau9b28e032012-10-12 23:49:43 +0200397 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398 write(-1, trash, size);
399 size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200400 size += snprintf(trash + size, trashlen - 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 Tarreau9b28e032012-10-12 23:49:43 +0200404 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->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 Tarreau9b28e032012-10-12 23:49:43 +0200424 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200444 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200613 delta = buffer_replace2(msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200633 delta = buffer_replace2(msg->chn->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 Tarreau8263d2b2012-08-28 00:06:31 +0200652 channel_auto_read(si->ob);
653 channel_abort(si->ob);
654 channel_auto_close(si->ob);
655 channel_erase(si->ob);
656 channel_auto_close(si->ib);
657 channel_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
Willy Tarreau783f2582012-09-04 12:19:04 +0200672struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100673{
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 Tarreau9b28e032012-10-12 23:49:43 +0200715 ptr = txn->req.chn->buf->p + 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.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200757 * NOTE: this function is designed to support being called once data are scheduled
758 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100761{
762 struct http_txn *txn;
763 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100765 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200766 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100767
768 /* 1: create the response header */
769 rdr.len = strlen(HTTP_302);
770 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200771 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772 memcpy(rdr.str, HTTP_302, rdr.len);
773
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 srv = target_srv(&s->target);
775
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100780 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100781 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
782 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
783 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786 /* 3: add the request URI. Since it was already forwarded, we need
787 * to temporarily rewind the buffer.
788 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200793 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200794
Willy Tarreau9b28e032012-10-12 23:49:43 +0200795 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200796
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 if (!path)
798 return;
799
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200800 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801 return;
802
803 memcpy(rdr.str + rdr.len, path, len);
804 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100805
806 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
807 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
808 rdr.len += 29;
809 } else {
810 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
811 rdr.len += 23;
812 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200815 si_shutr(si);
816 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 si->err_type = SI_ET_NONE;
818 si->err_loc = NULL;
819 si->state = SI_ST_CLO;
820
821 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823
824 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 if (srv)
826 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827}
828
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 * that the server side is closed. Note that err_type is actually a
831 * bitmask, where almost only aborts may be cumulated with other
832 * values. We consider that aborted operations are more important
833 * than timeouts or errors due to the fact that nobody else in the
834 * logs might explain incomplete retries. All others should avoid
835 * being cumulated. It should normally not be possible to have multiple
836 * aborts at once, but just in case, the first one in sequence is reported.
837 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100838void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841
842 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200844 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200847 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200850 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200853 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100855 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200856 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100858 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200859 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100860 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100861 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200862 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100863}
864
Willy Tarreau42250582007-04-01 01:30:43 +0200865extern const char sess_term_cond[8];
866extern const char sess_fin_state[8];
867extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200868struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200869struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100870struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871
Willy Tarreau117f59e2007-03-04 18:17:17 +0100872/*
873 * Capture headers from message starting at <som> according to header list
874 * <cap_hdr>, and fill the <idx> structure appropriately.
875 */
876void capture_headers(char *som, struct hdr_idx *idx,
877 char **cap, struct cap_hdr *cap_hdr)
878{
879 char *eol, *sol, *col, *sov;
880 int cur_idx;
881 struct cap_hdr *h;
882 int len;
883
884 sol = som + hdr_idx_first_pos(idx);
885 cur_idx = hdr_idx_first_idx(idx);
886
887 while (cur_idx) {
888 eol = sol + idx->v[cur_idx].len;
889
890 col = sol;
891 while (col < eol && *col != ':')
892 col++;
893
894 sov = col + 1;
895 while (sov < eol && http_is_lws[(unsigned char)*sov])
896 sov++;
897
898 for (h = cap_hdr; h; h = h->next) {
899 if ((h->namelen == col - sol) &&
900 (strncasecmp(sol, h->name, h->namelen) == 0)) {
901 if (cap[h->index] == NULL)
902 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200903 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100904
905 if (cap[h->index] == NULL) {
906 Alert("HTTP capture : out of memory.\n");
907 continue;
908 }
909
910 len = eol - sov;
911 if (len > h->len)
912 len = h->len;
913
914 memcpy(cap[h->index], sov, len);
915 cap[h->index][len]=0;
916 }
917 }
918 sol = eol + idx->v[cur_idx].cr + 1;
919 cur_idx = idx->v[cur_idx].next;
920 }
921}
922
923
Willy Tarreau42250582007-04-01 01:30:43 +0200924/* either we find an LF at <ptr> or we jump to <bad>.
925 */
926#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
927
928/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
929 * otherwise to <http_msg_ood> with <state> set to <st>.
930 */
931#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
932 ptr++; \
933 if (likely(ptr < end)) \
934 goto good; \
935 else { \
936 state = (st); \
937 goto http_msg_ood; \
938 } \
939 } while (0)
940
941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100943 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
945 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
946 * will give undefined results.
947 * Note that it is upon the caller's responsibility to ensure that ptr < end,
948 * and that msg->sol points to the beginning of the response.
949 * If a complete line is found (which implies that at least one CR or LF is
950 * found before <end>, the updated <ptr> is returned, otherwise NULL is
951 * returned indicating an incomplete line (which does not mean that parts have
952 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
953 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
954 * upon next call.
955 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200956 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
958 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200959 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200961const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100962 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100963 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100964{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200965 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100966
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100970 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
976 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100977 state = HTTP_MSG_ERROR;
978 break;
979
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200981 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100983 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 goto http_msg_rpcode;
985 }
986 if (likely(HTTP_IS_SPHT(*ptr)))
987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
988 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100989 state = HTTP_MSG_ERROR;
990 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200993 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 if (likely(!HTTP_IS_LWS(*ptr)))
995 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
996
997 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100998 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1000 }
1001
1002 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 http_msg_rsp_reason:
1005 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001006 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 msg->sl.st.r_l = 0;
1008 goto http_msg_rpline_eol;
1009
Willy Tarreau8973c702007-01-21 23:58:29 +01001010 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001011 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 goto http_msg_rpreason;
1015 }
1016 if (likely(HTTP_IS_SPHT(*ptr)))
1017 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1018 /* so it's a CR/LF, so there is no reason phrase */
1019 goto http_msg_rsp_reason;
1020
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001022 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 if (likely(!HTTP_IS_CRLF(*ptr)))
1024 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001025 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 http_msg_rpline_eol:
1027 /* We have seen the end of line. Note that we do not
1028 * necessarily have the \n yet, but at least we know that we
1029 * have EITHER \r OR \n, otherwise the response would not be
1030 * complete. We can then record the response length and return
1031 * to the caller which will be able to register it.
1032 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001033 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001034 return ptr;
1035
1036#ifdef DEBUG_FULL
1037 default:
1038 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1039 exit(1);
1040#endif
1041 }
1042
1043 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001044 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 if (ret_state)
1046 *ret_state = state;
1047 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001048 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001050}
1051
Willy Tarreau8973c702007-01-21 23:58:29 +01001052/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001053 * This function parses a request line between <ptr> and <end>, starting with
1054 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1055 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1056 * will give undefined results.
1057 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1058 * and that msg->sol points to the beginning of the request.
1059 * If a complete line is found (which implies that at least one CR or LF is
1060 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1061 * returned indicating an incomplete line (which does not mean that parts have
1062 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1063 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1064 * upon next call.
1065 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001066 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1068 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001069 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001071const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001072 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001073 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001075 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001079 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 if (likely(HTTP_IS_TOKEN(*ptr)))
1081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 if (likely(HTTP_IS_CRLF(*ptr))) {
1089 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 msg->sl.rq.v_l = 0;
1098 goto http_msg_rqline_eol;
1099 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001100 state = HTTP_MSG_ERROR;
1101 break;
1102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 goto http_msg_rquri;
1108 }
1109 if (likely(HTTP_IS_SPHT(*ptr)))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1111 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1112 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001115 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001116 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001119 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001120 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001125 /* non-ASCII chars are forbidden unless option
1126 * accept-invalid-http-request is enabled in the frontend.
1127 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001129 if (msg->err_pos < -1)
1130 goto invalid_char;
1131 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001132 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1134 }
1135
1136 if (likely(HTTP_IS_CRLF(*ptr))) {
1137 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1138 goto http_msg_req09_uri_e;
1139 }
1140
1141 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001142 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001143 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001144 state = HTTP_MSG_ERROR;
1145 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001148 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001150 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 goto http_msg_rqver;
1152 }
1153 if (likely(HTTP_IS_SPHT(*ptr)))
1154 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1155 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1156 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001159 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001162
1163 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001165 http_msg_rqline_eol:
1166 /* We have seen the end of line. Note that we do not
1167 * necessarily have the \n yet, but at least we know that we
1168 * have EITHER \r OR \n, otherwise the request would not be
1169 * complete. We can then record the request length and return
1170 * to the caller which will be able to register it.
1171 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001172 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001173 return ptr;
1174 }
1175
1176 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001177 state = HTTP_MSG_ERROR;
1178 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180#ifdef DEBUG_FULL
1181 default:
1182 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1183 exit(1);
1184#endif
1185 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001188 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189 if (ret_state)
1190 *ret_state = state;
1191 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001192 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001195
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001196/*
1197 * Returns the data from Authorization header. Function may be called more
1198 * than once so data is stored in txn->auth_data. When no header is found
1199 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1200 * searching again for something we are unable to find anyway.
1201 */
1202
1203char get_http_auth_buff[BUFSIZE];
1204
1205int
1206get_http_auth(struct session *s)
1207{
1208
1209 struct http_txn *txn = &s->txn;
1210 struct chunk auth_method;
1211 struct hdr_ctx ctx;
1212 char *h, *p;
1213 int len;
1214
1215#ifdef DEBUG_AUTH
1216 printf("Auth for session %p: %d\n", s, txn->auth.method);
1217#endif
1218
1219 if (txn->auth.method == HTTP_AUTH_WRONG)
1220 return 0;
1221
1222 if (txn->auth.method)
1223 return 1;
1224
1225 txn->auth.method = HTTP_AUTH_WRONG;
1226
1227 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001228
1229 if (txn->flags & TX_USE_PX_CONN) {
1230 h = "Proxy-Authorization";
1231 len = strlen(h);
1232 } else {
1233 h = "Authorization";
1234 len = strlen(h);
1235 }
1236
Willy Tarreau9b28e032012-10-12 23:49:43 +02001237 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001238 return 0;
1239
1240 h = ctx.line + ctx.val;
1241
1242 p = memchr(h, ' ', ctx.vlen);
1243 if (!p || p == h)
1244 return 0;
1245
1246 chunk_initlen(&auth_method, h, 0, p-h);
1247 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1248
1249 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1250
1251 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1252 get_http_auth_buff, BUFSIZE - 1);
1253
1254 if (len < 0)
1255 return 0;
1256
1257
1258 get_http_auth_buff[len] = '\0';
1259
1260 p = strchr(get_http_auth_buff, ':');
1261
1262 if (!p)
1263 return 0;
1264
1265 txn->auth.user = get_http_auth_buff;
1266 *p = '\0';
1267 txn->auth.pass = p+1;
1268
1269 txn->auth.method = HTTP_AUTH_BASIC;
1270 return 1;
1271 }
1272
1273 return 0;
1274}
1275
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8973c702007-01-21 23:58:29 +01001277/*
1278 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001279 * depending on the initial msg->msg_state. The caller is responsible for
1280 * ensuring that the message does not wrap. The function can be preempted
1281 * everywhere when data are missing and recalled at the exact same location
1282 * with no information loss. The message may even be realigned between two
1283 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001285 * fields. Note that msg->sol will be initialized after completing the first
1286 * state, so that none of the msg pointers has to be initialized prior to the
1287 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001289void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001291 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001293 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001296 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001297 ptr = buf->p + msg->next;
1298 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 if (unlikely(ptr >= end))
1301 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 /*
1305 * First, states that are specific to the response only.
1306 * We check them first so that request and headers are
1307 * closer to each other (accessed more often).
1308 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001310 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001312 /* we have a start of message, but we have to check
1313 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001314 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001315 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001316 if (unlikely(ptr != buf->p)) {
1317 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001318 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001319 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001320 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 }
Willy Tarreau26927362012-05-18 23:22:52 +02001322 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001323 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 hdr_idx_init(idx);
1325 state = HTTP_MSG_RPVER;
1326 goto http_msg_rpver;
1327 }
1328
1329 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1330 goto http_msg_invalid;
1331
1332 if (unlikely(*ptr == '\n'))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1334 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1335 /* stop here */
1336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 EXPECT_LF_HERE(ptr, http_msg_invalid);
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1341 /* stop here */
1342
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001344 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 case HTTP_MSG_RPVER_SP:
1346 case HTTP_MSG_RPCODE:
1347 case HTTP_MSG_RPCODE_SP:
1348 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001349 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001350 state, ptr, end,
1351 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (unlikely(!ptr))
1353 return;
1354
1355 /* we have a full response and we know that we have either a CR
1356 * or an LF at <ptr>.
1357 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1359
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001360 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 if (likely(*ptr == '\r'))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1363 goto http_msg_rpline_end;
1364
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001366 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 /* msg->sol must point to the first of CR or LF. */
1368 EXPECT_LF_HERE(ptr, http_msg_invalid);
1369 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1370 /* stop here */
1371
1372 /*
1373 * Second, states that are specific to the request only
1374 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001378 /* we have a start of message, but we have to check
1379 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001380 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001381 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001382 if (likely(ptr != buf->p)) {
1383 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001384 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001385 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001386 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau26927362012-05-18 23:22:52 +02001388 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001389 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 state = HTTP_MSG_RQMETH;
1391 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1395 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (unlikely(*ptr == '\n'))
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EXPECT_LF_HERE(ptr, http_msg_invalid);
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001406 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001409 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 case HTTP_MSG_RQMETH_SP:
1411 case HTTP_MSG_RQURI:
1412 case HTTP_MSG_RQURI_SP:
1413 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001414 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001415 state, ptr, end,
1416 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (unlikely(!ptr))
1418 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 /* we have a full request and we know that we have either a CR
1421 * or an LF at <ptr>.
1422 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001425 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (likely(*ptr == '\r'))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 /* check for HTTP/0.9 request : no version information available.
1433 * msg->sol must point to the first of CR or LF.
1434 */
1435 if (unlikely(msg->sl.rq.v_l == 0))
1436 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 EXPECT_LF_HERE(ptr, http_msg_invalid);
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 /*
1443 * Common states below
1444 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001447 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(!HTTP_IS_CRLF(*ptr))) {
1449 goto http_msg_hdr_name;
1450 }
1451
1452 if (likely(*ptr == '\r'))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1454 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 /* assumes msg->sol points to the first char */
1459 if (likely(HTTP_IS_TOKEN(*ptr)))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001462 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465 if (likely(msg->err_pos < -1) || *ptr == '\n')
1466 goto http_msg_invalid;
1467
1468 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001469 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001470
1471 /* and we still accept this non-token character */
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001475 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001476 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(HTTP_IS_SPHT(*ptr)))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001481 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (likely(!HTTP_IS_CRLF(*ptr))) {
1484 goto http_msg_hdr_val;
1485 }
1486
1487 if (likely(*ptr == '\r'))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1489 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EXPECT_LF_HERE(ptr, http_msg_invalid);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001497 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (likely(HTTP_IS_SPHT(*ptr))) {
1499 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001500 for (; buf->p + msg->sov < ptr; msg->sov++)
1501 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 goto http_msg_hdr_l1_sp;
1503 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001504 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001505 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 goto http_msg_complete_header;
1507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001509 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001510 /* assumes msg->sol points to the first char, and msg->sov
1511 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 */
1513 if (likely(!HTTP_IS_CRLF(*ptr)))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001516 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 /* Note: we could also copy eol into ->eoh so that we have the
1518 * real header end in case it ends with lots of LWS, but is this
1519 * really needed ?
1520 */
1521 if (likely(*ptr == '\r'))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1523 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001531 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1533 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001534 for (; buf->p + msg->eol < ptr; msg->eol++)
1535 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 goto http_msg_hdr_val;
1537 }
1538 http_msg_complete_header:
1539 /*
1540 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 * Assumes msg->sol points to the first char, msg->sov points
1542 * to the first character of the value and msg->eol to the
1543 * first CR or LF so we know how the line ends. We insert last
1544 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 idx, idx->tail) < 0))
1548 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001550 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (likely(!HTTP_IS_CRLF(*ptr))) {
1552 goto http_msg_hdr_name;
1553 }
1554
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1557 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 /* Assumes msg->sol points to the first of either CR or LF */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001564 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001565 msg->eoh = msg->sol;
1566 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001567 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001569
1570 case HTTP_MSG_ERROR:
1571 /* this may only happen if we call http_msg_analyser() twice with an error */
1572 break;
1573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574#ifdef DEBUG_FULL
1575 default:
1576 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1577 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001578#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 }
1580 http_msg_ood:
1581 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001583 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 http_msg_invalid:
1587 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001589 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 return;
1591}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001592
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1594 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1595 * nothing is done and 1 is returned.
1596 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001597static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598{
1599 int delta;
1600 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001601 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602
1603 if (msg->sl.rq.v_l != 0)
1604 return 1;
1605
Willy Tarreau9b28e032012-10-12 23:49:43 +02001606 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 delta = 0;
1608
1609 if (msg->sl.rq.u_l == 0) {
1610 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001611 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001613 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 }
1615 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001617 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001618 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001619 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001620 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001621 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 NULL, NULL);
1623 if (unlikely(!cur_end))
1624 return 0;
1625
1626 /* we have a full HTTP/1.0 request now and we know that
1627 * we have either a CR or an LF at <ptr>.
1628 */
1629 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1630 return 1;
1631}
1632
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001633/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001634 * and "keep-alive" values. If we already know that some headers may safely
1635 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001636 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1637 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1638 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1639 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1640 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001641 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001643void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001644{
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001646 const char *hdr_val = "Connection";
1647 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001648
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001650 return;
1651
Willy Tarreau88d349d2010-01-25 12:15:43 +01001652 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1653 hdr_val = "Proxy-Connection";
1654 hdr_len = 16;
1655 }
1656
Willy Tarreau5b154472009-12-21 20:11:07 +01001657 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001658 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001659 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1661 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001662 if (to_del & 2)
1663 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001664 else
1665 txn->flags |= TX_CON_KAL_SET;
1666 }
1667 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1668 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001669 if (to_del & 1)
1670 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 else
1672 txn->flags |= TX_CON_CLO_SET;
1673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001674 }
1675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676 txn->flags |= TX_HDR_CONN_PRS;
1677 return;
1678}
Willy Tarreau5b154472009-12-21 20:11:07 +01001679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680/* Apply desired changes on the Connection: header. Values may be removed and/or
1681 * added depending on the <wanted> flags, which are exclusively composed of
1682 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1683 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1684 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001685void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686{
1687 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688 const char *hdr_val = "Connection";
1689 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690
1691 ctx.idx = 0;
1692
Willy Tarreau88d349d2010-01-25 12:15:43 +01001693
1694 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1695 hdr_val = "Proxy-Connection";
1696 hdr_len = 16;
1697 }
1698
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001700 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1702 if (wanted & TX_CON_KAL_SET)
1703 txn->flags |= TX_CON_KAL_SET;
1704 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001705 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001706 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001707 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1708 if (wanted & TX_CON_CLO_SET)
1709 txn->flags |= TX_CON_CLO_SET;
1710 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001711 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001713 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001714
1715 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1716 return;
1717
1718 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1719 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001720 hdr_val = "Connection: close";
1721 hdr_len = 17;
1722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1723 hdr_val = "Proxy-Connection: close";
1724 hdr_len = 23;
1725 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001726 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001727 }
1728
1729 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1730 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001731 hdr_val = "Connection: keep-alive";
1732 hdr_len = 22;
1733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection: keep-alive";
1735 hdr_len = 28;
1736 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001737 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 }
1739 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001740}
1741
Willy Tarreaua458b672012-03-05 11:17:50 +01001742/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001744 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001745 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001747 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001748static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001749{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001750 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001752 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 const char *end = buf->data + buf->size;
1754 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 unsigned int chunk = 0;
1756
1757 /* The chunk size is in the following form, though we are only
1758 * interested in the size and CRLF :
1759 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1760 */
1761 while (1) {
1762 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001763 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 if (c < 0) /* not a hex digit anymore */
1767 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001769 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001770 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001771 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001772 chunk = (chunk << 4) + c;
1773 }
1774
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001776 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001777 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778
1779 while (http_is_spht[(unsigned char)*ptr]) {
1780 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001781 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001782 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 }
1785
Willy Tarreaud98cf932009-12-27 22:54:55 +01001786 /* Up to there, we know that at least one byte is present at *ptr. Check
1787 * for the end of chunk size.
1788 */
1789 while (1) {
1790 if (likely(HTTP_IS_CRLF(*ptr))) {
1791 /* we now have a CR or an LF at ptr */
1792 if (likely(*ptr == '\r')) {
1793 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001794 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001795 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 return 0;
1797 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001798
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001800 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001802 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803 /* done */
1804 break;
1805 }
1806 else if (*ptr == ';') {
1807 /* chunk extension, ends at next CRLF */
1808 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001809 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001810 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001811 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812
1813 while (!HTTP_IS_CRLF(*ptr)) {
1814 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001815 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001816 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 return 0;
1818 }
1819 /* we have a CRLF now, loop above */
1820 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001823 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001824 }
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001830 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001831 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001832 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001833 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001834 msg->chunk_len = chunk;
1835 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001837 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001838 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001839 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001840 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001841}
1842
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001843/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001844 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845 * the trailers is found, it is automatically scheduled to be forwarded,
1846 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1847 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001848 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001849 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001850 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001851 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1852 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001853 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001854 * matches the length of trailers already parsed and not forwarded. It is also
1855 * important to note that this function is designed to be able to parse wrapped
1856 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001858static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001860 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001861
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001864 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 const char *ptr = b_ptr(buf, msg->next);
1866 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001867 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868
1869 /* scan current line and stop at LF or CRLF */
1870 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001871 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872 return 0;
1873
1874 if (*ptr == '\n') {
1875 if (!p1)
1876 p1 = ptr;
1877 p2 = ptr;
1878 break;
1879 }
1880
1881 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001882 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001885 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 p1 = ptr;
1887 }
1888
1889 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001890 if (ptr >= buf->data + buf->size)
1891 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 }
1893
1894 /* after LF; point to beginning of next line */
1895 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001896 if (p2 >= buf->data + buf->size)
1897 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001900 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001901 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001902
1903 /* schedule this line for forwarding */
1904 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001905 if (msg->sov >= buf->size)
1906 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001908 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001909 /* LF/CRLF at beginning of line => end of trailers at p2.
1910 * Everything was scheduled for forwarding, there's nothing
1911 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001912 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 msg->msg_state = HTTP_MSG_DONE;
1915 return 1;
1916 }
1917 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001918 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 }
1920}
1921
1922/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1923 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001924 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001925 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1927 * not enough data are available, the function does not change anything and
1928 * returns zero. If a parse error is encountered, the function returns < 0 and
1929 * does not change anything. Note: this function is designed to parse wrapped
1930 * CRLF at the end of the buffer.
1931 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001932static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001934 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001935 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001936 int bytes;
1937
1938 /* NB: we'll check data availabilty at the end. It's not a
1939 * problem because whatever we match first will be checked
1940 * against the correct length.
1941 */
1942 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944 if (*ptr == '\r') {
1945 bytes++;
1946 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 if (ptr >= buf->data + buf->size)
1948 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 }
1950
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001951 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 return 0;
1953
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001954 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001955 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001957 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958
1959 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001960 if (ptr >= buf->data + buf->size)
1961 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001962 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1963 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001964 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001965 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1966 return 1;
1967}
Willy Tarreau5b154472009-12-21 20:11:07 +01001968
Willy Tarreaud787e662009-07-07 10:14:51 +02001969/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1970 * processing can continue on next analysers, or zero if it either needs more
1971 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1972 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1973 * when it has nothing left to do, and may remove any analyser when it wants to
1974 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001976int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001977{
Willy Tarreau59234e92008-11-30 23:51:27 +01001978 /*
1979 * We will parse the partial (or complete) lines.
1980 * We will check the request syntax, and also join multi-line
1981 * headers. An index of all the lines will be elaborated while
1982 * parsing.
1983 *
1984 * For the parsing, we use a 28 states FSM.
1985 *
1986 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001987 * req->buf->p = beginning of request
1988 * req->buf->p + msg->eoh = end of processed headers / start of current one
1989 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001990 * msg->eol = end of current header or line (LF or CRLF)
1991 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001992 *
1993 * At end of parsing, we may perform a capture of the error (if any), and
1994 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1995 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1996 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001997 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001998
Willy Tarreau59234e92008-11-30 23:51:27 +01001999 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002000 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002001 struct http_txn *txn = &s->txn;
2002 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002003 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002004
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002005 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 +01002006 now_ms, __FUNCTION__,
2007 s,
2008 req,
2009 req->rex, req->wex,
2010 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002011 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002012 req->analysers);
2013
Willy Tarreau52a0c602009-08-16 22:45:38 +02002014 /* we're speaking HTTP here, so let's speak HTTP to the client */
2015 s->srv_error = http_return_srv_error;
2016
Willy Tarreau83e3af02009-12-28 17:39:57 +01002017 /* There's a protected area at the end of the buffer for rewriting
2018 * purposes. We don't want to start to parse the request if the
2019 * protected area is affected, because we may have to move processed
2020 * data later, which is much more complicated.
2021 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002022 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002023 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002024 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002025 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2026 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2027 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002028 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002029 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002030 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002031 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002032 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002033 return 0;
2034 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002035 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2036 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2037 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002038 }
2039
Willy Tarreau065e8332010-01-08 00:30:20 +01002040 /* Note that we have the same problem with the response ; we
2041 * may want to send a redirect, error or anything which requires
2042 * some spare space. So we'll ensure that we have at least
2043 * maxrewrite bytes available in the response buffer before
2044 * processing that one. This will only affect pipelined
2045 * keep-alive requests.
2046 */
2047 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002048 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002049 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2050 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2051 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002052 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002053 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002054 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002055 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002056 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002057 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002058 return 0;
2059 }
2060 }
2061
Willy Tarreau9b28e032012-10-12 23:49:43 +02002062 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002063 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002064 }
2065
Willy Tarreau59234e92008-11-30 23:51:27 +01002066 /* 1: we might have to print this header in debug mode */
2067 if (unlikely((global.mode & MODE_DEBUG) &&
2068 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002069 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002070 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002071
Willy Tarreau9b28e032012-10-12 23:49:43 +02002072 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002073 /* this is a bit complex : in case of error on the request line,
2074 * we know that rq.l is still zero, so we display only the part
2075 * up to the end of the line (truncated by debug_hdr).
2076 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002077 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002078 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002079
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 sol += hdr_idx_first_pos(&txn->hdr_idx);
2081 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002082
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 while (cur_idx) {
2084 eol = sol + txn->hdr_idx.v[cur_idx].len;
2085 debug_hdr("clihdr", s, sol, eol);
2086 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2087 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002088 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002089 }
2090
Willy Tarreau58f10d72006-12-04 02:26:12 +01002091
Willy Tarreau59234e92008-11-30 23:51:27 +01002092 /*
2093 * Now we quickly check if we have found a full valid request.
2094 * If not so, we check the FD and buffer states before leaving.
2095 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002096 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002097 * requests are checked first. When waiting for a second request
2098 * on a keep-alive session, if we encounter and error, close, t/o,
2099 * we note the error in the session flags but don't set any state.
2100 * Since the error will be noted there, it will not be counted by
2101 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002102 * Last, we may increase some tracked counters' http request errors on
2103 * the cases that are deliberately the client's fault. For instance,
2104 * a timeout or connection reset is not counted as an error. However
2105 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002106 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002107
Willy Tarreau655dce92009-11-08 13:10:58 +01002108 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002112 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002113 session_inc_http_req_ctr(s);
2114 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002115 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 /* 1: Since we are in header mode, if there's no space
2120 * left for headers, we won't be able to free more
2121 * later, so the session will never terminate. We
2122 * must terminate it now.
2123 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002124 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 /* FIXME: check if URI is set and return Status
2126 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002127 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002128 session_inc_http_req_ctr(s);
2129 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002130 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002131 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002132 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 goto return_bad_req;
2134 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002135
Willy Tarreau59234e92008-11-30 23:51:27 +01002136 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002137 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002138 if (!(s->flags & SN_ERR_MASK))
2139 s->flags |= SN_ERR_CLICL;
2140
Willy Tarreaufcffa692010-01-10 14:21:19 +01002141 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002142 goto failed_keep_alive;
2143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002145 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002146 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002147 session_inc_http_err_ctr(s);
2148 }
2149
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 msg->msg_state = HTTP_MSG_ERROR;
2151 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002152
Willy Tarreauda7ff642010-06-23 11:44:09 +02002153 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002154 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002155 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002156 if (s->listener->counters)
2157 s->listener->counters->failed_req++;
2158
Willy Tarreau59234e92008-11-30 23:51:27 +01002159 if (!(s->flags & SN_FINST_MASK))
2160 s->flags |= SN_FINST_R;
2161 return 0;
2162 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002163
Willy Tarreau59234e92008-11-30 23:51:27 +01002164 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002166 if (!(s->flags & SN_ERR_MASK))
2167 s->flags |= SN_ERR_CLITO;
2168
Willy Tarreaufcffa692010-01-10 14:21:19 +01002169 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002170 goto failed_keep_alive;
2171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002173 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002174 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002175 session_inc_http_err_ctr(s);
2176 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002178 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002179 msg->msg_state = HTTP_MSG_ERROR;
2180 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002181
Willy Tarreauda7ff642010-06-23 11:44:09 +02002182 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002183 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002184 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002185 if (s->listener->counters)
2186 s->listener->counters->failed_req++;
2187
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 if (!(s->flags & SN_FINST_MASK))
2189 s->flags |= SN_FINST_R;
2190 return 0;
2191 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002192
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002194 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002195 if (!(s->flags & SN_ERR_MASK))
2196 s->flags |= SN_ERR_CLICL;
2197
Willy Tarreaufcffa692010-01-10 14:21:19 +01002198 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002199 goto failed_keep_alive;
2200
Willy Tarreau4076a152009-04-02 15:18:36 +02002201 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002202 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002204 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002205 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_err_ctr(s);
2209 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002210 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002211 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002212 if (s->listener->counters)
2213 s->listener->counters->failed_req++;
2214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 if (!(s->flags & SN_FINST_MASK))
2216 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002217 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002218 }
2219
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002220 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002221 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2222 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002223#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002224 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002225 /* We need more data, we have to re-enable quick-ack in case we
2226 * previously disabled it, otherwise we might cause the client
2227 * to delay next data.
2228 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002229 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002230 }
2231#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002232
Willy Tarreaufcffa692010-01-10 14:21:19 +01002233 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2234 /* If the client starts to talk, let's fall back to
2235 * request timeout processing.
2236 */
2237 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002238 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002239 }
2240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002242 if (!tick_isset(req->analyse_exp)) {
2243 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2244 (txn->flags & TX_WAIT_NEXT_RQ) &&
2245 tick_isset(s->be->timeout.httpka))
2246 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2247 else
2248 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002250
Willy Tarreau59234e92008-11-30 23:51:27 +01002251 /* we're not ready yet */
2252 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002253
2254 failed_keep_alive:
2255 /* Here we process low-level errors for keep-alive requests. In
2256 * short, if the request is not the first one and it experiences
2257 * a timeout, read error or shutdown, we just silently close so
2258 * that the client can try again.
2259 */
2260 txn->status = 0;
2261 msg->msg_state = HTTP_MSG_RQBEFORE;
2262 req->analysers = 0;
2263 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002264 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002265 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002266 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002268
Willy Tarreaud787e662009-07-07 10:14:51 +02002269 /* OK now we have a complete HTTP request with indexed headers. Let's
2270 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002271 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002272 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002273 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002274 * byte after the last LF. msg->sov points to the first byte of data.
2275 * msg->eol cannot be trusted because it may have been left uninitialized
2276 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002277 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002278
Willy Tarreauda7ff642010-06-23 11:44:09 +02002279 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002280 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2281
Willy Tarreaub16a5742010-01-10 14:46:16 +01002282 if (txn->flags & TX_WAIT_NEXT_RQ) {
2283 /* kill the pending keep-alive timeout */
2284 txn->flags &= ~TX_WAIT_NEXT_RQ;
2285 req->analyse_exp = TICK_ETERNITY;
2286 }
2287
2288
Willy Tarreaud787e662009-07-07 10:14:51 +02002289 /* Maybe we found in invalid header name while we were configured not
2290 * to block on that, so we have to capture it now.
2291 */
2292 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002293 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002294
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 /*
2296 * 1: identify the method
2297 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002298 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002299
2300 /* we can make use of server redirect on GET and HEAD */
2301 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2302 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 /*
2305 * 2: check if the URI matches the monitor_uri.
2306 * We have to do this for every request which gets in, because
2307 * the monitor-uri is defined by the frontend.
2308 */
2309 if (unlikely((s->fe->monitor_uri_len != 0) &&
2310 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002311 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 s->fe->monitor_uri,
2313 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002316 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002320 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002321
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002323 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002324 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 ret = acl_pass(ret);
2327 if (cond->pol == ACL_COND_UNLESS)
2328 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002329
Willy Tarreau59234e92008-11-30 23:51:27 +01002330 if (ret) {
2331 /* we fail this request, let's return 503 service unavail */
2332 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002333 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002335 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002336 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002337
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 /* nothing to fail, let's reply normaly */
2339 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002340 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 goto return_prx_cond;
2342 }
2343
2344 /*
2345 * 3: Maybe we have to copy the original REQURI for the logs ?
2346 * Note: we cannot log anymore if the request has been
2347 * classified as invalid.
2348 */
2349 if (unlikely(s->logs.logwait & LW_REQ)) {
2350 /* we have a complete HTTP request that we must log */
2351 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2352 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002353
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 if (urilen >= REQURI_LEN)
2355 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002356 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 if (!(s->logs.logwait &= ~LW_REQ))
2360 s->do_log(s);
2361 } else {
2362 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002363 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 }
Willy Tarreau06619262006-12-17 08:37:22 +01002365
William Lallemanda73203e2012-03-12 12:48:57 +01002366 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2367 s->unique_id = pool_alloc2(pool2_uniqueid);
2368 }
2369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002371 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002372 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002373
Willy Tarreau5b154472009-12-21 20:11:07 +01002374 /* ... and check if the request is HTTP/1.1 or above */
2375 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002376 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2377 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2378 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002379 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002380
2381 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002382 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002383
Willy Tarreau88d349d2010-01-25 12:15:43 +01002384 /* if the frontend has "option http-use-proxy-header", we'll check if
2385 * we have what looks like a proxied connection instead of a connection,
2386 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2387 * Note that this is *not* RFC-compliant, however browsers and proxies
2388 * happen to do that despite being non-standard :-(
2389 * We consider that a request not beginning with either '/' or '*' is
2390 * a proxied connection, which covers both "scheme://location" and
2391 * CONNECT ip:port.
2392 */
2393 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002394 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002395 txn->flags |= TX_USE_PX_CONN;
2396
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002397 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002398 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002399
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002401 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002402 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002403 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002404
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002405 /* 6: determine the transfer-length.
2406 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2407 * the presence of a message-body in a REQUEST and its transfer length
2408 * must be determined that way (in order of precedence) :
2409 * 1. The presence of a message-body in a request is signaled by the
2410 * inclusion of a Content-Length or Transfer-Encoding header field
2411 * in the request's header fields. When a request message contains
2412 * both a message-body of non-zero length and a method that does
2413 * not define any semantics for that request message-body, then an
2414 * origin server SHOULD either ignore the message-body or respond
2415 * with an appropriate error message (e.g., 413). A proxy or
2416 * gateway, when presented the same request, SHOULD either forward
2417 * the request inbound with the message- body or ignore the
2418 * message-body when determining a response.
2419 *
2420 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2421 * and the "chunked" transfer-coding (Section 6.2) is used, the
2422 * transfer-length is defined by the use of this transfer-coding.
2423 * If a Transfer-Encoding header field is present and the "chunked"
2424 * transfer-coding is not present, the transfer-length is defined
2425 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002426 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002427 * 3. If a Content-Length header field is present, its decimal value in
2428 * OCTETs represents both the entity-length and the transfer-length.
2429 * If a message is received with both a Transfer-Encoding header
2430 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002431 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002432 * 4. By the server closing the connection. (Closing the connection
2433 * cannot be used to indicate the end of a request body, since that
2434 * would leave no possibility for the server to send back a response.)
2435 *
2436 * Whenever a transfer-coding is applied to a message-body, the set of
2437 * transfer-codings MUST include "chunked", unless the message indicates
2438 * it is terminated by closing the connection. When the "chunked"
2439 * transfer-coding is used, it MUST be the last transfer-coding applied
2440 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002441 */
2442
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002443 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002444 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002446 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002447 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002449 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2450 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002451 /* bad transfer-encoding (chunked followed by something else) */
2452 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002453 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002454 break;
2455 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002456 }
2457
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002459 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002460 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002461 signed long long cl;
2462
Willy Tarreauad14f752011-09-02 20:33:27 +02002463 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002464 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002465 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002466 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467
Willy Tarreauad14f752011-09-02 20:33:27 +02002468 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002469 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002470 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002471 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472
Willy Tarreauad14f752011-09-02 20:33:27 +02002473 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002474 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002475 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002476 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002477
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002478 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002479 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002480 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002481 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002483 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002484 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002485 }
2486
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002487 /* bodyless requests have a known length */
2488 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002489 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002490
Willy Tarreaud787e662009-07-07 10:14:51 +02002491 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002492 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002493 req->analyse_exp = TICK_ETERNITY;
2494 return 1;
2495
2496 return_bad_req:
2497 /* We centralize bad requests processing here */
2498 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2499 /* we detected a parsing error. We want to archive this request
2500 * in the dedicated proxy area for later troubleshooting.
2501 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002502 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002503 }
2504
2505 txn->req.msg_state = HTTP_MSG_ERROR;
2506 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002507 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002508
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002509 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002510 if (s->listener->counters)
2511 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002512
2513 return_prx_cond:
2514 if (!(s->flags & SN_ERR_MASK))
2515 s->flags |= SN_ERR_PRXCOND;
2516 if (!(s->flags & SN_FINST_MASK))
2517 s->flags |= SN_FINST_R;
2518
2519 req->analysers = 0;
2520 req->analyse_exp = TICK_ETERNITY;
2521 return 0;
2522}
2523
Cyril Bonté70be45d2010-10-12 00:14:35 +02002524/* We reached the stats page through a POST request.
2525 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002526 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002527 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002528int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002529{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002530 struct proxy *px = NULL;
2531 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002532
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002533 char key[LINESIZE];
2534 int action = ST_ADM_ACTION_NONE;
2535 int reprocess = 0;
2536
2537 int total_servers = 0;
2538 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002539
2540 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002541 char *st_cur_param = NULL;
2542 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543
Willy Tarreau9b28e032012-10-12 23:49:43 +02002544 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002545 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002546
2547 cur_param = next_param = end_params;
2548
Willy Tarreau9b28e032012-10-12 23:49:43 +02002549 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002551 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002552 return 1;
2553 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002554 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002555 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002556 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002557 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002558 }
2559
2560 *end_params = '\0';
2561
Willy Tarreau295a8372011-03-10 11:25:07 +01002562 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002563
2564 /*
2565 * Parse the parameters in reverse order to only store the last value.
2566 * From the html form, the backend and the action are at the end.
2567 */
2568 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002569 char *value;
2570 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002571
2572 cur_param--;
2573 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002574 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002576 poffset = (cur_param != first_param ? 1 : 0);
2577 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2578 if ((plen > 0) && (plen <= sizeof(key))) {
2579 strncpy(key, cur_param + poffset, plen);
2580 key[plen - 1] = '\0';
2581 } else {
2582 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2583 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584 }
2585
2586 /* Parse the value */
2587 value = key;
2588 while (*value != '\0' && *value != '=') {
2589 value++;
2590 }
2591 if (*value == '=') {
2592 /* Ok, a value is found, we can mark the end of the key */
2593 *value++ = '\0';
2594 }
2595
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002596 if (!url_decode(key) || !url_decode(value))
2597 break;
2598
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002600 if (!px && (strcmp(key, "b") == 0)) {
2601 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2602 /* the backend name is unknown or ambiguous (duplicate names) */
2603 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2604 goto out;
2605 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002606 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002607 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002609 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002610 }
2611 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002612 action = ST_ADM_ACTION_ENABLE;
2613 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002614 else if (strcmp(value, "stop") == 0) {
2615 action = ST_ADM_ACTION_STOP;
2616 }
2617 else if (strcmp(value, "start") == 0) {
2618 action = ST_ADM_ACTION_START;
2619 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002620 else if (strcmp(value, "shutdown") == 0) {
2621 action = ST_ADM_ACTION_SHUTDOWN;
2622 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002623 else {
2624 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2625 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002626 }
2627 }
2628 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002629 if (!(px && action)) {
2630 /*
2631 * Indicates that we'll need to reprocess the parameters
2632 * as soon as backend and action are known
2633 */
2634 if (!reprocess) {
2635 st_cur_param = cur_param;
2636 st_next_param = next_param;
2637 }
2638 reprocess = 1;
2639 }
2640 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002642 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002643 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002644 /* Not already in maintenance, we can change the server state */
2645 sv->state |= SRV_MAINTAIN;
2646 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002647 altered_servers++;
2648 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649 }
2650 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002651 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002652 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002653 /* Already in maintenance, we can change the server state */
2654 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002655 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002656 altered_servers++;
2657 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002658 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002659 break;
2660 case ST_ADM_ACTION_STOP:
2661 case ST_ADM_ACTION_START:
2662 if (action == ST_ADM_ACTION_START)
2663 sv->uweight = sv->iweight;
2664 else
2665 sv->uweight = 0;
2666
2667 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2668 /* we must take care of not pushing the server to full throttle during slow starts */
2669 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2670 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2671 else
2672 sv->eweight = BE_WEIGHT_SCALE;
2673 sv->eweight *= sv->uweight;
2674 } else {
2675 sv->eweight = sv->uweight;
2676 }
2677
2678 /* static LB algorithms are a bit harder to update */
2679 if (px->lbprm.update_server_eweight)
2680 px->lbprm.update_server_eweight(sv);
2681 else if (sv->eweight) {
2682 if (px->lbprm.set_server_status_up)
2683 px->lbprm.set_server_status_up(sv);
2684 }
2685 else {
2686 if (px->lbprm.set_server_status_down)
2687 px->lbprm.set_server_status_down(sv);
2688 }
2689 altered_servers++;
2690 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002691 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002692 case ST_ADM_ACTION_SHUTDOWN:
2693 if (px->state != PR_STSTOPPED) {
2694 struct session *sess, *sess_bck;
2695
2696 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2697 if (sess->srv_conn == sv)
2698 session_shutdown(sess, SN_ERR_KILLED);
2699
2700 altered_servers++;
2701 total_servers++;
2702 }
2703 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002704 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002705 } else {
2706 /* the server name is unknown or ambiguous (duplicate names) */
2707 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002708 }
2709 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002710 if (reprocess && px && action) {
2711 /* Now, we know the backend and the action chosen by the user.
2712 * We can safely restart from the first server parameter
2713 * to reprocess them
2714 */
2715 cur_param = st_cur_param;
2716 next_param = st_next_param;
2717 reprocess = 0;
2718 goto reprocess_servers;
2719 }
2720
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721 next_param = cur_param;
2722 }
2723 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002724
2725 if (total_servers == 0) {
2726 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2727 }
2728 else if (altered_servers == 0) {
2729 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2730 }
2731 else if (altered_servers == total_servers) {
2732 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2733 }
2734 else {
2735 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2736 }
2737 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002738 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739}
2740
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002741/* returns a pointer to the first rule which forbids access (deny or http_auth),
2742 * or NULL if everything's OK.
2743 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002744static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002745http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2746{
Willy Tarreauff011f22011-01-06 17:51:27 +01002747 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002748
Willy Tarreauff011f22011-01-06 17:51:27 +01002749 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002750 int ret = 1;
2751
Willy Tarreauff011f22011-01-06 17:51:27 +01002752 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002753 continue;
2754
2755 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002756 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002757 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002758 ret = acl_pass(ret);
2759
Willy Tarreauff011f22011-01-06 17:51:27 +01002760 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002761 ret = !ret;
2762 }
2763
2764 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002765 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002766 return NULL; /* no problem */
2767 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002768 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002769 }
2770 }
2771 return NULL;
2772}
2773
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002774/* This stream analyser runs all HTTP request processing which is common to
2775 * frontends and backends, which means blocking ACLs, filters, connection-close,
2776 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002778 * either needs more data or wants to immediately abort the request (eg: deny,
2779 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002781int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002782{
Willy Tarreaud787e662009-07-07 10:14:51 +02002783 struct http_txn *txn = &s->txn;
2784 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002785 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002786 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002788 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002789 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002790
Willy Tarreau655dce92009-11-08 13:10:58 +01002791 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002792 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002793 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002794 return 0;
2795 }
2796
Willy Tarreau3a816292009-07-07 10:55:49 +02002797 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002798 req->analyse_exp = TICK_ETERNITY;
2799
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002800 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 +02002801 now_ms, __FUNCTION__,
2802 s,
2803 req,
2804 req->rex, req->wex,
2805 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002806 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002807 req->analysers);
2808
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002809 /* first check whether we have some ACLs set to block this request */
2810 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002811 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 ret = acl_pass(ret);
2814 if (cond->pol == ACL_COND_UNLESS)
2815 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002816
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002817 if (ret) {
2818 txn->status = 403;
2819 /* let's log the request time */
2820 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002821 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002822 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002823 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002824 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002825 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002826
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002827 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002828 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002829
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002830 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002831 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002832 do_stats = stats_check_uri(s->rep->prod, txn, px);
2833 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002834 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 +01002835 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002836 else
2837 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002838
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002839 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002840 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002841 txn->status = 403;
2842 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002843 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002844 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002845 s->fe->fe_counters.denied_req++;
2846 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2847 s->be->be_counters.denied_req++;
2848 if (s->listener->counters)
2849 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002850 goto return_prx_cond;
2851 }
2852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 /* try headers filters */
2854 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002855 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002856 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002857
Willy Tarreau59234e92008-11-30 23:51:27 +01002858 /* has the request been denied ? */
2859 if (txn->flags & TX_CLDENY) {
2860 /* no need to go further */
2861 txn->status = 403;
2862 /* let's log the request time */
2863 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002864 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002865 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 goto return_prx_cond;
2867 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002868
2869 /* When a connection is tarpitted, we use the tarpit timeout,
2870 * which may be the same as the connect timeout if unspecified.
2871 * If unset, then set it to zero because we really want it to
2872 * eventually expire. We build the tarpit as an analyser.
2873 */
2874 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002875 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002876 /* wipe the request out so that we can drop the connection early
2877 * if the client closes first.
2878 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002879 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002880 req->analysers = 0; /* remove switching rules etc... */
2881 req->analysers |= AN_REQ_HTTP_TARPIT;
2882 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2883 if (!req->analyse_exp)
2884 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002885 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002886 return 1;
2887 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002888 }
Willy Tarreau06619262006-12-17 08:37:22 +01002889
Willy Tarreau5b154472009-12-21 20:11:07 +01002890 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2891 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002892 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2893 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2895 * avoid to redo the same work if FE and BE have the same settings (common).
2896 * The method consists in checking if options changed between the two calls
2897 * (implying that either one is non-null, or one of them is non-null and we
2898 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002899 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002900
Willy Tarreaudc008c52010-02-01 16:20:08 +01002901 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2902 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2903 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2904 (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 +01002905 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002907 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2908 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002909 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002910 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2911 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002912 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002913 tmp = TX_CON_WANT_CLO;
2914
Willy Tarreau5b154472009-12-21 20:11:07 +01002915 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2916 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002917
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002918 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2919 /* parse the Connection header and possibly clean it */
2920 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002921 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002922 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2923 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002924 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002925 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002926 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002927 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002928 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002929
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002930 /* check if client or config asks for explicit close in KAL/SCL */
2931 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2932 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2933 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002934 (!(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 +01002935 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002936 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002937 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002938 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2939 }
Willy Tarreau78599912009-10-17 20:12:21 +02002940
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002941 /* we can be blocked here because the request needs to be authenticated,
2942 * either to pass or to access stats.
2943 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002944 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002945 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002946 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002947
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002948 if (!realm)
2949 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2950
Willy Tarreau844a7e72010-01-31 21:46:18 +01002951 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002952 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002953 txn->status = 401;
2954 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002955 /* on 401 we still count one error, because normal browsing
2956 * won't significantly increase the counter but brute force
2957 * attempts will.
2958 */
2959 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002960 goto return_prx_cond;
2961 }
2962
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002963 /* add request headers from the rule sets in the same order */
2964 list_for_each_entry(wl, &px->req_add, list) {
2965 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002966 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002967 ret = acl_pass(ret);
2968 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2969 ret = !ret;
2970 if (!ret)
2971 continue;
2972 }
2973
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002974 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002975 goto return_bad_req;
2976 }
2977
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002978 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002979 struct stats_admin_rule *stats_admin_rule;
2980
2981 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002982 * FIXME!!! that one is rather dangerous, we want to
2983 * make it follow standard rules (eg: clear req->analysers).
2984 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002985
Cyril Bonté474be412010-10-12 00:14:36 +02002986 /* now check whether we have some admin rules for this request */
2987 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2988 int ret = 1;
2989
2990 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002991 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 +02002992 ret = acl_pass(ret);
2993 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2994 ret = !ret;
2995 }
2996
2997 if (ret) {
2998 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002999 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003000 break;
3001 }
3002 }
3003
Cyril Bonté70be45d2010-10-12 00:14:35 +02003004 /* Was the status page requested with a POST ? */
3005 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003006 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003007 if (msg->msg_state < HTTP_MSG_100_SENT) {
3008 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3009 * send an HTTP/1.1 100 Continue intermediate response.
3010 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003011 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003012 struct hdr_ctx ctx;
3013 ctx.idx = 0;
3014 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003015 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003016 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003017 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003018 }
3019 }
3020 msg->msg_state = HTTP_MSG_100_SENT;
3021 s->logs.tv_request = now; /* update the request timer to reflect full request */
3022 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003023 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003024 /* we need more data */
3025 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003026 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003027 return 0;
3028 }
Cyril Bonté474be412010-10-12 00:14:36 +02003029 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003030 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003031 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003032 }
3033
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003034 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003035 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003036 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau3cefd522012-08-30 15:49:18 +02003037 copy_target(&s->target, &s->rep->prod->conn.target); // for logging only
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003038 s->rep->prod->conn.xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003039 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003040 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003041 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3042 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003043
3044 return 0;
3045
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003047
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003048 /* check whether we have some ACLs set to redirect this request */
3049 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003050 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003051
Willy Tarreauf285f542010-01-03 20:03:03 +01003052 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003053 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003054 ret = acl_pass(ret);
3055 if (rule->cond->pol == ACL_COND_UNLESS)
3056 ret = !ret;
3057 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003060 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003062
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 /* build redirect message */
3064 switch(rule->code) {
3065 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 msg_fmt = HTTP_303;
3067 break;
3068 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 msg_fmt = HTTP_301;
3070 break;
3071 case 302:
3072 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 msg_fmt = HTTP_302;
3074 break;
3075 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003077 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003081 case REDIRECT_TYPE_SCHEME: {
3082 const char *path;
3083 const char *host;
3084 struct hdr_ctx ctx;
3085 int pathlen;
3086 int hostlen;
3087
3088 host = "";
3089 hostlen = 0;
3090 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003091 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003092 host = ctx.line + ctx.val;
3093 hostlen = ctx.vlen;
3094 }
3095
3096 path = http_get_path(txn);
3097 /* build message using path */
3098 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003099 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003100 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3101 int qs = 0;
3102 while (qs < pathlen) {
3103 if (path[qs] == '?') {
3104 pathlen = qs;
3105 break;
3106 }
3107 qs++;
3108 }
3109 }
3110 } else {
3111 path = "/";
3112 pathlen = 1;
3113 }
3114
3115 /* check if we can add scheme + "://" + host + path */
3116 if (rdr.len + rule->rdr_len + 3 + hostlen + pathlen > rdr.size - 4)
3117 goto return_bad_req;
3118
3119 /* add scheme */
3120 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3121 rdr.len += rule->rdr_len;
3122
3123 /* add "://" */
3124 memcpy(rdr.str + rdr.len, "://", 3);
3125 rdr.len += 3;
3126
3127 /* add host */
3128 memcpy(rdr.str + rdr.len, host, hostlen);
3129 rdr.len += hostlen;
3130
3131 /* add path */
3132 memcpy(rdr.str + rdr.len, path, pathlen);
3133 rdr.len += pathlen;
3134
3135 /* append a slash at the end of the location is needed and missing */
3136 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3137 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3138 if (rdr.len > rdr.size - 5)
3139 goto return_bad_req;
3140 rdr.str[rdr.len] = '/';
3141 rdr.len++;
3142 }
3143
3144 break;
3145 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003146 case REDIRECT_TYPE_PREFIX: {
3147 const char *path;
3148 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003149
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003150 path = http_get_path(txn);
3151 /* build message using path */
3152 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003153 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3155 int qs = 0;
3156 while (qs < pathlen) {
3157 if (path[qs] == '?') {
3158 pathlen = qs;
3159 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003160 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003161 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003162 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003163 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003164 } else {
3165 path = "/";
3166 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003167 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003168
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003169 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003170 goto return_bad_req;
3171
3172 /* add prefix. Note that if prefix == "/", we don't want to
3173 * add anything, otherwise it makes it hard for the user to
3174 * configure a self-redirection.
3175 */
3176 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003177 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3178 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003179 }
3180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003181 /* add path */
3182 memcpy(rdr.str + rdr.len, path, pathlen);
3183 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003184
3185 /* append a slash at the end of the location is needed and missing */
3186 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3187 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3188 if (rdr.len > rdr.size - 5)
3189 goto return_bad_req;
3190 rdr.str[rdr.len] = '/';
3191 rdr.len++;
3192 }
3193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003194 break;
3195 }
3196 case REDIRECT_TYPE_LOCATION:
3197 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003198 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003199 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003200
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003201 /* add location */
3202 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3203 rdr.len += rule->rdr_len;
3204 break;
3205 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003207 if (rule->cookie_len) {
3208 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3209 rdr.len += 14;
3210 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3211 rdr.len += rule->cookie_len;
3212 memcpy(rdr.str + rdr.len, "\r\n", 2);
3213 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003214 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003215
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003216 /* add end of headers and the keep-alive/close status.
3217 * We may choose to set keep-alive if the Location begins
3218 * with a slash, because the client will come back to the
3219 * same server.
3220 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003221 txn->status = rule->code;
3222 /* let's log the request time */
3223 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003224
3225 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003226 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3227 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003228 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3229 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3230 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003231 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003232 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3233 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3234 rdr.len += 30;
3235 } else {
3236 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3237 rdr.len += 24;
3238 }
Willy Tarreau75661452010-01-10 10:35:01 +01003239 }
3240 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3241 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003242 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003243 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003244 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003245 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003246 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003247 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3248 txn->req.msg_state = HTTP_MSG_CLOSED;
3249 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003250 break;
3251 } else {
3252 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003253 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3254 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3255 rdr.len += 29;
3256 } else {
3257 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3258 rdr.len += 23;
3259 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003260 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003261 goto return_prx_cond;
3262 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003263 }
3264 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003265
Willy Tarreau2be39392010-01-03 17:24:51 +01003266 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3267 * If this happens, then the data will not come immediately, so we must
3268 * send all what we have without waiting. Note that due to the small gain
3269 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003270 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003271 * itself once used.
3272 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003273 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275 /* that's OK for us now, let's move on to next analysers */
3276 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003277
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003278 return_bad_req:
3279 /* We centralize bad requests processing here */
3280 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3281 /* we detected a parsing error. We want to archive this request
3282 * in the dedicated proxy area for later troubleshooting.
3283 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003284 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003285 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003286
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003287 txn->req.msg_state = HTTP_MSG_ERROR;
3288 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003289 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003290
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003291 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003292 if (s->listener->counters)
3293 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003294
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295 return_prx_cond:
3296 if (!(s->flags & SN_ERR_MASK))
3297 s->flags |= SN_ERR_PRXCOND;
3298 if (!(s->flags & SN_FINST_MASK))
3299 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003300
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 req->analysers = 0;
3302 req->analyse_exp = TICK_ETERNITY;
3303 return 0;
3304}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003305
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003306/* This function performs all the processing enabled for the current request.
3307 * It returns 1 if the processing can continue on next analysers, or zero if it
3308 * needs more data, encounters an error, or wants to immediately abort the
3309 * request. It relies on buffers flags, and updates s->req->analysers.
3310 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003311int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003312{
3313 struct http_txn *txn = &s->txn;
3314 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003315
Willy Tarreau655dce92009-11-08 13:10:58 +01003316 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003317 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003318 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003319 return 0;
3320 }
3321
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003322 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 +02003323 now_ms, __FUNCTION__,
3324 s,
3325 req,
3326 req->rex, req->wex,
3327 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003328 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003329 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003330
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 /*
3332 * Right now, we know that we have processed the entire headers
3333 * and that unwanted requests have been filtered out. We can do
3334 * whatever we want with the remaining request. Also, now we
3335 * may have separate values for ->fe, ->be.
3336 */
Willy Tarreau06619262006-12-17 08:37:22 +01003337
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 /*
3339 * If HTTP PROXY is set we simply get remote server address
3340 * parsing incoming request.
3341 */
3342 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003343 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn.addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003345
Willy Tarreau59234e92008-11-30 23:51:27 +01003346 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003347 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003348 * Note that doing so might move headers in the request, but
3349 * the fields will stay coherent and the URI will not move.
3350 * This should only be performed in the backend.
3351 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003352 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003353 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3354 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003355
Willy Tarreau59234e92008-11-30 23:51:27 +01003356 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003357 * 8: the appsession cookie was looked up very early in 1.2,
3358 * so let's do the same now.
3359 */
3360
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003361 /* It needs to look into the URI unless persistence must be ignored */
3362 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003363 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003364 }
3365
William Lallemanda73203e2012-03-12 12:48:57 +01003366 /* add unique-id if "header-unique-id" is specified */
3367
3368 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3369 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3370
3371 if (s->fe->header_unique_id && s->unique_id) {
3372 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3373 if (ret < 0 || ret > global.tune.bufsize)
3374 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003375 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003376 goto return_bad_req;
3377 }
3378
Cyril Bontéb21570a2009-11-29 20:04:48 +01003379 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003380 * 9: add X-Forwarded-For if either the frontend or the backend
3381 * asks for it.
3382 */
3383 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003384 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003385 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003386 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3387 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003388 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003389 /* The header is set to be added only if none is present
3390 * and we found it, so don't do anything.
3391 */
3392 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003393 else if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003394 /* Add an X-Forwarded-For header unless the source IP is
3395 * in the 'except' network range.
3396 */
3397 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003398 (((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 != s->fe->except_net.s_addr) &&
3400 (!s->be->except_mask.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003401 (((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003402 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003403 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003404 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003405 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003406
3407 /* Note: we rely on the backend to get the header name to be used for
3408 * x-forwarded-for, because the header is really meant for the backends.
3409 * However, if the backend did not specify any option, we have to rely
3410 * on the frontend's header name.
3411 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003412 if (s->be->fwdfor_hdr_len) {
3413 len = s->be->fwdfor_hdr_len;
3414 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003415 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003416 len = s->fe->fwdfor_hdr_len;
3417 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003418 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003419 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003420
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003421 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003422 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003423 }
3424 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003425 else if (s->req->prod->conn.addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003426 /* FIXME: for the sake of completeness, we should also support
3427 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003428 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 int len;
3430 char pn[INET6_ADDRSTRLEN];
3431 inet_ntop(AF_INET6,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003432 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn.addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003434
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 /* Note: we rely on the backend to get the header name to be used for
3436 * x-forwarded-for, because the header is really meant for the backends.
3437 * However, if the backend did not specify any option, we have to rely
3438 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003439 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003440 if (s->be->fwdfor_hdr_len) {
3441 len = s->be->fwdfor_hdr_len;
3442 memcpy(trash, s->be->fwdfor_hdr_name, len);
3443 } else {
3444 len = s->fe->fwdfor_hdr_len;
3445 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003446 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003448
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003449 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 goto return_bad_req;
3451 }
3452 }
3453
3454 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003455 * 10: add X-Original-To if either the frontend or the backend
3456 * asks for it.
3457 */
3458 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3459
3460 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003461 if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003462 /* Add an X-Original-To header unless the destination IP is
3463 * in the 'except' network range.
3464 */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003465 conn_get_to_addr(&s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003466
Willy Tarreau986a9d22012-08-30 21:11:38 +02003467 if (s->req->prod->conn.addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003468 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003469 (((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003470 != s->fe->except_to.s_addr) &&
3471 (!s->be->except_mask_to.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003472 (((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003473 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003474 int len;
3475 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003476 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003477
3478 /* Note: we rely on the backend to get the header name to be used for
3479 * x-original-to, because the header is really meant for the backends.
3480 * However, if the backend did not specify any option, we have to rely
3481 * on the frontend's header name.
3482 */
3483 if (s->be->orgto_hdr_len) {
3484 len = s->be->orgto_hdr_len;
3485 memcpy(trash, s->be->orgto_hdr_name, len);
3486 } else {
3487 len = s->fe->orgto_hdr_len;
3488 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003489 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003490 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3491
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003492 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003493 goto return_bad_req;
3494 }
3495 }
3496 }
3497
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003498 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3499 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003500 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003501 unsigned int want_flags = 0;
3502
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003503 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003504 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3505 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3506 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003507 want_flags |= TX_CON_CLO_SET;
3508 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003509 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3510 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3511 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003512 want_flags |= TX_CON_KAL_SET;
3513 }
3514
3515 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003516 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003517 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003518
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003519
Willy Tarreau522d6c02009-12-06 18:49:18 +01003520 /* If we have no server assigned yet and we're balancing on url_param
3521 * with a POST request, we may be interested in checking the body for
3522 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003523 */
3524 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3525 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003526 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003527 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003528 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003530 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003531
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003532 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003533 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003534#ifdef TCP_QUICKACK
3535 /* We expect some data from the client. Unless we know for sure
3536 * we already have a full request, we have to re-enable quick-ack
3537 * in case we previously disabled it, otherwise we might cause
3538 * the client to delay further data.
3539 */
3540 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003541 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003542 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003543 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003544#endif
3545 }
Willy Tarreau03945942009-12-22 16:50:27 +01003546
Willy Tarreau59234e92008-11-30 23:51:27 +01003547 /*************************************************************
3548 * OK, that's finished for the headers. We have done what we *
3549 * could. Let's switch to the DATA state. *
3550 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551 req->analyse_exp = TICK_ETERNITY;
3552 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003553
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003554 /* if the server closes the connection, we want to immediately react
3555 * and close the socket to save packets and syscalls.
3556 */
3557 req->cons->flags |= SI_FL_NOHALF;
3558
Willy Tarreau59234e92008-11-30 23:51:27 +01003559 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003560 /* OK let's go on with the BODY now */
3561 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003562
Willy Tarreau59234e92008-11-30 23:51:27 +01003563 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003564 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003565 /* we detected a parsing error. We want to archive this request
3566 * in the dedicated proxy area for later troubleshooting.
3567 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003568 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003569 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003570
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 txn->req.msg_state = HTTP_MSG_ERROR;
3572 txn->status = 400;
3573 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003574 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003575
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003576 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003577 if (s->listener->counters)
3578 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003579
Willy Tarreau59234e92008-11-30 23:51:27 +01003580 if (!(s->flags & SN_ERR_MASK))
3581 s->flags |= SN_ERR_PRXCOND;
3582 if (!(s->flags & SN_FINST_MASK))
3583 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003584 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003585}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003586
Willy Tarreau60b85b02008-11-30 23:28:40 +01003587/* This function is an analyser which processes the HTTP tarpit. It always
3588 * returns zero, at the beginning because it prevents any other processing
3589 * from occurring, and at the end because it terminates the request.
3590 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003591int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003592{
3593 struct http_txn *txn = &s->txn;
3594
3595 /* This connection is being tarpitted. The CLIENT side has
3596 * already set the connect expiration date to the right
3597 * timeout. We just have to check that the client is still
3598 * there and that the timeout has not expired.
3599 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003600 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003601 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003602 !tick_is_expired(req->analyse_exp, now_ms))
3603 return 0;
3604
3605 /* We will set the queue timer to the time spent, just for
3606 * logging purposes. We fake a 500 server error, so that the
3607 * attacker will not suspect his connection has been tarpitted.
3608 * It will not cause trouble to the logs because we can exclude
3609 * the tarpitted connections by filtering on the 'PT' status flags.
3610 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003611 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3612
3613 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003614 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003615 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003616
3617 req->analysers = 0;
3618 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003619
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003620 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003621 if (s->listener->counters)
3622 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003623
Willy Tarreau60b85b02008-11-30 23:28:40 +01003624 if (!(s->flags & SN_ERR_MASK))
3625 s->flags |= SN_ERR_PRXCOND;
3626 if (!(s->flags & SN_FINST_MASK))
3627 s->flags |= SN_FINST_T;
3628 return 0;
3629}
3630
Willy Tarreaud34af782008-11-30 23:36:37 +01003631/* This function is an analyser which processes the HTTP request body. It looks
3632 * for parameters to be used for the load balancing algorithm (url_param). It
3633 * must only be called after the standard HTTP request processing has occurred,
3634 * because it expects the request to be parsed. It returns zero if it needs to
3635 * read more data, or 1 once it has completed its analysis.
3636 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003637int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003638{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003639 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003640 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003641 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003642
3643 /* We have to parse the HTTP request body to find any required data.
3644 * "balance url_param check_post" should have been the only way to get
3645 * into this. We were brought here after HTTP header analysis, so all
3646 * related structures are ready.
3647 */
3648
Willy Tarreau522d6c02009-12-06 18:49:18 +01003649 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3650 goto missing_data;
3651
3652 if (msg->msg_state < HTTP_MSG_100_SENT) {
3653 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3654 * send an HTTP/1.1 100 Continue intermediate response.
3655 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003656 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657 struct hdr_ctx ctx;
3658 ctx.idx = 0;
3659 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003660 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003661 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003662 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003663 }
3664 }
3665 msg->msg_state = HTTP_MSG_100_SENT;
3666 }
3667
3668 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003669 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003670 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003671 * is still null. We must save the body in msg->next because it
3672 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003673 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003674 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003675
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003676 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003677 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3678 else
3679 msg->msg_state = HTTP_MSG_DATA;
3680 }
3681
3682 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003683 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003684 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003685 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003686 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003687 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003688
Willy Tarreau115acb92009-12-26 13:56:06 +01003689 if (!ret)
3690 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003691 else if (ret < 0) {
3692 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003693 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003694 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003695 }
3696
Willy Tarreaud98cf932009-12-27 22:54:55 +01003697 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003698 * We have the first data byte is in msg->sov. We're waiting for at
3699 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003700 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003701
Willy Tarreau124d9912011-03-01 20:30:48 +01003702 if (msg->body_len < limit)
3703 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003704
Willy Tarreau9b28e032012-10-12 23:49:43 +02003705 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003706 goto http_end;
3707
3708 missing_data:
3709 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003710 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003711 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003712 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003713 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003714
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003715 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003716 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003717 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003718
3719 if (!(s->flags & SN_ERR_MASK))
3720 s->flags |= SN_ERR_CLITO;
3721 if (!(s->flags & SN_FINST_MASK))
3722 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003723 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003724 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003725
3726 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003727 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003728 /* Not enough data. We'll re-use the http-request
3729 * timeout here. Ideally, we should set the timeout
3730 * relative to the accept() date. We just set the
3731 * request timeout once at the beginning of the
3732 * request.
3733 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003734 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003735 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003736 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003737 return 0;
3738 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003739
3740 http_end:
3741 /* The situation will not evolve, so let's give up on the analysis. */
3742 s->logs.tv_request = now; /* update the request timer to reflect full request */
3743 req->analysers &= ~an_bit;
3744 req->analyse_exp = TICK_ETERNITY;
3745 return 1;
3746
3747 return_bad_req: /* let's centralize all bad requests */
3748 txn->req.msg_state = HTTP_MSG_ERROR;
3749 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003751
Willy Tarreau79ebac62010-06-07 13:47:49 +02003752 if (!(s->flags & SN_ERR_MASK))
3753 s->flags |= SN_ERR_PRXCOND;
3754 if (!(s->flags & SN_FINST_MASK))
3755 s->flags |= SN_FINST_R;
3756
Willy Tarreau522d6c02009-12-06 18:49:18 +01003757 return_err_msg:
3758 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003759 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003760 if (s->listener->counters)
3761 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003762 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003763}
3764
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003765/* send a server's name with an outgoing request over an established connection.
3766 * Note: this function is designed to be called once the request has been scheduled
3767 * for being forwarded. This is the reason why it rewinds the buffer before
3768 * proceeding.
3769 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003770int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003771
3772 struct hdr_ctx ctx;
3773
Mark Lamourinec2247f02012-01-04 13:02:01 -05003774 char *hdr_name = be->server_id_hdr_name;
3775 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003776 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003777 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003778 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003779
William Lallemandd9e90662012-01-30 17:27:17 +01003780 ctx.idx = 0;
3781
Willy Tarreau9b28e032012-10-12 23:49:43 +02003782 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003783 if (old_o) {
3784 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003785 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003786 }
3787
Willy Tarreau9b28e032012-10-12 23:49:43 +02003788 old_i = chn->buf->i;
3789 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003790 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003791 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003792 }
3793
3794 /* Add the new header requested with the server value */
3795 hdr_val = trash;
3796 memcpy(hdr_val, hdr_name, hdr_name_len);
3797 hdr_val += hdr_name_len;
3798 *hdr_val++ = ':';
3799 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003800 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003801 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003802
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003803 if (old_o) {
3804 /* If this was a forwarded request, we must readjust the amount of
3805 * data to be forwarded in order to take into account the size
3806 * variations.
3807 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003808 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003809 }
3810
Mark Lamourinec2247f02012-01-04 13:02:01 -05003811 return 0;
3812}
3813
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814/* Terminate current transaction and prepare a new one. This is very tricky
3815 * right now but it works.
3816 */
3817void http_end_txn_clean_session(struct session *s)
3818{
3819 /* FIXME: We need a more portable way of releasing a backend's and a
3820 * server's connections. We need a safer way to reinitialize buffer
3821 * flags. We also need a more accurate method for computing per-request
3822 * data.
3823 */
3824 http_silent_debug(__LINE__, s);
3825
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003826 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003827 si_shutr(s->req->cons);
3828 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829
3830 http_silent_debug(__LINE__, s);
3831
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003832 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003833 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003834 if (unlikely(s->srv_conn))
3835 sess_change_server(s, NULL);
3836 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003837
3838 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3839 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003840 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003841
3842 if (s->txn.status) {
3843 int n;
3844
3845 n = s->txn.status / 100;
3846 if (n < 1 || n > 5)
3847 n = 0;
3848
3849 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003850 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003851
Willy Tarreau24657792010-02-26 10:30:28 +01003852 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003854 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855 }
3856
3857 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003858 s->logs.bytes_in -= s->req->buf->i;
3859 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860
3861 /* let's do a final log if we need it */
3862 if (s->logs.logwait &&
3863 !(s->flags & SN_MONITOR) &&
3864 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3865 s->do_log(s);
3866 }
3867
3868 s->logs.accept_date = date; /* user-visible date for logging */
3869 s->logs.tv_accept = now; /* corrected date for internal use */
3870 tv_zero(&s->logs.tv_request);
3871 s->logs.t_queue = -1;
3872 s->logs.t_connect = -1;
3873 s->logs.t_data = -1;
3874 s->logs.t_close = 0;
3875 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3876 s->logs.srv_queue_size = 0; /* we will get this number soon */
3877
Willy Tarreau9b28e032012-10-12 23:49:43 +02003878 s->logs.bytes_in = s->req->total = s->req->buf->i;
3879 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880
3881 if (s->pend_pos)
3882 pendconn_free(s->pend_pos);
3883
Willy Tarreau827aee92011-03-10 16:55:02 +01003884 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 if (s->flags & SN_CURR_SESS) {
3886 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003887 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003889 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3890 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 }
3892
Willy Tarreau9e000c62011-03-10 14:03:36 +01003893 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894
3895 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003896 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +02003897 s->req->cons->conn.flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003899 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 s->req->cons->err_loc = NULL;
3901 s->req->cons->exp = TICK_ETERNITY;
3902 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003903 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
3904 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003905 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 +01003906 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3907 s->txn.meth = 0;
3908 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003909 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003910 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003911 s->req->cons->flags |= SI_FL_INDEP_STR;
3912
Willy Tarreau96e31212011-05-30 18:10:30 +02003913 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003914 s->req->flags |= CF_NEVER_WAIT;
3915 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003916 }
3917
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 /* if the request buffer is not empty, it means we're
3919 * about to process another request, so send pending
3920 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003921 * Just don't do this if the buffer is close to be full,
3922 * because the request will wait for it to flush a little
3923 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003925 if (s->req->buf->i) {
3926 if (s->rep->buf->o &&
3927 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
3928 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003929 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01003930 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003931
3932 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003933 channel_auto_read(s->req);
3934 channel_auto_close(s->req);
3935 channel_auto_read(s->rep);
3936 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937
Willy Tarreau342b11c2010-11-24 16:22:09 +01003938 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003939 s->rep->analysers = 0;
3940
3941 http_silent_debug(__LINE__, s);
3942}
3943
3944
3945/* This function updates the request state machine according to the response
3946 * state machine and buffer flags. It returns 1 if it changes anything (flag
3947 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3948 * it is only used to find when a request/response couple is complete. Both
3949 * this function and its equivalent should loop until both return zero. It
3950 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3951 */
3952int http_sync_req_state(struct session *s)
3953{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003954 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003955 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003956 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 unsigned int old_state = txn->req.msg_state;
3958
3959 http_silent_debug(__LINE__, s);
3960 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3961 return 0;
3962
3963 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003964 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003965 * We can shut the read side unless we want to abort_on_close,
3966 * or we have a POST request. The issue with POST requests is
3967 * that some browsers still send a CRLF after the request, and
3968 * this CRLF must be read so that it does not remain in the kernel
3969 * buffers, otherwise a close could cause an RST on some systems
3970 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003971 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003972 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003973 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003974
3975 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3976 goto wait_other_side;
3977
3978 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3979 /* The server has not finished to respond, so we
3980 * don't want to move in order not to upset it.
3981 */
3982 goto wait_other_side;
3983 }
3984
3985 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3986 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003987 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02003989 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 goto wait_other_side;
3991 }
3992
3993 /* When we get here, it means that both the request and the
3994 * response have finished receiving. Depending on the connection
3995 * mode, we'll have to wait for the last bytes to leave in either
3996 * direction, and sometimes for a close to be effective.
3997 */
3998
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003999 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4000 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004001 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4002 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004003 }
4004 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4005 /* Option forceclose is set, or either side wants to close,
4006 * let's enforce it now that we're not expecting any new
4007 * data to come. The caller knows the session is complete
4008 * once both states are CLOSED.
4009 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004010 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4011 channel_shutr_now(chn);
4012 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 }
4015 else {
4016 /* The last possible modes are keep-alive and tunnel. Since tunnel
4017 * mode does not set the body analyser, we can't reach this place
4018 * in tunnel mode, so we're left with keep-alive only.
4019 * This mode is currently not implemented, we switch to tunnel mode.
4020 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004021 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004022 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004023 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024 }
4025
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004026 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004028 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004029
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004030 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 txn->req.msg_state = HTTP_MSG_CLOSING;
4032 goto http_msg_closing;
4033 }
4034 else {
4035 txn->req.msg_state = HTTP_MSG_CLOSED;
4036 goto http_msg_closed;
4037 }
4038 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004039 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040 }
4041
4042 if (txn->req.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 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004047 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 txn->req.msg_state = HTTP_MSG_CLOSED;
4049 goto http_msg_closed;
4050 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004051 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 txn->req.msg_state = HTTP_MSG_ERROR;
4053 goto wait_other_side;
4054 }
4055 }
4056
4057 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4058 http_msg_closed:
4059 goto wait_other_side;
4060 }
4061
4062 wait_other_side:
4063 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004064 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065}
4066
4067
4068/* This function updates the response state machine according to the request
4069 * state machine and buffer flags. It returns 1 if it changes anything (flag
4070 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4071 * it is only used to find when a request/response couple is complete. Both
4072 * this function and its equivalent should loop until both return zero. It
4073 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4074 */
4075int http_sync_res_state(struct session *s)
4076{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004077 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004079 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080 unsigned int old_state = txn->rsp.msg_state;
4081
4082 http_silent_debug(__LINE__, s);
4083 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4084 return 0;
4085
4086 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4087 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004088 * still monitor the server connection for a possible close
4089 * while the request is being uploaded, so we don't disable
4090 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004092 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093
4094 if (txn->req.msg_state == HTTP_MSG_ERROR)
4095 goto wait_other_side;
4096
4097 if (txn->req.msg_state < HTTP_MSG_DONE) {
4098 /* The client seems to still be sending data, probably
4099 * because we got an error response during an upload.
4100 * We have the choice of either breaking the connection
4101 * or letting it pass through. Let's do the later.
4102 */
4103 goto wait_other_side;
4104 }
4105
4106 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4107 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004108 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004110 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 goto wait_other_side;
4112 }
4113
4114 /* When we get here, it means that both the request and the
4115 * response have finished receiving. Depending on the connection
4116 * mode, we'll have to wait for the last bytes to leave in either
4117 * direction, and sometimes for a close to be effective.
4118 */
4119
4120 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4121 /* Server-close mode : shut read and wait for the request
4122 * side to close its output buffer. The caller will detect
4123 * when we're in DONE and the other is in CLOSED and will
4124 * catch that for the final cleanup.
4125 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004126 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4127 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004129 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4130 /* Option forceclose is set, or either side wants to close,
4131 * let's enforce it now that we're not expecting any new
4132 * data to come. The caller knows the session is complete
4133 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004135 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4136 channel_shutr_now(chn);
4137 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004138 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 }
4140 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004141 /* The last possible modes are keep-alive and tunnel. Since tunnel
4142 * mode does not set the body analyser, we can't reach this place
4143 * in tunnel mode, so we're left with keep-alive only.
4144 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004146 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004148 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004149 }
4150
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004151 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004153 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004154 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4155 goto http_msg_closing;
4156 }
4157 else {
4158 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4159 goto http_msg_closed;
4160 }
4161 }
4162 goto wait_other_side;
4163 }
4164
4165 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4166 http_msg_closing:
4167 /* nothing else to forward, just waiting for the output buffer
4168 * to be empty and for the shutw_now to take effect.
4169 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004170 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004171 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4172 goto http_msg_closed;
4173 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004174 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004176 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004177 if (target_srv(&s->target))
4178 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 goto wait_other_side;
4180 }
4181 }
4182
4183 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4184 http_msg_closed:
4185 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004186 bi_erase(chn);
4187 channel_auto_close(chn);
4188 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004189 goto wait_other_side;
4190 }
4191
4192 wait_other_side:
4193 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004194 /* We force the response to leave immediately if we're waiting for the
4195 * other side, since there is no pending shutdown to push it out.
4196 */
4197 if (!channel_is_empty(chn))
4198 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004199 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200}
4201
4202
4203/* Resync the request and response state machines. Return 1 if either state
4204 * changes.
4205 */
4206int http_resync_states(struct session *s)
4207{
4208 struct http_txn *txn = &s->txn;
4209 int old_req_state = txn->req.msg_state;
4210 int old_res_state = txn->rsp.msg_state;
4211
4212 http_silent_debug(__LINE__, s);
4213 http_sync_req_state(s);
4214 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004215 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 if (!http_sync_res_state(s))
4217 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004218 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219 if (!http_sync_req_state(s))
4220 break;
4221 }
4222 http_silent_debug(__LINE__, s);
4223 /* OK, both state machines agree on a compatible state.
4224 * There are a few cases we're interested in :
4225 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4226 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4227 * directions, so let's simply disable both analysers.
4228 * - HTTP_MSG_CLOSED on the response only means we must abort the
4229 * request.
4230 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4231 * with server-close mode means we've completed one request and we
4232 * must re-initialize the server connection.
4233 */
4234
4235 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4236 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4237 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4238 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4239 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004240 channel_auto_close(s->req);
4241 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004243 channel_auto_close(s->rep);
4244 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004246 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4247 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004248 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004249 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004250 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004251 channel_auto_close(s->rep);
4252 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004253 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004254 channel_abort(s->req);
4255 channel_auto_close(s->req);
4256 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004257 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258 }
4259 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4260 txn->rsp.msg_state == HTTP_MSG_DONE &&
4261 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4262 /* server-close: terminate this server connection and
4263 * reinitialize a fresh-new transaction.
4264 */
4265 http_end_txn_clean_session(s);
4266 }
4267
4268 http_silent_debug(__LINE__, s);
4269 return txn->req.msg_state != old_req_state ||
4270 txn->rsp.msg_state != old_res_state;
4271}
4272
Willy Tarreaud98cf932009-12-27 22:54:55 +01004273/* This function is an analyser which forwards request body (including chunk
4274 * sizes if any). It is called as soon as we must forward, even if we forward
4275 * zero byte. The only situation where it must not be called is when we're in
4276 * tunnel mode and we want to forward till the close. It's used both to forward
4277 * remaining data and to resync after end of body. It expects the msg_state to
4278 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4279 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004280 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004281 * bytes of pending data + the headers if not already done (between sol and sov).
4282 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004283 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004284int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004285{
4286 struct http_txn *txn = &s->txn;
4287 struct http_msg *msg = &s->txn.req;
4288
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004289 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4290 return 0;
4291
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004292 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004293 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004294 /* Output closed while we were sending data. We must abort and
4295 * wake the other side up.
4296 */
4297 msg->msg_state = HTTP_MSG_ERROR;
4298 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004299 return 1;
4300 }
4301
Willy Tarreau4fe41902010-06-07 22:27:41 +02004302 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004303 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004304
4305 /* Note that we don't have to send 100-continue back because we don't
4306 * need the data to complete our job, and it's up to the server to
4307 * decide whether to return 100, 417 or anything else in return of
4308 * an "Expect: 100-continue" header.
4309 */
4310
4311 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004312 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004313 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004314 * is still null. We must save the body in msg->next because it
4315 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004316 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004317 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004318
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004319 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004320 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4321 else {
4322 msg->msg_state = HTTP_MSG_DATA;
4323 }
4324 }
4325
Willy Tarreaud98cf932009-12-27 22:54:55 +01004326 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004327 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004328
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004330 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004331 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004332 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004333 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004334 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004335 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004336 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004337 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004338
Willy Tarreaucaabe412010-01-03 23:08:28 +01004339 if (msg->msg_state == HTTP_MSG_DATA) {
4340 /* must still forward */
4341 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004342 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004343
4344 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004345 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004346 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004348 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004349 }
4350 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004351 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004352 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004353 * TRAILERS state.
4354 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004355 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004356
4357 if (!ret)
4358 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004359 else if (ret < 0) {
4360 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004361 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004362 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004363 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004364 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004367 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4368 /* we want the CRLF after the data */
4369 int ret;
4370
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004371 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004372
4373 if (ret == 0)
4374 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004375 else if (ret < 0) {
4376 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004377 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004378 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004379 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004380 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004381 /* we're in MSG_CHUNK_SIZE now */
4382 }
4383 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004384 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004385
4386 if (ret == 0)
4387 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004388 else if (ret < 0) {
4389 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004390 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004391 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004392 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004393 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004394 /* we're in HTTP_MSG_DONE now */
4395 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004397 int old_state = msg->msg_state;
4398
Willy Tarreau610ecce2010-01-04 21:15:02 +01004399 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004400 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004401 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4402 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004403 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 if (http_resync_states(s)) {
4405 /* some state changes occurred, maybe the analyser
4406 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004407 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004408 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004409 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004410 /* request errors are most likely due to
4411 * the server aborting the transfer.
4412 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004413 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004414 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004415 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004416 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004418 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004419 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004420 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004421
4422 /* If "option abortonclose" is set on the backend, we
4423 * want to monitor the client's connection and forward
4424 * any shutdown notification to the server, which will
4425 * decide whether to close or to go on processing the
4426 * request.
4427 */
4428 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004429 channel_auto_read(req);
4430 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004431 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004432 else if (s->txn.meth == HTTP_METH_POST) {
4433 /* POST requests may require to read extra CRLF
4434 * sent by broken browsers and which could cause
4435 * an RST to be sent upon close on some systems
4436 * (eg: Linux).
4437 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004438 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004439 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004440
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004442 }
4443 }
4444
Willy Tarreaud98cf932009-12-27 22:54:55 +01004445 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004446 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004447 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004448 if (!(s->flags & SN_ERR_MASK))
4449 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004450 if (!(s->flags & SN_FINST_MASK)) {
4451 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4452 s->flags |= SN_FINST_H;
4453 else
4454 s->flags |= SN_FINST_D;
4455 }
4456
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004457 s->fe->fe_counters.cli_aborts++;
4458 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004459 if (target_srv(&s->target))
4460 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004461
4462 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004463 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004464
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004465 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004466 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004467 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004469 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004470 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004471 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004472 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004473 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004474
Willy Tarreau5c620922011-05-11 19:56:11 +02004475 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004476 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004477 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004478 * modes are already handled by the stream sock layer. We must not do
4479 * this in content-length mode because it could present the MSG_MORE
4480 * flag with the last block of forwarded data, which would cause an
4481 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004482 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004483 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004484 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004485
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004487 return 0;
4488
Willy Tarreaud98cf932009-12-27 22:54:55 +01004489 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004490 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004491 if (s->listener->counters)
4492 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004493 return_bad_req_stats_ok:
4494 txn->req.msg_state = HTTP_MSG_ERROR;
4495 if (txn->status) {
4496 /* Note: we don't send any error if some data were already sent */
4497 stream_int_retnclose(req->prod, NULL);
4498 } else {
4499 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004500 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004501 }
4502 req->analysers = 0;
4503 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004504
4505 if (!(s->flags & SN_ERR_MASK))
4506 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004507 if (!(s->flags & SN_FINST_MASK)) {
4508 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4509 s->flags |= SN_FINST_H;
4510 else
4511 s->flags |= SN_FINST_D;
4512 }
4513 return 0;
4514
4515 aborted_xfer:
4516 txn->req.msg_state = HTTP_MSG_ERROR;
4517 if (txn->status) {
4518 /* Note: we don't send any error if some data were already sent */
4519 stream_int_retnclose(req->prod, NULL);
4520 } else {
4521 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004522 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004523 }
4524 req->analysers = 0;
4525 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004527 s->fe->fe_counters.srv_aborts++;
4528 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004529 if (target_srv(&s->target))
4530 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004531
4532 if (!(s->flags & SN_ERR_MASK))
4533 s->flags |= SN_ERR_SRVCL;
4534 if (!(s->flags & SN_FINST_MASK)) {
4535 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4536 s->flags |= SN_FINST_H;
4537 else
4538 s->flags |= SN_FINST_D;
4539 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004540 return 0;
4541}
4542
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4544 * processing can continue on next analysers, or zero if it either needs more
4545 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4546 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4547 * when it has nothing left to do, and may remove any analyser when it wants to
4548 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004549 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004550int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004551{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004552 struct http_txn *txn = &s->txn;
4553 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004554 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004555 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004557 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004558
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004559 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 +02004560 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004561 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004562 rep,
4563 rep->rex, rep->wex,
4564 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004565 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004566 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004567
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 /*
4569 * Now parse the partial (or complete) lines.
4570 * We will check the response syntax, and also join multi-line
4571 * headers. An index of all the lines will be elaborated while
4572 * parsing.
4573 *
4574 * For the parsing, we use a 28 states FSM.
4575 *
4576 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004577 * rep->buf->p = beginning of response
4578 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4579 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004580 * msg->eol = end of current header or line (LF or CRLF)
4581 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004582 */
4583
Willy Tarreau83e3af02009-12-28 17:39:57 +01004584 /* There's a protected area at the end of the buffer for rewriting
4585 * purposes. We don't want to start to parse the request if the
4586 * protected area is affected, because we may have to move processed
4587 * data later, which is much more complicated.
4588 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004589 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004590 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004591 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4592 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4593 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004594 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004595 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004596 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004597 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004598 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004599 return 0;
4600 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004601 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4602 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004603 }
4604
Willy Tarreau9b28e032012-10-12 23:49:43 +02004605 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004606 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004607 }
4608
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004609 /* 1: we might have to print this header in debug mode */
4610 if (unlikely((global.mode & MODE_DEBUG) &&
4611 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004612 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004614
Willy Tarreau9b28e032012-10-12 23:49:43 +02004615 sol = rep->buf->p;
4616 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 sol += hdr_idx_first_pos(&txn->hdr_idx);
4620 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004621
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004622 while (cur_idx) {
4623 eol = sol + txn->hdr_idx.v[cur_idx].len;
4624 debug_hdr("srvhdr", s, sol, eol);
4625 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4626 cur_idx = txn->hdr_idx.v[cur_idx].next;
4627 }
4628 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004629
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004630 /*
4631 * Now we quickly check if we have found a full valid response.
4632 * If not so, we check the FD and buffer states before leaving.
4633 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004634 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635 * responses are checked first.
4636 *
4637 * Depending on whether the client is still there or not, we
4638 * may send an error response back or not. Note that normally
4639 * we should only check for HTTP status there, and check I/O
4640 * errors somewhere else.
4641 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004642
Willy Tarreau655dce92009-11-08 13:10:58 +01004643 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 /* Invalid response */
4645 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4646 /* we detected a parsing error. We want to archive this response
4647 * in the dedicated proxy area for later troubleshooting.
4648 */
4649 hdr_response_bad:
4650 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004651 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004653 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004654 if (target_srv(&s->target)) {
4655 target_srv(&s->target)->counters.failed_resp++;
4656 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004657 }
Willy Tarreau64648412010-03-05 10:41:54 +01004658 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004659 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 rep->analysers = 0;
4661 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004662 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004663 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004664 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665
4666 if (!(s->flags & SN_ERR_MASK))
4667 s->flags |= SN_ERR_PRXCOND;
4668 if (!(s->flags & SN_FINST_MASK))
4669 s->flags |= SN_FINST_H;
4670
4671 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004672 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004673
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004674 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004675 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004676 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004677 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678 goto hdr_response_bad;
4679 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004680
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004682 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004684 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004685
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004686 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004687 if (target_srv(&s->target)) {
4688 target_srv(&s->target)->counters.failed_resp++;
4689 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004690 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004691
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004692 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004693 rep->analysers = 0;
4694 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004695 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004696 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004697 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004698
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 if (!(s->flags & SN_ERR_MASK))
4700 s->flags |= SN_ERR_SRVCL;
4701 if (!(s->flags & SN_FINST_MASK))
4702 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004703 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004704 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004705
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004707 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004708 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004709 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004710
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004711 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004712 if (target_srv(&s->target)) {
4713 target_srv(&s->target)->counters.failed_resp++;
4714 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004715 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004716
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004717 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 rep->analysers = 0;
4719 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004720 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004721 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004722 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004723
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004724 if (!(s->flags & SN_ERR_MASK))
4725 s->flags |= SN_ERR_SRVTO;
4726 if (!(s->flags & SN_FINST_MASK))
4727 s->flags |= SN_FINST_H;
4728 return 0;
4729 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004730
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004731 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004732 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004733 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004734 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004735
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004736 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004737 if (target_srv(&s->target)) {
4738 target_srv(&s->target)->counters.failed_resp++;
4739 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004740 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004741
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004742 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 rep->analysers = 0;
4744 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004745 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004746 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004747 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004748
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 if (!(s->flags & SN_ERR_MASK))
4750 s->flags |= SN_ERR_SRVCL;
4751 if (!(s->flags & SN_FINST_MASK))
4752 s->flags |= SN_FINST_H;
4753 return 0;
4754 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004755
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004757 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004759 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004760
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004761 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004763 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764
4765 if (!(s->flags & SN_ERR_MASK))
4766 s->flags |= SN_ERR_CLICL;
4767 if (!(s->flags & SN_FINST_MASK))
4768 s->flags |= SN_FINST_H;
4769
4770 /* process_session() will take care of the error */
4771 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004773
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004774 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004775 return 0;
4776 }
4777
4778 /* More interesting part now : we know that we have a complete
4779 * response which at least looks like HTTP. We have an indicator
4780 * of each header's length, so we can parse them quickly.
4781 */
4782
4783 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004784 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 /*
4787 * 1: get the status code
4788 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004789 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004790 if (n < 1 || n > 5)
4791 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004792 /* when the client triggers a 4xx from the server, it's most often due
4793 * to a missing object or permission. These events should be tracked
4794 * because if they happen often, it may indicate a brute force or a
4795 * vulnerability scan.
4796 */
4797 if (n == 4)
4798 session_inc_http_err_ctr(s);
4799
Willy Tarreau827aee92011-03-10 16:55:02 +01004800 if (target_srv(&s->target))
4801 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802
Willy Tarreau5b154472009-12-21 20:11:07 +01004803 /* check if the response is HTTP/1.1 or above */
4804 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004805 ((rep->buf->p[5] > '1') ||
4806 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004807 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004808
4809 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004810 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 +01004811
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004812 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004813 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004814
Willy Tarreau9b28e032012-10-12 23:49:43 +02004815 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004816
Willy Tarreau39650402010-03-15 19:44:39 +01004817 /* Adjust server's health based on status code. Note: status codes 501
4818 * and 505 are triggered on demand by client request, so we must not
4819 * count them as server failures.
4820 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004821 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004822 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004823 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004824 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004825 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004826 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004827
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828 /*
4829 * 2: check for cacheability.
4830 */
4831
4832 switch (txn->status) {
4833 case 200:
4834 case 203:
4835 case 206:
4836 case 300:
4837 case 301:
4838 case 410:
4839 /* RFC2616 @13.4:
4840 * "A response received with a status code of
4841 * 200, 203, 206, 300, 301 or 410 MAY be stored
4842 * by a cache (...) unless a cache-control
4843 * directive prohibits caching."
4844 *
4845 * RFC2616 @9.5: POST method :
4846 * "Responses to this method are not cacheable,
4847 * unless the response includes appropriate
4848 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004849 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02004851 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4853 break;
4854 default:
4855 break;
4856 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004857
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004858 /*
4859 * 3: we may need to capture headers
4860 */
4861 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004862 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02004863 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 txn->rsp.cap, s->fe->rsp_cap);
4865
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004866 /* 4: determine the transfer-length.
4867 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4868 * the presence of a message-body in a RESPONSE and its transfer length
4869 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004870 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004871 * All responses to the HEAD request method MUST NOT include a
4872 * message-body, even though the presence of entity-header fields
4873 * might lead one to believe they do. All 1xx (informational), 204
4874 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4875 * message-body. All other responses do include a message-body,
4876 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004877 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004878 * 1. Any response which "MUST NOT" include a message-body (such as the
4879 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4880 * always terminated by the first empty line after the header fields,
4881 * regardless of the entity-header fields present in the message.
4882 *
4883 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4884 * the "chunked" transfer-coding (Section 6.2) is used, the
4885 * transfer-length is defined by the use of this transfer-coding.
4886 * If a Transfer-Encoding header field is present and the "chunked"
4887 * transfer-coding is not present, the transfer-length is defined by
4888 * the sender closing the connection.
4889 *
4890 * 3. If a Content-Length header field is present, its decimal value in
4891 * OCTETs represents both the entity-length and the transfer-length.
4892 * If a message is received with both a Transfer-Encoding header
4893 * field and a Content-Length header field, the latter MUST be ignored.
4894 *
4895 * 4. If the message uses the media type "multipart/byteranges", and
4896 * the transfer-length is not otherwise specified, then this self-
4897 * delimiting media type defines the transfer-length. This media
4898 * type MUST NOT be used unless the sender knows that the recipient
4899 * can parse it; the presence in a request of a Range header with
4900 * multiple byte-range specifiers from a 1.1 client implies that the
4901 * client can parse multipart/byteranges responses.
4902 *
4903 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004904 */
4905
4906 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004907 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004908 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004909 * FIXME: should we parse anyway and return an error on chunked encoding ?
4910 */
4911 if (txn->meth == HTTP_METH_HEAD ||
4912 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004913 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004914 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004915 goto skip_content_length;
4916 }
4917
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004918 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004919 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004920 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004921 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004922 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004923 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4924 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004925 /* bad transfer-encoding (chunked followed by something else) */
4926 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004927 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004928 break;
4929 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004930 }
4931
4932 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4933 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004934 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004935 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004936 signed long long cl;
4937
Willy Tarreauad14f752011-09-02 20:33:27 +02004938 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004939 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004940 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004941 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004942
Willy Tarreauad14f752011-09-02 20:33:27 +02004943 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004944 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004945 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004946 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004947
Willy Tarreauad14f752011-09-02 20:33:27 +02004948 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004949 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004951 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004952
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004953 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004954 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004955 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004956 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004957
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004959 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004960 }
4961
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004962 /* FIXME: we should also implement the multipart/byterange method.
4963 * For now on, we resort to close mode in this case (unknown length).
4964 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004965skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967 /* end of job, return OK */
4968 rep->analysers &= ~an_bit;
4969 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004970 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004971 return 1;
4972}
4973
4974/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004975 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4976 * and updates t->rep->analysers. It might make sense to explode it into several
4977 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004978 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004979int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980{
4981 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004982 struct http_msg *msg = &txn->rsp;
4983 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004984 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004985
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004986 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 +02004987 now_ms, __FUNCTION__,
4988 t,
4989 rep,
4990 rep->rex, rep->wex,
4991 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004992 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 rep->analysers);
4994
Willy Tarreau655dce92009-11-08 13:10:58 +01004995 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004996 return 0;
4997
4998 rep->analysers &= ~an_bit;
4999 rep->analyse_exp = TICK_ETERNITY;
5000
Willy Tarreau5b154472009-12-21 20:11:07 +01005001 /* Now we have to check if we need to modify the Connection header.
5002 * This is more difficult on the response than it is on the request,
5003 * because we can have two different HTTP versions and we don't know
5004 * how the client will interprete a response. For instance, let's say
5005 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5006 * HTTP/1.1 response without any header. Maybe it will bound itself to
5007 * HTTP/1.0 because it only knows about it, and will consider the lack
5008 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5009 * the lack of header as a keep-alive. Thus we will use two flags
5010 * indicating how a request MAY be understood by the client. In case
5011 * of multiple possibilities, we'll fix the header to be explicit. If
5012 * ambiguous cases such as both close and keepalive are seen, then we
5013 * will fall back to explicit close. Note that we won't take risks with
5014 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005015 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005016 */
5017
Willy Tarreaudc008c52010-02-01 16:20:08 +01005018 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5019 txn->status == 101)) {
5020 /* Either we've established an explicit tunnel, or we're
5021 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005022 * to understand the next protocols. We have to switch to tunnel
5023 * mode, so that we transfer the request and responses then let
5024 * this protocol pass unmodified. When we later implement specific
5025 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005026 * header which contains information about that protocol for
5027 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005028 */
5029 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5030 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005031 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5032 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5033 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005034 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005035
Willy Tarreau60466522010-01-18 19:08:45 +01005036 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005037 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005038 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5039 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005040
Willy Tarreau60466522010-01-18 19:08:45 +01005041 /* now adjust header transformations depending on current state */
5042 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5043 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5044 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005045 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005046 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005047 }
Willy Tarreau60466522010-01-18 19:08:45 +01005048 else { /* SCL / KAL */
5049 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005050 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005051 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005052 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005053
Willy Tarreau60466522010-01-18 19:08:45 +01005054 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005055 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005056
Willy Tarreau60466522010-01-18 19:08:45 +01005057 /* Some keep-alive responses are converted to Server-close if
5058 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005059 */
Willy Tarreau60466522010-01-18 19:08:45 +01005060 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5061 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005062 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005063 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005064 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005065 }
5066
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005068 /*
5069 * 3: we will have to evaluate the filters.
5070 * As opposed to version 1.2, now they will be evaluated in the
5071 * filters order and not in the header order. This means that
5072 * each filter has to be validated among all headers.
5073 *
5074 * Filters are tried with ->be first, then with ->fe if it is
5075 * different from ->be.
5076 */
5077
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005078 cur_proxy = t->be;
5079 while (1) {
5080 struct proxy *rule_set = cur_proxy;
5081
5082 /* try headers filters */
5083 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005084 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005086 if (target_srv(&t->target)) {
5087 target_srv(&t->target)->counters.failed_resp++;
5088 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005089 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005090 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005092 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005094 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005095 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005096 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005097 if (!(t->flags & SN_ERR_MASK))
5098 t->flags |= SN_ERR_PRXCOND;
5099 if (!(t->flags & SN_FINST_MASK))
5100 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005101 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005102 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005104
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 /* has the response been denied ? */
5106 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005107 if (target_srv(&t->target))
5108 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005109
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005110 t->be->be_counters.denied_resp++;
5111 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005112 if (t->listener->counters)
5113 t->listener->counters->denied_resp++;
5114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005117
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005119 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005120 if (txn->status < 200)
5121 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005122 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005123 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005124 ret = acl_pass(ret);
5125 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5126 ret = !ret;
5127 if (!ret)
5128 continue;
5129 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005130 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005131 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005132 }
5133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005134 /* check whether we're already working on the frontend */
5135 if (cur_proxy == t->fe)
5136 break;
5137 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005141 * We may be facing a 100-continue response, in which case this
5142 * is not the right response, and we're waiting for the next one.
5143 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005144 * next one.
5145 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005146 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005147 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005148 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005149 msg->msg_state = HTTP_MSG_RPBEFORE;
5150 txn->status = 0;
5151 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5152 return 1;
5153 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005154 else if (unlikely(txn->status < 200))
5155 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005156
5157 /* we don't have any 1xx status code now */
5158
5159 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005160 * 4: check for server cookie.
5161 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005162 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5163 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005164 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005165
Willy Tarreaubaaee002006-06-26 02:48:02 +02005166
Willy Tarreaua15645d2007-03-18 16:22:39 +01005167 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005169 */
Willy Tarreau67402132012-05-31 20:40:20 +02005170 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005172
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 /*
5174 * 6: add server cookie in the response if needed
5175 */
Willy Tarreau67402132012-05-31 20:40:20 +02005176 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5177 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005178 (!(t->flags & SN_DIRECT) ||
5179 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5180 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5181 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5182 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005183 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005184 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005186 /* the server is known, it's not the one the client requested, or the
5187 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 * insert a set-cookie here, except if we want to insert only on POST
5189 * requests and this one isn't. Note that servers which don't have cookies
5190 * (eg: some backup servers) will return a full cookie removal request.
5191 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005192 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005193 len = sprintf(trash,
5194 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5195 t->be->cookie_name);
5196 }
5197 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005198 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005199
5200 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5201 /* emit last_date, which is mandatory */
5202 trash[len++] = COOKIE_DELIM_DATE;
5203 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5204 if (t->be->cookie_maxlife) {
5205 /* emit first_date, which is either the original one or
5206 * the current date.
5207 */
5208 trash[len++] = COOKIE_DELIM_DATE;
5209 s30tob64(txn->cookie_first_date ?
5210 txn->cookie_first_date >> 2 :
5211 (date.tv_sec+3) >> 2, trash + len);
5212 len += 5;
5213 }
5214 }
5215 len += sprintf(trash + len, "; path=/");
5216 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005217
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005218 if (t->be->cookie_domain)
5219 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005220
Willy Tarreau4992dd22012-05-31 21:02:17 +02005221 if (t->be->ck_opts & PR_CK_HTTPONLY)
5222 len += sprintf(trash+len, "; HttpOnly");
5223
5224 if (t->be->ck_opts & PR_CK_SECURE)
5225 len += sprintf(trash+len, "; Secure");
5226
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005227 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005228 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005229
Willy Tarreauf1348312010-10-07 15:54:11 +02005230 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005231 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005232 /* the server did not change, only the date was updated */
5233 txn->flags |= TX_SCK_UPDATED;
5234 else
5235 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005236
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237 /* Here, we will tell an eventual cache on the client side that we don't
5238 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5239 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5240 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5241 */
Willy Tarreau67402132012-05-31 20:40:20 +02005242 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005243
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5245
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005246 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005247 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005249 }
5250 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005251
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005252 /*
5253 * 7: check if result will be cacheable with a cookie.
5254 * We'll block the response if security checks have caught
5255 * nasty things such as a cacheable cookie.
5256 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005257 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5258 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005259 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005260
5261 /* we're in presence of a cacheable response containing
5262 * a set-cookie header. We'll block it as requested by
5263 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005264 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005265 if (target_srv(&t->target))
5266 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005267
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005268 t->be->be_counters.denied_resp++;
5269 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005270 if (t->listener->counters)
5271 t->listener->counters->denied_resp++;
5272
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005274 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005275 send_log(t->be, LOG_ALERT,
5276 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005277 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005278 goto return_srv_prx_502;
5279 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005280
5281 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005282 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005283 */
Willy Tarreau60466522010-01-18 19:08:45 +01005284 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5285 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5286 unsigned int want_flags = 0;
5287
5288 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5289 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5290 /* we want a keep-alive response here. Keep-alive header
5291 * required if either side is not 1.1.
5292 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005293 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005294 want_flags |= TX_CON_KAL_SET;
5295 }
5296 else {
5297 /* we want a close response here. Close header required if
5298 * the server is 1.1, regardless of the client.
5299 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005300 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005301 want_flags |= TX_CON_CLO_SET;
5302 }
5303
5304 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005305 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005306 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005307
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005308 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005309 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005310 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005312
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005313 /*************************************************************
5314 * OK, that's finished for the headers. We have done what we *
5315 * could. Let's switch to the DATA state. *
5316 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005317
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005318 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005319
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005320 /* if the user wants to log as soon as possible, without counting
5321 * bytes from the server, then this is the right moment. We have
5322 * to temporarily assign bytes_out to log what we currently have.
5323 */
5324 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5325 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5326 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005327 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005328 t->logs.bytes_out = 0;
5329 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005330
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005331 /* Note: we must not try to cheat by jumping directly to DATA,
5332 * otherwise we would not let the client side wake up.
5333 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005334
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005335 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005336 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005337 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005338}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005339
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340/* This function is an analyser which forwards response body (including chunk
5341 * sizes if any). It is called as soon as we must forward, even if we forward
5342 * zero byte. The only situation where it must not be called is when we're in
5343 * tunnel mode and we want to forward till the close. It's used both to forward
5344 * remaining data and to resync after end of body. It expects the msg_state to
5345 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5346 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005347 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005348 * bytes of pending data + the headers if not already done (between sol and sov).
5349 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005350 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005351int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352{
5353 struct http_txn *txn = &s->txn;
5354 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005355 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005356
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005357 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5358 return 0;
5359
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005360 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005361 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005362 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005363 /* Output closed while we were sending data. We must abort and
5364 * wake the other side up.
5365 */
5366 msg->msg_state = HTTP_MSG_ERROR;
5367 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005368 return 1;
5369 }
5370
Willy Tarreau4fe41902010-06-07 22:27:41 +02005371 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005372 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005373
Willy Tarreaud98cf932009-12-27 22:54:55 +01005374 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005375 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02005376 * rep->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005377 * is still null. We must save the body in msg->next because it
5378 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005379 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005380 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005381
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005382 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5384 else {
5385 msg->msg_state = HTTP_MSG_DATA;
5386 }
5387 }
5388
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005390 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005391 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005392 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005393 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005394 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005395 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005396 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005397 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005398 }
5399
Willy Tarreaucaabe412010-01-03 23:08:28 +01005400 if (msg->msg_state == HTTP_MSG_DATA) {
5401 /* must still forward */
5402 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005403 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005404
5405 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005406 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005407 msg->msg_state = HTTP_MSG_DATA_CRLF;
5408 else
5409 msg->msg_state = HTTP_MSG_DONE;
5410 }
5411 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005412 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005413 * set ->sov and ->next to point to the body and switch to DATA or
5414 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005416 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417
5418 if (!ret)
5419 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005420 else if (ret < 0) {
5421 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005422 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005423 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005424 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005427 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5428 /* we want the CRLF after the data */
5429 int ret;
5430
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005431 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005432
5433 if (!ret)
5434 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005435 else if (ret < 0) {
5436 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005437 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005438 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005439 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 /* we're in MSG_CHUNK_SIZE now */
5441 }
5442 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005443 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005444
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 if (ret == 0)
5446 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005447 else if (ret < 0) {
5448 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005449 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005451 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 /* we're in HTTP_MSG_DONE now */
5453 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005455 int old_state = msg->msg_state;
5456
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005458 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5460 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005461 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 if (http_resync_states(s)) {
5463 http_silent_debug(__LINE__, s);
5464 /* some state changes occurred, maybe the analyser
5465 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005466 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005467 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005468 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005469 /* response errors are most likely due to
5470 * the client aborting the transfer.
5471 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005472 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005473 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005474 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005475 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005476 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005477 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005479 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005480 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005481 }
5482 }
5483
Willy Tarreaud98cf932009-12-27 22:54:55 +01005484 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005485 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005486 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005487 if (!(s->flags & SN_ERR_MASK))
5488 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005489 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005490 if (target_srv(&s->target))
5491 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005492 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005493 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005494
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005495 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005496 goto aborted_xfer;
5497
Willy Tarreau40dba092010-03-04 18:14:51 +01005498 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 if (!s->req->analysers)
5500 goto return_bad_res;
5501
Willy Tarreauea953162012-05-18 23:41:28 +02005502 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005503 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005504 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005505 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005506 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005507 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005508 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 }
5510
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005511 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005512 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005513 * Similarly, with keep-alive on the client side, we don't want to forward a
5514 * close.
5515 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005516 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005517 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5518 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005519 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005520
Willy Tarreau5c620922011-05-11 19:56:11 +02005521 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005522 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005523 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005524 * modes are already handled by the stream sock layer. We must not do
5525 * this in content-length mode because it could present the MSG_MORE
5526 * flag with the last block of forwarded data, which would cause an
5527 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005528 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005529 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005530 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005531
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005533 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534 return 0;
5535
Willy Tarreau40dba092010-03-04 18:14:51 +01005536 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005537 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005538 if (target_srv(&s->target))
5539 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005540
5541 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005543 /* don't send any error message as we're in the body */
5544 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005546 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005547 if (target_srv(&s->target))
5548 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005549
5550 if (!(s->flags & SN_ERR_MASK))
5551 s->flags |= SN_ERR_PRXCOND;
5552 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005553 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005554 return 0;
5555
5556 aborted_xfer:
5557 txn->rsp.msg_state = HTTP_MSG_ERROR;
5558 /* don't send any error message as we're in the body */
5559 stream_int_retnclose(res->cons, NULL);
5560 res->analysers = 0;
5561 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5562
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005563 s->fe->fe_counters.cli_aborts++;
5564 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005565 if (target_srv(&s->target))
5566 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567
5568 if (!(s->flags & SN_ERR_MASK))
5569 s->flags |= SN_ERR_CLICL;
5570 if (!(s->flags & SN_FINST_MASK))
5571 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 return 0;
5573}
5574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575/* Iterate the same filter through all request headers.
5576 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005577 * Since it can manage the switch to another backend, it updates the per-proxy
5578 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005579 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005580int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005581{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005582 char term;
5583 char *cur_ptr, *cur_end, *cur_next;
5584 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005585 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 struct hdr_idx_elem *cur_hdr;
5587 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005589 last_hdr = 0;
5590
Willy Tarreau9b28e032012-10-12 23:49:43 +02005591 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 old_idx = 0;
5593
5594 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005597 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 (exp->action == ACT_ALLOW ||
5599 exp->action == ACT_DENY ||
5600 exp->action == ACT_TARPIT))
5601 return 0;
5602
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005603 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 if (!cur_idx)
5605 break;
5606
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005607 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 cur_ptr = cur_next;
5609 cur_end = cur_ptr + cur_hdr->len;
5610 cur_next = cur_end + cur_hdr->cr + 1;
5611
5612 /* Now we have one header between cur_ptr and cur_end,
5613 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005614 */
5615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 /* The annoying part is that pattern matching needs
5617 * that we modify the contents to null-terminate all
5618 * strings before testing them.
5619 */
5620
5621 term = *cur_end;
5622 *cur_end = '\0';
5623
5624 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5625 switch (exp->action) {
5626 case ACT_SETBE:
5627 /* It is not possible to jump a second time.
5628 * FIXME: should we return an HTTP/500 here so that
5629 * the admin knows there's a problem ?
5630 */
5631 if (t->be != t->fe)
5632 break;
5633
5634 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005635 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636 last_hdr = 1;
5637 break;
5638
5639 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005640 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005641 last_hdr = 1;
5642 break;
5643
5644 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005645 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005646 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005647
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005648 t->fe->fe_counters.denied_req++;
5649 if (t->fe != t->be)
5650 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005651 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005652 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 break;
5655
5656 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005657 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005659
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005660 t->fe->fe_counters.denied_req++;
5661 if (t->fe != t->be)
5662 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005663 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005664 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 break;
5667
5668 case ACT_REPLACE:
5669 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02005670 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 /* 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 */
5675
5676 cur_end += delta;
5677 cur_next += delta;
5678 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005679 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 break;
5681
5682 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005683 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 cur_next += delta;
5685
Willy Tarreaufa355d42009-11-29 18:12:29 +01005686 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005687 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5688 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005689 cur_hdr->len = 0;
5690 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005691 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005692 break;
5693
5694 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696 if (cur_end)
5697 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 /* keep the link from this header to next one in case of later
5700 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005701 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 old_idx = cur_idx;
5703 }
5704 return 0;
5705}
5706
5707
5708/* Apply the filter to the request line.
5709 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5710 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 * Since it can manage the switch to another backend, it updates the per-proxy
5712 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005714int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715{
5716 char term;
5717 char *cur_ptr, *cur_end;
5718 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005719 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005720 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005721
Willy Tarreau58f10d72006-12-04 02:26:12 +01005722
Willy Tarreau3d300592007-03-18 18:34:41 +01005723 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005725 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005726 (exp->action == ACT_ALLOW ||
5727 exp->action == ACT_DENY ||
5728 exp->action == ACT_TARPIT))
5729 return 0;
5730 else if (exp->action == ACT_REMOVE)
5731 return 0;
5732
5733 done = 0;
5734
Willy Tarreau9b28e032012-10-12 23:49:43 +02005735 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005736 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005737
5738 /* Now we have the request line between cur_ptr and cur_end */
5739
5740 /* The annoying part is that pattern matching needs
5741 * that we modify the contents to null-terminate all
5742 * strings before testing them.
5743 */
5744
5745 term = *cur_end;
5746 *cur_end = '\0';
5747
5748 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5749 switch (exp->action) {
5750 case ACT_SETBE:
5751 /* It is not possible to jump a second time.
5752 * FIXME: should we return an HTTP/500 here so that
5753 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005754 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005755 if (t->be != t->fe)
5756 break;
5757
5758 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005759 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005760 done = 1;
5761 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005764 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005765 done = 1;
5766 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005769 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005770
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005771 t->fe->fe_counters.denied_req++;
5772 if (t->fe != t->be)
5773 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005774 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005775 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 done = 1;
5778 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005780 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005781 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005782
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005783 t->fe->fe_counters.denied_req++;
5784 if (t->fe != t->be)
5785 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005786 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005787 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 done = 1;
5790 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 case ACT_REPLACE:
5793 *cur_end = term; /* restore the string terminator */
5794 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02005795 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 /* FIXME: if the user adds a newline in the replacement, the
5797 * index will not be recalculated for now, and the new line
5798 * will not be counted as a new header.
5799 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005800
Willy Tarreaufa355d42009-11-29 18:12:29 +01005801 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005803 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 HTTP_MSG_RQMETH,
5805 cur_ptr, cur_end + 1,
5806 NULL, NULL);
5807 if (unlikely(!cur_end))
5808 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005810 /* we have a full request and we know that we have either a CR
5811 * or an LF at <ptr>.
5812 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005813 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5814 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005815 /* there is no point trying this regex on headers */
5816 return 1;
5817 }
5818 }
5819 *cur_end = term; /* restore the string terminator */
5820 return done;
5821}
Willy Tarreau97de6242006-12-27 17:18:38 +01005822
Willy Tarreau58f10d72006-12-04 02:26:12 +01005823
Willy Tarreau58f10d72006-12-04 02:26:12 +01005824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005826 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005827 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005828 * unparsable request. Since it can manage the switch to another backend, it
5829 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005831int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005833 struct http_txn *txn = &s->txn;
5834 struct hdr_exp *exp;
5835
5836 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 /*
5840 * The interleaving of transformations and verdicts
5841 * makes it difficult to decide to continue or stop
5842 * the evaluation.
5843 */
5844
Willy Tarreau6c123b12010-01-28 20:22:06 +01005845 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5846 break;
5847
Willy Tarreau3d300592007-03-18 18:34:41 +01005848 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005849 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005850 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005851 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005852
5853 /* if this filter had a condition, evaluate it now and skip to
5854 * next filter if the condition does not match.
5855 */
5856 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005857 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005858 ret = acl_pass(ret);
5859 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5860 ret = !ret;
5861
5862 if (!ret)
5863 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005864 }
5865
5866 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005867 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 if (unlikely(ret < 0))
5869 return -1;
5870
5871 if (likely(ret == 0)) {
5872 /* The filter did not match the request, it can be
5873 * iterated through all headers.
5874 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005875 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005877 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005878 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005879}
5880
5881
Willy Tarreaua15645d2007-03-18 16:22:39 +01005882
Willy Tarreau58f10d72006-12-04 02:26:12 +01005883/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005884 * Try to retrieve the server associated to the appsession.
5885 * If the server is found, it's assigned to the session.
5886 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005887void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005888 struct http_txn *txn = &t->txn;
5889 appsess *asession = NULL;
5890 char *sessid_temp = NULL;
5891
Cyril Bontéb21570a2009-11-29 20:04:48 +01005892 if (len > t->be->appsession_len) {
5893 len = t->be->appsession_len;
5894 }
5895
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005896 if (t->be->options2 & PR_O2_AS_REQL) {
5897 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005898 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005899 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005900 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005901 }
5902
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005903 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005904 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5905 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5906 return;
5907 }
5908
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005909 memcpy(txn->sessid, buf, len);
5910 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005911 }
5912
5913 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5914 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5915 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5916 return;
5917 }
5918
Cyril Bontéb21570a2009-11-29 20:04:48 +01005919 memcpy(sessid_temp, buf, len);
5920 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005921
5922 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5923 /* free previously allocated memory */
5924 pool_free2(apools.sessid, sessid_temp);
5925
5926 if (asession != NULL) {
5927 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5928 if (!(t->be->options2 & PR_O2_AS_REQL))
5929 asession->request_count++;
5930
5931 if (asession->serverid != NULL) {
5932 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005933
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005934 while (srv) {
5935 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005936 if ((srv->state & SRV_RUNNING) ||
5937 (t->be->options & PR_O_PERSIST) ||
5938 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005939 /* we found the server and it's usable */
5940 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005941 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005942 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005943 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005944
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005945 break;
5946 } else {
5947 txn->flags &= ~TX_CK_MASK;
5948 txn->flags |= TX_CK_DOWN;
5949 }
5950 }
5951 srv = srv->next;
5952 }
5953 }
5954 }
5955}
5956
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005957/* Find the end of a cookie value contained between <s> and <e>. It works the
5958 * same way as with headers above except that the semi-colon also ends a token.
5959 * See RFC2965 for more information. Note that it requires a valid header to
5960 * return a valid result.
5961 */
5962char *find_cookie_value_end(char *s, const char *e)
5963{
5964 int quoted, qdpair;
5965
5966 quoted = qdpair = 0;
5967 for (; s < e; s++) {
5968 if (qdpair) qdpair = 0;
5969 else if (quoted) {
5970 if (*s == '\\') qdpair = 1;
5971 else if (*s == '"') quoted = 0;
5972 }
5973 else if (*s == '"') quoted = 1;
5974 else if (*s == ',' || *s == ';') return s;
5975 }
5976 return s;
5977}
5978
5979/* Delete a value in a header between delimiters <from> and <next> in buffer
5980 * <buf>. The number of characters displaced is returned, and the pointer to
5981 * the first delimiter is updated if required. The function tries as much as
5982 * possible to respect the following principles :
5983 * - replace <from> delimiter by the <next> one unless <from> points to a
5984 * colon, in which case <next> is simply removed
5985 * - set exactly one space character after the new first delimiter, unless
5986 * there are not enough characters in the block being moved to do so.
5987 * - remove unneeded spaces before the previous delimiter and after the new
5988 * one.
5989 *
5990 * It is the caller's responsibility to ensure that :
5991 * - <from> points to a valid delimiter or the colon ;
5992 * - <next> points to a valid delimiter or the final CR/LF ;
5993 * - there are non-space chars before <from> ;
5994 * - there is a CR/LF at or after <next>.
5995 */
Willy Tarreauaf819352012-08-27 22:08:00 +02005996int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005997{
5998 char *prev = *from;
5999
6000 if (*prev == ':') {
6001 /* We're removing the first value, preserve the colon and add a
6002 * space if possible.
6003 */
6004 if (!http_is_crlf[(unsigned char)*next])
6005 next++;
6006 prev++;
6007 if (prev < next)
6008 *prev++ = ' ';
6009
6010 while (http_is_spht[(unsigned char)*next])
6011 next++;
6012 } else {
6013 /* Remove useless spaces before the old delimiter. */
6014 while (http_is_spht[(unsigned char)*(prev-1)])
6015 prev--;
6016 *from = prev;
6017
6018 /* copy the delimiter and if possible a space if we're
6019 * not at the end of the line.
6020 */
6021 if (!http_is_crlf[(unsigned char)*next]) {
6022 *prev++ = *next++;
6023 if (prev + 1 < next)
6024 *prev++ = ' ';
6025 while (http_is_spht[(unsigned char)*next])
6026 next++;
6027 }
6028 }
6029 return buffer_replace2(buf, prev, next, NULL, 0);
6030}
6031
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006032/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006033 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 * desirable to call it only when needed. This code is quite complex because
6035 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6036 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006038void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006040 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006041 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6044 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006045
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006046 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006047 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006048 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006050 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006052 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006053
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006054 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 hdr_beg = hdr_next;
6056 hdr_end = hdr_beg + cur_hdr->len;
6057 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 /* We have one full header between hdr_beg and hdr_end, and the
6060 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061 * "Cookie:" headers.
6062 */
6063
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006065 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006066 old_idx = cur_idx;
6067 continue;
6068 }
6069
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 del_from = NULL; /* nothing to be deleted */
6071 preserve_hdr = 0; /* assume we may kill the whole header */
6072
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073 /* Now look for cookies. Conforming to RFC2109, we have to support
6074 * attributes whose name begin with a '$', and associate them with
6075 * the right cookie, if we want to delete this cookie.
6076 * So there are 3 cases for each cookie read :
6077 * 1) it's a special attribute, beginning with a '$' : ignore it.
6078 * 2) it's a server id cookie that we *MAY* want to delete : save
6079 * some pointers on it (last semi-colon, beginning of cookie...)
6080 * 3) it's an application cookie : we *MAY* have to delete a previous
6081 * "special" cookie.
6082 * At the end of loop, if a "special" cookie remains, we may have to
6083 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006084 * *MUST* delete it.
6085 *
6086 * Note: RFC2965 is unclear about the processing of spaces around
6087 * the equal sign in the ATTR=VALUE form. A careful inspection of
6088 * the RFC explicitly allows spaces before it, and not within the
6089 * tokens (attrs or values). An inspection of RFC2109 allows that
6090 * too but section 10.1.3 lets one think that spaces may be allowed
6091 * after the equal sign too, resulting in some (rare) buggy
6092 * implementations trying to do that. So let's do what servers do.
6093 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6094 * allowed quoted strings in values, with any possible character
6095 * after a backslash, including control chars and delimitors, which
6096 * causes parsing to become ambiguous. Browsers also allow spaces
6097 * within values even without quotes.
6098 *
6099 * We have to keep multiple pointers in order to support cookie
6100 * removal at the beginning, middle or end of header without
6101 * corrupting the header. All of these headers are valid :
6102 *
6103 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6104 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6105 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6106 * | | | | | | | | |
6107 * | | | | | | | | hdr_end <--+
6108 * | | | | | | | +--> next
6109 * | | | | | | +----> val_end
6110 * | | | | | +-----------> val_beg
6111 * | | | | +--------------> equal
6112 * | | | +----------------> att_end
6113 * | | +---------------------> att_beg
6114 * | +--------------------------> prev
6115 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116 */
6117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6119 /* Iterate through all cookies on this line */
6120
6121 /* find att_beg */
6122 att_beg = prev + 1;
6123 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6124 att_beg++;
6125
6126 /* find att_end : this is the first character after the last non
6127 * space before the equal. It may be equal to hdr_end.
6128 */
6129 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006131 while (equal < hdr_end) {
6132 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006133 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006134 if (http_is_spht[(unsigned char)*equal++])
6135 continue;
6136 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006137 }
6138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6140 * is between <att_beg> and <equal>, both may be identical.
6141 */
6142
6143 /* look for end of cookie if there is an equal sign */
6144 if (equal < hdr_end && *equal == '=') {
6145 /* look for the beginning of the value */
6146 val_beg = equal + 1;
6147 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6148 val_beg++;
6149
6150 /* find the end of the value, respecting quotes */
6151 next = find_cookie_value_end(val_beg, hdr_end);
6152
6153 /* make val_end point to the first white space or delimitor after the value */
6154 val_end = next;
6155 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6156 val_end--;
6157 } else {
6158 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006160
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 /* We have nothing to do with attributes beginning with '$'. However,
6162 * they will automatically be removed if a header before them is removed,
6163 * since they're supposed to be linked together.
6164 */
6165 if (*att_beg == '$')
6166 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006168 /* Ignore cookies with no equal sign */
6169 if (equal == next) {
6170 /* This is not our cookie, so we must preserve it. But if we already
6171 * scheduled another cookie for removal, we cannot remove the
6172 * complete header, but we can remove the previous block itself.
6173 */
6174 preserve_hdr = 1;
6175 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006176 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006177 val_end += delta;
6178 next += delta;
6179 hdr_end += delta;
6180 hdr_next += delta;
6181 cur_hdr->len += delta;
6182 http_msg_move_end(&txn->req, delta);
6183 prev = del_from;
6184 del_from = NULL;
6185 }
6186 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006187 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006188
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006189 /* if there are spaces around the equal sign, we need to
6190 * strip them otherwise we'll get trouble for cookie captures,
6191 * or even for rewrites. Since this happens extremely rarely,
6192 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006193 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6195 int stripped_before = 0;
6196 int stripped_after = 0;
6197
6198 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006199 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200 equal += stripped_before;
6201 val_beg += stripped_before;
6202 }
6203
6204 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006205 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 val_beg += stripped_after;
6207 stripped_before += stripped_after;
6208 }
6209
6210 val_end += stripped_before;
6211 next += stripped_before;
6212 hdr_end += stripped_before;
6213 hdr_next += stripped_before;
6214 cur_hdr->len += stripped_before;
6215 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 /* First, let's see if we want to capture this cookie. We check
6220 * that we don't already have a client side cookie, because we
6221 * can only capture one. Also as an optimisation, we ignore
6222 * cookies shorter than the declared name.
6223 */
6224 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6225 (val_end - att_beg >= t->fe->capture_namelen) &&
6226 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6227 int log_len = val_end - att_beg;
6228
6229 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6230 Alert("HTTP logging : out of memory.\n");
6231 } else {
6232 if (log_len > t->fe->capture_len)
6233 log_len = t->fe->capture_len;
6234 memcpy(txn->cli_cookie, att_beg, log_len);
6235 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238
Willy Tarreaubca99692010-10-06 19:25:55 +02006239 /* Persistence cookies in passive, rewrite or insert mode have the
6240 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006242 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006244 * For cookies in prefix mode, the form is :
6245 *
6246 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 */
6248 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6249 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6250 struct server *srv = t->be->srv;
6251 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6254 * have the server ID between val_beg and delim, and the original cookie between
6255 * delim+1 and val_end. Otherwise, delim==val_end :
6256 *
6257 * Cookie: NAME=SRV; # in all but prefix modes
6258 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6259 * | || || | |+-> next
6260 * | || || | +--> val_end
6261 * | || || +---------> delim
6262 * | || |+------------> val_beg
6263 * | || +-------------> att_end = equal
6264 * | |+-----------------> att_beg
6265 * | +------------------> prev
6266 * +-------------------------> hdr_beg
6267 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268
Willy Tarreau67402132012-05-31 20:40:20 +02006269 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 for (delim = val_beg; delim < val_end; delim++)
6271 if (*delim == COOKIE_DELIM)
6272 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006273 } else {
6274 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006276 /* Now check if the cookie contains a date field, which would
6277 * appear after a vertical bar ('|') just after the server name
6278 * and before the delimiter.
6279 */
6280 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6281 if (vbar1) {
6282 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006283 * right is the last seen date. It is a base64 encoded
6284 * 30-bit value representing the UNIX date since the
6285 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006286 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006287 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006288 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006289 if (val_end - vbar1 >= 5) {
6290 val = b64tos30(vbar1);
6291 if (val > 0)
6292 txn->cookie_last_date = val << 2;
6293 }
6294 /* look for a second vertical bar */
6295 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6296 if (vbar1 && (val_end - vbar1 > 5)) {
6297 val = b64tos30(vbar1 + 1);
6298 if (val > 0)
6299 txn->cookie_first_date = val << 2;
6300 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006301 }
6302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006303
Willy Tarreauf64d1412010-10-07 20:06:11 +02006304 /* if the cookie has an expiration date and the proxy wants to check
6305 * it, then we do that now. We first check if the cookie is too old,
6306 * then only if it has expired. We detect strict overflow because the
6307 * time resolution here is not great (4 seconds). Cookies with dates
6308 * in the future are ignored if their offset is beyond one day. This
6309 * allows an admin to fix timezone issues without expiring everyone
6310 * and at the same time avoids keeping unwanted side effects for too
6311 * long.
6312 */
6313 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006314 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6315 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006316 txn->flags &= ~TX_CK_MASK;
6317 txn->flags |= TX_CK_OLD;
6318 delim = val_beg; // let's pretend we have not found the cookie
6319 txn->cookie_first_date = 0;
6320 txn->cookie_last_date = 0;
6321 }
6322 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006323 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6324 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006325 txn->flags &= ~TX_CK_MASK;
6326 txn->flags |= TX_CK_EXPIRED;
6327 delim = val_beg; // let's pretend we have not found the cookie
6328 txn->cookie_first_date = 0;
6329 txn->cookie_last_date = 0;
6330 }
6331
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 /* Here, we'll look for the first running server which supports the cookie.
6333 * This allows to share a same cookie between several servers, for example
6334 * to dedicate backup servers to specific servers only.
6335 * However, to prevent clients from sticking to cookie-less backup server
6336 * when they have incidentely learned an empty cookie, we simply ignore
6337 * empty cookies and mark them as invalid.
6338 * The same behaviour is applied when persistence must be ignored.
6339 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006340 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 while (srv) {
6344 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6345 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6346 if ((srv->state & SRV_RUNNING) ||
6347 (t->be->options & PR_O_PERSIST) ||
6348 (t->flags & SN_FORCE_PRST)) {
6349 /* we found the server and we can use it */
6350 txn->flags &= ~TX_CK_MASK;
6351 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6352 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006353 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 break;
6355 } else {
6356 /* we found a server, but it's down,
6357 * mark it as such and go on in case
6358 * another one is available.
6359 */
6360 txn->flags &= ~TX_CK_MASK;
6361 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006362 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 srv = srv->next;
6365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366
Willy Tarreauf64d1412010-10-07 20:06:11 +02006367 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006368 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006370 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6371 txn->flags |= TX_CK_UNUSED;
6372 else
6373 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 /* depending on the cookie mode, we may have to either :
6377 * - delete the complete cookie if we're in insert+indirect mode, so that
6378 * the server never sees it ;
6379 * - remove the server id from the cookie value, and tag the cookie as an
6380 * application cookie so that it does not get accidentely removed later,
6381 * if we're in cookie prefix mode
6382 */
Willy Tarreau67402132012-05-31 20:40:20 +02006383 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006384 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385
Willy Tarreau9b28e032012-10-12 23:49:43 +02006386 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 val_end += delta;
6388 next += delta;
6389 hdr_end += delta;
6390 hdr_next += delta;
6391 cur_hdr->len += delta;
6392 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 del_from = NULL;
6395 preserve_hdr = 1; /* we want to keep this cookie */
6396 }
6397 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006398 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 del_from = prev;
6400 }
6401 } else {
6402 /* This is not our cookie, so we must preserve it. But if we already
6403 * scheduled another cookie for removal, we cannot remove the
6404 * complete header, but we can remove the previous block itself.
6405 */
6406 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006407
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006409 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006410 if (att_beg >= del_from)
6411 att_beg += delta;
6412 if (att_end >= del_from)
6413 att_end += delta;
6414 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 val_end += delta;
6416 next += delta;
6417 hdr_end += delta;
6418 hdr_next += delta;
6419 cur_hdr->len += delta;
6420 http_msg_move_end(&txn->req, delta);
6421 prev = del_from;
6422 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 /* Look for the appsession cookie unless persistence must be ignored */
6427 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6428 int cmp_len, value_len;
6429 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006430
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006431 if (t->be->options2 & PR_O2_AS_PFX) {
6432 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6433 value_begin = att_beg + t->be->appsession_name_len;
6434 value_len = val_end - att_beg - t->be->appsession_name_len;
6435 } else {
6436 cmp_len = att_end - att_beg;
6437 value_begin = val_beg;
6438 value_len = val_end - val_beg;
6439 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006440
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 /* let's see if the cookie is our appcookie */
6442 if (cmp_len == t->be->appsession_name_len &&
6443 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6444 manage_client_side_appsession(t, value_begin, value_len);
6445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446 }
6447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 /* continue with next cookie on this header line */
6449 att_beg = next;
6450 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 /* There are no more cookies on this line.
6453 * We may still have one (or several) marked for deletion at the
6454 * end of the line. We must do this now in two ways :
6455 * - if some cookies must be preserved, we only delete from the
6456 * mark to the end of line ;
6457 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006458 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006462 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006463 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464 cur_hdr->len += delta;
6465 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006466 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467
6468 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006469 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6470 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006472 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006475 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476 }
6477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006480 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006481}
6482
6483
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484/* Iterate the same filter through all response headers contained in <rtr>.
6485 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6486 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006487int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006488{
6489 char term;
6490 char *cur_ptr, *cur_end, *cur_next;
6491 int cur_idx, old_idx, last_hdr;
6492 struct http_txn *txn = &t->txn;
6493 struct hdr_idx_elem *cur_hdr;
6494 int len, delta;
6495
6496 last_hdr = 0;
6497
Willy Tarreau9b28e032012-10-12 23:49:43 +02006498 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 old_idx = 0;
6500
6501 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006502 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006504 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 (exp->action == ACT_ALLOW ||
6506 exp->action == ACT_DENY))
6507 return 0;
6508
6509 cur_idx = txn->hdr_idx.v[old_idx].next;
6510 if (!cur_idx)
6511 break;
6512
6513 cur_hdr = &txn->hdr_idx.v[cur_idx];
6514 cur_ptr = cur_next;
6515 cur_end = cur_ptr + cur_hdr->len;
6516 cur_next = cur_end + cur_hdr->cr + 1;
6517
6518 /* Now we have one header between cur_ptr and cur_end,
6519 * and the next header starts at cur_next.
6520 */
6521
6522 /* The annoying part is that pattern matching needs
6523 * that we modify the contents to null-terminate all
6524 * strings before testing them.
6525 */
6526
6527 term = *cur_end;
6528 *cur_end = '\0';
6529
6530 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6531 switch (exp->action) {
6532 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006533 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534 last_hdr = 1;
6535 break;
6536
6537 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006538 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539 last_hdr = 1;
6540 break;
6541
6542 case ACT_REPLACE:
6543 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02006544 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 /* FIXME: if the user adds a newline in the replacement, the
6546 * index will not be recalculated for now, and the new line
6547 * will not be counted as a new header.
6548 */
6549
6550 cur_end += delta;
6551 cur_next += delta;
6552 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 break;
6555
6556 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006557 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 cur_next += delta;
6559
Willy Tarreaufa355d42009-11-29 18:12:29 +01006560 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6562 txn->hdr_idx.used--;
6563 cur_hdr->len = 0;
6564 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006565 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 break;
6567
6568 }
6569 }
6570 if (cur_end)
6571 *cur_end = term; /* restore the string terminator */
6572
6573 /* keep the link from this header to next one in case of later
6574 * removal of next header.
6575 */
6576 old_idx = cur_idx;
6577 }
6578 return 0;
6579}
6580
6581
6582/* Apply the filter to the status line in the response buffer <rtr>.
6583 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6584 * or -1 if a replacement resulted in an invalid status line.
6585 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006586int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587{
6588 char term;
6589 char *cur_ptr, *cur_end;
6590 int done;
6591 struct http_txn *txn = &t->txn;
6592 int len, delta;
6593
6594
Willy Tarreau3d300592007-03-18 18:34:41 +01006595 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006597 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 (exp->action == ACT_ALLOW ||
6599 exp->action == ACT_DENY))
6600 return 0;
6601 else if (exp->action == ACT_REMOVE)
6602 return 0;
6603
6604 done = 0;
6605
Willy Tarreau9b28e032012-10-12 23:49:43 +02006606 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006607 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608
6609 /* Now we have the status line between cur_ptr and cur_end */
6610
6611 /* The annoying part is that pattern matching needs
6612 * that we modify the contents to null-terminate all
6613 * strings before testing them.
6614 */
6615
6616 term = *cur_end;
6617 *cur_end = '\0';
6618
6619 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6620 switch (exp->action) {
6621 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006622 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 done = 1;
6624 break;
6625
6626 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006627 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 done = 1;
6629 break;
6630
6631 case ACT_REPLACE:
6632 *cur_end = term; /* restore the string terminator */
6633 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02006634 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 /* FIXME: if the user adds a newline in the replacement, the
6636 * index will not be recalculated for now, and the new line
6637 * will not be counted as a new header.
6638 */
6639
Willy Tarreaufa355d42009-11-29 18:12:29 +01006640 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006642 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006643 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644 cur_ptr, cur_end + 1,
6645 NULL, NULL);
6646 if (unlikely(!cur_end))
6647 return -1;
6648
6649 /* we have a full respnse and we know that we have either a CR
6650 * or an LF at <ptr>.
6651 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006652 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006653 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654 /* there is no point trying this regex on headers */
6655 return 1;
6656 }
6657 }
6658 *cur_end = term; /* restore the string terminator */
6659 return done;
6660}
6661
6662
6663
6664/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006665 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6667 * unparsable response.
6668 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006669int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006671 struct http_txn *txn = &s->txn;
6672 struct hdr_exp *exp;
6673
6674 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 int ret;
6676
6677 /*
6678 * The interleaving of transformations and verdicts
6679 * makes it difficult to decide to continue or stop
6680 * the evaluation.
6681 */
6682
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006683 if (txn->flags & TX_SVDENY)
6684 break;
6685
Willy Tarreau3d300592007-03-18 18:34:41 +01006686 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6688 exp->action == ACT_PASS)) {
6689 exp = exp->next;
6690 continue;
6691 }
6692
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006693 /* if this filter had a condition, evaluate it now and skip to
6694 * next filter if the condition does not match.
6695 */
6696 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006697 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006698 ret = acl_pass(ret);
6699 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6700 ret = !ret;
6701 if (!ret)
6702 continue;
6703 }
6704
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006706 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 if (unlikely(ret < 0))
6708 return -1;
6709
6710 if (likely(ret == 0)) {
6711 /* The filter did not match the response, it can be
6712 * iterated through all headers.
6713 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006714 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 }
6717 return 0;
6718}
6719
6720
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006722 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006723 * desirable to call it only when needed. This function is also used when we
6724 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006726void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727{
6728 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006729 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006730 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006732 char *hdr_beg, *hdr_end, *hdr_next;
6733 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734
Willy Tarreaua15645d2007-03-18 16:22:39 +01006735 /* Iterate through the headers.
6736 * we start with the start line.
6737 */
6738 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006739 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
6741 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6742 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006743 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744
6745 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 hdr_beg = hdr_next;
6747 hdr_end = hdr_beg + cur_hdr->len;
6748 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 /* We have one full header between hdr_beg and hdr_end, and the
6751 * next header starts at hdr_next. We're only interested in
6752 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 */
6754
Willy Tarreau24581ba2010-08-31 22:39:35 +02006755 is_cookie2 = 0;
6756 prev = hdr_beg + 10;
6757 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006758 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006759 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6760 if (!val) {
6761 old_idx = cur_idx;
6762 continue;
6763 }
6764 is_cookie2 = 1;
6765 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 }
6767
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6769 * <prev> points to the colon.
6770 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006771 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772
Willy Tarreau24581ba2010-08-31 22:39:35 +02006773 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6774 * check-cache is enabled) and we are not interested in checking
6775 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006776 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006777 if (t->be->cookie_name == NULL &&
6778 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006779 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780 return;
6781
Willy Tarreau24581ba2010-08-31 22:39:35 +02006782 /* OK so now we know we have to process this response cookie.
6783 * The format of the Set-Cookie header is slightly different
6784 * from the format of the Cookie header in that it does not
6785 * support the comma as a cookie delimiter (thus the header
6786 * cannot be folded) because the Expires attribute described in
6787 * the original Netscape's spec may contain an unquoted date
6788 * with a comma inside. We have to live with this because
6789 * many browsers don't support Max-Age and some browsers don't
6790 * support quoted strings. However the Set-Cookie2 header is
6791 * clean.
6792 *
6793 * We have to keep multiple pointers in order to support cookie
6794 * removal at the beginning, middle or end of header without
6795 * corrupting the header (in case of set-cookie2). A special
6796 * pointer, <scav> points to the beginning of the set-cookie-av
6797 * fields after the first semi-colon. The <next> pointer points
6798 * either to the end of line (set-cookie) or next unquoted comma
6799 * (set-cookie2). All of these headers are valid :
6800 *
6801 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6802 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6803 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6804 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6805 * | | | | | | | | | |
6806 * | | | | | | | | +-> next hdr_end <--+
6807 * | | | | | | | +------------> scav
6808 * | | | | | | +--------------> val_end
6809 * | | | | | +--------------------> val_beg
6810 * | | | | +----------------------> equal
6811 * | | | +------------------------> att_end
6812 * | | +----------------------------> att_beg
6813 * | +------------------------------> prev
6814 * +-----------------------------------------> hdr_beg
6815 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 for (; prev < hdr_end; prev = next) {
6818 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 /* find att_beg */
6821 att_beg = prev + 1;
6822 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6823 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 /* find att_end : this is the first character after the last non
6826 * space before the equal. It may be equal to hdr_end.
6827 */
6828 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829
Willy Tarreau24581ba2010-08-31 22:39:35 +02006830 while (equal < hdr_end) {
6831 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6832 break;
6833 if (http_is_spht[(unsigned char)*equal++])
6834 continue;
6835 att_end = equal;
6836 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837
Willy Tarreau24581ba2010-08-31 22:39:35 +02006838 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6839 * is between <att_beg> and <equal>, both may be identical.
6840 */
6841
6842 /* look for end of cookie if there is an equal sign */
6843 if (equal < hdr_end && *equal == '=') {
6844 /* look for the beginning of the value */
6845 val_beg = equal + 1;
6846 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6847 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 /* find the end of the value, respecting quotes */
6850 next = find_cookie_value_end(val_beg, hdr_end);
6851
6852 /* make val_end point to the first white space or delimitor after the value */
6853 val_end = next;
6854 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6855 val_end--;
6856 } else {
6857 /* <equal> points to next comma, semi-colon or EOL */
6858 val_beg = val_end = next = equal;
6859 }
6860
6861 if (next < hdr_end) {
6862 /* Set-Cookie2 supports multiple cookies, and <next> points to
6863 * a colon or semi-colon before the end. So skip all attr-value
6864 * pairs and look for the next comma. For Set-Cookie, since
6865 * commas are permitted in values, skip to the end.
6866 */
6867 if (is_cookie2)
6868 next = find_hdr_value_end(next, hdr_end);
6869 else
6870 next = hdr_end;
6871 }
6872
6873 /* Now everything is as on the diagram above */
6874
6875 /* Ignore cookies with no equal sign */
6876 if (equal == val_end)
6877 continue;
6878
6879 /* If there are spaces around the equal sign, we need to
6880 * strip them otherwise we'll get trouble for cookie captures,
6881 * or even for rewrites. Since this happens extremely rarely,
6882 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6885 int stripped_before = 0;
6886 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887
Willy Tarreau24581ba2010-08-31 22:39:35 +02006888 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006889 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006890 equal += stripped_before;
6891 val_beg += stripped_before;
6892 }
6893
6894 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006895 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 val_beg += stripped_after;
6897 stripped_before += stripped_after;
6898 }
6899
6900 val_end += stripped_before;
6901 next += stripped_before;
6902 hdr_end += stripped_before;
6903 hdr_next += stripped_before;
6904 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006905 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 }
6907
6908 /* First, let's see if we want to capture this cookie. We check
6909 * that we don't already have a server side cookie, because we
6910 * can only capture one. Also as an optimisation, we ignore
6911 * cookies shorter than the declared name.
6912 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006913 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006914 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006915 (val_end - att_beg >= t->fe->capture_namelen) &&
6916 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6917 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006918 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 Alert("HTTP logging : out of memory.\n");
6920 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006921 else {
6922 if (log_len > t->fe->capture_len)
6923 log_len = t->fe->capture_len;
6924 memcpy(txn->srv_cookie, att_beg, log_len);
6925 txn->srv_cookie[log_len] = 0;
6926 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006927 }
6928
Willy Tarreau827aee92011-03-10 16:55:02 +01006929 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006931 if (!(t->flags & SN_IGNORE_PRST) &&
6932 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6933 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006934 /* assume passive cookie by default */
6935 txn->flags &= ~TX_SCK_MASK;
6936 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006937
6938 /* If the cookie is in insert mode on a known server, we'll delete
6939 * this occurrence because we'll insert another one later.
6940 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006941 * a direct access.
6942 */
Willy Tarreau67402132012-05-31 20:40:20 +02006943 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006944 /* The "preserve" flag was set, we don't want to touch the
6945 * server's cookie.
6946 */
6947 }
Willy Tarreau67402132012-05-31 20:40:20 +02006948 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
6949 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 /* this cookie must be deleted */
6951 if (*prev == ':' && next == hdr_end) {
6952 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006953 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6955 txn->hdr_idx.used--;
6956 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006957 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958 hdr_next += delta;
6959 http_msg_move_end(&txn->rsp, delta);
6960 /* note: while both invalid now, <next> and <hdr_end>
6961 * are still equal, so the for() will stop as expected.
6962 */
6963 } else {
6964 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006965 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 next = prev;
6967 hdr_end += delta;
6968 hdr_next += delta;
6969 cur_hdr->len += delta;
6970 http_msg_move_end(&txn->rsp, delta);
6971 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006972 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006973 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006974 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975 }
Willy Tarreau67402132012-05-31 20:40:20 +02006976 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 * with this server since we know it.
6979 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006980 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 next += delta;
6982 hdr_end += delta;
6983 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006985 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986
Willy Tarreauf1348312010-10-07 15:54:11 +02006987 txn->flags &= ~TX_SCK_MASK;
6988 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 }
Willy Tarreaua0590312012-06-06 16:07:00 +02006990 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006992 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006994 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 next += delta;
6996 hdr_end += delta;
6997 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006999 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000
Willy Tarreau827aee92011-03-10 16:55:02 +01007001 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007002 txn->flags &= ~TX_SCK_MASK;
7003 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 }
7005 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007006 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7007 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007008 int cmp_len, value_len;
7009 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010
Cyril Bontéb21570a2009-11-29 20:04:48 +01007011 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007012 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7013 value_begin = att_beg + t->be->appsession_name_len;
7014 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007015 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 cmp_len = att_end - att_beg;
7017 value_begin = val_beg;
7018 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007019 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007020
Cyril Bonté17530c32010-04-06 21:11:10 +02007021 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7023 /* free a possibly previously allocated memory */
7024 pool_free2(apools.sessid, txn->sessid);
7025
Cyril Bontéb21570a2009-11-29 20:04:48 +01007026 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007027 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007028 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7029 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7030 return;
7031 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007032 memcpy(txn->sessid, value_begin, value_len);
7033 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007035 }
7036 /* that's done for this cookie, check the next one on the same
7037 * line when next != hdr_end (only if is_cookie2).
7038 */
7039 }
7040 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007042 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007043
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007044 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007045 appsess *asession = NULL;
7046 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007047 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007048 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007049 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007050 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7051 Alert("Not enough Memory process_srv():asession:calloc().\n");
7052 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7053 return;
7054 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007055 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7056
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007057 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7058 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7059 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007060 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007061 return;
7062 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007063 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007064 asession->sessid[t->be->appsession_len] = 0;
7065
Willy Tarreau827aee92011-03-10 16:55:02 +01007066 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007067 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007068 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007069 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007070 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007071 return;
7072 }
7073 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007074 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007075
7076 asession->request_count = 0;
7077 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7078 }
7079
7080 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7081 asession->request_count++;
7082 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083}
7084
7085
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086/*
7087 * Check if response is cacheable or not. Updates t->flags.
7088 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007089void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007090{
7091 struct http_txn *txn = &t->txn;
7092 char *p1, *p2;
7093
7094 char *cur_ptr, *cur_end, *cur_next;
7095 int cur_idx;
7096
Willy Tarreau5df51872007-11-25 16:20:08 +01007097 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 return;
7099
7100 /* Iterate through the headers.
7101 * we start with the start line.
7102 */
7103 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007104 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105
7106 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7107 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007108 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007109
7110 cur_hdr = &txn->hdr_idx.v[cur_idx];
7111 cur_ptr = cur_next;
7112 cur_end = cur_ptr + cur_hdr->len;
7113 cur_next = cur_end + cur_hdr->cr + 1;
7114
7115 /* We have one full header between cur_ptr and cur_end, and the
7116 * next header starts at cur_next. We're only interested in
7117 * "Cookie:" headers.
7118 */
7119
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007120 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7121 if (val) {
7122 if ((cur_end - (cur_ptr + val) >= 8) &&
7123 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7124 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7125 return;
7126 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127 }
7128
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007129 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7130 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 continue;
7132
7133 /* OK, right now we know we have a cache-control header at cur_ptr */
7134
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007135 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136
7137 if (p1 >= cur_end) /* no more info */
7138 continue;
7139
7140 /* p1 is at the beginning of the value */
7141 p2 = p1;
7142
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007143 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 p2++;
7145
7146 /* we have a complete value between p1 and p2 */
7147 if (p2 < cur_end && *p2 == '=') {
7148 /* we have something of the form no-cache="set-cookie" */
7149 if ((cur_end - p1 >= 21) &&
7150 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7151 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007152 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 continue;
7154 }
7155
7156 /* OK, so we know that either p2 points to the end of string or to a comma */
7157 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7158 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7159 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7160 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007161 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 return;
7163 }
7164
7165 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007166 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167 continue;
7168 }
7169 }
7170}
7171
7172
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173/*
7174 * Try to retrieve a known appsession in the URI, then the associated server.
7175 * If the server is found, it's assigned to the session.
7176 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007177void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007178{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007179 char *end_params, *first_param, *cur_param, *next_param;
7180 char separator;
7181 int value_len;
7182
7183 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007185 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007186 (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 +01007187 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007188 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189
Cyril Bontéb21570a2009-11-29 20:04:48 +01007190 first_param = NULL;
7191 switch (mode) {
7192 case PR_O2_AS_M_PP:
7193 first_param = memchr(begin, ';', len);
7194 break;
7195 case PR_O2_AS_M_QS:
7196 first_param = memchr(begin, '?', len);
7197 break;
7198 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199
Cyril Bontéb21570a2009-11-29 20:04:48 +01007200 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007203
Cyril Bontéb21570a2009-11-29 20:04:48 +01007204 switch (mode) {
7205 case PR_O2_AS_M_PP:
7206 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7207 end_params = (char *) begin + len;
7208 }
7209 separator = ';';
7210 break;
7211 case PR_O2_AS_M_QS:
7212 end_params = (char *) begin + len;
7213 separator = '&';
7214 break;
7215 default:
7216 /* unknown mode, shouldn't happen */
7217 return;
7218 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219
Cyril Bontéb21570a2009-11-29 20:04:48 +01007220 cur_param = next_param = end_params;
7221 while (cur_param > first_param) {
7222 cur_param--;
7223 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7224 /* let's see if this is the appsession parameter */
7225 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7226 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7227 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7228 /* Cool... it's the right one */
7229 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7230 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7231 if (value_len > 0) {
7232 manage_client_side_appsession(t, cur_param, value_len);
7233 }
7234 break;
7235 }
7236 next_param = cur_param;
7237 }
7238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007240 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007241 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243}
7244
Willy Tarreaub2513902006-12-17 14:52:38 +01007245/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007246 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007247 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007248 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007249 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007250 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007251 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007252 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007253 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007254int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007255{
7256 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007257 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007258 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007259 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007260
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007261 if (!uri_auth)
7262 return 0;
7263
Cyril Bonté70be45d2010-10-12 00:14:35 +02007264 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007265 return 0;
7266
Willy Tarreau295a8372011-03-10 11:25:07 +01007267 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007268 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007270 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007271 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007272 return 0;
7273
Willy Tarreau3a215be2012-03-09 21:39:51 +01007274 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007275 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007276 return 0;
7277
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007278 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007279 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007280 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007281 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007282 break;
7283 }
7284 h++;
7285 }
7286
7287 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007288 h = uri + uri_auth->uri_len;
7289 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007290 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007291 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007292 break;
7293 }
7294 h++;
7295 }
7296 }
7297
Willy Tarreau3a215be2012-03-09 21:39:51 +01007298 h = uri + uri_auth->uri_len;
7299 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007300 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007301 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007302 break;
7303 }
7304 h++;
7305 }
7306
Willy Tarreau3a215be2012-03-09 21:39:51 +01007307 h = uri + uri_auth->uri_len;
7308 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007309 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007310 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007311 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007312 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007313 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7314 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7315 si->applet.ctx.stats.st_code = i;
7316 break;
7317 }
7318 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007319 break;
7320 }
7321 h++;
7322 }
7323
Willy Tarreau295a8372011-03-10 11:25:07 +01007324 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007325
Willy Tarreaub2513902006-12-17 14:52:38 +01007326 return 1;
7327}
7328
Willy Tarreau4076a152009-04-02 15:18:36 +02007329/*
7330 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007331 * By default it tries to report the error position as msg->err_pos. However if
7332 * this one is not set, it will then report msg->next, which is the last known
7333 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007334 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007335 */
7336void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007337 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007338 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007339{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007340 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007341 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007342
Willy Tarreau9b28e032012-10-12 23:49:43 +02007343 es->len = MIN(chn->buf->i, sizeof(es->buf));
7344 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007345 len1 = MIN(len1, es->len);
7346 len2 = es->len - len1; /* remaining data if buffer wraps */
7347
Willy Tarreau9b28e032012-10-12 23:49:43 +02007348 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007349 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007350 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007351
Willy Tarreau4076a152009-04-02 15:18:36 +02007352 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007353 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007354 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007355 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007356
Willy Tarreau4076a152009-04-02 15:18:36 +02007357 es->when = date; // user-visible date
7358 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007359 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007360 es->oe = other_end;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007361 es->src = s->req->prod->conn.addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007362 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007363 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007364 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007365 es->s_flags = s->flags;
7366 es->t_flags = s->txn.flags;
7367 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007368 es->b_out = chn->buf->o;
7369 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007370 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007371 es->m_clen = msg->chunk_len;
7372 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007373}
Willy Tarreaub2513902006-12-17 14:52:38 +01007374
Willy Tarreau294c4732011-12-16 21:35:50 +01007375/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7376 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7377 * performed over the whole headers. Otherwise it must contain a valid header
7378 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7379 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7380 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7381 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7382 * -1.
7383 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007384 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007385unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007386 struct hdr_idx *idx, int occ,
7387 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007388{
Willy Tarreau294c4732011-12-16 21:35:50 +01007389 struct hdr_ctx local_ctx;
7390 char *ptr_hist[MAX_HDR_HISTORY];
7391 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007392 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007393 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007394
Willy Tarreau294c4732011-12-16 21:35:50 +01007395 if (!ctx) {
7396 local_ctx.idx = 0;
7397 ctx = &local_ctx;
7398 }
7399
Willy Tarreaubce70882009-09-07 11:51:47 +02007400 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007401 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007402 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007403 occ--;
7404 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007405 *vptr = ctx->line + ctx->val;
7406 *vlen = ctx->vlen;
7407 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007408 }
7409 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007410 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007411 }
7412
7413 /* negative occurrence, we scan all the list then walk back */
7414 if (-occ > MAX_HDR_HISTORY)
7415 return 0;
7416
Willy Tarreau294c4732011-12-16 21:35:50 +01007417 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007418 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007419 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7420 len_hist[hist_ptr] = ctx->vlen;
7421 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007422 hist_ptr = 0;
7423 found++;
7424 }
7425 if (-occ > found)
7426 return 0;
7427 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7428 * find occurrence -occ, so we have to check [hist_ptr+occ].
7429 */
7430 hist_ptr += occ;
7431 if (hist_ptr >= MAX_HDR_HISTORY)
7432 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007433 *vptr = ptr_hist[hist_ptr];
7434 *vlen = len_hist[hist_ptr];
7435 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007436}
7437
Willy Tarreaubaaee002006-06-26 02:48:02 +02007438/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007439 * Print a debug line with a header. Always stop at the first CR or LF char,
7440 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7441 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442 */
7443void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7444{
7445 int len, max;
7446 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007447 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreaue92693a2012-09-24 21:13:39 +02007448
7449 for (max = 0; start + max < end; max++)
7450 if (start[max] == '\r' || start[max] == '\n')
7451 break;
7452
7453 UBOUND(max, trashlen - len - 3);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454 len += strlcpy2(trash + len, start, max + 1);
7455 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007456 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007457}
7458
Willy Tarreau0937bc42009-12-22 15:03:09 +01007459/*
7460 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7461 * the required fields are properly allocated and that we only need to (re)init
7462 * them. This should be used before processing any new request.
7463 */
7464void http_init_txn(struct session *s)
7465{
7466 struct http_txn *txn = &s->txn;
7467 struct proxy *fe = s->fe;
7468
7469 txn->flags = 0;
7470 txn->status = -1;
7471
William Lallemand5f232402012-04-05 18:02:55 +02007472 global.req_count++;
7473
Willy Tarreauf64d1412010-10-07 20:06:11 +02007474 txn->cookie_first_date = 0;
7475 txn->cookie_last_date = 0;
7476
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007477 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007478 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007479 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007480 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007481 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007482 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007483 txn->req.chunk_len = 0LL;
7484 txn->req.body_len = 0LL;
7485 txn->rsp.chunk_len = 0LL;
7486 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007487 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7488 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007489 txn->req.chn = s->req;
7490 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007491
7492 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007493
7494 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7495 if (fe->options2 & PR_O2_REQBUG_OK)
7496 txn->req.err_pos = -1; /* let buggy requests pass */
7497
Willy Tarreau46023632010-01-07 22:51:47 +01007498 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007499 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7500
Willy Tarreau46023632010-01-07 22:51:47 +01007501 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007502 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7503
7504 if (txn->hdr_idx.v)
7505 hdr_idx_init(&txn->hdr_idx);
7506}
7507
7508/* to be used at the end of a transaction */
7509void http_end_txn(struct session *s)
7510{
7511 struct http_txn *txn = &s->txn;
7512
7513 /* these ones will have been dynamically allocated */
7514 pool_free2(pool2_requri, txn->uri);
7515 pool_free2(pool2_capture, txn->cli_cookie);
7516 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007517 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007518 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007519
William Lallemanda73203e2012-03-12 12:48:57 +01007520 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007521 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007522 txn->uri = NULL;
7523 txn->srv_cookie = NULL;
7524 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007525
7526 if (txn->req.cap) {
7527 struct cap_hdr *h;
7528 for (h = s->fe->req_cap; h; h = h->next)
7529 pool_free2(h->pool, txn->req.cap[h->index]);
7530 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7531 }
7532
7533 if (txn->rsp.cap) {
7534 struct cap_hdr *h;
7535 for (h = s->fe->rsp_cap; h; h = h->next)
7536 pool_free2(h->pool, txn->rsp.cap[h->index]);
7537 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7538 }
7539
Willy Tarreau0937bc42009-12-22 15:03:09 +01007540}
7541
7542/* to be used at the end of a transaction to prepare a new one */
7543void http_reset_txn(struct session *s)
7544{
7545 http_end_txn(s);
7546 http_init_txn(s);
7547
7548 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007549 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007550 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007551 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007552 /* re-init store persistence */
7553 s->store_count = 0;
7554
Willy Tarreau0937bc42009-12-22 15:03:09 +01007555 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007556
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007557 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007558
Willy Tarreau739cfba2010-01-25 23:11:14 +01007559 /* We must trim any excess data from the response buffer, because we
7560 * may have blocked an invalid response from a server that we don't
7561 * want to accidentely forward once we disable the analysers, nor do
7562 * we want those data to come along with next response. A typical
7563 * example of such data would be from a buggy server responding to
7564 * a HEAD with some data, or sending more than the advertised
7565 * content-length.
7566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007567 if (unlikely(s->rep->buf->i))
7568 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007569
Willy Tarreau0937bc42009-12-22 15:03:09 +01007570 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007571 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007572
Willy Tarreaud04e8582010-05-31 12:31:35 +02007573 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007574 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007575
7576 s->req->rex = TICK_ETERNITY;
7577 s->req->wex = TICK_ETERNITY;
7578 s->req->analyse_exp = TICK_ETERNITY;
7579 s->rep->rex = TICK_ETERNITY;
7580 s->rep->wex = TICK_ETERNITY;
7581 s->rep->analyse_exp = TICK_ETERNITY;
7582}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583
Willy Tarreauff011f22011-01-06 17:51:27 +01007584void free_http_req_rules(struct list *r) {
7585 struct http_req_rule *tr, *pr;
7586
7587 list_for_each_entry_safe(pr, tr, r, list) {
7588 LIST_DEL(&pr->list);
7589 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7590 free(pr->http_auth.realm);
7591
7592 free(pr);
7593 }
7594}
7595
7596struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7597{
7598 struct http_req_rule *rule;
7599 int cur_arg;
7600
7601 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7602 if (!rule) {
7603 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7604 return NULL;
7605 }
7606
7607 if (!*args[0]) {
7608 goto req_error_parsing;
7609 } else if (!strcmp(args[0], "allow")) {
7610 rule->action = HTTP_REQ_ACT_ALLOW;
7611 cur_arg = 1;
7612 } else if (!strcmp(args[0], "deny")) {
7613 rule->action = HTTP_REQ_ACT_DENY;
7614 cur_arg = 1;
7615 } else if (!strcmp(args[0], "auth")) {
7616 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7617 cur_arg = 1;
7618
7619 while(*args[cur_arg]) {
7620 if (!strcmp(args[cur_arg], "realm")) {
7621 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7622 cur_arg+=2;
7623 continue;
7624 } else
7625 break;
7626 }
7627 } else {
7628req_error_parsing:
7629 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7630 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7631 return NULL;
7632 }
7633
7634 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7635 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007636 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007637
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007638 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7639 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7640 file, linenum, args[0], errmsg);
7641 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007642 return NULL;
7643 }
7644 rule->cond = cond;
7645 }
7646 else if (*args[cur_arg]) {
7647 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7648 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7649 file, linenum, args[0], args[cur_arg]);
7650 return NULL;
7651 }
7652
7653 return rule;
7654}
7655
Willy Tarreau8797c062007-05-07 00:55:35 +02007656/************************************************************************/
7657/* The code below is dedicated to ACL parsing and matching */
7658/************************************************************************/
7659
7660
Willy Tarreau14174bc2012-04-16 14:34:04 +02007661/* This function ensures that the prerequisites for an L7 fetch are ready,
7662 * which means that a request or response is ready. If some data is missing,
7663 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007664 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7665 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007666 *
7667 * The function returns :
7668 * 0 if some data is missing or if the requested data cannot be fetched
7669 * -1 if it is certain that we'll never have any HTTP message there
7670 * 1 if an HTTP message is ready
7671 */
7672static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007673acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007674 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007675{
7676 struct http_txn *txn = l7;
7677 struct http_msg *msg = &txn->req;
7678
7679 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7680 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7681 */
7682
7683 if (unlikely(!s || !txn))
7684 return 0;
7685
7686 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007687 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007688
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007689 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007690 if (unlikely(!s->req))
7691 return 0;
7692
7693 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007694 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007695 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007696 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007697 return -1;
7698 }
7699
7700 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007701 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007702 http_msg_analyzer(msg, &txn->hdr_idx);
7703
7704 /* Still no valid request ? */
7705 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007706 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007707 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007708 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007709 return -1;
7710 }
7711 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007712 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007713 return 0;
7714 }
7715
7716 /* OK we just got a valid HTTP request. We have some minor
7717 * preparation to perform so that further checks can rely
7718 * on HTTP tests.
7719 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007720 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007721 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7722 s->flags |= SN_REDIRECTABLE;
7723
7724 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007725 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007726 return -1;
7727 }
7728 }
7729
Willy Tarreau24e32d82012-04-23 23:55:44 +02007730 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007731 return 0; /* data might have moved and indexes changed */
7732
7733 /* otherwise everything's ready for the request */
7734 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007735 else {
7736 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007737 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7738 return 0;
7739 }
7740
7741 /* everything's OK */
7742 return 1;
7743}
Willy Tarreau8797c062007-05-07 00:55:35 +02007744
Willy Tarreauc0239e02012-04-16 14:42:55 +02007745#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007746 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 +02007747
Willy Tarreau24e32d82012-04-23 23:55:44 +02007748#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007749 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 +02007750
Willy Tarreau8797c062007-05-07 00:55:35 +02007751
7752/* 1. Check on METHOD
7753 * We use the pre-parsed method if it is known, and store its number as an
7754 * integer. If it is unknown, we use the pointer and the length.
7755 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007756static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007757{
7758 int len, meth;
7759
Willy Tarreauae8b7962007-06-09 23:10:04 +02007760 len = strlen(*text);
7761 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007762
7763 pattern->val.i = meth;
7764 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007765 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007766 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007767 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007768 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007769 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007770 pattern->len = len;
7771 }
7772 return 1;
7773}
7774
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007775/* This function fetches the method of current HTTP request and stores
7776 * it in the global pattern struct as a chunk. There are two possibilities :
7777 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7778 * in <len> and <ptr> is NULL ;
7779 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7780 * <len> to its length.
7781 * This is intended to be used with acl_match_meth() only.
7782 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007783static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007784acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007785 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007786{
7787 int meth;
7788 struct http_txn *txn = l7;
7789
Willy Tarreau24e32d82012-04-23 23:55:44 +02007790 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007791
Willy Tarreau8797c062007-05-07 00:55:35 +02007792 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007793 smp->type = SMP_T_UINT;
7794 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007795 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007796 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7797 /* ensure the indexes are not affected */
7798 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007799 smp->type = SMP_T_CSTR;
7800 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007801 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007802 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007803 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007804 return 1;
7805}
7806
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007807/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007808static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007809{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007810 int icase;
7811
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007812
Willy Tarreauf853c462012-04-23 18:53:56 +02007813 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007814 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007815 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007816 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007817 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007818 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007819
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007820 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7821 if (pattern->val.i != HTTP_METH_OTHER)
7822 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007823
7824 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007825 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007826 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007827
7828 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007829 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7830 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007831 return ACL_PAT_FAIL;
7832 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007833}
7834
7835/* 2. Check on Request/Status Version
7836 * We simply compare strings here.
7837 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007838static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007839{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007840 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007841 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007842 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007843 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007844 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007845 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007846 return 1;
7847}
7848
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007849static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007850acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007851 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007852{
7853 struct http_txn *txn = l7;
7854 char *ptr;
7855 int len;
7856
Willy Tarreauc0239e02012-04-16 14:42:55 +02007857 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007858
Willy Tarreau8797c062007-05-07 00:55:35 +02007859 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007860 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007861
7862 while ((len-- > 0) && (*ptr++ != '/'));
7863 if (len <= 0)
7864 return 0;
7865
Willy Tarreauf853c462012-04-23 18:53:56 +02007866 smp->type = SMP_T_CSTR;
7867 smp->data.str.str = ptr;
7868 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007869
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007870 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007871 return 1;
7872}
7873
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007874static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007875acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007876 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007877{
7878 struct http_txn *txn = l7;
7879 char *ptr;
7880 int len;
7881
Willy Tarreauc0239e02012-04-16 14:42:55 +02007882 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007883
Willy Tarreau8797c062007-05-07 00:55:35 +02007884 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007885 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007886
7887 while ((len-- > 0) && (*ptr++ != '/'));
7888 if (len <= 0)
7889 return 0;
7890
Willy Tarreauf853c462012-04-23 18:53:56 +02007891 smp->type = SMP_T_CSTR;
7892 smp->data.str.str = ptr;
7893 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007894
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007895 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007896 return 1;
7897}
7898
7899/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007900static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007901acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007902 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007903{
7904 struct http_txn *txn = l7;
7905 char *ptr;
7906 int len;
7907
Willy Tarreauc0239e02012-04-16 14:42:55 +02007908 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007909
Willy Tarreau8797c062007-05-07 00:55:35 +02007910 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007911 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007912
Willy Tarreauf853c462012-04-23 18:53:56 +02007913 smp->type = SMP_T_UINT;
7914 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007915 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007916 return 1;
7917}
7918
7919/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007920static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007921smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007922 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007923{
7924 struct http_txn *txn = l7;
7925
Willy Tarreauc0239e02012-04-16 14:42:55 +02007926 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007927
Willy Tarreauf853c462012-04-23 18:53:56 +02007928 smp->type = SMP_T_CSTR;
7929 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007930 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007931 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007932 return 1;
7933}
7934
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007935static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007936smp_fetch_url_ip(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)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007938{
7939 struct http_txn *txn = l7;
7940
Willy Tarreauc0239e02012-04-16 14:42:55 +02007941 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007942
7943 /* Parse HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007944 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to);
Willy Tarreau986a9d22012-08-30 21:11:38 +02007945 if (((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01007946 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007947 smp->type = SMP_T_IPV4;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007948 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007949
7950 /*
7951 * If we are parsing url in frontend space, we prepare backend stage
7952 * to not parse again the same url ! optimization lazyness...
7953 */
7954 if (px->options & PR_O_HTTP_PROXY)
7955 l4->flags |= SN_ADDR_SET;
7956
Willy Tarreau37406352012-04-23 16:16:37 +02007957 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007958 return 1;
7959}
7960
7961static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007962smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007963 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007964{
7965 struct http_txn *txn = l7;
7966
Willy Tarreauc0239e02012-04-16 14:42:55 +02007967 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007968
7969 /* Same optimization as url_ip */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007970 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02007971 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007972 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007973
7974 if (px->options & PR_O_HTTP_PROXY)
7975 l4->flags |= SN_ADDR_SET;
7976
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007977 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007978 return 1;
7979}
7980
Willy Tarreau185b5c42012-04-26 15:11:51 +02007981/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7982 * Accepts an optional argument of type string containing the header field name,
7983 * and an optional argument of type signed or unsigned integer to request an
7984 * explicit occurrence of the header. Note that in the event of a missing name,
7985 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007986 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007987static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007988smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007989 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007990{
7991 struct http_txn *txn = l7;
7992 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007993 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007994 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007995 int occ = 0;
7996 const char *name_str = NULL;
7997 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007998
Willy Tarreau185b5c42012-04-26 15:11:51 +02007999 if (args) {
8000 if (args[0].type != ARGT_STR)
8001 return 0;
8002 name_str = args[0].data.str.str;
8003 name_len = args[0].data.str.len;
8004
8005 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8006 occ = args[1].data.uint;
8007 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008008
Willy Tarreaue333ec92012-04-16 16:26:40 +02008009 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008010
Willy Tarreau185b5c42012-04-26 15:11:51 +02008011 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008012 /* search for header from the beginning */
8013 ctx->idx = 0;
8014
Willy Tarreau185b5c42012-04-26 15:11:51 +02008015 if (!occ && !(opt & SMP_OPT_ITERATE))
8016 /* no explicit occurrence and single fetch => last header by default */
8017 occ = -1;
8018
8019 if (!occ)
8020 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008021 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008022
Willy Tarreau185b5c42012-04-26 15:11:51 +02008023 smp->type = SMP_T_CSTR;
8024 smp->flags |= SMP_F_VOL_HDR;
8025 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 +02008026 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008027
Willy Tarreau37406352012-04-23 16:16:37 +02008028 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008029 return 0;
8030}
8031
Willy Tarreauc11416f2007-06-17 16:58:38 +02008032/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008033 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008034 */
8035static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008036smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008037 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008038{
8039 struct http_txn *txn = l7;
8040 struct hdr_idx *idx = &txn->hdr_idx;
8041 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008042 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008043 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008044
Willy Tarreau24e32d82012-04-23 23:55:44 +02008045 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008046 return 0;
8047
Willy Tarreaue333ec92012-04-16 16:26:40 +02008048 CHECK_HTTP_MESSAGE_FIRST();
8049
Willy Tarreau33a7e692007-06-10 19:45:56 +02008050 ctx.idx = 0;
8051 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008052 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008053 cnt++;
8054
Willy Tarreauf853c462012-04-23 18:53:56 +02008055 smp->type = SMP_T_UINT;
8056 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008057 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008058 return 1;
8059}
8060
Willy Tarreau185b5c42012-04-26 15:11:51 +02008061/* Fetch an HTTP header's integer value. The integer value is returned. It
8062 * takes a mandatory argument of type string and an optional one of type int
8063 * to designate a specific occurrence. It returns an unsigned integer, which
8064 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008065 */
8066static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008067smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008068 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008069{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008070 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008071
Willy Tarreauf853c462012-04-23 18:53:56 +02008072 if (ret > 0) {
8073 smp->type = SMP_T_UINT;
8074 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8075 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008076
Willy Tarreaud53e2422012-04-16 17:21:11 +02008077 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008078}
8079
Cyril Bonté69fa9922012-10-25 00:01:06 +02008080/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8081 * and an optional one of type int to designate a specific occurrence.
8082 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008083 */
8084static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008085smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008086 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008087{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008088 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008089
Willy Tarreau185b5c42012-04-26 15:11:51 +02008090 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008091 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8092 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008093 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008094 } else {
8095 struct chunk *temp = sample_get_trash_chunk();
8096 if (smp->data.str.len < temp->size - 1) {
8097 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8098 temp->str[smp->data.str.len] = '\0';
8099 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8100 smp->type = SMP_T_IPV6;
8101 break;
8102 }
8103 }
8104 }
8105
Willy Tarreaud53e2422012-04-16 17:21:11 +02008106 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008107 if (!(smp->flags & SMP_F_NOT_LAST))
8108 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008109 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008110 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008111}
8112
Willy Tarreau737b0c12007-06-10 21:28:46 +02008113/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8114 * the first '/' after the possible hostname, and ends before the possible '?'.
8115 */
8116static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008117smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008118 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008119{
8120 struct http_txn *txn = l7;
8121 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008122
Willy Tarreauc0239e02012-04-16 14:42:55 +02008123 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008124
Willy Tarreau9b28e032012-10-12 23:49:43 +02008125 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008126 ptr = http_get_path(txn);
8127 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008128 return 0;
8129
8130 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008131 smp->type = SMP_T_CSTR;
8132 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008133
8134 while (ptr < end && *ptr != '?')
8135 ptr++;
8136
Willy Tarreauf853c462012-04-23 18:53:56 +02008137 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008138 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008139 return 1;
8140}
8141
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008142/* This produces a concatenation of the first occurrence of the Host header
8143 * followed by the path component if it begins with a slash ('/'). This means
8144 * that '*' will not be added, resulting in exactly the first Host entry.
8145 * If no Host header is found, then the path is returned as-is. The returned
8146 * value is stored in the trash so it does not need to be marked constant.
8147 */
8148static int
8149smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8150 const struct arg *args, struct sample *smp)
8151{
8152 struct http_txn *txn = l7;
8153 char *ptr, *end, *beg;
8154 struct hdr_ctx ctx;
8155
8156 CHECK_HTTP_MESSAGE_FIRST();
8157
8158 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008159 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008160 !ctx.vlen)
8161 return smp_fetch_path(px, l4, l7, opt, args, smp);
8162
8163 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8164 memcpy(trash, ctx.line + ctx.val, ctx.vlen);
8165 smp->type = SMP_T_STR;
8166 smp->data.str.str = trash;
8167 smp->data.str.len = ctx.vlen;
8168
8169 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008170 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008171 beg = http_get_path(txn);
8172 if (!beg)
8173 beg = end;
8174
8175 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8176
8177 if (beg < ptr && *beg == '/') {
8178 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8179 smp->data.str.len += ptr - beg;
8180 }
8181
8182 smp->flags = SMP_F_VOL_1ST;
8183 return 1;
8184}
8185
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008186static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008187acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008188 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008189{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008190 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8191 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8192 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008193
Willy Tarreau24e32d82012-04-23 23:55:44 +02008194 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008195
Willy Tarreauf853c462012-04-23 18:53:56 +02008196 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008197 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008198 return 1;
8199}
8200
Willy Tarreau7f18e522010-10-22 20:04:13 +02008201/* return a valid test if the current request is the first one on the connection */
8202static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008203acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008204 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008205{
8206 if (!s)
8207 return 0;
8208
Willy Tarreauf853c462012-04-23 18:53:56 +02008209 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008210 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008211 return 1;
8212}
8213
Willy Tarreau34db1082012-04-19 17:16:54 +02008214/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008215static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008216acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008217 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008218{
8219
Willy Tarreau24e32d82012-04-23 23:55:44 +02008220 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008221 return 0;
8222
Willy Tarreauc0239e02012-04-16 14:42:55 +02008223 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008224
Willy Tarreauc0239e02012-04-16 14:42:55 +02008225 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008226 return 0;
8227
Willy Tarreauf853c462012-04-23 18:53:56 +02008228 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008229 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 +01008230 return 1;
8231}
Willy Tarreau8797c062007-05-07 00:55:35 +02008232
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008233/* Accepts exactly 1 argument of type userlist */
8234static int
8235acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8236 const struct arg *args, struct sample *smp)
8237{
8238
8239 if (!args || args->type != ARGT_USR)
8240 return 0;
8241
8242 CHECK_HTTP_MESSAGE_FIRST();
8243
8244 if (!get_http_auth(l4))
8245 return 0;
8246
8247 /* acl_match_auth() will need several information at once */
8248 smp->ctx.a[0] = args->data.usr; /* user list */
8249 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8250 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8251
8252 /* if the user does not belong to the userlist or has a wrong password,
8253 * report that it unconditionally does not match. Otherwise we return
8254 * a non-zero integer which will be ignored anyway since all the params
8255 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8256 */
8257 smp->type = SMP_T_BOOL;
8258 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8259 if (smp->data.uint)
8260 smp->type = SMP_T_UINT;
8261
8262 return 1;
8263}
8264
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008265/* Try to find the next occurrence of a cookie name in a cookie header value.
8266 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8267 * the cookie value is returned into *value and *value_l, and the function
8268 * returns a pointer to the next pointer to search from if the value was found.
8269 * Otherwise if the cookie was not found, NULL is returned and neither value
8270 * nor value_l are touched. The input <hdr> string should first point to the
8271 * header's value, and the <hdr_end> pointer must point to the first character
8272 * not part of the value. <list> must be non-zero if value may represent a list
8273 * of values (cookie headers). This makes it faster to abort parsing when no
8274 * list is expected.
8275 */
8276static char *
8277extract_cookie_value(char *hdr, const char *hdr_end,
8278 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008279 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008280{
8281 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8282 char *next;
8283
8284 /* we search at least a cookie name followed by an equal, and more
8285 * generally something like this :
8286 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8287 */
8288 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8289 /* Iterate through all cookies on this line */
8290
8291 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8292 att_beg++;
8293
8294 /* find att_end : this is the first character after the last non
8295 * space before the equal. It may be equal to hdr_end.
8296 */
8297 equal = att_end = att_beg;
8298
8299 while (equal < hdr_end) {
8300 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8301 break;
8302 if (http_is_spht[(unsigned char)*equal++])
8303 continue;
8304 att_end = equal;
8305 }
8306
8307 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8308 * is between <att_beg> and <equal>, both may be identical.
8309 */
8310
8311 /* look for end of cookie if there is an equal sign */
8312 if (equal < hdr_end && *equal == '=') {
8313 /* look for the beginning of the value */
8314 val_beg = equal + 1;
8315 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8316 val_beg++;
8317
8318 /* find the end of the value, respecting quotes */
8319 next = find_cookie_value_end(val_beg, hdr_end);
8320
8321 /* make val_end point to the first white space or delimitor after the value */
8322 val_end = next;
8323 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8324 val_end--;
8325 } else {
8326 val_beg = val_end = next = equal;
8327 }
8328
8329 /* We have nothing to do with attributes beginning with '$'. However,
8330 * they will automatically be removed if a header before them is removed,
8331 * since they're supposed to be linked together.
8332 */
8333 if (*att_beg == '$')
8334 continue;
8335
8336 /* Ignore cookies with no equal sign */
8337 if (equal == next)
8338 continue;
8339
8340 /* Now we have the cookie name between att_beg and att_end, and
8341 * its value between val_beg and val_end.
8342 */
8343
8344 if (att_end - att_beg == cookie_name_l &&
8345 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8346 /* let's return this value and indicate where to go on from */
8347 *value = val_beg;
8348 *value_l = val_end - val_beg;
8349 return next + 1;
8350 }
8351
8352 /* Set-Cookie headers only have the name in the first attr=value part */
8353 if (!list)
8354 break;
8355 }
8356
8357 return NULL;
8358}
8359
Willy Tarreaue333ec92012-04-16 16:26:40 +02008360/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008361 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8362 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008363 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008364 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008365 * Accepts exactly 1 argument of type string. If the input options indicate
8366 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008367 */
8368static int
Willy Tarreau51539362012-05-08 12:46:28 +02008369smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8370 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008371{
8372 struct http_txn *txn = l7;
8373 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008374 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008375 const struct http_msg *msg;
8376 const char *hdr_name;
8377 int hdr_name_len;
8378 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008379 int occ = 0;
8380 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008381
Willy Tarreau24e32d82012-04-23 23:55:44 +02008382 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008383 return 0;
8384
Willy Tarreaue333ec92012-04-16 16:26:40 +02008385 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008386
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008387 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008388 msg = &txn->req;
8389 hdr_name = "Cookie";
8390 hdr_name_len = 6;
8391 } else {
8392 msg = &txn->rsp;
8393 hdr_name = "Set-Cookie";
8394 hdr_name_len = 10;
8395 }
8396
Willy Tarreau28376d62012-04-26 21:26:10 +02008397 if (!occ && !(opt & SMP_OPT_ITERATE))
8398 /* no explicit occurrence and single fetch => last cookie by default */
8399 occ = -1;
8400
8401 /* OK so basically here, either we want only one value and it's the
8402 * last one, or we want to iterate over all of them and we fetch the
8403 * next one.
8404 */
8405
Willy Tarreau9b28e032012-10-12 23:49:43 +02008406 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008407 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008408 /* search for the header from the beginning, we must first initialize
8409 * the search parameters.
8410 */
Willy Tarreau37406352012-04-23 16:16:37 +02008411 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008412 ctx->idx = 0;
8413 }
8414
Willy Tarreau28376d62012-04-26 21:26:10 +02008415 smp->flags |= SMP_F_VOL_HDR;
8416
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008417 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008418 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8419 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008420 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8421 goto out;
8422
Willy Tarreau24e32d82012-04-23 23:55:44 +02008423 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008424 continue;
8425
Willy Tarreau37406352012-04-23 16:16:37 +02008426 smp->ctx.a[0] = ctx->line + ctx->val;
8427 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008428 }
8429
Willy Tarreauf853c462012-04-23 18:53:56 +02008430 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008431 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008432 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008433 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008434 &smp->data.str.str,
8435 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008436 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008437 found = 1;
8438 if (occ >= 0) {
8439 /* one value was returned into smp->data.str.{str,len} */
8440 smp->flags |= SMP_F_NOT_LAST;
8441 return 1;
8442 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008443 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008444 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008445 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008446 /* all cookie headers and values were scanned. If we're looking for the
8447 * last occurrence, we may return it now.
8448 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008449 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008450 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008451 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008452}
8453
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008454/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008455 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008456 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008457 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008458 */
8459static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008460acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008461 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008462{
8463 struct http_txn *txn = l7;
8464 struct hdr_idx *idx = &txn->hdr_idx;
8465 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008466 const struct http_msg *msg;
8467 const char *hdr_name;
8468 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008469 int cnt;
8470 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008471 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008472
Willy Tarreau24e32d82012-04-23 23:55:44 +02008473 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008474 return 0;
8475
Willy Tarreaue333ec92012-04-16 16:26:40 +02008476 CHECK_HTTP_MESSAGE_FIRST();
8477
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008478 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008479 msg = &txn->req;
8480 hdr_name = "Cookie";
8481 hdr_name_len = 6;
8482 } else {
8483 msg = &txn->rsp;
8484 hdr_name = "Set-Cookie";
8485 hdr_name_len = 10;
8486 }
8487
Willy Tarreau9b28e032012-10-12 23:49:43 +02008488 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008489 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008490 ctx.idx = 0;
8491 cnt = 0;
8492
8493 while (1) {
8494 /* Note: val_beg == NULL every time we need to fetch a new header */
8495 if (!val_beg) {
8496 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8497 break;
8498
Willy Tarreau24e32d82012-04-23 23:55:44 +02008499 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008500 continue;
8501
8502 val_beg = ctx.line + ctx.val;
8503 val_end = val_beg + ctx.vlen;
8504 }
8505
Willy Tarreauf853c462012-04-23 18:53:56 +02008506 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008507 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008508 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008509 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008510 &smp->data.str.str,
8511 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008512 cnt++;
8513 }
8514 }
8515
Willy Tarreauf853c462012-04-23 18:53:56 +02008516 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008517 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008518 return 1;
8519}
8520
Willy Tarreau51539362012-05-08 12:46:28 +02008521/* Fetch an cookie's integer value. The integer value is returned. It
8522 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8523 */
8524static int
8525smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8526 const struct arg *args, struct sample *smp)
8527{
8528 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8529
8530 if (ret > 0) {
8531 smp->type = SMP_T_UINT;
8532 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8533 }
8534
8535 return ret;
8536}
8537
Willy Tarreau8797c062007-05-07 00:55:35 +02008538/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008539/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008540/************************************************************************/
8541
David Cournapeau16023ee2010-12-23 20:55:41 +09008542/*
8543 * Given a path string and its length, find the position of beginning of the
8544 * query string. Returns NULL if no query string is found in the path.
8545 *
8546 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8547 *
8548 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8549 */
bedis4c75cca2012-10-05 08:38:24 +02008550static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008551{
8552 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008553
bedis4c75cca2012-10-05 08:38:24 +02008554 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008555 return p ? p + 1 : NULL;
8556}
8557
bedis4c75cca2012-10-05 08:38:24 +02008558static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008559{
bedis4c75cca2012-10-05 08:38:24 +02008560 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008561}
8562
8563/*
8564 * Given a url parameter, find the starting position of the first occurence,
8565 * or NULL if the parameter is not found.
8566 *
8567 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8568 * the function will return query_string+8.
8569 */
8570static char*
8571find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008572 char* url_param_name, size_t url_param_name_l,
8573 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008574{
8575 char *pos, *last;
8576
8577 pos = query_string;
8578 last = query_string + query_string_l - url_param_name_l - 1;
8579
8580 while (pos <= last) {
8581 if (pos[url_param_name_l] == '=') {
8582 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8583 return pos;
8584 pos += url_param_name_l + 1;
8585 }
bedis4c75cca2012-10-05 08:38:24 +02008586 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008587 pos++;
8588 pos++;
8589 }
8590 return NULL;
8591}
8592
8593/*
8594 * Given a url parameter name, returns its value and size into *value and
8595 * *value_l respectively, and returns non-zero. If the parameter is not found,
8596 * zero is returned and value/value_l are not touched.
8597 */
8598static int
8599find_url_param_value(char* path, size_t path_l,
8600 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008601 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008602{
8603 char *query_string, *qs_end;
8604 char *arg_start;
8605 char *value_start, *value_end;
8606
bedis4c75cca2012-10-05 08:38:24 +02008607 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008608 if (!query_string)
8609 return 0;
8610
8611 qs_end = path + path_l;
8612 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008613 url_param_name, url_param_name_l,
8614 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008615 if (!arg_start)
8616 return 0;
8617
8618 value_start = arg_start + url_param_name_l + 1;
8619 value_end = value_start;
8620
bedis4c75cca2012-10-05 08:38:24 +02008621 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008622 value_end++;
8623
8624 *value = value_start;
8625 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008626 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008627}
8628
8629static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008630smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8631 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008632{
bedis4c75cca2012-10-05 08:38:24 +02008633 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008634 struct http_txn *txn = l7;
8635 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008636
bedis4c75cca2012-10-05 08:38:24 +02008637 if (!args || args[0].type != ARGT_STR ||
8638 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008639 return 0;
8640
8641 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008642
bedis4c75cca2012-10-05 08:38:24 +02008643 if (args[1].type)
8644 delim = *args[1].data.str.str;
8645
Willy Tarreau9b28e032012-10-12 23:49:43 +02008646 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008647 args->data.str.str, args->data.str.len,
8648 &smp->data.str.str, &smp->data.str.len,
8649 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008650 return 0;
8651
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008652 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008653 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008654 return 1;
8655}
8656
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008657/* Return the signed integer value for the specified url parameter (see url_param
8658 * above).
8659 */
8660static int
8661smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8662 const struct arg *args, struct sample *smp)
8663{
8664 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8665
8666 if (ret > 0) {
8667 smp->type = SMP_T_UINT;
8668 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8669 }
8670
8671 return ret;
8672}
8673
Willy Tarreau185b5c42012-04-26 15:11:51 +02008674/* This function is used to validate the arguments passed to any "hdr" fetch
8675 * keyword. These keywords support an optional positive or negative occurrence
8676 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8677 * is assumed that the types are already the correct ones. Returns 0 on error,
8678 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8679 * error message in case of error, that the caller is responsible for freeing.
8680 * The initial location must either be freeable or NULL.
8681 */
8682static int val_hdr(struct arg *arg, char **err_msg)
8683{
8684 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008685 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008686 return 0;
8687 }
8688 return 1;
8689}
8690
Willy Tarreau4a568972010-05-12 08:08:50 +02008691/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008692/* All supported ACL keywords must be declared here. */
8693/************************************************************************/
8694
8695/* Note: must not be declared <const> as its list will be overwritten.
8696 * Please take care of keeping this list alphabetically sorted.
8697 */
8698static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008699 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8700 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8701 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8702 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8703 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8704 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8705 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8706 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8707
Willy Tarreau51539362012-05-08 12:46:28 +02008708 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8709 { "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 +02008710 { "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 +02008711 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8712 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8713 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8714 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8715 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8716 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8717 { "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 +02008718
Willy Tarreau185b5c42012-04-26 15:11:51 +02008719 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8720 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8721 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8722 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8723 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8724 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8725 { "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 },
8726 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8727 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8728 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8729 { "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 +02008730
8731 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008732 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008733 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8734
8735 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8736
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008737 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8738 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8739 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8740 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8741 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8742 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8743 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8744 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008745
8746 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8747 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8748 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8749
Willy Tarreau51539362012-05-08 12:46:28 +02008750 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8751 { "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 +02008752 { "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 +02008753 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8754 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8755 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8756 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8757 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8758 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8759 { "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 +02008760
Willy Tarreau185b5c42012-04-26 15:11:51 +02008761 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8762 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8763 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8764 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8765 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8766 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8767 { "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 },
8768 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8769 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8770 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8771 { "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 +02008772
8773 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8774
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008775 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8776 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8777 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8778 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8779 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8780 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8781 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8782 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8783 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8784 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008785
8786 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8787 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8788 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8789 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8790 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8791 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8792 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8793 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8794 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008795 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008796
8797 { NULL, NULL, NULL, NULL },
8798}};
8799
8800/************************************************************************/
8801/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008802/************************************************************************/
8803/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008804static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02008805 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
8806 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
8807 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
8808 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
8809 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
8810 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
8811 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
8812 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
8813 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008814 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008815}};
8816
Willy Tarreau8797c062007-05-07 00:55:35 +02008817
8818__attribute__((constructor))
8819static void __http_protocol_init(void)
8820{
8821 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008822 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008823}
8824
8825
Willy Tarreau58f10d72006-12-04 02:26:12 +01008826/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008827 * Local variables:
8828 * c-indent-level: 8
8829 * c-basic-offset: 8
8830 * End:
8831 */