blob: 983d3946d17bef300bb0908c832223a53a23783a [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 Tarreau572bf902012-07-02 17:01:20 +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 Tarreau572bf902012-07-02 17:01:20 +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 Tarreauaf819352012-08-27 22:08:00 +0200424 bytes = buffer_insert_line2(&msg->buf->buf, msg->buf->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 Tarreauaf819352012-08-27 22:08:00 +0200444 bytes = buffer_insert_line2(&msg->buf->buf, msg->buf->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 Tarreauaf819352012-08-27 22:08:00 +0200613 delta = buffer_replace2(&msg->buf->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 Tarreauaf819352012-08-27 22:08:00 +0200633 delta = buffer_replace2(&msg->buf->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 Tarreau572bf902012-07-02 17:01:20 +0200715 ptr = txn->req.buf->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 Tarreaua75bcef2012-08-24 22:56:11 +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 Tarreau572bf902012-07-02 17:01:20 +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 Tarreaua75bcef2012-08-24 22:56:11 +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 Tarreau572bf902012-07-02 17:01:20 +0200965 const char *msg_start = msg->buf->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 Tarreau572bf902012-07-02 17:01:20 +02001075 const char *msg_start = msg->buf->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 Tarreau572bf902012-07-02 17:01:20 +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 Tarreau7421efb2012-07-02 15:11:27 +02001293 struct channel *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 state = msg->msg_state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001296 ptr = buf->buf.p + msg->next;
1297 end = buf->buf.p + buf->buf.i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(ptr >= end))
1300 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 /*
1304 * First, states that are specific to the response only.
1305 * We check them first so that request and headers are
1306 * closer to each other (accessed more often).
1307 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001309 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001311 /* we have a start of message, but we have to check
1312 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001313 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001314 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001315 if (unlikely(ptr != buf->buf.p)) {
1316 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001317 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001318 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001319 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 }
Willy Tarreau26927362012-05-18 23:22:52 +02001321 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001322 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 hdr_idx_init(idx);
1324 state = HTTP_MSG_RPVER;
1325 goto http_msg_rpver;
1326 }
1327
1328 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1329 goto http_msg_invalid;
1330
1331 if (unlikely(*ptr == '\n'))
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1334 /* stop here */
1335
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001337 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 EXPECT_LF_HERE(ptr, http_msg_invalid);
1339 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1340 /* stop here */
1341
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001343 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 case HTTP_MSG_RPVER_SP:
1345 case HTTP_MSG_RPCODE:
1346 case HTTP_MSG_RPCODE_SP:
1347 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001348 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001349 state, ptr, end,
1350 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 if (unlikely(!ptr))
1352 return;
1353
1354 /* we have a full response and we know that we have either a CR
1355 * or an LF at <ptr>.
1356 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1358
Willy Tarreau572bf902012-07-02 17:01:20 +02001359 msg->sol = ptr - buf->buf.p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 if (likely(*ptr == '\r'))
1361 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1362 goto http_msg_rpline_end;
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001365 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 /* msg->sol must point to the first of CR or LF. */
1367 EXPECT_LF_HERE(ptr, http_msg_invalid);
1368 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1369 /* stop here */
1370
1371 /*
1372 * Second, states that are specific to the request only
1373 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001375 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001377 /* we have a start of message, but we have to check
1378 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001379 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001380 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001381 if (likely(ptr != buf->buf.p)) {
1382 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001383 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001384 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001385 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 }
Willy Tarreau26927362012-05-18 23:22:52 +02001387 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001388 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 state = HTTP_MSG_RQMETH;
1390 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1394 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 if (unlikely(*ptr == '\n'))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 EXPECT_LF_HERE(ptr, http_msg_invalid);
1404 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQMETH_SP:
1410 case HTTP_MSG_RQURI:
1411 case HTTP_MSG_RQURI_SP:
1412 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001413 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001414 state, ptr, end,
1415 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 if (unlikely(!ptr))
1417 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 /* we have a full request and we know that we have either a CR
1420 * or an LF at <ptr>.
1421 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau572bf902012-07-02 17:01:20 +02001424 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 if (likely(*ptr == '\r'))
1426 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001428
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001430 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 /* check for HTTP/0.9 request : no version information available.
1432 * msg->sol must point to the first of CR or LF.
1433 */
1434 if (unlikely(msg->sl.rq.v_l == 0))
1435 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 EXPECT_LF_HERE(ptr, http_msg_invalid);
1438 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001440
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 /*
1442 * Common states below
1443 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001445 http_msg_hdr_first:
Willy Tarreau572bf902012-07-02 17:01:20 +02001446 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 if (likely(!HTTP_IS_CRLF(*ptr))) {
1448 goto http_msg_hdr_name;
1449 }
1450
1451 if (likely(*ptr == '\r'))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1453 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 /* assumes msg->sol points to the first char */
1458 if (likely(HTTP_IS_TOKEN(*ptr)))
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001461 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001463
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001464 if (likely(msg->err_pos < -1) || *ptr == '\n')
1465 goto http_msg_invalid;
1466
1467 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau572bf902012-07-02 17:01:20 +02001468 msg->err_pos = ptr - buf->buf.p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001469
1470 /* and we still accept this non-token character */
1471 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001474 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001475 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (likely(HTTP_IS_SPHT(*ptr)))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 /* header value can be basically anything except CR/LF */
Willy Tarreau572bf902012-07-02 17:01:20 +02001480 msg->sov = ptr - buf->buf.p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 if (likely(!HTTP_IS_CRLF(*ptr))) {
1483 goto http_msg_hdr_val;
1484 }
1485
1486 if (likely(*ptr == '\r'))
1487 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1488 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 EXPECT_LF_HERE(ptr, http_msg_invalid);
1493 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001496 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (likely(HTTP_IS_SPHT(*ptr))) {
1498 /* replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001499 for (; buf->buf.p + msg->sov < ptr; msg->sov++)
1500 buf->buf.p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_hdr_l1_sp;
1502 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001503 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001504 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 goto http_msg_complete_header;
1506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001508 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001509 /* assumes msg->sol points to the first char, and msg->sov
1510 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 */
1512 if (likely(!HTTP_IS_CRLF(*ptr)))
1513 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreau572bf902012-07-02 17:01:20 +02001515 msg->eol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 /* Note: we could also copy eol into ->eoh so that we have the
1517 * real header end in case it ends with lots of LWS, but is this
1518 * really needed ?
1519 */
1520 if (likely(*ptr == '\r'))
1521 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1522 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 EXPECT_LF_HERE(ptr, http_msg_invalid);
1527 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001530 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1532 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001533 for (; buf->buf.p + msg->eol < ptr; msg->eol++)
1534 buf->buf.p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 goto http_msg_hdr_val;
1536 }
1537 http_msg_complete_header:
1538 /*
1539 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001540 * Assumes msg->sol points to the first char, msg->sov points
1541 * to the first character of the value and msg->eol to the
1542 * first CR or LF so we know how the line ends. We insert last
1543 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001545 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->buf.p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 idx, idx->tail) < 0))
1547 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001548
Willy Tarreau572bf902012-07-02 17:01:20 +02001549 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(!HTTP_IS_CRLF(*ptr))) {
1551 goto http_msg_hdr_name;
1552 }
1553
1554 if (likely(*ptr == '\r'))
1555 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1556 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001559 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 /* Assumes msg->sol points to the first of either CR or LF */
1561 EXPECT_LF_HERE(ptr, http_msg_invalid);
1562 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001563 msg->sov = msg->next = ptr - buf->buf.p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001564 msg->eoh = msg->sol;
1565 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001566 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001568
1569 case HTTP_MSG_ERROR:
1570 /* this may only happen if we call http_msg_analyser() twice with an error */
1571 break;
1572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573#ifdef DEBUG_FULL
1574 default:
1575 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1576 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001577#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 }
1579 http_msg_ood:
1580 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001581 msg->msg_state = state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001582 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 http_msg_invalid:
1586 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001587 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau572bf902012-07-02 17:01:20 +02001588 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 return;
1590}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001591
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001592/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1593 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1594 * nothing is done and 1 is returned.
1595 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597{
1598 int delta;
1599 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001600 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601
1602 if (msg->sl.rq.v_l != 0)
1603 return 1;
1604
Willy Tarreau572bf902012-07-02 17:01:20 +02001605 cur_end = msg->buf->buf.p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 delta = 0;
1607
1608 if (msg->sl.rq.u_l == 0) {
1609 /* if no URI was set, add "/" */
Willy Tarreauaf819352012-08-27 22:08:00 +02001610 delta = buffer_replace2(&msg->buf->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001611 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 }
1614 /* add HTTP version */
Willy Tarreauaf819352012-08-27 22:08:00 +02001615 delta = buffer_replace2(&msg->buf->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001616 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001618 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001619 HTTP_MSG_RQMETH,
Willy Tarreau572bf902012-07-02 17:01:20 +02001620 msg->buf->buf.p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001621 NULL, NULL);
1622 if (unlikely(!cur_end))
1623 return 0;
1624
1625 /* we have a full HTTP/1.0 request now and we know that
1626 * we have either a CR or an LF at <ptr>.
1627 */
1628 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1629 return 1;
1630}
1631
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001632/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633 * and "keep-alive" values. If we already know that some headers may safely
1634 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001635 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1636 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1637 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1638 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1639 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001640 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001642void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001643{
Willy Tarreau5b154472009-12-21 20:11:07 +01001644 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001645 const char *hdr_val = "Connection";
1646 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001647
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001648 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 return;
1650
Willy Tarreau88d349d2010-01-25 12:15:43 +01001651 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1652 hdr_val = "Proxy-Connection";
1653 hdr_len = 16;
1654 }
1655
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001658 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1660 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001661 if (to_del & 2)
1662 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 else
1664 txn->flags |= TX_CON_KAL_SET;
1665 }
1666 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1667 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001668 if (to_del & 1)
1669 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001670 else
1671 txn->flags |= TX_CON_CLO_SET;
1672 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001673 }
1674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 txn->flags |= TX_HDR_CONN_PRS;
1676 return;
1677}
Willy Tarreau5b154472009-12-21 20:11:07 +01001678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679/* Apply desired changes on the Connection: header. Values may be removed and/or
1680 * added depending on the <wanted> flags, which are exclusively composed of
1681 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1682 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001684void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685{
1686 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687 const char *hdr_val = "Connection";
1688 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689
1690 ctx.idx = 0;
1691
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692
1693 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1694 hdr_val = "Proxy-Connection";
1695 hdr_len = 16;
1696 }
1697
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001699 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1701 if (wanted & TX_CON_KAL_SET)
1702 txn->flags |= TX_CON_KAL_SET;
1703 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001704 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1707 if (wanted & TX_CON_CLO_SET)
1708 txn->flags |= TX_CON_CLO_SET;
1709 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001711 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001712 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713
1714 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1715 return;
1716
1717 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1718 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001719 hdr_val = "Connection: close";
1720 hdr_len = 17;
1721 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1722 hdr_val = "Proxy-Connection: close";
1723 hdr_len = 23;
1724 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 }
1727
1728 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1729 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001730 hdr_val = "Connection: keep-alive";
1731 hdr_len = 22;
1732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection: keep-alive";
1734 hdr_len = 28;
1735 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001736 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001737 }
1738 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739}
1740
Willy Tarreaua458b672012-03-05 11:17:50 +01001741/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001743 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001747int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001749 const struct channel *buf = msg->buf;
Willy Tarreau572bf902012-07-02 17:01:20 +02001750 const char *ptr = b_ptr(&buf->buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001751 const char *ptr_old = ptr;
Willy Tarreau572bf902012-07-02 17:01:20 +02001752 const char *end = buf->buf.data + buf->buf.size;
1753 const char *stop = bi_end(&buf->buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001754 unsigned int chunk = 0;
1755
1756 /* The chunk size is in the following form, though we are only
1757 * interested in the size and CRLF :
1758 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1759 */
1760 while (1) {
1761 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001762 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 if (c < 0) /* not a hex digit anymore */
1766 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001768 ptr = buf->buf.data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001769 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001771 chunk = (chunk << 4) + c;
1772 }
1773
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001775 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001776 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777
1778 while (http_is_spht[(unsigned char)*ptr]) {
1779 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001780 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001781 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 }
1784
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 /* Up to there, we know that at least one byte is present at *ptr. Check
1786 * for the end of chunk size.
1787 */
1788 while (1) {
1789 if (likely(HTTP_IS_CRLF(*ptr))) {
1790 /* we now have a CR or an LF at ptr */
1791 if (likely(*ptr == '\r')) {
1792 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001793 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001794 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 return 0;
1796 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001797
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001799 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001801 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 /* done */
1803 break;
1804 }
1805 else if (*ptr == ';') {
1806 /* chunk extension, ends at next CRLF */
1807 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001808 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001810 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811
1812 while (!HTTP_IS_CRLF(*ptr)) {
1813 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001814 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001815 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816 return 0;
1817 }
1818 /* we have a CRLF now, loop above */
1819 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001822 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 }
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001826 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001829 msg->sov += ptr - ptr_old;
Willy Tarreau572bf902012-07-02 17:01:20 +02001830 msg->next = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001831 msg->chunk_len = chunk;
1832 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001834 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001835 error:
Willy Tarreau572bf902012-07-02 17:01:20 +02001836 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838}
1839
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001840/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 * the trailers is found, it is automatically scheduled to be forwarded,
1843 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1844 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001845 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001847 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001848 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1849 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001850 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001851 * matches the length of trailers already parsed and not forwarded. It is also
1852 * important to note that this function is designed to be able to parse wrapped
1853 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001855int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001857 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001858
Willy Tarreaua458b672012-03-05 11:17:50 +01001859 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001861 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau572bf902012-07-02 17:01:20 +02001862 const char *ptr = b_ptr(&buf->buf, msg->next);
1863 const char *stop = bi_end(&buf->buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001864 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865
1866 /* scan current line and stop at LF or CRLF */
1867 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001868 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 return 0;
1870
1871 if (*ptr == '\n') {
1872 if (!p1)
1873 p1 = ptr;
1874 p2 = ptr;
1875 break;
1876 }
1877
1878 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001879 if (p1) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001880 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001882 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 p1 = ptr;
1884 }
1885
1886 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001887 if (ptr >= buf->buf.data + buf->buf.size)
1888 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001889 }
1890
1891 /* after LF; point to beginning of next line */
1892 p2++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001893 if (p2 >= buf->buf.data + buf->buf.size)
1894 p2 = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
Willy Tarreau572bf902012-07-02 17:01:20 +02001896 bytes = p2 - b_ptr(&buf->buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001897 if (bytes < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02001898 bytes += buf->buf.size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001899
1900 /* schedule this line for forwarding */
1901 msg->sov += bytes;
Willy Tarreau572bf902012-07-02 17:01:20 +02001902 if (msg->sov >= buf->buf.size)
1903 msg->sov -= buf->buf.size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001904
Willy Tarreau572bf902012-07-02 17:01:20 +02001905 if (p1 == b_ptr(&buf->buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001906 /* LF/CRLF at beginning of line => end of trailers at p2.
1907 * Everything was scheduled for forwarding, there's nothing
1908 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001909 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001910 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 msg->msg_state = HTTP_MSG_DONE;
1912 return 1;
1913 }
1914 /* OK, next line then */
Willy Tarreau572bf902012-07-02 17:01:20 +02001915 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916 }
1917}
1918
1919/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1920 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001921 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001922 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1924 * not enough data are available, the function does not change anything and
1925 * returns zero. If a parse error is encountered, the function returns < 0 and
1926 * does not change anything. Note: this function is designed to parse wrapped
1927 * CRLF at the end of the buffer.
1928 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001929int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001931 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001932 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 int bytes;
1934
1935 /* NB: we'll check data availabilty at the end. It's not a
1936 * problem because whatever we match first will be checked
1937 * against the correct length.
1938 */
1939 bytes = 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02001940 ptr = buf->buf.p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 if (*ptr == '\r') {
1942 bytes++;
1943 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001944 if (ptr >= buf->buf.data + buf->buf.size)
1945 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 }
1947
Willy Tarreau572bf902012-07-02 17:01:20 +02001948 if (bytes > buf->buf.i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 return 0;
1950
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001951 if (*ptr != '\n') {
Willy Tarreau572bf902012-07-02 17:01:20 +02001952 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001954 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955
1956 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001957 if (ptr >= buf->buf.data + buf->buf.size)
1958 ptr = buf->buf.data;
Willy Tarreau26927362012-05-18 23:22:52 +02001959 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1960 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001961 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1963 return 1;
1964}
Willy Tarreau5b154472009-12-21 20:11:07 +01001965
Willy Tarreaud787e662009-07-07 10:14:51 +02001966/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1967 * processing can continue on next analysers, or zero if it either needs more
1968 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1969 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1970 * when it has nothing left to do, and may remove any analyser when it wants to
1971 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001973int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974{
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 /*
1976 * We will parse the partial (or complete) lines.
1977 * We will check the request syntax, and also join multi-line
1978 * headers. An index of all the lines will be elaborated while
1979 * parsing.
1980 *
1981 * For the parsing, we use a 28 states FSM.
1982 *
1983 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02001984 * req->buf.p = beginning of request
1985 * req->buf.p + msg->eoh = end of processed headers / start of current one
1986 * req->buf.p + req->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001987 * msg->eol = end of current header or line (LF or CRLF)
1988 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001989 *
1990 * At end of parsing, we may perform a capture of the error (if any), and
1991 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1992 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1993 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001994 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001995
Willy Tarreau59234e92008-11-30 23:51:27 +01001996 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001997 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001998 struct http_txn *txn = &s->txn;
1999 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002000 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002001
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002002 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 +01002003 now_ms, __FUNCTION__,
2004 s,
2005 req,
2006 req->rex, req->wex,
2007 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002008 req->buf.i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002009 req->analysers);
2010
Willy Tarreau52a0c602009-08-16 22:45:38 +02002011 /* we're speaking HTTP here, so let's speak HTTP to the client */
2012 s->srv_error = http_return_srv_error;
2013
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014 /* There's a protected area at the end of the buffer for rewriting
2015 * purposes. We don't want to start to parse the request if the
2016 * protected area is affected, because we may have to move processed
2017 * data later, which is much more complicated.
2018 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002019 if (buffer_not_empty(&req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002020 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002021 unlikely(channel_full(req) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002022 bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2023 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)) {
2024 if (req->buf.o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002025 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002026 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002027 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002028 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002029 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002030 return 0;
2031 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002032 if (bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2033 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)
2034 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002035 }
2036
Willy Tarreau065e8332010-01-08 00:30:20 +01002037 /* Note that we have the same problem with the response ; we
2038 * may want to send a redirect, error or anything which requires
2039 * some spare space. So we'll ensure that we have at least
2040 * maxrewrite bytes available in the response buffer before
2041 * processing that one. This will only affect pipelined
2042 * keep-alive requests.
2043 */
2044 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002045 unlikely(channel_full(s->rep) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002046 bi_end(&s->rep->buf) < b_ptr(&s->rep->buf, txn->rsp.next) ||
2047 bi_end(&s->rep->buf) > s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)) {
2048 if (s->rep->buf.o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002049 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002050 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002051 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002052 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002053 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002054 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002055 return 0;
2056 }
2057 }
2058
Willy Tarreau572bf902012-07-02 17:01:20 +02002059 if (likely(msg->next < req->buf.i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002060 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002061 }
2062
Willy Tarreau59234e92008-11-30 23:51:27 +01002063 /* 1: we might have to print this header in debug mode */
2064 if (unlikely((global.mode & MODE_DEBUG) &&
2065 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002066 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002067 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002068
Willy Tarreau572bf902012-07-02 17:01:20 +02002069 sol = req->buf.p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002070 /* this is a bit complex : in case of error on the request line,
2071 * we know that rq.l is still zero, so we display only the part
2072 * up to the end of the line (truncated by debug_hdr).
2073 */
2074 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf.i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002075 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002076
Willy Tarreau59234e92008-11-30 23:51:27 +01002077 sol += hdr_idx_first_pos(&txn->hdr_idx);
2078 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002079
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 while (cur_idx) {
2081 eol = sol + txn->hdr_idx.v[cur_idx].len;
2082 debug_hdr("clihdr", s, sol, eol);
2083 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2084 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002085 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002086 }
2087
Willy Tarreau58f10d72006-12-04 02:26:12 +01002088
Willy Tarreau59234e92008-11-30 23:51:27 +01002089 /*
2090 * Now we quickly check if we have found a full valid request.
2091 * If not so, we check the FD and buffer states before leaving.
2092 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002093 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002094 * requests are checked first. When waiting for a second request
2095 * on a keep-alive session, if we encounter and error, close, t/o,
2096 * we note the error in the session flags but don't set any state.
2097 * Since the error will be noted there, it will not be counted by
2098 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002099 * Last, we may increase some tracked counters' http request errors on
2100 * the cases that are deliberately the client's fault. For instance,
2101 * a timeout or connection reset is not counted as an error. However
2102 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002103 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002104
Willy Tarreau655dce92009-11-08 13:10:58 +01002105 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002106 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002108 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002109 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002110 session_inc_http_req_ctr(s);
2111 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002112 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002115
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 /* 1: Since we are in header mode, if there's no space
2117 * left for headers, we won't be able to free more
2118 * later, so the session will never terminate. We
2119 * must terminate it now.
2120 */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002121 if (unlikely(buffer_full(&req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 /* FIXME: check if URI is set and return Status
2123 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002124 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002125 session_inc_http_req_ctr(s);
2126 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002127 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002128 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02002129 msg->err_pos = req->buf.i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 goto return_bad_req;
2131 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002132
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002134 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002135 if (!(s->flags & SN_ERR_MASK))
2136 s->flags |= SN_ERR_CLICL;
2137
Willy Tarreaufcffa692010-01-10 14:21:19 +01002138 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002139 goto failed_keep_alive;
2140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002142 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002143 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002144 session_inc_http_err_ctr(s);
2145 }
2146
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 msg->msg_state = HTTP_MSG_ERROR;
2148 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002149
Willy Tarreauda7ff642010-06-23 11:44:09 +02002150 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002151 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002152 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002153 if (s->listener->counters)
2154 s->listener->counters->failed_req++;
2155
Willy Tarreau59234e92008-11-30 23:51:27 +01002156 if (!(s->flags & SN_FINST_MASK))
2157 s->flags |= SN_FINST_R;
2158 return 0;
2159 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002160
Willy Tarreau59234e92008-11-30 23:51:27 +01002161 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002162 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002163 if (!(s->flags & SN_ERR_MASK))
2164 s->flags |= SN_ERR_CLITO;
2165
Willy Tarreaufcffa692010-01-10 14:21:19 +01002166 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002167 goto failed_keep_alive;
2168
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002170 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002171 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002172 session_inc_http_err_ctr(s);
2173 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002175 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002176 msg->msg_state = HTTP_MSG_ERROR;
2177 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178
Willy Tarreauda7ff642010-06-23 11:44:09 +02002179 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002180 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002181 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002182 if (s->listener->counters)
2183 s->listener->counters->failed_req++;
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 if (!(s->flags & SN_FINST_MASK))
2186 s->flags |= SN_FINST_R;
2187 return 0;
2188 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002189
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002191 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002192 if (!(s->flags & SN_ERR_MASK))
2193 s->flags |= SN_ERR_CLICL;
2194
Willy Tarreaufcffa692010-01-10 14:21:19 +01002195 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002196 goto failed_keep_alive;
2197
Willy Tarreau4076a152009-04-02 15:18:36 +02002198 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002199 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002201 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 msg->msg_state = HTTP_MSG_ERROR;
2203 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002204
Willy Tarreauda7ff642010-06-23 11:44:09 +02002205 session_inc_http_err_ctr(s);
2206 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002207 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002208 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002209 if (s->listener->counters)
2210 s->listener->counters->failed_req++;
2211
Willy Tarreau59234e92008-11-30 23:51:27 +01002212 if (!(s->flags & SN_FINST_MASK))
2213 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002214 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002215 }
2216
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002217 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002218 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2219 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002220#ifdef TCP_QUICKACK
Willy Tarreau572bf902012-07-02 17:01:20 +02002221 if (s->listener->options & LI_O_NOQUICKACK && req->buf.i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002222 /* We need more data, we have to re-enable quick-ack in case we
2223 * previously disabled it, otherwise we might cause the client
2224 * to delay next data.
2225 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002226 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002227 }
2228#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002229
Willy Tarreaufcffa692010-01-10 14:21:19 +01002230 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2231 /* If the client starts to talk, let's fall back to
2232 * request timeout processing.
2233 */
2234 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002235 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002236 }
2237
Willy Tarreau59234e92008-11-30 23:51:27 +01002238 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002239 if (!tick_isset(req->analyse_exp)) {
2240 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2241 (txn->flags & TX_WAIT_NEXT_RQ) &&
2242 tick_isset(s->be->timeout.httpka))
2243 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2244 else
2245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002247
Willy Tarreau59234e92008-11-30 23:51:27 +01002248 /* we're not ready yet */
2249 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002250
2251 failed_keep_alive:
2252 /* Here we process low-level errors for keep-alive requests. In
2253 * short, if the request is not the first one and it experiences
2254 * a timeout, read error or shutdown, we just silently close so
2255 * that the client can try again.
2256 */
2257 txn->status = 0;
2258 msg->msg_state = HTTP_MSG_RQBEFORE;
2259 req->analysers = 0;
2260 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002261 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002262 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002263 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002265
Willy Tarreaud787e662009-07-07 10:14:51 +02002266 /* OK now we have a complete HTTP request with indexed headers. Let's
2267 * complete the request parsing by setting a few fields we will need
Willy Tarreau572bf902012-07-02 17:01:20 +02002268 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002269 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002270 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002271 * byte after the last LF. msg->sov points to the first byte of data.
2272 * msg->eol cannot be trusted because it may have been left uninitialized
2273 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002274 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002275
Willy Tarreauda7ff642010-06-23 11:44:09 +02002276 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002277 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2278
Willy Tarreaub16a5742010-01-10 14:46:16 +01002279 if (txn->flags & TX_WAIT_NEXT_RQ) {
2280 /* kill the pending keep-alive timeout */
2281 txn->flags &= ~TX_WAIT_NEXT_RQ;
2282 req->analyse_exp = TICK_ETERNITY;
2283 }
2284
2285
Willy Tarreaud787e662009-07-07 10:14:51 +02002286 /* Maybe we found in invalid header name while we were configured not
2287 * to block on that, so we have to capture it now.
2288 */
2289 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002290 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002291
Willy Tarreau59234e92008-11-30 23:51:27 +01002292 /*
2293 * 1: identify the method
2294 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002295 txn->meth = find_http_meth(req->buf.p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002296
2297 /* we can make use of server redirect on GET and HEAD */
2298 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2299 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002300
Willy Tarreau59234e92008-11-30 23:51:27 +01002301 /*
2302 * 2: check if the URI matches the monitor_uri.
2303 * We have to do this for every request which gets in, because
2304 * the monitor-uri is defined by the frontend.
2305 */
2306 if (unlikely((s->fe->monitor_uri_len != 0) &&
2307 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002308 !memcmp(req->buf.p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 s->fe->monitor_uri,
2310 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002311 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002313 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002314 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002317 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002320 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002321 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 ret = acl_pass(ret);
2324 if (cond->pol == ACL_COND_UNLESS)
2325 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 if (ret) {
2328 /* we fail this request, let's return 503 service unavail */
2329 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002330 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002332 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002333 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002334
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 /* nothing to fail, let's reply normaly */
2336 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002337 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 goto return_prx_cond;
2339 }
2340
2341 /*
2342 * 3: Maybe we have to copy the original REQURI for the logs ?
2343 * Note: we cannot log anymore if the request has been
2344 * classified as invalid.
2345 */
2346 if (unlikely(s->logs.logwait & LW_REQ)) {
2347 /* we have a complete HTTP request that we must log */
2348 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2349 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 if (urilen >= REQURI_LEN)
2352 urilen = REQURI_LEN - 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02002353 memcpy(txn->uri, req->buf.p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 if (!(s->logs.logwait &= ~LW_REQ))
2357 s->do_log(s);
2358 } else {
2359 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002360 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 }
Willy Tarreau06619262006-12-17 08:37:22 +01002362
William Lallemanda73203e2012-03-12 12:48:57 +01002363 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2364 s->unique_id = pool_alloc2(pool2_uniqueid);
2365 }
2366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002368 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002369 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002370
Willy Tarreau5b154472009-12-21 20:11:07 +01002371 /* ... and check if the request is HTTP/1.1 or above */
2372 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002373 ((req->buf.p[msg->sl.rq.v + 5] > '1') ||
2374 ((req->buf.p[msg->sl.rq.v + 5] == '1') &&
2375 (req->buf.p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002376 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002377
2378 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002379 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002380
Willy Tarreau88d349d2010-01-25 12:15:43 +01002381 /* if the frontend has "option http-use-proxy-header", we'll check if
2382 * we have what looks like a proxied connection instead of a connection,
2383 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2384 * Note that this is *not* RFC-compliant, however browsers and proxies
2385 * happen to do that despite being non-standard :-(
2386 * We consider that a request not beginning with either '/' or '*' is
2387 * a proxied connection, which covers both "scheme://location" and
2388 * CONNECT ip:port.
2389 */
2390 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002391 req->buf.p[msg->sl.rq.u] != '/' && req->buf.p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002392 txn->flags |= TX_USE_PX_CONN;
2393
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002394 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002395 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002396
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002398 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02002399 capture_headers(req->buf.p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002401
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002402 /* 6: determine the transfer-length.
2403 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2404 * the presence of a message-body in a REQUEST and its transfer length
2405 * must be determined that way (in order of precedence) :
2406 * 1. The presence of a message-body in a request is signaled by the
2407 * inclusion of a Content-Length or Transfer-Encoding header field
2408 * in the request's header fields. When a request message contains
2409 * both a message-body of non-zero length and a method that does
2410 * not define any semantics for that request message-body, then an
2411 * origin server SHOULD either ignore the message-body or respond
2412 * with an appropriate error message (e.g., 413). A proxy or
2413 * gateway, when presented the same request, SHOULD either forward
2414 * the request inbound with the message- body or ignore the
2415 * message-body when determining a response.
2416 *
2417 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2418 * and the "chunked" transfer-coding (Section 6.2) is used, the
2419 * transfer-length is defined by the use of this transfer-coding.
2420 * If a Transfer-Encoding header field is present and the "chunked"
2421 * transfer-coding is not present, the transfer-length is defined
2422 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002423 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002424 * 3. If a Content-Length header field is present, its decimal value in
2425 * OCTETs represents both the entity-length and the transfer-length.
2426 * If a message is received with both a Transfer-Encoding header
2427 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002428 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002429 * 4. By the server closing the connection. (Closing the connection
2430 * cannot be used to indicate the end of a request body, since that
2431 * would leave no possibility for the server to send back a response.)
2432 *
2433 * Whenever a transfer-coding is applied to a message-body, the set of
2434 * transfer-codings MUST include "chunked", unless the message indicates
2435 * it is terminated by closing the connection. When the "chunked"
2436 * transfer-coding is used, it MUST be the last transfer-coding applied
2437 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002438 */
2439
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002440 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002441 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002443 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002444 http_find_header2("Transfer-Encoding", 17, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002446 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2447 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 /* bad transfer-encoding (chunked followed by something else) */
2449 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002450 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002451 break;
2452 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002453 }
2454
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002456 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002457 http_find_header2("Content-Length", 14, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002458 signed long long cl;
2459
Willy Tarreauad14f752011-09-02 20:33:27 +02002460 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002461 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002463 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464
Willy Tarreauad14f752011-09-02 20:33:27 +02002465 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002466 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002468 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002469
Willy Tarreauad14f752011-09-02 20:33:27 +02002470 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002471 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002473 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002475 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002476 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002477 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002478 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002480 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002481 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482 }
2483
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484 /* bodyless requests have a known length */
2485 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002486 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002487
Willy Tarreaud787e662009-07-07 10:14:51 +02002488 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002489 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002490 req->analyse_exp = TICK_ETERNITY;
2491 return 1;
2492
2493 return_bad_req:
2494 /* We centralize bad requests processing here */
2495 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2496 /* we detected a parsing error. We want to archive this request
2497 * in the dedicated proxy area for later troubleshooting.
2498 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002499 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002500 }
2501
2502 txn->req.msg_state = HTTP_MSG_ERROR;
2503 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002504 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002505
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002506 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002507 if (s->listener->counters)
2508 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002509
2510 return_prx_cond:
2511 if (!(s->flags & SN_ERR_MASK))
2512 s->flags |= SN_ERR_PRXCOND;
2513 if (!(s->flags & SN_FINST_MASK))
2514 s->flags |= SN_FINST_R;
2515
2516 req->analysers = 0;
2517 req->analyse_exp = TICK_ETERNITY;
2518 return 0;
2519}
2520
Cyril Bonté70be45d2010-10-12 00:14:35 +02002521/* We reached the stats page through a POST request.
2522 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002523 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002524 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002525int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002526{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002527 struct proxy *px = NULL;
2528 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002529
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002530 char key[LINESIZE];
2531 int action = ST_ADM_ACTION_NONE;
2532 int reprocess = 0;
2533
2534 int total_servers = 0;
2535 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002536
2537 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002538 char *st_cur_param = NULL;
2539 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002540
Willy Tarreau572bf902012-07-02 17:01:20 +02002541 first_param = req->buf.p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002542 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543
2544 cur_param = next_param = end_params;
2545
Willy Tarreau572bf902012-07-02 17:01:20 +02002546 if (end_params >= req->buf.data + req->buf.size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002547 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002548 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002549 return 1;
2550 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002551 else if (end_params > req->buf.p + req->buf.i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002552 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002553 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002554 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002555 }
2556
2557 *end_params = '\0';
2558
Willy Tarreau295a8372011-03-10 11:25:07 +01002559 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560
2561 /*
2562 * Parse the parameters in reverse order to only store the last value.
2563 * From the html form, the backend and the action are at the end.
2564 */
2565 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002566 char *value;
2567 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568
2569 cur_param--;
2570 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002571 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002572 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002573 poffset = (cur_param != first_param ? 1 : 0);
2574 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2575 if ((plen > 0) && (plen <= sizeof(key))) {
2576 strncpy(key, cur_param + poffset, plen);
2577 key[plen - 1] = '\0';
2578 } else {
2579 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2580 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 }
2582
2583 /* Parse the value */
2584 value = key;
2585 while (*value != '\0' && *value != '=') {
2586 value++;
2587 }
2588 if (*value == '=') {
2589 /* Ok, a value is found, we can mark the end of the key */
2590 *value++ = '\0';
2591 }
2592
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002593 if (!url_decode(key) || !url_decode(value))
2594 break;
2595
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002597 if (!px && (strcmp(key, "b") == 0)) {
2598 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2599 /* the backend name is unknown or ambiguous (duplicate names) */
2600 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2601 goto out;
2602 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002603 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002604 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002605 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002606 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002607 }
2608 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002609 action = ST_ADM_ACTION_ENABLE;
2610 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002611 else if (strcmp(value, "stop") == 0) {
2612 action = ST_ADM_ACTION_STOP;
2613 }
2614 else if (strcmp(value, "start") == 0) {
2615 action = ST_ADM_ACTION_START;
2616 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002617 else if (strcmp(value, "shutdown") == 0) {
2618 action = ST_ADM_ACTION_SHUTDOWN;
2619 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002620 else {
2621 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2622 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002623 }
2624 }
2625 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002626 if (!(px && action)) {
2627 /*
2628 * Indicates that we'll need to reprocess the parameters
2629 * as soon as backend and action are known
2630 */
2631 if (!reprocess) {
2632 st_cur_param = cur_param;
2633 st_next_param = next_param;
2634 }
2635 reprocess = 1;
2636 }
2637 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002638 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002639 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002640 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 /* Not already in maintenance, we can change the server state */
2642 sv->state |= SRV_MAINTAIN;
2643 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002644 altered_servers++;
2645 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646 }
2647 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002648 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002649 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002650 /* Already in maintenance, we can change the server state */
2651 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002652 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002653 altered_servers++;
2654 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002655 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002656 break;
2657 case ST_ADM_ACTION_STOP:
2658 case ST_ADM_ACTION_START:
2659 if (action == ST_ADM_ACTION_START)
2660 sv->uweight = sv->iweight;
2661 else
2662 sv->uweight = 0;
2663
2664 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2665 /* we must take care of not pushing the server to full throttle during slow starts */
2666 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2667 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2668 else
2669 sv->eweight = BE_WEIGHT_SCALE;
2670 sv->eweight *= sv->uweight;
2671 } else {
2672 sv->eweight = sv->uweight;
2673 }
2674
2675 /* static LB algorithms are a bit harder to update */
2676 if (px->lbprm.update_server_eweight)
2677 px->lbprm.update_server_eweight(sv);
2678 else if (sv->eweight) {
2679 if (px->lbprm.set_server_status_up)
2680 px->lbprm.set_server_status_up(sv);
2681 }
2682 else {
2683 if (px->lbprm.set_server_status_down)
2684 px->lbprm.set_server_status_down(sv);
2685 }
2686 altered_servers++;
2687 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002688 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002689 case ST_ADM_ACTION_SHUTDOWN:
2690 if (px->state != PR_STSTOPPED) {
2691 struct session *sess, *sess_bck;
2692
2693 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2694 if (sess->srv_conn == sv)
2695 session_shutdown(sess, SN_ERR_KILLED);
2696
2697 altered_servers++;
2698 total_servers++;
2699 }
2700 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002701 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002702 } else {
2703 /* the server name is unknown or ambiguous (duplicate names) */
2704 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002705 }
2706 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002707 if (reprocess && px && action) {
2708 /* Now, we know the backend and the action chosen by the user.
2709 * We can safely restart from the first server parameter
2710 * to reprocess them
2711 */
2712 cur_param = st_cur_param;
2713 next_param = st_next_param;
2714 reprocess = 0;
2715 goto reprocess_servers;
2716 }
2717
Cyril Bonté70be45d2010-10-12 00:14:35 +02002718 next_param = cur_param;
2719 }
2720 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002721
2722 if (total_servers == 0) {
2723 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2724 }
2725 else if (altered_servers == 0) {
2726 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2727 }
2728 else if (altered_servers == total_servers) {
2729 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2730 }
2731 else {
2732 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2733 }
2734 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002735 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002736}
2737
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738/* returns a pointer to the first rule which forbids access (deny or http_auth),
2739 * or NULL if everything's OK.
2740 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002741static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002742http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2743{
Willy Tarreauff011f22011-01-06 17:51:27 +01002744 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002745
Willy Tarreauff011f22011-01-06 17:51:27 +01002746 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002747 int ret = 1;
2748
Willy Tarreauff011f22011-01-06 17:51:27 +01002749 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002750 continue;
2751
2752 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002753 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002754 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002755 ret = acl_pass(ret);
2756
Willy Tarreauff011f22011-01-06 17:51:27 +01002757 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002758 ret = !ret;
2759 }
2760
2761 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002762 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002763 return NULL; /* no problem */
2764 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002765 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002766 }
2767 }
2768 return NULL;
2769}
2770
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771/* This stream analyser runs all HTTP request processing which is common to
2772 * frontends and backends, which means blocking ACLs, filters, connection-close,
2773 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002774 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 * either needs more data or wants to immediately abort the request (eg: deny,
2776 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002778int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002779{
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 struct http_txn *txn = &s->txn;
2781 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002782 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002783 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002785 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002786 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002787
Willy Tarreau655dce92009-11-08 13:10:58 +01002788 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002789 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002790 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002791 return 0;
2792 }
2793
Willy Tarreau3a816292009-07-07 10:55:49 +02002794 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002795 req->analyse_exp = TICK_ETERNITY;
2796
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002797 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 +02002798 now_ms, __FUNCTION__,
2799 s,
2800 req,
2801 req->rex, req->wex,
2802 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002803 req->buf.i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002804 req->analysers);
2805
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002806 /* first check whether we have some ACLs set to block this request */
2807 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002808 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002809
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002810 ret = acl_pass(ret);
2811 if (cond->pol == ACL_COND_UNLESS)
2812 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002813
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002814 if (ret) {
2815 txn->status = 403;
2816 /* let's log the request time */
2817 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002818 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002819 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002823
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002824 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002825 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002826
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002827 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002828 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002829 do_stats = stats_check_uri(s->rep->prod, txn, px);
2830 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002831 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 +01002832 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002833 else
2834 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002835
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002836 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002837 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002838 txn->status = 403;
2839 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002840 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002841 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002842 s->fe->fe_counters.denied_req++;
2843 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2844 s->be->be_counters.denied_req++;
2845 if (s->listener->counters)
2846 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002847 goto return_prx_cond;
2848 }
2849
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002850 /* try headers filters */
2851 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002852 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002854
Willy Tarreau59234e92008-11-30 23:51:27 +01002855 /* has the request been denied ? */
2856 if (txn->flags & TX_CLDENY) {
2857 /* no need to go further */
2858 txn->status = 403;
2859 /* let's log the request time */
2860 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002861 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002862 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002863 goto return_prx_cond;
2864 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002865
2866 /* When a connection is tarpitted, we use the tarpit timeout,
2867 * which may be the same as the connect timeout if unspecified.
2868 * If unset, then set it to zero because we really want it to
2869 * eventually expire. We build the tarpit as an analyser.
2870 */
2871 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002872 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002873 /* wipe the request out so that we can drop the connection early
2874 * if the client closes first.
2875 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002876 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002877 req->analysers = 0; /* remove switching rules etc... */
2878 req->analysers |= AN_REQ_HTTP_TARPIT;
2879 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2880 if (!req->analyse_exp)
2881 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002882 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002883 return 1;
2884 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002885 }
Willy Tarreau06619262006-12-17 08:37:22 +01002886
Willy Tarreau5b154472009-12-21 20:11:07 +01002887 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2888 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002889 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2890 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002891 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2892 * avoid to redo the same work if FE and BE have the same settings (common).
2893 * The method consists in checking if options changed between the two calls
2894 * (implying that either one is non-null, or one of them is non-null and we
2895 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002896 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002897
Willy Tarreaudc008c52010-02-01 16:20:08 +01002898 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2899 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2900 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2901 (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 +01002902 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002903
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002904 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2905 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002906 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002907 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2908 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002909 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002910 tmp = TX_CON_WANT_CLO;
2911
Willy Tarreau5b154472009-12-21 20:11:07 +01002912 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2913 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002914
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002915 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2916 /* parse the Connection header and possibly clean it */
2917 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002918 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002919 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2920 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002921 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002922 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002923 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002924 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002925 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002926
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002927 /* check if client or config asks for explicit close in KAL/SCL */
2928 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2929 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2930 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002931 (!(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 +01002932 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002933 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002934 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002935 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2936 }
Willy Tarreau78599912009-10-17 20:12:21 +02002937
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002938 /* we can be blocked here because the request needs to be authenticated,
2939 * either to pass or to access stats.
2940 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002941 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002942 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002943 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002944
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002945 if (!realm)
2946 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2947
Willy Tarreau844a7e72010-01-31 21:46:18 +01002948 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002949 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002950 txn->status = 401;
2951 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002952 /* on 401 we still count one error, because normal browsing
2953 * won't significantly increase the counter but brute force
2954 * attempts will.
2955 */
2956 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002957 goto return_prx_cond;
2958 }
2959
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002960 /* add request headers from the rule sets in the same order */
2961 list_for_each_entry(wl, &px->req_add, list) {
2962 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002963 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002964 ret = acl_pass(ret);
2965 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2966 ret = !ret;
2967 if (!ret)
2968 continue;
2969 }
2970
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002971 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002972 goto return_bad_req;
2973 }
2974
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002975 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002976 struct stats_admin_rule *stats_admin_rule;
2977
2978 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 * FIXME!!! that one is rather dangerous, we want to
2980 * make it follow standard rules (eg: clear req->analysers).
2981 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002982
Cyril Bonté474be412010-10-12 00:14:36 +02002983 /* now check whether we have some admin rules for this request */
2984 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2985 int ret = 1;
2986
2987 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002988 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 +02002989 ret = acl_pass(ret);
2990 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2991 ret = !ret;
2992 }
2993
2994 if (ret) {
2995 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002996 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002997 break;
2998 }
2999 }
3000
Cyril Bonté70be45d2010-10-12 00:14:35 +02003001 /* Was the status page requested with a POST ? */
3002 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003003 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003004 if (msg->msg_state < HTTP_MSG_100_SENT) {
3005 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3006 * send an HTTP/1.1 100 Continue intermediate response.
3007 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003008 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003009 struct hdr_ctx ctx;
3010 ctx.idx = 0;
3011 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003012 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003013 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003014 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003015 }
3016 }
3017 msg->msg_state = HTTP_MSG_100_SENT;
3018 s->logs.tv_request = now; /* update the request timer to reflect full request */
3019 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003020 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003021 /* we need more data */
3022 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003023 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003024 return 0;
3025 }
Cyril Bonté474be412010-10-12 00:14:36 +02003026 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003027 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003028 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003029 }
3030
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003031 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003032 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003033 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau3cefd522012-08-30 15:49:18 +02003034 copy_target(&s->target, &s->rep->prod->conn.target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +02003035 s->rep->prod->conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003036 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003037 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003038 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3039 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003040
3041 return 0;
3042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 /* check whether we have some ACLs set to redirect this request */
3046 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003047 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003048
Willy Tarreauf285f542010-01-03 20:03:03 +01003049 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003050 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003051 ret = acl_pass(ret);
3052 if (rule->cond->pol == ACL_COND_UNLESS)
3053 ret = !ret;
3054 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003055
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003056 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003057 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003059
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 /* build redirect message */
3061 switch(rule->code) {
3062 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 msg_fmt = HTTP_303;
3064 break;
3065 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 msg_fmt = HTTP_301;
3067 break;
3068 case 302:
3069 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003070 msg_fmt = HTTP_302;
3071 break;
3072 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003074 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003078 case REDIRECT_TYPE_SCHEME: {
3079 const char *path;
3080 const char *host;
3081 struct hdr_ctx ctx;
3082 int pathlen;
3083 int hostlen;
3084
3085 host = "";
3086 hostlen = 0;
3087 ctx.idx = 0;
3088 if (http_find_header2("Host", 4, txn->req.buf->buf.p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3089 host = ctx.line + ctx.val;
3090 hostlen = ctx.vlen;
3091 }
3092
3093 path = http_get_path(txn);
3094 /* build message using path */
3095 if (path) {
3096 pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path;
3097 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3098 int qs = 0;
3099 while (qs < pathlen) {
3100 if (path[qs] == '?') {
3101 pathlen = qs;
3102 break;
3103 }
3104 qs++;
3105 }
3106 }
3107 } else {
3108 path = "/";
3109 pathlen = 1;
3110 }
3111
3112 /* check if we can add scheme + "://" + host + path */
3113 if (rdr.len + rule->rdr_len + 3 + hostlen + pathlen > rdr.size - 4)
3114 goto return_bad_req;
3115
3116 /* add scheme */
3117 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3118 rdr.len += rule->rdr_len;
3119
3120 /* add "://" */
3121 memcpy(rdr.str + rdr.len, "://", 3);
3122 rdr.len += 3;
3123
3124 /* add host */
3125 memcpy(rdr.str + rdr.len, host, hostlen);
3126 rdr.len += hostlen;
3127
3128 /* add path */
3129 memcpy(rdr.str + rdr.len, path, pathlen);
3130 rdr.len += pathlen;
3131
3132 /* append a slash at the end of the location is needed and missing */
3133 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3134 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3135 if (rdr.len > rdr.size - 5)
3136 goto return_bad_req;
3137 rdr.str[rdr.len] = '/';
3138 rdr.len++;
3139 }
3140
3141 break;
3142 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003143 case REDIRECT_TYPE_PREFIX: {
3144 const char *path;
3145 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003146
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003147 path = http_get_path(txn);
3148 /* build message using path */
3149 if (path) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003150 pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003151 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3152 int qs = 0;
3153 while (qs < pathlen) {
3154 if (path[qs] == '?') {
3155 pathlen = qs;
3156 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003157 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003158 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003159 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003160 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003161 } else {
3162 path = "/";
3163 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003164 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003165
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003166 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003167 goto return_bad_req;
3168
3169 /* add prefix. Note that if prefix == "/", we don't want to
3170 * add anything, otherwise it makes it hard for the user to
3171 * configure a self-redirection.
3172 */
3173 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003174 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3175 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003176 }
3177
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003178 /* add path */
3179 memcpy(rdr.str + rdr.len, path, pathlen);
3180 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003181
3182 /* append a slash at the end of the location is needed and missing */
3183 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3184 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3185 if (rdr.len > rdr.size - 5)
3186 goto return_bad_req;
3187 rdr.str[rdr.len] = '/';
3188 rdr.len++;
3189 }
3190
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003191 break;
3192 }
3193 case REDIRECT_TYPE_LOCATION:
3194 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003195 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003196 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 /* add location */
3199 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3200 rdr.len += rule->rdr_len;
3201 break;
3202 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 if (rule->cookie_len) {
3205 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3206 rdr.len += 14;
3207 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3208 rdr.len += rule->cookie_len;
3209 memcpy(rdr.str + rdr.len, "\r\n", 2);
3210 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003211 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003212
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003213 /* add end of headers and the keep-alive/close status.
3214 * We may choose to set keep-alive if the Location begins
3215 * with a slash, because the client will come back to the
3216 * same server.
3217 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 txn->status = rule->code;
3219 /* let's log the request time */
3220 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003221
3222 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003223 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3224 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003225 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3226 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3227 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003228 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003229 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3230 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3231 rdr.len += 30;
3232 } else {
3233 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3234 rdr.len += 24;
3235 }
Willy Tarreau75661452010-01-10 10:35:01 +01003236 }
3237 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3238 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003239 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003240 /* "eat" the request */
Willy Tarreau572bf902012-07-02 17:01:20 +02003241 bi_fast_delete(&req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003242 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003243 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003244 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3245 txn->req.msg_state = HTTP_MSG_CLOSED;
3246 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003247 break;
3248 } else {
3249 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003250 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3251 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3252 rdr.len += 29;
3253 } else {
3254 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3255 rdr.len += 23;
3256 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003257 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003258 goto return_prx_cond;
3259 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003260 }
3261 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003262
Willy Tarreau2be39392010-01-03 17:24:51 +01003263 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3264 * If this happens, then the data will not come immediately, so we must
3265 * send all what we have without waiting. Note that due to the small gain
3266 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003267 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003268 * itself once used.
3269 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003270 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003271
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003272 /* that's OK for us now, let's move on to next analysers */
3273 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275 return_bad_req:
3276 /* We centralize bad requests processing here */
3277 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3278 /* we detected a parsing error. We want to archive this request
3279 * in the dedicated proxy area for later troubleshooting.
3280 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003281 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003282 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003283
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003284 txn->req.msg_state = HTTP_MSG_ERROR;
3285 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003286 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003287
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003288 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003289 if (s->listener->counters)
3290 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003292 return_prx_cond:
3293 if (!(s->flags & SN_ERR_MASK))
3294 s->flags |= SN_ERR_PRXCOND;
3295 if (!(s->flags & SN_FINST_MASK))
3296 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003297
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003298 req->analysers = 0;
3299 req->analyse_exp = TICK_ETERNITY;
3300 return 0;
3301}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303/* This function performs all the processing enabled for the current request.
3304 * It returns 1 if the processing can continue on next analysers, or zero if it
3305 * needs more data, encounters an error, or wants to immediately abort the
3306 * request. It relies on buffers flags, and updates s->req->analysers.
3307 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003308int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309{
3310 struct http_txn *txn = &s->txn;
3311 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003312
Willy Tarreau655dce92009-11-08 13:10:58 +01003313 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003314 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003315 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003316 return 0;
3317 }
3318
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003319 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 +02003320 now_ms, __FUNCTION__,
3321 s,
3322 req,
3323 req->rex, req->wex,
3324 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003325 req->buf.i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003326 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003327
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 /*
3329 * Right now, we know that we have processed the entire headers
3330 * and that unwanted requests have been filtered out. We can do
3331 * whatever we want with the remaining request. Also, now we
3332 * may have separate values for ->fe, ->be.
3333 */
Willy Tarreau06619262006-12-17 08:37:22 +01003334
Willy Tarreau59234e92008-11-30 23:51:27 +01003335 /*
3336 * If HTTP PROXY is set we simply get remote server address
3337 * parsing incoming request.
3338 */
3339 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau986a9d22012-08-30 21:11:38 +02003340 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 +01003341 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003342
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003344 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 * Note that doing so might move headers in the request, but
3346 * the fields will stay coherent and the URI will not move.
3347 * This should only be performed in the backend.
3348 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003349 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003350 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3351 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003352
Willy Tarreau59234e92008-11-30 23:51:27 +01003353 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003354 * 8: the appsession cookie was looked up very early in 1.2,
3355 * so let's do the same now.
3356 */
3357
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003358 /* It needs to look into the URI unless persistence must be ignored */
3359 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003360 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 +01003361 }
3362
William Lallemanda73203e2012-03-12 12:48:57 +01003363 /* add unique-id if "header-unique-id" is specified */
3364
3365 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3366 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3367
3368 if (s->fe->header_unique_id && s->unique_id) {
3369 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3370 if (ret < 0 || ret > global.tune.bufsize)
3371 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003372 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003373 goto return_bad_req;
3374 }
3375
Cyril Bontéb21570a2009-11-29 20:04:48 +01003376 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003377 * 9: add X-Forwarded-For if either the frontend or the backend
3378 * asks for it.
3379 */
3380 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003381 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003382 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003383 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3384 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau572bf902012-07-02 17:01:20 +02003385 req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003386 /* The header is set to be added only if none is present
3387 * and we found it, so don't do anything.
3388 */
3389 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003390 else if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003391 /* Add an X-Forwarded-For header unless the source IP is
3392 * in the 'except' network range.
3393 */
3394 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003395 (((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 +01003396 != s->fe->except_net.s_addr) &&
3397 (!s->be->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->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003400 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003401 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003402 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003403
3404 /* Note: we rely on the backend to get the header name to be used for
3405 * x-forwarded-for, because the header is really meant for the backends.
3406 * However, if the backend did not specify any option, we have to rely
3407 * on the frontend's header name.
3408 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003409 if (s->be->fwdfor_hdr_len) {
3410 len = s->be->fwdfor_hdr_len;
3411 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003412 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003413 len = s->fe->fwdfor_hdr_len;
3414 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003415 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003416 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003417
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003418 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003419 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003420 }
3421 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003422 else if (s->req->prod->conn.addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003423 /* FIXME: for the sake of completeness, we should also support
3424 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003425 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003426 int len;
3427 char pn[INET6_ADDRSTRLEN];
3428 inet_ntop(AF_INET6,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003429 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn.addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003430 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003431
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 /* Note: we rely on the backend to get the header name to be used for
3433 * x-forwarded-for, because the header is really meant for the backends.
3434 * However, if the backend did not specify any option, we have to rely
3435 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003436 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 if (s->be->fwdfor_hdr_len) {
3438 len = s->be->fwdfor_hdr_len;
3439 memcpy(trash, s->be->fwdfor_hdr_name, len);
3440 } else {
3441 len = s->fe->fwdfor_hdr_len;
3442 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003443 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003444 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003445
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003446 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 goto return_bad_req;
3448 }
3449 }
3450
3451 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003452 * 10: add X-Original-To if either the frontend or the backend
3453 * asks for it.
3454 */
3455 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3456
3457 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003458 if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003459 /* Add an X-Original-To header unless the destination IP is
3460 * in the 'except' network range.
3461 */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003462 conn_get_to_addr(&s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003463
Willy Tarreau986a9d22012-08-30 21:11:38 +02003464 if (s->req->prod->conn.addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003465 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003466 (((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 +02003467 != s->fe->except_to.s_addr) &&
3468 (!s->be->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->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003470 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003471 int len;
3472 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003473 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003474
3475 /* Note: we rely on the backend to get the header name to be used for
3476 * x-original-to, because the header is really meant for the backends.
3477 * However, if the backend did not specify any option, we have to rely
3478 * on the frontend's header name.
3479 */
3480 if (s->be->orgto_hdr_len) {
3481 len = s->be->orgto_hdr_len;
3482 memcpy(trash, s->be->orgto_hdr_name, len);
3483 } else {
3484 len = s->fe->orgto_hdr_len;
3485 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003486 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003487 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3488
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003489 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003490 goto return_bad_req;
3491 }
3492 }
3493 }
3494
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003495 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3496 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003497 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003498 unsigned int want_flags = 0;
3499
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003500 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003501 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3502 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3503 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003504 want_flags |= TX_CON_CLO_SET;
3505 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003506 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3507 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3508 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003509 want_flags |= TX_CON_KAL_SET;
3510 }
3511
3512 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003513 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003514 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003515
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003516
Willy Tarreau522d6c02009-12-06 18:49:18 +01003517 /* If we have no server assigned yet and we're balancing on url_param
3518 * with a POST request, we may be interested in checking the body for
3519 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003520 */
3521 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3522 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003523 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003524 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003525 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003526 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003527 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003528
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003529 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003530 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003531#ifdef TCP_QUICKACK
3532 /* We expect some data from the client. Unless we know for sure
3533 * we already have a full request, we have to re-enable quick-ack
3534 * in case we previously disabled it, otherwise we might cause
3535 * the client to delay further data.
3536 */
3537 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003538 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02003539 (msg->body_len > req->buf.i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003540 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003541#endif
3542 }
Willy Tarreau03945942009-12-22 16:50:27 +01003543
Willy Tarreau59234e92008-11-30 23:51:27 +01003544 /*************************************************************
3545 * OK, that's finished for the headers. We have done what we *
3546 * could. Let's switch to the DATA state. *
3547 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 req->analyse_exp = TICK_ETERNITY;
3549 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003550
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003551 /* if the server closes the connection, we want to immediately react
3552 * and close the socket to save packets and syscalls.
3553 */
3554 req->cons->flags |= SI_FL_NOHALF;
3555
Willy Tarreau59234e92008-11-30 23:51:27 +01003556 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003557 /* OK let's go on with the BODY now */
3558 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003559
Willy Tarreau59234e92008-11-30 23:51:27 +01003560 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003561 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003562 /* we detected a parsing error. We want to archive this request
3563 * in the dedicated proxy area for later troubleshooting.
3564 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003565 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003566 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003567
Willy Tarreau59234e92008-11-30 23:51:27 +01003568 txn->req.msg_state = HTTP_MSG_ERROR;
3569 txn->status = 400;
3570 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003571 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003572
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003573 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003574 if (s->listener->counters)
3575 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003576
Willy Tarreau59234e92008-11-30 23:51:27 +01003577 if (!(s->flags & SN_ERR_MASK))
3578 s->flags |= SN_ERR_PRXCOND;
3579 if (!(s->flags & SN_FINST_MASK))
3580 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003581 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003582}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003583
Willy Tarreau60b85b02008-11-30 23:28:40 +01003584/* This function is an analyser which processes the HTTP tarpit. It always
3585 * returns zero, at the beginning because it prevents any other processing
3586 * from occurring, and at the end because it terminates the request.
3587 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003588int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003589{
3590 struct http_txn *txn = &s->txn;
3591
3592 /* This connection is being tarpitted. The CLIENT side has
3593 * already set the connect expiration date to the right
3594 * timeout. We just have to check that the client is still
3595 * there and that the timeout has not expired.
3596 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003597 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003598 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003599 !tick_is_expired(req->analyse_exp, now_ms))
3600 return 0;
3601
3602 /* We will set the queue timer to the time spent, just for
3603 * logging purposes. We fake a 500 server error, so that the
3604 * attacker will not suspect his connection has been tarpitted.
3605 * It will not cause trouble to the logs because we can exclude
3606 * the tarpitted connections by filtering on the 'PT' status flags.
3607 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003608 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3609
3610 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003611 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003612 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003613
3614 req->analysers = 0;
3615 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003616
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003617 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003618 if (s->listener->counters)
3619 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003620
Willy Tarreau60b85b02008-11-30 23:28:40 +01003621 if (!(s->flags & SN_ERR_MASK))
3622 s->flags |= SN_ERR_PRXCOND;
3623 if (!(s->flags & SN_FINST_MASK))
3624 s->flags |= SN_FINST_T;
3625 return 0;
3626}
3627
Willy Tarreaud34af782008-11-30 23:36:37 +01003628/* This function is an analyser which processes the HTTP request body. It looks
3629 * for parameters to be used for the load balancing algorithm (url_param). It
3630 * must only be called after the standard HTTP request processing has occurred,
3631 * because it expects the request to be parsed. It returns zero if it needs to
3632 * read more data, or 1 once it has completed its analysis.
3633 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003634int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003635{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003637 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003638 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003639
3640 /* We have to parse the HTTP request body to find any required data.
3641 * "balance url_param check_post" should have been the only way to get
3642 * into this. We were brought here after HTTP header analysis, so all
3643 * related structures are ready.
3644 */
3645
Willy Tarreau522d6c02009-12-06 18:49:18 +01003646 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3647 goto missing_data;
3648
3649 if (msg->msg_state < HTTP_MSG_100_SENT) {
3650 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3651 * send an HTTP/1.1 100 Continue intermediate response.
3652 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003653 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654 struct hdr_ctx ctx;
3655 ctx.idx = 0;
3656 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003657 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003658 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003659 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003660 }
3661 }
3662 msg->msg_state = HTTP_MSG_100_SENT;
3663 }
3664
3665 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003666 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02003667 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003668 * is still null. We must save the body in msg->next because it
3669 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003670 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003671 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003672
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003673 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003674 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3675 else
3676 msg->msg_state = HTTP_MSG_DATA;
3677 }
3678
3679 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003680 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003681 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003682 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003683 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003684 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003685
Willy Tarreau115acb92009-12-26 13:56:06 +01003686 if (!ret)
3687 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003688 else if (ret < 0) {
3689 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003690 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003691 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003692 }
3693
Willy Tarreaud98cf932009-12-27 22:54:55 +01003694 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003695 * We have the first data byte is in msg->sov. We're waiting for at
3696 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003697 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003698
Willy Tarreau124d9912011-03-01 20:30:48 +01003699 if (msg->body_len < limit)
3700 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003701
Willy Tarreau572bf902012-07-02 17:01:20 +02003702 if (req->buf.i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003703 goto http_end;
3704
3705 missing_data:
3706 /* we get here if we need to wait for more data */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02003707 if (buffer_full(&req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003708 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003709 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003710 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003711
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003712 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003713 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003714 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003715
3716 if (!(s->flags & SN_ERR_MASK))
3717 s->flags |= SN_ERR_CLITO;
3718 if (!(s->flags & SN_FINST_MASK))
3719 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003720 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003721 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003722
3723 /* we get here if we need to wait for more data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003724 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(&req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003725 /* Not enough data. We'll re-use the http-request
3726 * timeout here. Ideally, we should set the timeout
3727 * relative to the accept() date. We just set the
3728 * request timeout once at the beginning of the
3729 * request.
3730 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003731 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003732 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003733 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003734 return 0;
3735 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003736
3737 http_end:
3738 /* The situation will not evolve, so let's give up on the analysis. */
3739 s->logs.tv_request = now; /* update the request timer to reflect full request */
3740 req->analysers &= ~an_bit;
3741 req->analyse_exp = TICK_ETERNITY;
3742 return 1;
3743
3744 return_bad_req: /* let's centralize all bad requests */
3745 txn->req.msg_state = HTTP_MSG_ERROR;
3746 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003747 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003748
Willy Tarreau79ebac62010-06-07 13:47:49 +02003749 if (!(s->flags & SN_ERR_MASK))
3750 s->flags |= SN_ERR_PRXCOND;
3751 if (!(s->flags & SN_FINST_MASK))
3752 s->flags |= SN_FINST_R;
3753
Willy Tarreau522d6c02009-12-06 18:49:18 +01003754 return_err_msg:
3755 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003756 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003757 if (s->listener->counters)
3758 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003759 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003760}
3761
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003762/* send a server's name with an outgoing request over an established connection.
3763 * Note: this function is designed to be called once the request has been scheduled
3764 * for being forwarded. This is the reason why it rewinds the buffer before
3765 * proceeding.
3766 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003767int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003768
3769 struct hdr_ctx ctx;
3770
Mark Lamourinec2247f02012-01-04 13:02:01 -05003771 char *hdr_name = be->server_id_hdr_name;
3772 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau7421efb2012-07-02 15:11:27 +02003773 struct channel *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003774 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003775 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003776
William Lallemandd9e90662012-01-30 17:27:17 +01003777 ctx.idx = 0;
3778
Willy Tarreau572bf902012-07-02 17:01:20 +02003779 old_o = req->buf.o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003780 if (old_o) {
3781 /* The request was already skipped, let's restore it */
Willy Tarreaua75bcef2012-08-24 22:56:11 +02003782 b_rew(&req->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003783 }
3784
Willy Tarreau572bf902012-07-02 17:01:20 +02003785 old_i = req->buf.i;
3786 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->buf.p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003787 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003788 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003789 }
3790
3791 /* Add the new header requested with the server value */
3792 hdr_val = trash;
3793 memcpy(hdr_val, hdr_name, hdr_name_len);
3794 hdr_val += hdr_name_len;
3795 *hdr_val++ = ':';
3796 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003797 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003798 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003799
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003800 if (old_o) {
3801 /* If this was a forwarded request, we must readjust the amount of
3802 * data to be forwarded in order to take into account the size
3803 * variations.
3804 */
Willy Tarreaua75bcef2012-08-24 22:56:11 +02003805 b_adv(&req->buf, old_o + req->buf.i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003806 }
3807
Mark Lamourinec2247f02012-01-04 13:02:01 -05003808 return 0;
3809}
3810
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811/* Terminate current transaction and prepare a new one. This is very tricky
3812 * right now but it works.
3813 */
3814void http_end_txn_clean_session(struct session *s)
3815{
3816 /* FIXME: We need a more portable way of releasing a backend's and a
3817 * server's connections. We need a safer way to reinitialize buffer
3818 * flags. We also need a more accurate method for computing per-request
3819 * data.
3820 */
3821 http_silent_debug(__LINE__, s);
3822
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003823 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003824 si_shutr(s->req->cons);
3825 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826
3827 http_silent_debug(__LINE__, s);
3828
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003829 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003830 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003831 if (unlikely(s->srv_conn))
3832 sess_change_server(s, NULL);
3833 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834
3835 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3836 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003837 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003838
3839 if (s->txn.status) {
3840 int n;
3841
3842 n = s->txn.status / 100;
3843 if (n < 1 || n > 5)
3844 n = 0;
3845
3846 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003847 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003848
Willy Tarreau24657792010-02-26 10:30:28 +01003849 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003851 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852 }
3853
3854 /* don't count other requests' data */
Willy Tarreau572bf902012-07-02 17:01:20 +02003855 s->logs.bytes_in -= s->req->buf.i;
3856 s->logs.bytes_out -= s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003857
3858 /* let's do a final log if we need it */
3859 if (s->logs.logwait &&
3860 !(s->flags & SN_MONITOR) &&
3861 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3862 s->do_log(s);
3863 }
3864
3865 s->logs.accept_date = date; /* user-visible date for logging */
3866 s->logs.tv_accept = now; /* corrected date for internal use */
3867 tv_zero(&s->logs.tv_request);
3868 s->logs.t_queue = -1;
3869 s->logs.t_connect = -1;
3870 s->logs.t_data = -1;
3871 s->logs.t_close = 0;
3872 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3873 s->logs.srv_queue_size = 0; /* we will get this number soon */
3874
Willy Tarreau572bf902012-07-02 17:01:20 +02003875 s->logs.bytes_in = s->req->total = s->req->buf.i;
3876 s->logs.bytes_out = s->rep->total = s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877
3878 if (s->pend_pos)
3879 pendconn_free(s->pend_pos);
3880
Willy Tarreau827aee92011-03-10 16:55:02 +01003881 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 if (s->flags & SN_CURR_SESS) {
3883 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003884 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003886 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3887 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003888 }
3889
Willy Tarreau9e000c62011-03-10 14:03:36 +01003890 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891
3892 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003893 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +02003894 s->req->cons->conn.flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003896 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003897 s->req->cons->err_loc = NULL;
3898 s->req->cons->exp = TICK_ETERNITY;
3899 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003900 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
3901 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 +02003902 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 +01003903 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3904 s->txn.meth = 0;
3905 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003906 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003907 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908 s->req->cons->flags |= SI_FL_INDEP_STR;
3909
Willy Tarreau96e31212011-05-30 18:10:30 +02003910 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003911 s->req->flags |= CF_NEVER_WAIT;
3912 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003913 }
3914
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 /* if the request buffer is not empty, it means we're
3916 * about to process another request, so send pending
3917 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003918 * Just don't do this if the buffer is close to be full,
3919 * because the request will wait for it to flush a little
3920 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003921 */
Willy Tarreau572bf902012-07-02 17:01:20 +02003922 if (s->req->buf.i) {
3923 if (s->rep->buf.o &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02003924 !buffer_full(&s->rep->buf, global.tune.maxrewrite) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02003925 bi_end(&s->rep->buf) <= s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003926 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01003927 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003928
3929 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003930 channel_auto_read(s->req);
3931 channel_auto_close(s->req);
3932 channel_auto_read(s->rep);
3933 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003934
Willy Tarreau342b11c2010-11-24 16:22:09 +01003935 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 s->rep->analysers = 0;
3937
3938 http_silent_debug(__LINE__, s);
3939}
3940
3941
3942/* This function updates the request state machine according to the response
3943 * state machine and buffer flags. It returns 1 if it changes anything (flag
3944 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3945 * it is only used to find when a request/response couple is complete. Both
3946 * this function and its equivalent should loop until both return zero. It
3947 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3948 */
3949int http_sync_req_state(struct session *s)
3950{
Willy Tarreau7421efb2012-07-02 15:11:27 +02003951 struct channel *buf = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 struct http_txn *txn = &s->txn;
3953 unsigned int old_flags = buf->flags;
3954 unsigned int old_state = txn->req.msg_state;
3955
3956 http_silent_debug(__LINE__, s);
3957 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3958 return 0;
3959
3960 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003961 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003962 * We can shut the read side unless we want to abort_on_close,
3963 * or we have a POST request. The issue with POST requests is
3964 * that some browsers still send a CRLF after the request, and
3965 * this CRLF must be read so that it does not remain in the kernel
3966 * buffers, otherwise a close could cause an RST on some systems
3967 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003968 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003969 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003970 channel_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971
3972 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3973 goto wait_other_side;
3974
3975 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3976 /* The server has not finished to respond, so we
3977 * don't want to move in order not to upset it.
3978 */
3979 goto wait_other_side;
3980 }
3981
3982 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3983 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003984 channel_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 txn->req.msg_state = HTTP_MSG_TUNNEL;
3986 goto wait_other_side;
3987 }
3988
3989 /* When we get here, it means that both the request and the
3990 * response have finished receiving. Depending on the connection
3991 * mode, we'll have to wait for the last bytes to leave in either
3992 * direction, and sometimes for a close to be effective.
3993 */
3994
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003995 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3996 /* Server-close mode : queue a connection close to the server */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003997 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003998 channel_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003999 }
4000 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4001 /* Option forceclose is set, or either side wants to close,
4002 * let's enforce it now that we're not expecting any new
4003 * data to come. The caller knows the session is complete
4004 * once both states are CLOSED.
4005 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004006 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004007 channel_shutr_now(buf);
4008 channel_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004010 }
4011 else {
4012 /* The last possible modes are keep-alive and tunnel. Since tunnel
4013 * mode does not set the body analyser, we can't reach this place
4014 * in tunnel mode, so we're left with keep-alive only.
4015 * This mode is currently not implemented, we switch to tunnel mode.
4016 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004017 channel_auto_read(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004018 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 }
4020
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004021 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004023 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4024
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004025 if (!channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 txn->req.msg_state = HTTP_MSG_CLOSING;
4027 goto http_msg_closing;
4028 }
4029 else {
4030 txn->req.msg_state = HTTP_MSG_CLOSED;
4031 goto http_msg_closed;
4032 }
4033 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004034 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 }
4036
4037 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4038 http_msg_closing:
4039 /* nothing else to forward, just waiting for the output buffer
4040 * to be empty and for the shutw_now to take effect.
4041 */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004042 if (channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 txn->req.msg_state = HTTP_MSG_CLOSED;
4044 goto http_msg_closed;
4045 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004046 else if (buf->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 txn->req.msg_state = HTTP_MSG_ERROR;
4048 goto wait_other_side;
4049 }
4050 }
4051
4052 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4053 http_msg_closed:
4054 goto wait_other_side;
4055 }
4056
4057 wait_other_side:
4058 http_silent_debug(__LINE__, s);
4059 return txn->req.msg_state != old_state || buf->flags != old_flags;
4060}
4061
4062
4063/* This function updates the response state machine according to the request
4064 * state machine and buffer flags. It returns 1 if it changes anything (flag
4065 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4066 * it is only used to find when a request/response couple is complete. Both
4067 * this function and its equivalent should loop until both return zero. It
4068 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4069 */
4070int http_sync_res_state(struct session *s)
4071{
Willy Tarreau7421efb2012-07-02 15:11:27 +02004072 struct channel *buf = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 struct http_txn *txn = &s->txn;
4074 unsigned int old_flags = buf->flags;
4075 unsigned int old_state = txn->rsp.msg_state;
4076
4077 http_silent_debug(__LINE__, s);
4078 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4079 return 0;
4080
4081 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4082 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004083 * still monitor the server connection for a possible close
4084 * while the request is being uploaded, so we don't disable
4085 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004087 /* channel_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088
4089 if (txn->req.msg_state == HTTP_MSG_ERROR)
4090 goto wait_other_side;
4091
4092 if (txn->req.msg_state < HTTP_MSG_DONE) {
4093 /* The client seems to still be sending data, probably
4094 * because we got an error response during an upload.
4095 * We have the choice of either breaking the connection
4096 * or letting it pass through. Let's do the later.
4097 */
4098 goto wait_other_side;
4099 }
4100
4101 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4102 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004103 channel_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4105 goto wait_other_side;
4106 }
4107
4108 /* When we get here, it means that both the request and the
4109 * response have finished receiving. Depending on the connection
4110 * mode, we'll have to wait for the last bytes to leave in either
4111 * direction, and sometimes for a close to be effective.
4112 */
4113
4114 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4115 /* Server-close mode : shut read and wait for the request
4116 * side to close its output buffer. The caller will detect
4117 * when we're in DONE and the other is in CLOSED and will
4118 * catch that for the final cleanup.
4119 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004120 if (!(buf->flags & (CF_SHUTR|CF_SHUTR_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004121 channel_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004123 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4124 /* Option forceclose is set, or either side wants to close,
4125 * let's enforce it now that we're not expecting any new
4126 * data to come. The caller knows the session is complete
4127 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004129 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004130 channel_shutr_now(buf);
4131 channel_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004132 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 }
4134 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004135 /* The last possible modes are keep-alive and tunnel. Since tunnel
4136 * mode does not set the body analyser, we can't reach this place
4137 * in tunnel mode, so we're left with keep-alive only.
4138 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004140 channel_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 }
4143
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004144 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 /* if we've just closed an output, let's switch */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004146 if (!channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4148 goto http_msg_closing;
4149 }
4150 else {
4151 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4152 goto http_msg_closed;
4153 }
4154 }
4155 goto wait_other_side;
4156 }
4157
4158 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4159 http_msg_closing:
4160 /* nothing else to forward, just waiting for the output buffer
4161 * to be empty and for the shutw_now to take effect.
4162 */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004163 if (channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4165 goto http_msg_closed;
4166 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004167 else if (buf->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004168 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004169 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004170 if (target_srv(&s->target))
4171 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004172 goto wait_other_side;
4173 }
4174 }
4175
4176 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4177 http_msg_closed:
4178 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004179 bi_erase(buf);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004180 channel_auto_close(buf);
4181 channel_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182 goto wait_other_side;
4183 }
4184
4185 wait_other_side:
4186 http_silent_debug(__LINE__, s);
4187 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4188}
4189
4190
4191/* Resync the request and response state machines. Return 1 if either state
4192 * changes.
4193 */
4194int http_resync_states(struct session *s)
4195{
4196 struct http_txn *txn = &s->txn;
4197 int old_req_state = txn->req.msg_state;
4198 int old_res_state = txn->rsp.msg_state;
4199
4200 http_silent_debug(__LINE__, s);
4201 http_sync_req_state(s);
4202 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004203 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 if (!http_sync_res_state(s))
4205 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004206 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 if (!http_sync_req_state(s))
4208 break;
4209 }
4210 http_silent_debug(__LINE__, s);
4211 /* OK, both state machines agree on a compatible state.
4212 * There are a few cases we're interested in :
4213 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4214 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4215 * directions, so let's simply disable both analysers.
4216 * - HTTP_MSG_CLOSED on the response only means we must abort the
4217 * request.
4218 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4219 * with server-close mode means we've completed one request and we
4220 * must re-initialize the server connection.
4221 */
4222
4223 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4224 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4225 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4226 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4227 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004228 channel_auto_close(s->req);
4229 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004231 channel_auto_close(s->rep);
4232 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004233 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004234 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4235 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004236 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004237 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004238 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004239 channel_auto_close(s->rep);
4240 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004241 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004242 channel_abort(s->req);
4243 channel_auto_close(s->req);
4244 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004245 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 }
4247 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4248 txn->rsp.msg_state == HTTP_MSG_DONE &&
4249 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4250 /* server-close: terminate this server connection and
4251 * reinitialize a fresh-new transaction.
4252 */
4253 http_end_txn_clean_session(s);
4254 }
4255
4256 http_silent_debug(__LINE__, s);
4257 return txn->req.msg_state != old_req_state ||
4258 txn->rsp.msg_state != old_res_state;
4259}
4260
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261/* This function is an analyser which forwards request body (including chunk
4262 * sizes if any). It is called as soon as we must forward, even if we forward
4263 * zero byte. The only situation where it must not be called is when we're in
4264 * tunnel mode and we want to forward till the close. It's used both to forward
4265 * remaining data and to resync after end of body. It expects the msg_state to
4266 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4267 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004268 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004269 * bytes of pending data + the headers if not already done (between sol and sov).
4270 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004271 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004272int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004273{
4274 struct http_txn *txn = &s->txn;
4275 struct http_msg *msg = &s->txn.req;
4276
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004277 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4278 return 0;
4279
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004280 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
4281 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf.o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004282 /* Output closed while we were sending data. We must abort and
4283 * wake the other side up.
4284 */
4285 msg->msg_state = HTTP_MSG_ERROR;
4286 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004287 return 1;
4288 }
4289
Willy Tarreau4fe41902010-06-07 22:27:41 +02004290 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004291 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004292
4293 /* Note that we don't have to send 100-continue back because we don't
4294 * need the data to complete our job, and it's up to the server to
4295 * decide whether to return 100, 417 or anything else in return of
4296 * an "Expect: 100-continue" header.
4297 */
4298
4299 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004300 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02004301 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004302 * is still null. We must save the body in msg->next because it
4303 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004304 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004305 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004306
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004307 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004308 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4309 else {
4310 msg->msg_state = HTTP_MSG_DATA;
4311 }
4312 }
4313
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004315 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004316
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004318 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004319 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004320 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004321 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004322 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004323 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004324 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004325 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004326
Willy Tarreaucaabe412010-01-03 23:08:28 +01004327 if (msg->msg_state == HTTP_MSG_DATA) {
4328 /* must still forward */
4329 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004330 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004331
4332 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004333 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004334 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004336 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004337 }
4338 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004339 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004340 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004341 * TRAILERS state.
4342 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004343 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004344
4345 if (!ret)
4346 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004347 else if (ret < 0) {
4348 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004349 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004350 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004351 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004352 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004353 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004355 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4356 /* we want the CRLF after the data */
4357 int ret;
4358
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004359 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004360
4361 if (ret == 0)
4362 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004363 else if (ret < 0) {
4364 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004365 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004366 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004367 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004368 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004369 /* we're in MSG_CHUNK_SIZE now */
4370 }
4371 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004372 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004373
4374 if (ret == 0)
4375 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004376 else if (ret < 0) {
4377 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004378 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004379 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004380 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004381 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004382 /* we're in HTTP_MSG_DONE now */
4383 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004385 int old_state = msg->msg_state;
4386
Willy Tarreau610ecce2010-01-04 21:15:02 +01004387 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004388 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004389 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4390 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004391 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 if (http_resync_states(s)) {
4393 /* some state changes occurred, maybe the analyser
4394 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004395 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004396 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004397 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004398 /* request errors are most likely due to
4399 * the server aborting the transfer.
4400 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004401 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004402 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004403 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004404 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004406 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004408 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004409
4410 /* If "option abortonclose" is set on the backend, we
4411 * want to monitor the client's connection and forward
4412 * any shutdown notification to the server, which will
4413 * decide whether to close or to go on processing the
4414 * request.
4415 */
4416 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004417 channel_auto_read(req);
4418 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004419 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004420 else if (s->txn.meth == HTTP_METH_POST) {
4421 /* POST requests may require to read extra CRLF
4422 * sent by broken browsers and which could cause
4423 * an RST to be sent upon close on some systems
4424 * (eg: Linux).
4425 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004426 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004427 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004428
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004430 }
4431 }
4432
Willy Tarreaud98cf932009-12-27 22:54:55 +01004433 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004434 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004435 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004436 if (!(s->flags & SN_ERR_MASK))
4437 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004438 if (!(s->flags & SN_FINST_MASK)) {
4439 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4440 s->flags |= SN_FINST_H;
4441 else
4442 s->flags |= SN_FINST_D;
4443 }
4444
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004445 s->fe->fe_counters.cli_aborts++;
4446 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004447 if (target_srv(&s->target))
4448 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004449
4450 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004451 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004452
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004453 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004454 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004455 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004456
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004457 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004458 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004459 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004460 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004461 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004462
Willy Tarreau5c620922011-05-11 19:56:11 +02004463 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004464 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004465 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004466 * modes are already handled by the stream sock layer. We must not do
4467 * this in content-length mode because it could present the MSG_MORE
4468 * flag with the last block of forwarded data, which would cause an
4469 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004470 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004471 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004472 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004473
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004475 return 0;
4476
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004478 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004479 if (s->listener->counters)
4480 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004481 return_bad_req_stats_ok:
4482 txn->req.msg_state = HTTP_MSG_ERROR;
4483 if (txn->status) {
4484 /* Note: we don't send any error if some data were already sent */
4485 stream_int_retnclose(req->prod, NULL);
4486 } else {
4487 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004488 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004489 }
4490 req->analysers = 0;
4491 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004492
4493 if (!(s->flags & SN_ERR_MASK))
4494 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004495 if (!(s->flags & SN_FINST_MASK)) {
4496 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4497 s->flags |= SN_FINST_H;
4498 else
4499 s->flags |= SN_FINST_D;
4500 }
4501 return 0;
4502
4503 aborted_xfer:
4504 txn->req.msg_state = HTTP_MSG_ERROR;
4505 if (txn->status) {
4506 /* Note: we don't send any error if some data were already sent */
4507 stream_int_retnclose(req->prod, NULL);
4508 } else {
4509 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004510 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004511 }
4512 req->analysers = 0;
4513 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4514
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004515 s->fe->fe_counters.srv_aborts++;
4516 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004517 if (target_srv(&s->target))
4518 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004519
4520 if (!(s->flags & SN_ERR_MASK))
4521 s->flags |= SN_ERR_SRVCL;
4522 if (!(s->flags & SN_FINST_MASK)) {
4523 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4524 s->flags |= SN_FINST_H;
4525 else
4526 s->flags |= SN_FINST_D;
4527 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004528 return 0;
4529}
4530
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4532 * processing can continue on next analysers, or zero if it either needs more
4533 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4534 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4535 * when it has nothing left to do, and may remove any analyser when it wants to
4536 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004537 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004538int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004539{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 struct http_txn *txn = &s->txn;
4541 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004542 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004543 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004545 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004546
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004547 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 +02004548 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004550 rep,
4551 rep->rex, rep->wex,
4552 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004553 rep->buf.i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004554 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 /*
4557 * Now parse the partial (or complete) lines.
4558 * We will check the response syntax, and also join multi-line
4559 * headers. An index of all the lines will be elaborated while
4560 * parsing.
4561 *
4562 * For the parsing, we use a 28 states FSM.
4563 *
4564 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02004565 * rep->buf.p = beginning of response
4566 * rep->buf.p + msg->eoh = end of processed headers / start of current one
4567 * rep->buf.p + rep->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004568 * msg->eol = end of current header or line (LF or CRLF)
4569 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 */
4571
Willy Tarreau83e3af02009-12-28 17:39:57 +01004572 /* There's a protected area at the end of the buffer for rewriting
4573 * purposes. We don't want to start to parse the request if the
4574 * protected area is affected, because we may have to move processed
4575 * data later, which is much more complicated.
4576 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004577 if (buffer_not_empty(&rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004578 if (unlikely(channel_full(rep) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02004579 bi_end(&rep->buf) < b_ptr(&rep->buf, msg->next) ||
4580 bi_end(&rep->buf) > rep->buf.data + rep->buf.size - global.tune.maxrewrite)) {
4581 if (rep->buf.o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004582 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004583 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004584 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004585 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004586 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004587 return 0;
4588 }
Willy Tarreau572bf902012-07-02 17:01:20 +02004589 if (rep->buf.i <= rep->buf.size - global.tune.maxrewrite)
4590 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004591 }
4592
Willy Tarreau572bf902012-07-02 17:01:20 +02004593 if (likely(msg->next < rep->buf.i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004594 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004595 }
4596
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 /* 1: we might have to print this header in debug mode */
4598 if (unlikely((global.mode & MODE_DEBUG) &&
4599 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004600 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004602
Willy Tarreau572bf902012-07-02 17:01:20 +02004603 sol = rep->buf.p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02004604 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf.i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004605 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004606
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 sol += hdr_idx_first_pos(&txn->hdr_idx);
4608 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 while (cur_idx) {
4611 eol = sol + txn->hdr_idx.v[cur_idx].len;
4612 debug_hdr("srvhdr", s, sol, eol);
4613 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4614 cur_idx = txn->hdr_idx.v[cur_idx].next;
4615 }
4616 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 /*
4619 * Now we quickly check if we have found a full valid response.
4620 * If not so, we check the FD and buffer states before leaving.
4621 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004622 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 * responses are checked first.
4624 *
4625 * Depending on whether the client is still there or not, we
4626 * may send an error response back or not. Note that normally
4627 * we should only check for HTTP status there, and check I/O
4628 * errors somewhere else.
4629 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004630
Willy Tarreau655dce92009-11-08 13:10:58 +01004631 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 /* Invalid response */
4633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4634 /* we detected a parsing error. We want to archive this response
4635 * in the dedicated proxy area for later troubleshooting.
4636 */
4637 hdr_response_bad:
4638 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004639 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004641 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004642 if (target_srv(&s->target)) {
4643 target_srv(&s->target)->counters.failed_resp++;
4644 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004645 }
Willy Tarreau64648412010-03-05 10:41:54 +01004646 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004647 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 rep->analysers = 0;
4649 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004650 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004651 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004652 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653
4654 if (!(s->flags & SN_ERR_MASK))
4655 s->flags |= SN_ERR_PRXCOND;
4656 if (!(s->flags & SN_FINST_MASK))
4657 s->flags |= SN_FINST_H;
4658
4659 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 /* too large response does not fit in buffer. */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004663 else if (buffer_full(&rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004664 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02004665 msg->err_pos = rep->buf.i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 goto hdr_response_bad;
4667 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004668
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004670 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004672 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004673
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004674 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004675 if (target_srv(&s->target)) {
4676 target_srv(&s->target)->counters.failed_resp++;
4677 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004678 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004679
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004680 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681 rep->analysers = 0;
4682 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004683 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004684 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004685 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 if (!(s->flags & SN_ERR_MASK))
4688 s->flags |= SN_ERR_SRVCL;
4689 if (!(s->flags & SN_FINST_MASK))
4690 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004691 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004692 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004695 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004696 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004697 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004698
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004699 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004700 if (target_srv(&s->target)) {
4701 target_srv(&s->target)->counters.failed_resp++;
4702 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004703 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004704
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004705 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706 rep->analysers = 0;
4707 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004708 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004709 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004710 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004711
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004712 if (!(s->flags & SN_ERR_MASK))
4713 s->flags |= SN_ERR_SRVTO;
4714 if (!(s->flags & SN_FINST_MASK))
4715 s->flags |= SN_FINST_H;
4716 return 0;
4717 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004718
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004719 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004720 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004721 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004722 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004723
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004724 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004725 if (target_srv(&s->target)) {
4726 target_srv(&s->target)->counters.failed_resp++;
4727 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004728 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004729
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004730 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 rep->analysers = 0;
4732 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004733 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004734 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004735 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004736
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004737 if (!(s->flags & SN_ERR_MASK))
4738 s->flags |= SN_ERR_SRVCL;
4739 if (!(s->flags & SN_FINST_MASK))
4740 s->flags |= SN_FINST_H;
4741 return 0;
4742 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004745 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004747 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004748
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004749 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004750 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004751 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752
4753 if (!(s->flags & SN_ERR_MASK))
4754 s->flags |= SN_ERR_CLICL;
4755 if (!(s->flags & SN_FINST_MASK))
4756 s->flags |= SN_FINST_H;
4757
4758 /* process_session() will take care of the error */
4759 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004760 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004761
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004762 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004763 return 0;
4764 }
4765
4766 /* More interesting part now : we know that we have a complete
4767 * response which at least looks like HTTP. We have an indicator
4768 * of each header's length, so we can parse them quickly.
4769 */
4770
4771 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004772 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004774 /*
4775 * 1: get the status code
4776 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004777 n = rep->buf.p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 if (n < 1 || n > 5)
4779 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004780 /* when the client triggers a 4xx from the server, it's most often due
4781 * to a missing object or permission. These events should be tracked
4782 * because if they happen often, it may indicate a brute force or a
4783 * vulnerability scan.
4784 */
4785 if (n == 4)
4786 session_inc_http_err_ctr(s);
4787
Willy Tarreau827aee92011-03-10 16:55:02 +01004788 if (target_srv(&s->target))
4789 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004790
Willy Tarreau5b154472009-12-21 20:11:07 +01004791 /* check if the response is HTTP/1.1 or above */
4792 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004793 ((rep->buf.p[5] > '1') ||
4794 ((rep->buf.p[5] == '1') && (rep->buf.p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004795 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004796
4797 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004798 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 +01004799
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004800 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004801 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004802
Willy Tarreau572bf902012-07-02 17:01:20 +02004803 txn->status = strl2ui(rep->buf.p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004804
Willy Tarreau39650402010-03-15 19:44:39 +01004805 /* Adjust server's health based on status code. Note: status codes 501
4806 * and 505 are triggered on demand by client request, so we must not
4807 * count them as server failures.
4808 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004809 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004810 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004811 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004812 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004813 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004814 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004815
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004816 /*
4817 * 2: check for cacheability.
4818 */
4819
4820 switch (txn->status) {
4821 case 200:
4822 case 203:
4823 case 206:
4824 case 300:
4825 case 301:
4826 case 410:
4827 /* RFC2616 @13.4:
4828 * "A response received with a status code of
4829 * 200, 203, 206, 300, 301 or 410 MAY be stored
4830 * by a cache (...) unless a cache-control
4831 * directive prohibits caching."
4832 *
4833 * RFC2616 @9.5: POST method :
4834 * "Responses to this method are not cacheable,
4835 * unless the response includes appropriate
4836 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004837 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004838 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02004839 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004840 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4841 break;
4842 default:
4843 break;
4844 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004845
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004846 /*
4847 * 3: we may need to capture headers
4848 */
4849 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004850 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02004851 capture_headers(rep->buf.p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 txn->rsp.cap, s->fe->rsp_cap);
4853
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004854 /* 4: determine the transfer-length.
4855 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4856 * the presence of a message-body in a RESPONSE and its transfer length
4857 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004858 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004859 * All responses to the HEAD request method MUST NOT include a
4860 * message-body, even though the presence of entity-header fields
4861 * might lead one to believe they do. All 1xx (informational), 204
4862 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4863 * message-body. All other responses do include a message-body,
4864 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004865 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004866 * 1. Any response which "MUST NOT" include a message-body (such as the
4867 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4868 * always terminated by the first empty line after the header fields,
4869 * regardless of the entity-header fields present in the message.
4870 *
4871 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4872 * the "chunked" transfer-coding (Section 6.2) is used, the
4873 * transfer-length is defined by the use of this transfer-coding.
4874 * If a Transfer-Encoding header field is present and the "chunked"
4875 * transfer-coding is not present, the transfer-length is defined by
4876 * the sender closing the connection.
4877 *
4878 * 3. If a Content-Length header field is present, its decimal value in
4879 * OCTETs represents both the entity-length and the transfer-length.
4880 * If a message is received with both a Transfer-Encoding header
4881 * field and a Content-Length header field, the latter MUST be ignored.
4882 *
4883 * 4. If the message uses the media type "multipart/byteranges", and
4884 * the transfer-length is not otherwise specified, then this self-
4885 * delimiting media type defines the transfer-length. This media
4886 * type MUST NOT be used unless the sender knows that the recipient
4887 * can parse it; the presence in a request of a Range header with
4888 * multiple byte-range specifiers from a 1.1 client implies that the
4889 * client can parse multipart/byteranges responses.
4890 *
4891 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004892 */
4893
4894 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004895 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004896 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004897 * FIXME: should we parse anyway and return an error on chunked encoding ?
4898 */
4899 if (txn->meth == HTTP_METH_HEAD ||
4900 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004901 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004902 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004903 goto skip_content_length;
4904 }
4905
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004906 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004907 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004908 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004909 http_find_header2("Transfer-Encoding", 17, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004910 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004911 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4912 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004913 /* bad transfer-encoding (chunked followed by something else) */
4914 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004915 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004916 break;
4917 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918 }
4919
4920 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4921 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004922 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004923 http_find_header2("Content-Length", 14, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004924 signed long long cl;
4925
Willy Tarreauad14f752011-09-02 20:33:27 +02004926 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004927 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004928 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004929 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004930
Willy Tarreauad14f752011-09-02 20:33:27 +02004931 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004932 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004933 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004934 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004935
Willy Tarreauad14f752011-09-02 20:33:27 +02004936 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004937 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004938 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004939 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004940
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004941 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004942 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004943 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004944 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004945
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004946 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004947 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004948 }
4949
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004950 /* FIXME: we should also implement the multipart/byterange method.
4951 * For now on, we resort to close mode in this case (unknown length).
4952 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004953skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004955 /* end of job, return OK */
4956 rep->analysers &= ~an_bit;
4957 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004958 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004959 return 1;
4960}
4961
4962/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004963 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4964 * and updates t->rep->analysers. It might make sense to explode it into several
4965 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004966 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004967int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004968{
4969 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004970 struct http_msg *msg = &txn->rsp;
4971 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004972 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004973
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004974 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 +02004975 now_ms, __FUNCTION__,
4976 t,
4977 rep,
4978 rep->rex, rep->wex,
4979 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004980 rep->buf.i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004981 rep->analysers);
4982
Willy Tarreau655dce92009-11-08 13:10:58 +01004983 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004984 return 0;
4985
4986 rep->analysers &= ~an_bit;
4987 rep->analyse_exp = TICK_ETERNITY;
4988
Willy Tarreau5b154472009-12-21 20:11:07 +01004989 /* Now we have to check if we need to modify the Connection header.
4990 * This is more difficult on the response than it is on the request,
4991 * because we can have two different HTTP versions and we don't know
4992 * how the client will interprete a response. For instance, let's say
4993 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4994 * HTTP/1.1 response without any header. Maybe it will bound itself to
4995 * HTTP/1.0 because it only knows about it, and will consider the lack
4996 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4997 * the lack of header as a keep-alive. Thus we will use two flags
4998 * indicating how a request MAY be understood by the client. In case
4999 * of multiple possibilities, we'll fix the header to be explicit. If
5000 * ambiguous cases such as both close and keepalive are seen, then we
5001 * will fall back to explicit close. Note that we won't take risks with
5002 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005003 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005004 */
5005
Willy Tarreaudc008c52010-02-01 16:20:08 +01005006 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5007 txn->status == 101)) {
5008 /* Either we've established an explicit tunnel, or we're
5009 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005010 * to understand the next protocols. We have to switch to tunnel
5011 * mode, so that we transfer the request and responses then let
5012 * this protocol pass unmodified. When we later implement specific
5013 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005014 * header which contains information about that protocol for
5015 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005016 */
5017 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5018 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005019 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5020 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5021 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005022 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005023
Willy Tarreau60466522010-01-18 19:08:45 +01005024 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005025 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005026 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5027 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005028
Willy Tarreau60466522010-01-18 19:08:45 +01005029 /* now adjust header transformations depending on current state */
5030 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5031 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5032 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005033 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005034 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005035 }
Willy Tarreau60466522010-01-18 19:08:45 +01005036 else { /* SCL / KAL */
5037 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005038 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005039 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005040 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005041
Willy Tarreau60466522010-01-18 19:08:45 +01005042 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005043 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005044
Willy Tarreau60466522010-01-18 19:08:45 +01005045 /* Some keep-alive responses are converted to Server-close if
5046 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005047 */
Willy Tarreau60466522010-01-18 19:08:45 +01005048 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5049 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005050 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005051 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005052 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005053 }
5054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056 /*
5057 * 3: we will have to evaluate the filters.
5058 * As opposed to version 1.2, now they will be evaluated in the
5059 * filters order and not in the header order. This means that
5060 * each filter has to be validated among all headers.
5061 *
5062 * Filters are tried with ->be first, then with ->fe if it is
5063 * different from ->be.
5064 */
5065
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066 cur_proxy = t->be;
5067 while (1) {
5068 struct proxy *rule_set = cur_proxy;
5069
5070 /* try headers filters */
5071 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005072 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005073 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005074 if (target_srv(&t->target)) {
5075 target_srv(&t->target)->counters.failed_resp++;
5076 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005077 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005078 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005080 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005082 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005083 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005084 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 if (!(t->flags & SN_ERR_MASK))
5086 t->flags |= SN_ERR_PRXCOND;
5087 if (!(t->flags & SN_FINST_MASK))
5088 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005089 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005090 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005092
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 /* has the response been denied ? */
5094 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005095 if (target_srv(&t->target))
5096 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005097
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005098 t->be->be_counters.denied_resp++;
5099 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005100 if (t->listener->counters)
5101 t->listener->counters->denied_resp++;
5102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005104 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005105
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005106 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005107 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005108 if (txn->status < 200)
5109 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005110 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005111 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005112 ret = acl_pass(ret);
5113 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5114 ret = !ret;
5115 if (!ret)
5116 continue;
5117 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005118 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005120 }
5121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122 /* check whether we're already working on the frontend */
5123 if (cur_proxy == t->fe)
5124 break;
5125 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005127
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005128 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005129 * We may be facing a 100-continue response, in which case this
5130 * is not the right response, and we're waiting for the next one.
5131 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005132 * next one.
5133 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005134 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005135 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005136 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005137 msg->msg_state = HTTP_MSG_RPBEFORE;
5138 txn->status = 0;
5139 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5140 return 1;
5141 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005142 else if (unlikely(txn->status < 200))
5143 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005144
5145 /* we don't have any 1xx status code now */
5146
5147 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 * 4: check for server cookie.
5149 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005150 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5151 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005153
Willy Tarreaubaaee002006-06-26 02:48:02 +02005154
Willy Tarreaua15645d2007-03-18 16:22:39 +01005155 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005156 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005157 */
Willy Tarreau67402132012-05-31 20:40:20 +02005158 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005159 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161 /*
5162 * 6: add server cookie in the response if needed
5163 */
Willy Tarreau67402132012-05-31 20:40:20 +02005164 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5165 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005166 (!(t->flags & SN_DIRECT) ||
5167 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5168 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5169 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5170 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005171 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005172 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005174 /* the server is known, it's not the one the client requested, or the
5175 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 * insert a set-cookie here, except if we want to insert only on POST
5177 * requests and this one isn't. Note that servers which don't have cookies
5178 * (eg: some backup servers) will return a full cookie removal request.
5179 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005180 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005181 len = sprintf(trash,
5182 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5183 t->be->cookie_name);
5184 }
5185 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005186 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005187
5188 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5189 /* emit last_date, which is mandatory */
5190 trash[len++] = COOKIE_DELIM_DATE;
5191 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5192 if (t->be->cookie_maxlife) {
5193 /* emit first_date, which is either the original one or
5194 * the current date.
5195 */
5196 trash[len++] = COOKIE_DELIM_DATE;
5197 s30tob64(txn->cookie_first_date ?
5198 txn->cookie_first_date >> 2 :
5199 (date.tv_sec+3) >> 2, trash + len);
5200 len += 5;
5201 }
5202 }
5203 len += sprintf(trash + len, "; path=/");
5204 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005205
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206 if (t->be->cookie_domain)
5207 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005208
Willy Tarreau4992dd22012-05-31 21:02:17 +02005209 if (t->be->ck_opts & PR_CK_HTTPONLY)
5210 len += sprintf(trash+len, "; HttpOnly");
5211
5212 if (t->be->ck_opts & PR_CK_SECURE)
5213 len += sprintf(trash+len, "; Secure");
5214
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005215 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005217
Willy Tarreauf1348312010-10-07 15:54:11 +02005218 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005219 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005220 /* the server did not change, only the date was updated */
5221 txn->flags |= TX_SCK_UPDATED;
5222 else
5223 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005224
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225 /* Here, we will tell an eventual cache on the client side that we don't
5226 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5227 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5228 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5229 */
Willy Tarreau67402132012-05-31 20:40:20 +02005230 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005231
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5233
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005234 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005235 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005236 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005237 }
5238 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 /*
5241 * 7: check if result will be cacheable with a cookie.
5242 * We'll block the response if security checks have caught
5243 * nasty things such as a cacheable cookie.
5244 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005245 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5246 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005247 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248
5249 /* we're in presence of a cacheable response containing
5250 * a set-cookie header. We'll block it as requested by
5251 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005253 if (target_srv(&t->target))
5254 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005255
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005256 t->be->be_counters.denied_resp++;
5257 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005258 if (t->listener->counters)
5259 t->listener->counters->denied_resp++;
5260
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005262 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005263 send_log(t->be, LOG_ALERT,
5264 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005265 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005266 goto return_srv_prx_502;
5267 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005268
5269 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005270 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005271 */
Willy Tarreau60466522010-01-18 19:08:45 +01005272 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5273 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5274 unsigned int want_flags = 0;
5275
5276 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5277 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5278 /* we want a keep-alive response here. Keep-alive header
5279 * required if either side is not 1.1.
5280 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005281 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005282 want_flags |= TX_CON_KAL_SET;
5283 }
5284 else {
5285 /* we want a close response here. Close header required if
5286 * the server is 1.1, regardless of the client.
5287 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005288 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005289 want_flags |= TX_CON_CLO_SET;
5290 }
5291
5292 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005293 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005294 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005295
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005296 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005297 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005298 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005300
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005301 /*************************************************************
5302 * OK, that's finished for the headers. We have done what we *
5303 * could. Let's switch to the DATA state. *
5304 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005307
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005308 /* if the user wants to log as soon as possible, without counting
5309 * bytes from the server, then this is the right moment. We have
5310 * to temporarily assign bytes_out to log what we currently have.
5311 */
5312 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5313 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5314 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005315 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005316 t->logs.bytes_out = 0;
5317 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005318
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 /* Note: we must not try to cheat by jumping directly to DATA,
5320 * otherwise we would not let the client side wake up.
5321 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005322
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005323 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005324 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005325 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328/* This function is an analyser which forwards response body (including chunk
5329 * sizes if any). It is called as soon as we must forward, even if we forward
5330 * zero byte. The only situation where it must not be called is when we're in
5331 * tunnel mode and we want to forward till the close. It's used both to forward
5332 * remaining data and to resync after end of body. It expects the msg_state to
5333 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5334 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005335 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005336 * bytes of pending data + the headers if not already done (between sol and sov).
5337 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005338 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005339int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340{
5341 struct http_txn *txn = &s->txn;
5342 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005343 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005344
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005345 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5346 return 0;
5347
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005348 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
5349 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf.o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005350 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005351 /* Output closed while we were sending data. We must abort and
5352 * wake the other side up.
5353 */
5354 msg->msg_state = HTTP_MSG_ERROR;
5355 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005356 return 1;
5357 }
5358
Willy Tarreau4fe41902010-06-07 22:27:41 +02005359 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005360 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005361
Willy Tarreaud98cf932009-12-27 22:54:55 +01005362 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005363 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02005364 * rep->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005365 * is still null. We must save the body in msg->next because it
5366 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005367 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005368 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005369
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005370 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5372 else {
5373 msg->msg_state = HTTP_MSG_DATA;
5374 }
5375 }
5376
Willy Tarreaud98cf932009-12-27 22:54:55 +01005377 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005379 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005380 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005381 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005382 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005383 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005384 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005385 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005386 }
5387
Willy Tarreaucaabe412010-01-03 23:08:28 +01005388 if (msg->msg_state == HTTP_MSG_DATA) {
5389 /* must still forward */
5390 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005391 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005392
5393 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005394 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005395 msg->msg_state = HTTP_MSG_DATA_CRLF;
5396 else
5397 msg->msg_state = HTTP_MSG_DONE;
5398 }
5399 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005400 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005401 * set ->sov and ->next to point to the body and switch to DATA or
5402 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005404 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405
5406 if (!ret)
5407 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005408 else if (ret < 0) {
5409 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005410 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005412 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5416 /* we want the CRLF after the data */
5417 int ret;
5418
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005419 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005420
5421 if (!ret)
5422 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005423 else if (ret < 0) {
5424 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005425 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005427 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 /* we're in MSG_CHUNK_SIZE now */
5429 }
5430 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005431 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005432
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433 if (ret == 0)
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_TRAILERS, 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 HTTP_MSG_DONE now */
5441 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005443 int old_state = msg->msg_state;
5444
Willy Tarreau610ecce2010-01-04 21:15:02 +01005445 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005446 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005447 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5448 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005449 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 if (http_resync_states(s)) {
5451 http_silent_debug(__LINE__, s);
5452 /* some state changes occurred, maybe the analyser
5453 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005454 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005455 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005456 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005457 /* response errors are most likely due to
5458 * the client aborting the transfer.
5459 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005460 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005461 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005462 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005463 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005464 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005465 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005467 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005469 }
5470 }
5471
Willy Tarreaud98cf932009-12-27 22:54:55 +01005472 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005473 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005474 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005475 if (!(s->flags & SN_ERR_MASK))
5476 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005477 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005478 if (target_srv(&s->target))
5479 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005480 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005481 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005482
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005483 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005484 goto aborted_xfer;
5485
Willy Tarreau40dba092010-03-04 18:14:51 +01005486 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005487 if (!s->req->analysers)
5488 goto return_bad_res;
5489
Willy Tarreauea953162012-05-18 23:41:28 +02005490 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005491 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005492 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005493 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005494 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005495 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005496 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497 }
5498
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005499 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005500 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005501 * Similarly, with keep-alive on the client side, we don't want to forward a
5502 * close.
5503 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005504 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005505 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5506 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005507 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005508
Willy Tarreau5c620922011-05-11 19:56:11 +02005509 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005510 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005511 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005512 * modes are already handled by the stream sock layer. We must not do
5513 * this in content-length mode because it could present the MSG_MORE
5514 * flag with the last block of forwarded data, which would cause an
5515 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005516 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005517 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005518 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005519
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 return 0;
5523
Willy Tarreau40dba092010-03-04 18:14:51 +01005524 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005525 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005526 if (target_srv(&s->target))
5527 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005528
5529 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005531 /* don't send any error message as we're in the body */
5532 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005534 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005535 if (target_srv(&s->target))
5536 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005537
5538 if (!(s->flags & SN_ERR_MASK))
5539 s->flags |= SN_ERR_PRXCOND;
5540 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005541 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005542 return 0;
5543
5544 aborted_xfer:
5545 txn->rsp.msg_state = HTTP_MSG_ERROR;
5546 /* don't send any error message as we're in the body */
5547 stream_int_retnclose(res->cons, NULL);
5548 res->analysers = 0;
5549 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5550
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005551 s->fe->fe_counters.cli_aborts++;
5552 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005553 if (target_srv(&s->target))
5554 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005555
5556 if (!(s->flags & SN_ERR_MASK))
5557 s->flags |= SN_ERR_CLICL;
5558 if (!(s->flags & SN_FINST_MASK))
5559 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005560 return 0;
5561}
5562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005563/* Iterate the same filter through all request headers.
5564 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005565 * Since it can manage the switch to another backend, it updates the per-proxy
5566 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005568int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005569{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005570 char term;
5571 char *cur_ptr, *cur_end, *cur_next;
5572 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005573 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 struct hdr_idx_elem *cur_hdr;
5575 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005577 last_hdr = 0;
5578
Willy Tarreau572bf902012-07-02 17:01:20 +02005579 cur_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005580 old_idx = 0;
5581
5582 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005583 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005584 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005585 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 (exp->action == ACT_ALLOW ||
5587 exp->action == ACT_DENY ||
5588 exp->action == ACT_TARPIT))
5589 return 0;
5590
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005591 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 if (!cur_idx)
5593 break;
5594
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005595 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 cur_ptr = cur_next;
5597 cur_end = cur_ptr + cur_hdr->len;
5598 cur_next = cur_end + cur_hdr->cr + 1;
5599
5600 /* Now we have one header between cur_ptr and cur_end,
5601 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005602 */
5603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 /* The annoying part is that pattern matching needs
5605 * that we modify the contents to null-terminate all
5606 * strings before testing them.
5607 */
5608
5609 term = *cur_end;
5610 *cur_end = '\0';
5611
5612 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5613 switch (exp->action) {
5614 case ACT_SETBE:
5615 /* It is not possible to jump a second time.
5616 * FIXME: should we return an HTTP/500 here so that
5617 * the admin knows there's a problem ?
5618 */
5619 if (t->be != t->fe)
5620 break;
5621
5622 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005623 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005624 last_hdr = 1;
5625 break;
5626
5627 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005628 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005629 last_hdr = 1;
5630 break;
5631
5632 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005633 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005635
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005636 t->fe->fe_counters.denied_req++;
5637 if (t->fe != t->be)
5638 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005639 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005640 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 break;
5643
5644 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005645 txn->flags |= TX_CLTARPIT;
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_REPLACE:
5657 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02005658 delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 /* FIXME: if the user adds a newline in the replacement, the
5660 * index will not be recalculated for now, and the new line
5661 * will not be counted as a new header.
5662 */
5663
5664 cur_end += delta;
5665 cur_next += delta;
5666 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005667 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 break;
5669
5670 case ACT_REMOVE:
Willy Tarreauaf819352012-08-27 22:08:00 +02005671 delta = buffer_replace2(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 cur_next += delta;
5673
Willy Tarreaufa355d42009-11-29 18:12:29 +01005674 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005675 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5676 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 cur_hdr->len = 0;
5678 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005679 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 break;
5681
5682 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005683 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 if (cur_end)
5685 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687 /* keep the link from this header to next one in case of later
5688 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005689 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005690 old_idx = cur_idx;
5691 }
5692 return 0;
5693}
5694
5695
5696/* Apply the filter to the request line.
5697 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5698 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 * Since it can manage the switch to another backend, it updates the per-proxy
5700 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005702int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703{
5704 char term;
5705 char *cur_ptr, *cur_end;
5706 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005707 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005709
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710
Willy Tarreau3d300592007-03-18 18:34:41 +01005711 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005713 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714 (exp->action == ACT_ALLOW ||
5715 exp->action == ACT_DENY ||
5716 exp->action == ACT_TARPIT))
5717 return 0;
5718 else if (exp->action == ACT_REMOVE)
5719 return 0;
5720
5721 done = 0;
5722
Willy Tarreau572bf902012-07-02 17:01:20 +02005723 cur_ptr = req->buf.p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005724 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725
5726 /* Now we have the request line between cur_ptr and cur_end */
5727
5728 /* The annoying part is that pattern matching needs
5729 * that we modify the contents to null-terminate all
5730 * strings before testing them.
5731 */
5732
5733 term = *cur_end;
5734 *cur_end = '\0';
5735
5736 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5737 switch (exp->action) {
5738 case ACT_SETBE:
5739 /* It is not possible to jump a second time.
5740 * FIXME: should we return an HTTP/500 here so that
5741 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005742 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 if (t->be != t->fe)
5744 break;
5745
5746 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005747 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 done = 1;
5749 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005752 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 done = 1;
5754 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005757 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005758
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005759 t->fe->fe_counters.denied_req++;
5760 if (t->fe != t->be)
5761 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005762 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005763 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005764
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005769 txn->flags |= TX_CLTARPIT;
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_REPLACE:
5781 *cur_end = term; /* restore the string terminator */
5782 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02005783 delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784 /* FIXME: if the user adds a newline in the replacement, the
5785 * index will not be recalculated for now, and the new line
5786 * will not be counted as a new header.
5787 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005788
Willy Tarreaufa355d42009-11-29 18:12:29 +01005789 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005790 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005791 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 HTTP_MSG_RQMETH,
5793 cur_ptr, cur_end + 1,
5794 NULL, NULL);
5795 if (unlikely(!cur_end))
5796 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005797
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798 /* we have a full request and we know that we have either a CR
5799 * or an LF at <ptr>.
5800 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005801 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5802 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005803 /* there is no point trying this regex on headers */
5804 return 1;
5805 }
5806 }
5807 *cur_end = term; /* restore the string terminator */
5808 return done;
5809}
Willy Tarreau97de6242006-12-27 17:18:38 +01005810
Willy Tarreau58f10d72006-12-04 02:26:12 +01005811
Willy Tarreau58f10d72006-12-04 02:26:12 +01005812
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005813/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005814 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005815 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005816 * unparsable request. Since it can manage the switch to another backend, it
5817 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005819int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005820{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005821 struct http_txn *txn = &s->txn;
5822 struct hdr_exp *exp;
5823
5824 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005827 /*
5828 * The interleaving of transformations and verdicts
5829 * makes it difficult to decide to continue or stop
5830 * the evaluation.
5831 */
5832
Willy Tarreau6c123b12010-01-28 20:22:06 +01005833 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5834 break;
5835
Willy Tarreau3d300592007-03-18 18:34:41 +01005836 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005838 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005840
5841 /* if this filter had a condition, evaluate it now and skip to
5842 * next filter if the condition does not match.
5843 */
5844 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005845 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005846 ret = acl_pass(ret);
5847 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5848 ret = !ret;
5849
5850 if (!ret)
5851 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005852 }
5853
5854 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005855 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005856 if (unlikely(ret < 0))
5857 return -1;
5858
5859 if (likely(ret == 0)) {
5860 /* The filter did not match the request, it can be
5861 * iterated through all headers.
5862 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005863 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005865 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005866 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005867}
5868
5869
Willy Tarreaua15645d2007-03-18 16:22:39 +01005870
Willy Tarreau58f10d72006-12-04 02:26:12 +01005871/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005872 * Try to retrieve the server associated to the appsession.
5873 * If the server is found, it's assigned to the session.
5874 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005875void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005876 struct http_txn *txn = &t->txn;
5877 appsess *asession = NULL;
5878 char *sessid_temp = NULL;
5879
Cyril Bontéb21570a2009-11-29 20:04:48 +01005880 if (len > t->be->appsession_len) {
5881 len = t->be->appsession_len;
5882 }
5883
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005884 if (t->be->options2 & PR_O2_AS_REQL) {
5885 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005886 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005887 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005888 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005889 }
5890
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005891 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005892 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5893 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5894 return;
5895 }
5896
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005897 memcpy(txn->sessid, buf, len);
5898 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005899 }
5900
5901 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5902 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5903 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5904 return;
5905 }
5906
Cyril Bontéb21570a2009-11-29 20:04:48 +01005907 memcpy(sessid_temp, buf, len);
5908 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005909
5910 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5911 /* free previously allocated memory */
5912 pool_free2(apools.sessid, sessid_temp);
5913
5914 if (asession != NULL) {
5915 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5916 if (!(t->be->options2 & PR_O2_AS_REQL))
5917 asession->request_count++;
5918
5919 if (asession->serverid != NULL) {
5920 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005921
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005922 while (srv) {
5923 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005924 if ((srv->state & SRV_RUNNING) ||
5925 (t->be->options & PR_O_PERSIST) ||
5926 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005927 /* we found the server and it's usable */
5928 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005929 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005930 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005931 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005932
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005933 break;
5934 } else {
5935 txn->flags &= ~TX_CK_MASK;
5936 txn->flags |= TX_CK_DOWN;
5937 }
5938 }
5939 srv = srv->next;
5940 }
5941 }
5942 }
5943}
5944
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005945/* Find the end of a cookie value contained between <s> and <e>. It works the
5946 * same way as with headers above except that the semi-colon also ends a token.
5947 * See RFC2965 for more information. Note that it requires a valid header to
5948 * return a valid result.
5949 */
5950char *find_cookie_value_end(char *s, const char *e)
5951{
5952 int quoted, qdpair;
5953
5954 quoted = qdpair = 0;
5955 for (; s < e; s++) {
5956 if (qdpair) qdpair = 0;
5957 else if (quoted) {
5958 if (*s == '\\') qdpair = 1;
5959 else if (*s == '"') quoted = 0;
5960 }
5961 else if (*s == '"') quoted = 1;
5962 else if (*s == ',' || *s == ';') return s;
5963 }
5964 return s;
5965}
5966
5967/* Delete a value in a header between delimiters <from> and <next> in buffer
5968 * <buf>. The number of characters displaced is returned, and the pointer to
5969 * the first delimiter is updated if required. The function tries as much as
5970 * possible to respect the following principles :
5971 * - replace <from> delimiter by the <next> one unless <from> points to a
5972 * colon, in which case <next> is simply removed
5973 * - set exactly one space character after the new first delimiter, unless
5974 * there are not enough characters in the block being moved to do so.
5975 * - remove unneeded spaces before the previous delimiter and after the new
5976 * one.
5977 *
5978 * It is the caller's responsibility to ensure that :
5979 * - <from> points to a valid delimiter or the colon ;
5980 * - <next> points to a valid delimiter or the final CR/LF ;
5981 * - there are non-space chars before <from> ;
5982 * - there is a CR/LF at or after <next>.
5983 */
Willy Tarreauaf819352012-08-27 22:08:00 +02005984int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005985{
5986 char *prev = *from;
5987
5988 if (*prev == ':') {
5989 /* We're removing the first value, preserve the colon and add a
5990 * space if possible.
5991 */
5992 if (!http_is_crlf[(unsigned char)*next])
5993 next++;
5994 prev++;
5995 if (prev < next)
5996 *prev++ = ' ';
5997
5998 while (http_is_spht[(unsigned char)*next])
5999 next++;
6000 } else {
6001 /* Remove useless spaces before the old delimiter. */
6002 while (http_is_spht[(unsigned char)*(prev-1)])
6003 prev--;
6004 *from = prev;
6005
6006 /* copy the delimiter and if possible a space if we're
6007 * not at the end of the line.
6008 */
6009 if (!http_is_crlf[(unsigned char)*next]) {
6010 *prev++ = *next++;
6011 if (prev + 1 < next)
6012 *prev++ = ' ';
6013 while (http_is_spht[(unsigned char)*next])
6014 next++;
6015 }
6016 }
6017 return buffer_replace2(buf, prev, next, NULL, 0);
6018}
6019
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006020/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006021 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006022 * desirable to call it only when needed. This code is quite complex because
6023 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6024 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006025 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006026void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006028 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006029 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006031 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6032 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006035 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02006036 hdr_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006038 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006040 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006042 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 hdr_beg = hdr_next;
6044 hdr_end = hdr_beg + cur_hdr->len;
6045 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047 /* We have one full header between hdr_beg and hdr_end, and the
6048 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049 * "Cookie:" headers.
6050 */
6051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006053 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054 old_idx = cur_idx;
6055 continue;
6056 }
6057
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006058 del_from = NULL; /* nothing to be deleted */
6059 preserve_hdr = 0; /* assume we may kill the whole header */
6060
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061 /* Now look for cookies. Conforming to RFC2109, we have to support
6062 * attributes whose name begin with a '$', and associate them with
6063 * the right cookie, if we want to delete this cookie.
6064 * So there are 3 cases for each cookie read :
6065 * 1) it's a special attribute, beginning with a '$' : ignore it.
6066 * 2) it's a server id cookie that we *MAY* want to delete : save
6067 * some pointers on it (last semi-colon, beginning of cookie...)
6068 * 3) it's an application cookie : we *MAY* have to delete a previous
6069 * "special" cookie.
6070 * At the end of loop, if a "special" cookie remains, we may have to
6071 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006072 * *MUST* delete it.
6073 *
6074 * Note: RFC2965 is unclear about the processing of spaces around
6075 * the equal sign in the ATTR=VALUE form. A careful inspection of
6076 * the RFC explicitly allows spaces before it, and not within the
6077 * tokens (attrs or values). An inspection of RFC2109 allows that
6078 * too but section 10.1.3 lets one think that spaces may be allowed
6079 * after the equal sign too, resulting in some (rare) buggy
6080 * implementations trying to do that. So let's do what servers do.
6081 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6082 * allowed quoted strings in values, with any possible character
6083 * after a backslash, including control chars and delimitors, which
6084 * causes parsing to become ambiguous. Browsers also allow spaces
6085 * within values even without quotes.
6086 *
6087 * We have to keep multiple pointers in order to support cookie
6088 * removal at the beginning, middle or end of header without
6089 * corrupting the header. All of these headers are valid :
6090 *
6091 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6092 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6093 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6094 * | | | | | | | | |
6095 * | | | | | | | | hdr_end <--+
6096 * | | | | | | | +--> next
6097 * | | | | | | +----> val_end
6098 * | | | | | +-----------> val_beg
6099 * | | | | +--------------> equal
6100 * | | | +----------------> att_end
6101 * | | +---------------------> att_beg
6102 * | +--------------------------> prev
6103 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104 */
6105
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006106 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6107 /* Iterate through all cookies on this line */
6108
6109 /* find att_beg */
6110 att_beg = prev + 1;
6111 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6112 att_beg++;
6113
6114 /* find att_end : this is the first character after the last non
6115 * space before the equal. It may be equal to hdr_end.
6116 */
6117 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 while (equal < hdr_end) {
6120 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006122 if (http_is_spht[(unsigned char)*equal++])
6123 continue;
6124 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125 }
6126
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6128 * is between <att_beg> and <equal>, both may be identical.
6129 */
6130
6131 /* look for end of cookie if there is an equal sign */
6132 if (equal < hdr_end && *equal == '=') {
6133 /* look for the beginning of the value */
6134 val_beg = equal + 1;
6135 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6136 val_beg++;
6137
6138 /* find the end of the value, respecting quotes */
6139 next = find_cookie_value_end(val_beg, hdr_end);
6140
6141 /* make val_end point to the first white space or delimitor after the value */
6142 val_end = next;
6143 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6144 val_end--;
6145 } else {
6146 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006147 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006149 /* We have nothing to do with attributes beginning with '$'. However,
6150 * they will automatically be removed if a header before them is removed,
6151 * since they're supposed to be linked together.
6152 */
6153 if (*att_beg == '$')
6154 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006155
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006156 /* Ignore cookies with no equal sign */
6157 if (equal == next) {
6158 /* This is not our cookie, so we must preserve it. But if we already
6159 * scheduled another cookie for removal, we cannot remove the
6160 * complete header, but we can remove the previous block itself.
6161 */
6162 preserve_hdr = 1;
6163 if (del_from != NULL) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006164 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 val_end += delta;
6166 next += delta;
6167 hdr_end += delta;
6168 hdr_next += delta;
6169 cur_hdr->len += delta;
6170 http_msg_move_end(&txn->req, delta);
6171 prev = del_from;
6172 del_from = NULL;
6173 }
6174 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006177 /* if there are spaces around the equal sign, we need to
6178 * strip them otherwise we'll get trouble for cookie captures,
6179 * or even for rewrites. Since this happens extremely rarely,
6180 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6183 int stripped_before = 0;
6184 int stripped_after = 0;
6185
6186 if (att_end != equal) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006187 stripped_before = buffer_replace2(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188 equal += stripped_before;
6189 val_beg += stripped_before;
6190 }
6191
6192 if (val_beg > equal + 1) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006193 stripped_after = buffer_replace2(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 val_beg += stripped_after;
6195 stripped_before += stripped_after;
6196 }
6197
6198 val_end += stripped_before;
6199 next += stripped_before;
6200 hdr_end += stripped_before;
6201 hdr_next += stripped_before;
6202 cur_hdr->len += stripped_before;
6203 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006204 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* First, let's see if we want to capture this cookie. We check
6208 * that we don't already have a client side cookie, because we
6209 * can only capture one. Also as an optimisation, we ignore
6210 * cookies shorter than the declared name.
6211 */
6212 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6213 (val_end - att_beg >= t->fe->capture_namelen) &&
6214 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6215 int log_len = val_end - att_beg;
6216
6217 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6218 Alert("HTTP logging : out of memory.\n");
6219 } else {
6220 if (log_len > t->fe->capture_len)
6221 log_len = t->fe->capture_len;
6222 memcpy(txn->cli_cookie, att_beg, log_len);
6223 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226
Willy Tarreaubca99692010-10-06 19:25:55 +02006227 /* Persistence cookies in passive, rewrite or insert mode have the
6228 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006230 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006232 * For cookies in prefix mode, the form is :
6233 *
6234 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 */
6236 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6237 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6238 struct server *srv = t->be->srv;
6239 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006240
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6242 * have the server ID between val_beg and delim, and the original cookie between
6243 * delim+1 and val_end. Otherwise, delim==val_end :
6244 *
6245 * Cookie: NAME=SRV; # in all but prefix modes
6246 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6247 * | || || | |+-> next
6248 * | || || | +--> val_end
6249 * | || || +---------> delim
6250 * | || |+------------> val_beg
6251 * | || +-------------> att_end = equal
6252 * | |+-----------------> att_beg
6253 * | +------------------> prev
6254 * +-------------------------> hdr_beg
6255 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256
Willy Tarreau67402132012-05-31 20:40:20 +02006257 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 for (delim = val_beg; delim < val_end; delim++)
6259 if (*delim == COOKIE_DELIM)
6260 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006261 } else {
6262 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006264 /* Now check if the cookie contains a date field, which would
6265 * appear after a vertical bar ('|') just after the server name
6266 * and before the delimiter.
6267 */
6268 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6269 if (vbar1) {
6270 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006271 * right is the last seen date. It is a base64 encoded
6272 * 30-bit value representing the UNIX date since the
6273 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006274 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006275 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006276 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006277 if (val_end - vbar1 >= 5) {
6278 val = b64tos30(vbar1);
6279 if (val > 0)
6280 txn->cookie_last_date = val << 2;
6281 }
6282 /* look for a second vertical bar */
6283 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6284 if (vbar1 && (val_end - vbar1 > 5)) {
6285 val = b64tos30(vbar1 + 1);
6286 if (val > 0)
6287 txn->cookie_first_date = val << 2;
6288 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006289 }
6290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291
Willy Tarreauf64d1412010-10-07 20:06:11 +02006292 /* if the cookie has an expiration date and the proxy wants to check
6293 * it, then we do that now. We first check if the cookie is too old,
6294 * then only if it has expired. We detect strict overflow because the
6295 * time resolution here is not great (4 seconds). Cookies with dates
6296 * in the future are ignored if their offset is beyond one day. This
6297 * allows an admin to fix timezone issues without expiring everyone
6298 * and at the same time avoids keeping unwanted side effects for too
6299 * long.
6300 */
6301 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006302 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6303 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006304 txn->flags &= ~TX_CK_MASK;
6305 txn->flags |= TX_CK_OLD;
6306 delim = val_beg; // let's pretend we have not found the cookie
6307 txn->cookie_first_date = 0;
6308 txn->cookie_last_date = 0;
6309 }
6310 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006311 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6312 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006313 txn->flags &= ~TX_CK_MASK;
6314 txn->flags |= TX_CK_EXPIRED;
6315 delim = val_beg; // let's pretend we have not found the cookie
6316 txn->cookie_first_date = 0;
6317 txn->cookie_last_date = 0;
6318 }
6319
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 /* Here, we'll look for the first running server which supports the cookie.
6321 * This allows to share a same cookie between several servers, for example
6322 * to dedicate backup servers to specific servers only.
6323 * However, to prevent clients from sticking to cookie-less backup server
6324 * when they have incidentely learned an empty cookie, we simply ignore
6325 * empty cookies and mark them as invalid.
6326 * The same behaviour is applied when persistence must be ignored.
6327 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006328 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 while (srv) {
6332 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6333 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6334 if ((srv->state & SRV_RUNNING) ||
6335 (t->be->options & PR_O_PERSIST) ||
6336 (t->flags & SN_FORCE_PRST)) {
6337 /* we found the server and we can use it */
6338 txn->flags &= ~TX_CK_MASK;
6339 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6340 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006341 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 break;
6343 } else {
6344 /* we found a server, but it's down,
6345 * mark it as such and go on in case
6346 * another one is available.
6347 */
6348 txn->flags &= ~TX_CK_MASK;
6349 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 srv = srv->next;
6353 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354
Willy Tarreauf64d1412010-10-07 20:06:11 +02006355 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006356 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006358 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6359 txn->flags |= TX_CK_UNUSED;
6360 else
6361 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006362 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 /* depending on the cookie mode, we may have to either :
6365 * - delete the complete cookie if we're in insert+indirect mode, so that
6366 * the server never sees it ;
6367 * - remove the server id from the cookie value, and tag the cookie as an
6368 * application cookie so that it does not get accidentely removed later,
6369 * if we're in cookie prefix mode
6370 */
Willy Tarreau67402132012-05-31 20:40:20 +02006371 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373
Willy Tarreauaf819352012-08-27 22:08:00 +02006374 delta = buffer_replace2(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006375 val_end += delta;
6376 next += delta;
6377 hdr_end += delta;
6378 hdr_next += delta;
6379 cur_hdr->len += delta;
6380 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 del_from = NULL;
6383 preserve_hdr = 1; /* we want to keep this cookie */
6384 }
6385 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006386 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 del_from = prev;
6388 }
6389 } else {
6390 /* This is not our cookie, so we must preserve it. But if we already
6391 * scheduled another cookie for removal, we cannot remove the
6392 * complete header, but we can remove the previous block itself.
6393 */
6394 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006396 if (del_from != NULL) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006397 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006398 if (att_beg >= del_from)
6399 att_beg += delta;
6400 if (att_end >= del_from)
6401 att_end += delta;
6402 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 val_end += delta;
6404 next += delta;
6405 hdr_end += delta;
6406 hdr_next += delta;
6407 cur_hdr->len += delta;
6408 http_msg_move_end(&txn->req, delta);
6409 prev = del_from;
6410 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006413
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006414 /* Look for the appsession cookie unless persistence must be ignored */
6415 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6416 int cmp_len, value_len;
6417 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006418
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006419 if (t->be->options2 & PR_O2_AS_PFX) {
6420 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6421 value_begin = att_beg + t->be->appsession_name_len;
6422 value_len = val_end - att_beg - t->be->appsession_name_len;
6423 } else {
6424 cmp_len = att_end - att_beg;
6425 value_begin = val_beg;
6426 value_len = val_end - val_beg;
6427 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006428
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 /* let's see if the cookie is our appcookie */
6430 if (cmp_len == t->be->appsession_name_len &&
6431 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6432 manage_client_side_appsession(t, value_begin, value_len);
6433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006434 }
6435
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 /* continue with next cookie on this header line */
6437 att_beg = next;
6438 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006440 /* There are no more cookies on this line.
6441 * We may still have one (or several) marked for deletion at the
6442 * end of the line. We must do this now in two ways :
6443 * - if some cookies must be preserved, we only delete from the
6444 * mark to the end of line ;
6445 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006448 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 if (preserve_hdr) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006450 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006452 cur_hdr->len += delta;
6453 } else {
Willy Tarreauaf819352012-08-27 22:08:00 +02006454 delta = buffer_replace2(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006455
6456 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006457 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6458 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006460 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006462 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006463 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464 }
6465
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006466 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006469}
6470
6471
Willy Tarreaua15645d2007-03-18 16:22:39 +01006472/* Iterate the same filter through all response headers contained in <rtr>.
6473 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6474 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006475int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006476{
6477 char term;
6478 char *cur_ptr, *cur_end, *cur_next;
6479 int cur_idx, old_idx, last_hdr;
6480 struct http_txn *txn = &t->txn;
6481 struct hdr_idx_elem *cur_hdr;
6482 int len, delta;
6483
6484 last_hdr = 0;
6485
Willy Tarreau572bf902012-07-02 17:01:20 +02006486 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 old_idx = 0;
6488
6489 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006490 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006491 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006492 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006493 (exp->action == ACT_ALLOW ||
6494 exp->action == ACT_DENY))
6495 return 0;
6496
6497 cur_idx = txn->hdr_idx.v[old_idx].next;
6498 if (!cur_idx)
6499 break;
6500
6501 cur_hdr = &txn->hdr_idx.v[cur_idx];
6502 cur_ptr = cur_next;
6503 cur_end = cur_ptr + cur_hdr->len;
6504 cur_next = cur_end + cur_hdr->cr + 1;
6505
6506 /* Now we have one header between cur_ptr and cur_end,
6507 * and the next header starts at cur_next.
6508 */
6509
6510 /* The annoying part is that pattern matching needs
6511 * that we modify the contents to null-terminate all
6512 * strings before testing them.
6513 */
6514
6515 term = *cur_end;
6516 *cur_end = '\0';
6517
6518 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6519 switch (exp->action) {
6520 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006521 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522 last_hdr = 1;
6523 break;
6524
6525 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006526 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006527 last_hdr = 1;
6528 break;
6529
6530 case ACT_REPLACE:
6531 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02006532 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006533 /* FIXME: if the user adds a newline in the replacement, the
6534 * index will not be recalculated for now, and the new line
6535 * will not be counted as a new header.
6536 */
6537
6538 cur_end += delta;
6539 cur_next += delta;
6540 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006541 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542 break;
6543
6544 case ACT_REMOVE:
Willy Tarreauaf819352012-08-27 22:08:00 +02006545 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546 cur_next += delta;
6547
Willy Tarreaufa355d42009-11-29 18:12:29 +01006548 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6550 txn->hdr_idx.used--;
6551 cur_hdr->len = 0;
6552 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006553 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 break;
6555
6556 }
6557 }
6558 if (cur_end)
6559 *cur_end = term; /* restore the string terminator */
6560
6561 /* keep the link from this header to next one in case of later
6562 * removal of next header.
6563 */
6564 old_idx = cur_idx;
6565 }
6566 return 0;
6567}
6568
6569
6570/* Apply the filter to the status line in the response buffer <rtr>.
6571 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6572 * or -1 if a replacement resulted in an invalid status line.
6573 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006574int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575{
6576 char term;
6577 char *cur_ptr, *cur_end;
6578 int done;
6579 struct http_txn *txn = &t->txn;
6580 int len, delta;
6581
6582
Willy Tarreau3d300592007-03-18 18:34:41 +01006583 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006584 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006585 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 (exp->action == ACT_ALLOW ||
6587 exp->action == ACT_DENY))
6588 return 0;
6589 else if (exp->action == ACT_REMOVE)
6590 return 0;
6591
6592 done = 0;
6593
Willy Tarreau572bf902012-07-02 17:01:20 +02006594 cur_ptr = rtr->buf.p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006595 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596
6597 /* Now we have the status line between cur_ptr and cur_end */
6598
6599 /* The annoying part is that pattern matching needs
6600 * that we modify the contents to null-terminate all
6601 * strings before testing them.
6602 */
6603
6604 term = *cur_end;
6605 *cur_end = '\0';
6606
6607 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6608 switch (exp->action) {
6609 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006610 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611 done = 1;
6612 break;
6613
6614 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006615 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 done = 1;
6617 break;
6618
6619 case ACT_REPLACE:
6620 *cur_end = term; /* restore the string terminator */
6621 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02006622 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 /* FIXME: if the user adds a newline in the replacement, the
6624 * index will not be recalculated for now, and the new line
6625 * will not be counted as a new header.
6626 */
6627
Willy Tarreaufa355d42009-11-29 18:12:29 +01006628 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006630 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006631 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632 cur_ptr, cur_end + 1,
6633 NULL, NULL);
6634 if (unlikely(!cur_end))
6635 return -1;
6636
6637 /* we have a full respnse and we know that we have either a CR
6638 * or an LF at <ptr>.
6639 */
Willy Tarreau572bf902012-07-02 17:01:20 +02006640 txn->status = strl2ui(rtr->buf.p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006641 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 /* there is no point trying this regex on headers */
6643 return 1;
6644 }
6645 }
6646 *cur_end = term; /* restore the string terminator */
6647 return done;
6648}
6649
6650
6651
6652/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006653 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6655 * unparsable response.
6656 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006657int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006659 struct http_txn *txn = &s->txn;
6660 struct hdr_exp *exp;
6661
6662 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663 int ret;
6664
6665 /*
6666 * The interleaving of transformations and verdicts
6667 * makes it difficult to decide to continue or stop
6668 * the evaluation.
6669 */
6670
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006671 if (txn->flags & TX_SVDENY)
6672 break;
6673
Willy Tarreau3d300592007-03-18 18:34:41 +01006674 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6676 exp->action == ACT_PASS)) {
6677 exp = exp->next;
6678 continue;
6679 }
6680
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006681 /* if this filter had a condition, evaluate it now and skip to
6682 * next filter if the condition does not match.
6683 */
6684 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006685 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006686 ret = acl_pass(ret);
6687 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6688 ret = !ret;
6689 if (!ret)
6690 continue;
6691 }
6692
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006694 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695 if (unlikely(ret < 0))
6696 return -1;
6697
6698 if (likely(ret == 0)) {
6699 /* The filter did not match the response, it can be
6700 * iterated through all headers.
6701 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006702 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704 }
6705 return 0;
6706}
6707
6708
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006710 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006711 * desirable to call it only when needed. This function is also used when we
6712 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006714void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715{
6716 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006717 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006718 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006720 char *hdr_beg, *hdr_end, *hdr_next;
6721 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723 /* Iterate through the headers.
6724 * we start with the start line.
6725 */
6726 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02006727 hdr_next = res->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728
6729 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6730 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006731 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732
6733 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006734 hdr_beg = hdr_next;
6735 hdr_end = hdr_beg + cur_hdr->len;
6736 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreau24581ba2010-08-31 22:39:35 +02006738 /* We have one full header between hdr_beg and hdr_end, and the
6739 * next header starts at hdr_next. We're only interested in
6740 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 */
6742
Willy Tarreau24581ba2010-08-31 22:39:35 +02006743 is_cookie2 = 0;
6744 prev = hdr_beg + 10;
6745 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006746 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006747 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6748 if (!val) {
6749 old_idx = cur_idx;
6750 continue;
6751 }
6752 is_cookie2 = 1;
6753 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754 }
6755
Willy Tarreau24581ba2010-08-31 22:39:35 +02006756 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6757 * <prev> points to the colon.
6758 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006759 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006760
Willy Tarreau24581ba2010-08-31 22:39:35 +02006761 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6762 * check-cache is enabled) and we are not interested in checking
6763 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006764 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006765 if (t->be->cookie_name == NULL &&
6766 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006767 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768 return;
6769
Willy Tarreau24581ba2010-08-31 22:39:35 +02006770 /* OK so now we know we have to process this response cookie.
6771 * The format of the Set-Cookie header is slightly different
6772 * from the format of the Cookie header in that it does not
6773 * support the comma as a cookie delimiter (thus the header
6774 * cannot be folded) because the Expires attribute described in
6775 * the original Netscape's spec may contain an unquoted date
6776 * with a comma inside. We have to live with this because
6777 * many browsers don't support Max-Age and some browsers don't
6778 * support quoted strings. However the Set-Cookie2 header is
6779 * clean.
6780 *
6781 * We have to keep multiple pointers in order to support cookie
6782 * removal at the beginning, middle or end of header without
6783 * corrupting the header (in case of set-cookie2). A special
6784 * pointer, <scav> points to the beginning of the set-cookie-av
6785 * fields after the first semi-colon. The <next> pointer points
6786 * either to the end of line (set-cookie) or next unquoted comma
6787 * (set-cookie2). All of these headers are valid :
6788 *
6789 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6790 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6791 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6792 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6793 * | | | | | | | | | |
6794 * | | | | | | | | +-> next hdr_end <--+
6795 * | | | | | | | +------------> scav
6796 * | | | | | | +--------------> val_end
6797 * | | | | | +--------------------> val_beg
6798 * | | | | +----------------------> equal
6799 * | | | +------------------------> att_end
6800 * | | +----------------------------> att_beg
6801 * | +------------------------------> prev
6802 * +-----------------------------------------> hdr_beg
6803 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 for (; prev < hdr_end; prev = next) {
6806 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807
Willy Tarreau24581ba2010-08-31 22:39:35 +02006808 /* find att_beg */
6809 att_beg = prev + 1;
6810 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6811 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812
Willy Tarreau24581ba2010-08-31 22:39:35 +02006813 /* find att_end : this is the first character after the last non
6814 * space before the equal. It may be equal to hdr_end.
6815 */
6816 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817
Willy Tarreau24581ba2010-08-31 22:39:35 +02006818 while (equal < hdr_end) {
6819 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6820 break;
6821 if (http_is_spht[(unsigned char)*equal++])
6822 continue;
6823 att_end = equal;
6824 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6827 * is between <att_beg> and <equal>, both may be identical.
6828 */
6829
6830 /* look for end of cookie if there is an equal sign */
6831 if (equal < hdr_end && *equal == '=') {
6832 /* look for the beginning of the value */
6833 val_beg = equal + 1;
6834 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6835 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836
Willy Tarreau24581ba2010-08-31 22:39:35 +02006837 /* find the end of the value, respecting quotes */
6838 next = find_cookie_value_end(val_beg, hdr_end);
6839
6840 /* make val_end point to the first white space or delimitor after the value */
6841 val_end = next;
6842 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6843 val_end--;
6844 } else {
6845 /* <equal> points to next comma, semi-colon or EOL */
6846 val_beg = val_end = next = equal;
6847 }
6848
6849 if (next < hdr_end) {
6850 /* Set-Cookie2 supports multiple cookies, and <next> points to
6851 * a colon or semi-colon before the end. So skip all attr-value
6852 * pairs and look for the next comma. For Set-Cookie, since
6853 * commas are permitted in values, skip to the end.
6854 */
6855 if (is_cookie2)
6856 next = find_hdr_value_end(next, hdr_end);
6857 else
6858 next = hdr_end;
6859 }
6860
6861 /* Now everything is as on the diagram above */
6862
6863 /* Ignore cookies with no equal sign */
6864 if (equal == val_end)
6865 continue;
6866
6867 /* If there are spaces around the equal sign, we need to
6868 * strip them otherwise we'll get trouble for cookie captures,
6869 * or even for rewrites. Since this happens extremely rarely,
6870 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006872 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6873 int stripped_before = 0;
6874 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 if (att_end != equal) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006877 stripped_before = buffer_replace2(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006878 equal += stripped_before;
6879 val_beg += stripped_before;
6880 }
6881
6882 if (val_beg > equal + 1) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006883 stripped_after = buffer_replace2(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 val_beg += stripped_after;
6885 stripped_before += stripped_after;
6886 }
6887
6888 val_end += stripped_before;
6889 next += stripped_before;
6890 hdr_end += stripped_before;
6891 hdr_next += stripped_before;
6892 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006893 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 }
6895
6896 /* First, let's see if we want to capture this cookie. We check
6897 * that we don't already have a server side cookie, because we
6898 * can only capture one. Also as an optimisation, we ignore
6899 * cookies shorter than the declared name.
6900 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006901 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006902 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 (val_end - att_beg >= t->fe->capture_namelen) &&
6904 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6905 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006906 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907 Alert("HTTP logging : out of memory.\n");
6908 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006909 else {
6910 if (log_len > t->fe->capture_len)
6911 log_len = t->fe->capture_len;
6912 memcpy(txn->srv_cookie, att_beg, log_len);
6913 txn->srv_cookie[log_len] = 0;
6914 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915 }
6916
Willy Tarreau827aee92011-03-10 16:55:02 +01006917 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006919 if (!(t->flags & SN_IGNORE_PRST) &&
6920 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6921 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006922 /* assume passive cookie by default */
6923 txn->flags &= ~TX_SCK_MASK;
6924 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006925
6926 /* If the cookie is in insert mode on a known server, we'll delete
6927 * this occurrence because we'll insert another one later.
6928 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 * a direct access.
6930 */
Willy Tarreau67402132012-05-31 20:40:20 +02006931 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006932 /* The "preserve" flag was set, we don't want to touch the
6933 * server's cookie.
6934 */
6935 }
Willy Tarreau67402132012-05-31 20:40:20 +02006936 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
6937 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006938 /* this cookie must be deleted */
6939 if (*prev == ':' && next == hdr_end) {
6940 /* whole header */
Willy Tarreauaf819352012-08-27 22:08:00 +02006941 delta = buffer_replace2(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6943 txn->hdr_idx.used--;
6944 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006945 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006946 hdr_next += delta;
6947 http_msg_move_end(&txn->rsp, delta);
6948 /* note: while both invalid now, <next> and <hdr_end>
6949 * are still equal, so the for() will stop as expected.
6950 */
6951 } else {
6952 /* just remove the value */
Willy Tarreauaf819352012-08-27 22:08:00 +02006953 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 next = prev;
6955 hdr_end += delta;
6956 hdr_next += delta;
6957 cur_hdr->len += delta;
6958 http_msg_move_end(&txn->rsp, delta);
6959 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006960 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006961 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 }
Willy Tarreau67402132012-05-31 20:40:20 +02006964 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006965 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966 * with this server since we know it.
6967 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006968 delta = buffer_replace2(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006969 next += delta;
6970 hdr_end += delta;
6971 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006973 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974
Willy Tarreauf1348312010-10-07 15:54:11 +02006975 txn->flags &= ~TX_SCK_MASK;
6976 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977 }
Willy Tarreaua0590312012-06-06 16:07:00 +02006978 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006980 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006982 delta = buffer_replace2(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006983 next += delta;
6984 hdr_end += delta;
6985 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006987 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988
Willy Tarreau827aee92011-03-10 16:55:02 +01006989 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006990 txn->flags &= ~TX_SCK_MASK;
6991 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 }
6993 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006994 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6995 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006996 int cmp_len, value_len;
6997 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998
Cyril Bontéb21570a2009-11-29 20:04:48 +01006999 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007000 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7001 value_begin = att_beg + t->be->appsession_name_len;
7002 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007003 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007004 cmp_len = att_end - att_beg;
7005 value_begin = val_beg;
7006 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007007 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007008
Cyril Bonté17530c32010-04-06 21:11:10 +02007009 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7011 /* free a possibly previously allocated memory */
7012 pool_free2(apools.sessid, txn->sessid);
7013
Cyril Bontéb21570a2009-11-29 20:04:48 +01007014 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007015 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007016 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7017 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7018 return;
7019 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007020 memcpy(txn->sessid, value_begin, value_len);
7021 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 }
7024 /* that's done for this cookie, check the next one on the same
7025 * line when next != hdr_end (only if is_cookie2).
7026 */
7027 }
7028 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007030 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007031
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007032 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007033 appsess *asession = NULL;
7034 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007035 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007036 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007037 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007038 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7039 Alert("Not enough Memory process_srv():asession:calloc().\n");
7040 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7041 return;
7042 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007043 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7044
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007045 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7046 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7047 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007048 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007049 return;
7050 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007051 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007052 asession->sessid[t->be->appsession_len] = 0;
7053
Willy Tarreau827aee92011-03-10 16:55:02 +01007054 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007055 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007056 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007057 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007058 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007059 return;
7060 }
7061 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007062 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007063
7064 asession->request_count = 0;
7065 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7066 }
7067
7068 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7069 asession->request_count++;
7070 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071}
7072
7073
Willy Tarreaua15645d2007-03-18 16:22:39 +01007074/*
7075 * Check if response is cacheable or not. Updates t->flags.
7076 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007077void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078{
7079 struct http_txn *txn = &t->txn;
7080 char *p1, *p2;
7081
7082 char *cur_ptr, *cur_end, *cur_next;
7083 int cur_idx;
7084
Willy Tarreau5df51872007-11-25 16:20:08 +01007085 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086 return;
7087
7088 /* Iterate through the headers.
7089 * we start with the start line.
7090 */
7091 cur_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007092 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007093
7094 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7095 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007096 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007097
7098 cur_hdr = &txn->hdr_idx.v[cur_idx];
7099 cur_ptr = cur_next;
7100 cur_end = cur_ptr + cur_hdr->len;
7101 cur_next = cur_end + cur_hdr->cr + 1;
7102
7103 /* We have one full header between cur_ptr and cur_end, and the
7104 * next header starts at cur_next. We're only interested in
7105 * "Cookie:" headers.
7106 */
7107
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007108 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7109 if (val) {
7110 if ((cur_end - (cur_ptr + val) >= 8) &&
7111 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7112 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7113 return;
7114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115 }
7116
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007117 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7118 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 continue;
7120
7121 /* OK, right now we know we have a cache-control header at cur_ptr */
7122
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007123 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124
7125 if (p1 >= cur_end) /* no more info */
7126 continue;
7127
7128 /* p1 is at the beginning of the value */
7129 p2 = p1;
7130
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007131 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 p2++;
7133
7134 /* we have a complete value between p1 and p2 */
7135 if (p2 < cur_end && *p2 == '=') {
7136 /* we have something of the form no-cache="set-cookie" */
7137 if ((cur_end - p1 >= 21) &&
7138 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7139 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007140 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007141 continue;
7142 }
7143
7144 /* OK, so we know that either p2 points to the end of string or to a comma */
7145 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7146 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7147 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7148 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007149 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 return;
7151 }
7152
7153 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007154 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 continue;
7156 }
7157 }
7158}
7159
7160
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161/*
7162 * Try to retrieve a known appsession in the URI, then the associated server.
7163 * If the server is found, it's assigned to the session.
7164 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007166{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007167 char *end_params, *first_param, *cur_param, *next_param;
7168 char separator;
7169 int value_len;
7170
7171 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007173 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007174 (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 +01007175 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177
Cyril Bontéb21570a2009-11-29 20:04:48 +01007178 first_param = NULL;
7179 switch (mode) {
7180 case PR_O2_AS_M_PP:
7181 first_param = memchr(begin, ';', len);
7182 break;
7183 case PR_O2_AS_M_QS:
7184 first_param = memchr(begin, '?', len);
7185 break;
7186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007187
Cyril Bontéb21570a2009-11-29 20:04:48 +01007188 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007190 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007191
Cyril Bontéb21570a2009-11-29 20:04:48 +01007192 switch (mode) {
7193 case PR_O2_AS_M_PP:
7194 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7195 end_params = (char *) begin + len;
7196 }
7197 separator = ';';
7198 break;
7199 case PR_O2_AS_M_QS:
7200 end_params = (char *) begin + len;
7201 separator = '&';
7202 break;
7203 default:
7204 /* unknown mode, shouldn't happen */
7205 return;
7206 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007207
Cyril Bontéb21570a2009-11-29 20:04:48 +01007208 cur_param = next_param = end_params;
7209 while (cur_param > first_param) {
7210 cur_param--;
7211 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7212 /* let's see if this is the appsession parameter */
7213 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7214 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7215 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7216 /* Cool... it's the right one */
7217 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7218 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7219 if (value_len > 0) {
7220 manage_client_side_appsession(t, cur_param, value_len);
7221 }
7222 break;
7223 }
7224 next_param = cur_param;
7225 }
7226 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007227#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007228 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007229 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007231}
7232
Willy Tarreaub2513902006-12-17 14:52:38 +01007233/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007234 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007235 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007236 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007237 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007238 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007239 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007240 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007241 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007242int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007243{
7244 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007245 struct http_msg *msg = &txn->req;
Willy Tarreau572bf902012-07-02 17:01:20 +02007246 const char *uri = msg->buf->buf.p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007247 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007248
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007249 if (!uri_auth)
7250 return 0;
7251
Cyril Bonté70be45d2010-10-12 00:14:35 +02007252 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007253 return 0;
7254
Willy Tarreau295a8372011-03-10 11:25:07 +01007255 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007256 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007259 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007260 return 0;
7261
Willy Tarreau3a215be2012-03-09 21:39:51 +01007262 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007263 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007264 return 0;
7265
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007266 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007267 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007268 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007269 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007270 break;
7271 }
7272 h++;
7273 }
7274
7275 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007276 h = uri + uri_auth->uri_len;
7277 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007278 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007279 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007280 break;
7281 }
7282 h++;
7283 }
7284 }
7285
Willy Tarreau3a215be2012-03-09 21:39:51 +01007286 h = uri + uri_auth->uri_len;
7287 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007288 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007289 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007290 break;
7291 }
7292 h++;
7293 }
7294
Willy Tarreau3a215be2012-03-09 21:39:51 +01007295 h = uri + uri_auth->uri_len;
7296 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007297 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007298 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007299 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007300 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007301 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7302 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7303 si->applet.ctx.stats.st_code = i;
7304 break;
7305 }
7306 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007307 break;
7308 }
7309 h++;
7310 }
7311
Willy Tarreau295a8372011-03-10 11:25:07 +01007312 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007313
Willy Tarreaub2513902006-12-17 14:52:38 +01007314 return 1;
7315}
7316
Willy Tarreau4076a152009-04-02 15:18:36 +02007317/*
7318 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007319 * By default it tries to report the error position as msg->err_pos. However if
7320 * this one is not set, it will then report msg->next, which is the last known
7321 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreau572bf902012-07-02 17:01:20 +02007322 * displays buffers as a contiguous area starting at buf->buf.p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007323 */
7324void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007325 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007326 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007327{
Willy Tarreau7421efb2012-07-02 15:11:27 +02007328 struct channel *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007329 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007330
Willy Tarreau572bf902012-07-02 17:01:20 +02007331 es->len = MIN(buf->buf.i, sizeof(es->buf));
7332 len1 = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007333 len1 = MIN(len1, es->len);
7334 len2 = es->len - len1; /* remaining data if buffer wraps */
7335
Willy Tarreau572bf902012-07-02 17:01:20 +02007336 memcpy(es->buf, buf->buf.p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007337 if (len2)
Willy Tarreau572bf902012-07-02 17:01:20 +02007338 memcpy(es->buf + len1, buf->buf.data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007339
Willy Tarreau4076a152009-04-02 15:18:36 +02007340 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007341 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007342 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007343 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007344
Willy Tarreau4076a152009-04-02 15:18:36 +02007345 es->when = date; // user-visible date
7346 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007347 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007348 es->oe = other_end;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007349 es->src = s->req->prod->conn.addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007350 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007351 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007352 es->b_flags = buf->flags;
7353 es->s_flags = s->flags;
7354 es->t_flags = s->txn.flags;
7355 es->m_flags = msg->flags;
Willy Tarreau572bf902012-07-02 17:01:20 +02007356 es->b_out = buf->buf.o;
7357 es->b_wrap = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007358 es->b_tot = buf->total;
7359 es->m_clen = msg->chunk_len;
7360 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007361}
Willy Tarreaub2513902006-12-17 14:52:38 +01007362
Willy Tarreau294c4732011-12-16 21:35:50 +01007363/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7364 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7365 * performed over the whole headers. Otherwise it must contain a valid header
7366 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7367 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7368 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7369 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7370 * -1.
7371 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007372 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007373unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007374 struct hdr_idx *idx, int occ,
7375 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007376{
Willy Tarreau294c4732011-12-16 21:35:50 +01007377 struct hdr_ctx local_ctx;
7378 char *ptr_hist[MAX_HDR_HISTORY];
7379 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007380 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007381 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007382
Willy Tarreau294c4732011-12-16 21:35:50 +01007383 if (!ctx) {
7384 local_ctx.idx = 0;
7385 ctx = &local_ctx;
7386 }
7387
Willy Tarreaubce70882009-09-07 11:51:47 +02007388 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007389 /* search from the beginning */
Willy Tarreau572bf902012-07-02 17:01:20 +02007390 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007391 occ--;
7392 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007393 *vptr = ctx->line + ctx->val;
7394 *vlen = ctx->vlen;
7395 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007396 }
7397 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007398 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007399 }
7400
7401 /* negative occurrence, we scan all the list then walk back */
7402 if (-occ > MAX_HDR_HISTORY)
7403 return 0;
7404
Willy Tarreau294c4732011-12-16 21:35:50 +01007405 found = hist_ptr = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007406 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007407 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7408 len_hist[hist_ptr] = ctx->vlen;
7409 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007410 hist_ptr = 0;
7411 found++;
7412 }
7413 if (-occ > found)
7414 return 0;
7415 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7416 * find occurrence -occ, so we have to check [hist_ptr+occ].
7417 */
7418 hist_ptr += occ;
7419 if (hist_ptr >= MAX_HDR_HISTORY)
7420 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007421 *vptr = ptr_hist[hist_ptr];
7422 *vlen = len_hist[hist_ptr];
7423 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007424}
7425
Willy Tarreaubaaee002006-06-26 02:48:02 +02007426/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007427 * Print a debug line with a header. Always stop at the first CR or LF char,
7428 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7429 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007430 */
7431void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7432{
7433 int len, max;
7434 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007435 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreaue92693a2012-09-24 21:13:39 +02007436
7437 for (max = 0; start + max < end; max++)
7438 if (start[max] == '\r' || start[max] == '\n')
7439 break;
7440
7441 UBOUND(max, trashlen - len - 3);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442 len += strlcpy2(trash + len, start, max + 1);
7443 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007444 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445}
7446
Willy Tarreau0937bc42009-12-22 15:03:09 +01007447/*
7448 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7449 * the required fields are properly allocated and that we only need to (re)init
7450 * them. This should be used before processing any new request.
7451 */
7452void http_init_txn(struct session *s)
7453{
7454 struct http_txn *txn = &s->txn;
7455 struct proxy *fe = s->fe;
7456
7457 txn->flags = 0;
7458 txn->status = -1;
7459
William Lallemand5f232402012-04-05 18:02:55 +02007460 global.req_count++;
7461
Willy Tarreauf64d1412010-10-07 20:06:11 +02007462 txn->cookie_first_date = 0;
7463 txn->cookie_last_date = 0;
7464
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007465 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007466 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007467 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007468 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007469 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007470 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007471 txn->req.chunk_len = 0LL;
7472 txn->req.body_len = 0LL;
7473 txn->rsp.chunk_len = 0LL;
7474 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007475 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7476 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007477 txn->req.buf = s->req;
7478 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007479
7480 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007481
7482 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7483 if (fe->options2 & PR_O2_REQBUG_OK)
7484 txn->req.err_pos = -1; /* let buggy requests pass */
7485
Willy Tarreau46023632010-01-07 22:51:47 +01007486 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007487 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7488
Willy Tarreau46023632010-01-07 22:51:47 +01007489 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007490 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7491
7492 if (txn->hdr_idx.v)
7493 hdr_idx_init(&txn->hdr_idx);
7494}
7495
7496/* to be used at the end of a transaction */
7497void http_end_txn(struct session *s)
7498{
7499 struct http_txn *txn = &s->txn;
7500
7501 /* these ones will have been dynamically allocated */
7502 pool_free2(pool2_requri, txn->uri);
7503 pool_free2(pool2_capture, txn->cli_cookie);
7504 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007505 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007506 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007507
William Lallemanda73203e2012-03-12 12:48:57 +01007508 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007509 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007510 txn->uri = NULL;
7511 txn->srv_cookie = NULL;
7512 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007513
7514 if (txn->req.cap) {
7515 struct cap_hdr *h;
7516 for (h = s->fe->req_cap; h; h = h->next)
7517 pool_free2(h->pool, txn->req.cap[h->index]);
7518 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7519 }
7520
7521 if (txn->rsp.cap) {
7522 struct cap_hdr *h;
7523 for (h = s->fe->rsp_cap; h; h = h->next)
7524 pool_free2(h->pool, txn->rsp.cap[h->index]);
7525 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7526 }
7527
Willy Tarreau0937bc42009-12-22 15:03:09 +01007528}
7529
7530/* to be used at the end of a transaction to prepare a new one */
7531void http_reset_txn(struct session *s)
7532{
7533 http_end_txn(s);
7534 http_init_txn(s);
7535
7536 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007537 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007538 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007539 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007540 /* re-init store persistence */
7541 s->store_count = 0;
7542
Willy Tarreau0937bc42009-12-22 15:03:09 +01007543 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007544
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007545 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007546
Willy Tarreau739cfba2010-01-25 23:11:14 +01007547 /* We must trim any excess data from the response buffer, because we
7548 * may have blocked an invalid response from a server that we don't
7549 * want to accidentely forward once we disable the analysers, nor do
7550 * we want those data to come along with next response. A typical
7551 * example of such data would be from a buggy server responding to
7552 * a HEAD with some data, or sending more than the advertised
7553 * content-length.
7554 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007555 if (unlikely(s->rep->buf.i))
7556 s->rep->buf.i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007557
Willy Tarreau0937bc42009-12-22 15:03:09 +01007558 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007559 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007560
Willy Tarreaud04e8582010-05-31 12:31:35 +02007561 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007562 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007563
7564 s->req->rex = TICK_ETERNITY;
7565 s->req->wex = TICK_ETERNITY;
7566 s->req->analyse_exp = TICK_ETERNITY;
7567 s->rep->rex = TICK_ETERNITY;
7568 s->rep->wex = TICK_ETERNITY;
7569 s->rep->analyse_exp = TICK_ETERNITY;
7570}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007571
Willy Tarreauff011f22011-01-06 17:51:27 +01007572void free_http_req_rules(struct list *r) {
7573 struct http_req_rule *tr, *pr;
7574
7575 list_for_each_entry_safe(pr, tr, r, list) {
7576 LIST_DEL(&pr->list);
7577 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7578 free(pr->http_auth.realm);
7579
7580 free(pr);
7581 }
7582}
7583
7584struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7585{
7586 struct http_req_rule *rule;
7587 int cur_arg;
7588
7589 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7590 if (!rule) {
7591 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7592 return NULL;
7593 }
7594
7595 if (!*args[0]) {
7596 goto req_error_parsing;
7597 } else if (!strcmp(args[0], "allow")) {
7598 rule->action = HTTP_REQ_ACT_ALLOW;
7599 cur_arg = 1;
7600 } else if (!strcmp(args[0], "deny")) {
7601 rule->action = HTTP_REQ_ACT_DENY;
7602 cur_arg = 1;
7603 } else if (!strcmp(args[0], "auth")) {
7604 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7605 cur_arg = 1;
7606
7607 while(*args[cur_arg]) {
7608 if (!strcmp(args[cur_arg], "realm")) {
7609 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7610 cur_arg+=2;
7611 continue;
7612 } else
7613 break;
7614 }
7615 } else {
7616req_error_parsing:
7617 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7618 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7619 return NULL;
7620 }
7621
7622 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7623 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007624 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007625
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007626 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7627 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7628 file, linenum, args[0], errmsg);
7629 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007630 return NULL;
7631 }
7632 rule->cond = cond;
7633 }
7634 else if (*args[cur_arg]) {
7635 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7636 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7637 file, linenum, args[0], args[cur_arg]);
7638 return NULL;
7639 }
7640
7641 return rule;
7642}
7643
Willy Tarreau8797c062007-05-07 00:55:35 +02007644/************************************************************************/
7645/* The code below is dedicated to ACL parsing and matching */
7646/************************************************************************/
7647
7648
Willy Tarreau14174bc2012-04-16 14:34:04 +02007649/* This function ensures that the prerequisites for an L7 fetch are ready,
7650 * which means that a request or response is ready. If some data is missing,
7651 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007652 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7653 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007654 *
7655 * The function returns :
7656 * 0 if some data is missing or if the requested data cannot be fetched
7657 * -1 if it is certain that we'll never have any HTTP message there
7658 * 1 if an HTTP message is ready
7659 */
7660static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007661acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007662 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007663{
7664 struct http_txn *txn = l7;
7665 struct http_msg *msg = &txn->req;
7666
7667 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7668 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7669 */
7670
7671 if (unlikely(!s || !txn))
7672 return 0;
7673
7674 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007675 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007676
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007677 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007678 if (unlikely(!s->req))
7679 return 0;
7680
7681 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007682 if ((msg->msg_state == HTTP_MSG_ERROR) ||
7683 buffer_full(&s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007684 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007685 return -1;
7686 }
7687
7688 /* Try to decode HTTP request */
Willy Tarreau572bf902012-07-02 17:01:20 +02007689 if (likely(msg->next < s->req->buf.i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007690 http_msg_analyzer(msg, &txn->hdr_idx);
7691
7692 /* Still no valid request ? */
7693 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007694 if ((msg->msg_state == HTTP_MSG_ERROR) ||
7695 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 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007700 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007701 return 0;
7702 }
7703
7704 /* OK we just got a valid HTTP request. We have some minor
7705 * preparation to perform so that further checks can rely
7706 * on HTTP tests.
7707 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007708 txn->meth = find_http_meth(msg->buf->buf.p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007709 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7710 s->flags |= SN_REDIRECTABLE;
7711
7712 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007713 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007714 return -1;
7715 }
7716 }
7717
Willy Tarreau24e32d82012-04-23 23:55:44 +02007718 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007719 return 0; /* data might have moved and indexes changed */
7720
7721 /* otherwise everything's ready for the request */
7722 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007723 else {
7724 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007725 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7726 return 0;
7727 }
7728
7729 /* everything's OK */
7730 return 1;
7731}
Willy Tarreau8797c062007-05-07 00:55:35 +02007732
Willy Tarreauc0239e02012-04-16 14:42:55 +02007733#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007734 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 +02007735
Willy Tarreau24e32d82012-04-23 23:55:44 +02007736#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007737 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 +02007738
Willy Tarreau8797c062007-05-07 00:55:35 +02007739
7740/* 1. Check on METHOD
7741 * We use the pre-parsed method if it is known, and store its number as an
7742 * integer. If it is unknown, we use the pointer and the length.
7743 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007744static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007745{
7746 int len, meth;
7747
Willy Tarreauae8b7962007-06-09 23:10:04 +02007748 len = strlen(*text);
7749 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007750
7751 pattern->val.i = meth;
7752 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007753 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007754 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007755 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007756 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007757 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007758 pattern->len = len;
7759 }
7760 return 1;
7761}
7762
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007763/* This function fetches the method of current HTTP request and stores
7764 * it in the global pattern struct as a chunk. There are two possibilities :
7765 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7766 * in <len> and <ptr> is NULL ;
7767 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7768 * <len> to its length.
7769 * This is intended to be used with acl_match_meth() only.
7770 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007771static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007772acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007773 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007774{
7775 int meth;
7776 struct http_txn *txn = l7;
7777
Willy Tarreau24e32d82012-04-23 23:55:44 +02007778 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007779
Willy Tarreau8797c062007-05-07 00:55:35 +02007780 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007781 smp->type = SMP_T_UINT;
7782 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007783 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007784 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7785 /* ensure the indexes are not affected */
7786 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007787 smp->type = SMP_T_CSTR;
7788 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007789 smp->data.str.str = txn->req.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007790 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007791 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007792 return 1;
7793}
7794
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007795/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007796static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007797{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007798 int icase;
7799
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007800
Willy Tarreauf853c462012-04-23 18:53:56 +02007801 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007802 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007803 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007804 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007805 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007806 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007807
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007808 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7809 if (pattern->val.i != HTTP_METH_OTHER)
7810 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007811
7812 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007813 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007814 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007815
7816 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007817 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7818 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007819 return ACL_PAT_FAIL;
7820 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007821}
7822
7823/* 2. Check on Request/Status Version
7824 * We simply compare strings here.
7825 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007826static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007827{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007828 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007829 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007830 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007831 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007832 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007833 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007834 return 1;
7835}
7836
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007837static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007838acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007839 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007840{
7841 struct http_txn *txn = l7;
7842 char *ptr;
7843 int len;
7844
Willy Tarreauc0239e02012-04-16 14:42:55 +02007845 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007846
Willy Tarreau8797c062007-05-07 00:55:35 +02007847 len = txn->req.sl.rq.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007848 ptr = txn->req.buf->buf.p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007849
7850 while ((len-- > 0) && (*ptr++ != '/'));
7851 if (len <= 0)
7852 return 0;
7853
Willy Tarreauf853c462012-04-23 18:53:56 +02007854 smp->type = SMP_T_CSTR;
7855 smp->data.str.str = ptr;
7856 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007857
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007858 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007859 return 1;
7860}
7861
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007862static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007863acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007864 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007865{
7866 struct http_txn *txn = l7;
7867 char *ptr;
7868 int len;
7869
Willy Tarreauc0239e02012-04-16 14:42:55 +02007870 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007871
Willy Tarreau8797c062007-05-07 00:55:35 +02007872 len = txn->rsp.sl.st.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007873 ptr = txn->rsp.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007874
7875 while ((len-- > 0) && (*ptr++ != '/'));
7876 if (len <= 0)
7877 return 0;
7878
Willy Tarreauf853c462012-04-23 18:53:56 +02007879 smp->type = SMP_T_CSTR;
7880 smp->data.str.str = ptr;
7881 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007882
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007883 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007884 return 1;
7885}
7886
7887/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007888static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007889acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007890 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007891{
7892 struct http_txn *txn = l7;
7893 char *ptr;
7894 int len;
7895
Willy Tarreauc0239e02012-04-16 14:42:55 +02007896 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007897
Willy Tarreau8797c062007-05-07 00:55:35 +02007898 len = txn->rsp.sl.st.c_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007899 ptr = txn->rsp.buf->buf.p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007900
Willy Tarreauf853c462012-04-23 18:53:56 +02007901 smp->type = SMP_T_UINT;
7902 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007903 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007904 return 1;
7905}
7906
7907/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007908static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007909smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007910 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007911{
7912 struct http_txn *txn = l7;
7913
Willy Tarreauc0239e02012-04-16 14:42:55 +02007914 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007915
Willy Tarreauf853c462012-04-23 18:53:56 +02007916 smp->type = SMP_T_CSTR;
7917 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007918 smp->data.str.str = txn->req.buf->buf.p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007919 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007920 return 1;
7921}
7922
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007923static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007924smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007925 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007926{
7927 struct http_txn *txn = l7;
7928
Willy Tarreauc0239e02012-04-16 14:42:55 +02007929 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007930
7931 /* Parse HTTP request */
Willy Tarreau986a9d22012-08-30 21:11:38 +02007932 url2sa(txn->req.buf->buf.p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to);
7933 if (((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01007934 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007935 smp->type = SMP_T_IPV4;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007936 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007937
7938 /*
7939 * If we are parsing url in frontend space, we prepare backend stage
7940 * to not parse again the same url ! optimization lazyness...
7941 */
7942 if (px->options & PR_O_HTTP_PROXY)
7943 l4->flags |= SN_ADDR_SET;
7944
Willy Tarreau37406352012-04-23 16:16:37 +02007945 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007946 return 1;
7947}
7948
7949static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007950smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007951 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007952{
7953 struct http_txn *txn = l7;
7954
Willy Tarreauc0239e02012-04-16 14:42:55 +02007955 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007956
7957 /* Same optimization as url_ip */
Willy Tarreau986a9d22012-08-30 21:11:38 +02007958 url2sa(txn->req.buf->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 +02007959 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007960 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007961
7962 if (px->options & PR_O_HTTP_PROXY)
7963 l4->flags |= SN_ADDR_SET;
7964
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007965 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007966 return 1;
7967}
7968
Willy Tarreau185b5c42012-04-26 15:11:51 +02007969/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7970 * Accepts an optional argument of type string containing the header field name,
7971 * and an optional argument of type signed or unsigned integer to request an
7972 * explicit occurrence of the header. Note that in the event of a missing name,
7973 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007974 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007975static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007976smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007977 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007978{
7979 struct http_txn *txn = l7;
7980 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007981 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007982 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007983 int occ = 0;
7984 const char *name_str = NULL;
7985 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007986
Willy Tarreau185b5c42012-04-26 15:11:51 +02007987 if (args) {
7988 if (args[0].type != ARGT_STR)
7989 return 0;
7990 name_str = args[0].data.str.str;
7991 name_len = args[0].data.str.len;
7992
7993 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7994 occ = args[1].data.uint;
7995 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007996
Willy Tarreaue333ec92012-04-16 16:26:40 +02007997 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007998
Willy Tarreau185b5c42012-04-26 15:11:51 +02007999 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008000 /* search for header from the beginning */
8001 ctx->idx = 0;
8002
Willy Tarreau185b5c42012-04-26 15:11:51 +02008003 if (!occ && !(opt & SMP_OPT_ITERATE))
8004 /* no explicit occurrence and single fetch => last header by default */
8005 occ = -1;
8006
8007 if (!occ)
8008 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008009 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008010
Willy Tarreau185b5c42012-04-26 15:11:51 +02008011 smp->type = SMP_T_CSTR;
8012 smp->flags |= SMP_F_VOL_HDR;
8013 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 +02008014 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008015
Willy Tarreau37406352012-04-23 16:16:37 +02008016 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008017 return 0;
8018}
8019
Willy Tarreauc11416f2007-06-17 16:58:38 +02008020/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008021 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008022 */
8023static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008024smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008025 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008026{
8027 struct http_txn *txn = l7;
8028 struct hdr_idx *idx = &txn->hdr_idx;
8029 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008030 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008031 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008032
Willy Tarreau24e32d82012-04-23 23:55:44 +02008033 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008034 return 0;
8035
Willy Tarreaue333ec92012-04-16 16:26:40 +02008036 CHECK_HTTP_MESSAGE_FIRST();
8037
Willy Tarreau33a7e692007-06-10 19:45:56 +02008038 ctx.idx = 0;
8039 cnt = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02008040 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->buf.p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008041 cnt++;
8042
Willy Tarreauf853c462012-04-23 18:53:56 +02008043 smp->type = SMP_T_UINT;
8044 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008045 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008046 return 1;
8047}
8048
Willy Tarreau185b5c42012-04-26 15:11:51 +02008049/* Fetch an HTTP header's integer value. The integer value is returned. It
8050 * takes a mandatory argument of type string and an optional one of type int
8051 * to designate a specific occurrence. It returns an unsigned integer, which
8052 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008053 */
8054static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008055smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008056 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008057{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008058 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008059
Willy Tarreauf853c462012-04-23 18:53:56 +02008060 if (ret > 0) {
8061 smp->type = SMP_T_UINT;
8062 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8063 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008064
Willy Tarreaud53e2422012-04-16 17:21:11 +02008065 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008066}
8067
Willy Tarreau185b5c42012-04-26 15:11:51 +02008068/* Fetch an HTTP header's integer value. The integer value is returned. It
8069 * takes a mandatory argument of type string and an optional one of type int
8070 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008071 */
8072static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008073smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008074 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008075{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008076 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008077
Willy Tarreau185b5c42012-04-26 15:11:51 +02008078 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02008079 smp->type = SMP_T_IPV4;
8080 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02008081 break;
8082 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008083 if (!(smp->flags & SMP_F_NOT_LAST))
8084 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008085 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008086 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008087}
8088
Willy Tarreau737b0c12007-06-10 21:28:46 +02008089/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8090 * the first '/' after the possible hostname, and ends before the possible '?'.
8091 */
8092static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008093smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008094 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008095{
8096 struct http_txn *txn = l7;
8097 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008098
Willy Tarreauc0239e02012-04-16 14:42:55 +02008099 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008100
Willy Tarreau572bf902012-07-02 17:01:20 +02008101 end = txn->req.buf->buf.p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008102 ptr = http_get_path(txn);
8103 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008104 return 0;
8105
8106 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008107 smp->type = SMP_T_CSTR;
8108 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008109
8110 while (ptr < end && *ptr != '?')
8111 ptr++;
8112
Willy Tarreauf853c462012-04-23 18:53:56 +02008113 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008114 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008115 return 1;
8116}
8117
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008118/* This produces a concatenation of the first occurrence of the Host header
8119 * followed by the path component if it begins with a slash ('/'). This means
8120 * that '*' will not be added, resulting in exactly the first Host entry.
8121 * If no Host header is found, then the path is returned as-is. The returned
8122 * value is stored in the trash so it does not need to be marked constant.
8123 */
8124static int
8125smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8126 const struct arg *args, struct sample *smp)
8127{
8128 struct http_txn *txn = l7;
8129 char *ptr, *end, *beg;
8130 struct hdr_ctx ctx;
8131
8132 CHECK_HTTP_MESSAGE_FIRST();
8133
8134 ctx.idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02008135 if (!http_find_header2("Host", 4, txn->req.buf->buf.p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008136 !ctx.vlen)
8137 return smp_fetch_path(px, l4, l7, opt, args, smp);
8138
8139 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8140 memcpy(trash, ctx.line + ctx.val, ctx.vlen);
8141 smp->type = SMP_T_STR;
8142 smp->data.str.str = trash;
8143 smp->data.str.len = ctx.vlen;
8144
8145 /* now retrieve the path */
Willy Tarreau572bf902012-07-02 17:01:20 +02008146 end = txn->req.buf->buf.p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008147 beg = http_get_path(txn);
8148 if (!beg)
8149 beg = end;
8150
8151 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8152
8153 if (beg < ptr && *beg == '/') {
8154 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8155 smp->data.str.len += ptr - beg;
8156 }
8157
8158 smp->flags = SMP_F_VOL_1ST;
8159 return 1;
8160}
8161
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008162static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008163acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008164 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008165{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008166 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8167 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8168 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008169
Willy Tarreau24e32d82012-04-23 23:55:44 +02008170 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008171
Willy Tarreauf853c462012-04-23 18:53:56 +02008172 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008173 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008174 return 1;
8175}
8176
Willy Tarreau7f18e522010-10-22 20:04:13 +02008177/* return a valid test if the current request is the first one on the connection */
8178static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008179acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008180 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008181{
8182 if (!s)
8183 return 0;
8184
Willy Tarreauf853c462012-04-23 18:53:56 +02008185 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008186 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008187 return 1;
8188}
8189
Willy Tarreau34db1082012-04-19 17:16:54 +02008190/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008191static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008192acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008193 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008194{
8195
Willy Tarreau24e32d82012-04-23 23:55:44 +02008196 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008197 return 0;
8198
Willy Tarreauc0239e02012-04-16 14:42:55 +02008199 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008200
Willy Tarreauc0239e02012-04-16 14:42:55 +02008201 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008202 return 0;
8203
Willy Tarreauf853c462012-04-23 18:53:56 +02008204 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008205 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 +01008206 return 1;
8207}
Willy Tarreau8797c062007-05-07 00:55:35 +02008208
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008209/* Accepts exactly 1 argument of type userlist */
8210static int
8211acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8212 const struct arg *args, struct sample *smp)
8213{
8214
8215 if (!args || args->type != ARGT_USR)
8216 return 0;
8217
8218 CHECK_HTTP_MESSAGE_FIRST();
8219
8220 if (!get_http_auth(l4))
8221 return 0;
8222
8223 /* acl_match_auth() will need several information at once */
8224 smp->ctx.a[0] = args->data.usr; /* user list */
8225 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8226 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8227
8228 /* if the user does not belong to the userlist or has a wrong password,
8229 * report that it unconditionally does not match. Otherwise we return
8230 * a non-zero integer which will be ignored anyway since all the params
8231 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8232 */
8233 smp->type = SMP_T_BOOL;
8234 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8235 if (smp->data.uint)
8236 smp->type = SMP_T_UINT;
8237
8238 return 1;
8239}
8240
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008241/* Try to find the next occurrence of a cookie name in a cookie header value.
8242 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8243 * the cookie value is returned into *value and *value_l, and the function
8244 * returns a pointer to the next pointer to search from if the value was found.
8245 * Otherwise if the cookie was not found, NULL is returned and neither value
8246 * nor value_l are touched. The input <hdr> string should first point to the
8247 * header's value, and the <hdr_end> pointer must point to the first character
8248 * not part of the value. <list> must be non-zero if value may represent a list
8249 * of values (cookie headers). This makes it faster to abort parsing when no
8250 * list is expected.
8251 */
8252static char *
8253extract_cookie_value(char *hdr, const char *hdr_end,
8254 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008255 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008256{
8257 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8258 char *next;
8259
8260 /* we search at least a cookie name followed by an equal, and more
8261 * generally something like this :
8262 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8263 */
8264 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8265 /* Iterate through all cookies on this line */
8266
8267 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8268 att_beg++;
8269
8270 /* find att_end : this is the first character after the last non
8271 * space before the equal. It may be equal to hdr_end.
8272 */
8273 equal = att_end = att_beg;
8274
8275 while (equal < hdr_end) {
8276 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8277 break;
8278 if (http_is_spht[(unsigned char)*equal++])
8279 continue;
8280 att_end = equal;
8281 }
8282
8283 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8284 * is between <att_beg> and <equal>, both may be identical.
8285 */
8286
8287 /* look for end of cookie if there is an equal sign */
8288 if (equal < hdr_end && *equal == '=') {
8289 /* look for the beginning of the value */
8290 val_beg = equal + 1;
8291 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8292 val_beg++;
8293
8294 /* find the end of the value, respecting quotes */
8295 next = find_cookie_value_end(val_beg, hdr_end);
8296
8297 /* make val_end point to the first white space or delimitor after the value */
8298 val_end = next;
8299 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8300 val_end--;
8301 } else {
8302 val_beg = val_end = next = equal;
8303 }
8304
8305 /* We have nothing to do with attributes beginning with '$'. However,
8306 * they will automatically be removed if a header before them is removed,
8307 * since they're supposed to be linked together.
8308 */
8309 if (*att_beg == '$')
8310 continue;
8311
8312 /* Ignore cookies with no equal sign */
8313 if (equal == next)
8314 continue;
8315
8316 /* Now we have the cookie name between att_beg and att_end, and
8317 * its value between val_beg and val_end.
8318 */
8319
8320 if (att_end - att_beg == cookie_name_l &&
8321 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8322 /* let's return this value and indicate where to go on from */
8323 *value = val_beg;
8324 *value_l = val_end - val_beg;
8325 return next + 1;
8326 }
8327
8328 /* Set-Cookie headers only have the name in the first attr=value part */
8329 if (!list)
8330 break;
8331 }
8332
8333 return NULL;
8334}
8335
Willy Tarreaue333ec92012-04-16 16:26:40 +02008336/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008337 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8338 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008339 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008340 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008341 * Accepts exactly 1 argument of type string. If the input options indicate
8342 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008343 */
8344static int
Willy Tarreau51539362012-05-08 12:46:28 +02008345smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8346 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008347{
8348 struct http_txn *txn = l7;
8349 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008350 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008351 const struct http_msg *msg;
8352 const char *hdr_name;
8353 int hdr_name_len;
8354 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008355 int occ = 0;
8356 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008357
Willy Tarreau24e32d82012-04-23 23:55:44 +02008358 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008359 return 0;
8360
Willy Tarreaue333ec92012-04-16 16:26:40 +02008361 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008362
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008363 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008364 msg = &txn->req;
8365 hdr_name = "Cookie";
8366 hdr_name_len = 6;
8367 } else {
8368 msg = &txn->rsp;
8369 hdr_name = "Set-Cookie";
8370 hdr_name_len = 10;
8371 }
8372
Willy Tarreau28376d62012-04-26 21:26:10 +02008373 if (!occ && !(opt & SMP_OPT_ITERATE))
8374 /* no explicit occurrence and single fetch => last cookie by default */
8375 occ = -1;
8376
8377 /* OK so basically here, either we want only one value and it's the
8378 * last one, or we want to iterate over all of them and we fetch the
8379 * next one.
8380 */
8381
Willy Tarreau572bf902012-07-02 17:01:20 +02008382 sol = msg->buf->buf.p;
Willy Tarreau37406352012-04-23 16:16:37 +02008383 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008384 /* search for the header from the beginning, we must first initialize
8385 * the search parameters.
8386 */
Willy Tarreau37406352012-04-23 16:16:37 +02008387 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008388 ctx->idx = 0;
8389 }
8390
Willy Tarreau28376d62012-04-26 21:26:10 +02008391 smp->flags |= SMP_F_VOL_HDR;
8392
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008393 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008394 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8395 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008396 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8397 goto out;
8398
Willy Tarreau24e32d82012-04-23 23:55:44 +02008399 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008400 continue;
8401
Willy Tarreau37406352012-04-23 16:16:37 +02008402 smp->ctx.a[0] = ctx->line + ctx->val;
8403 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008404 }
8405
Willy Tarreauf853c462012-04-23 18:53:56 +02008406 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008407 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008408 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008409 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008410 &smp->data.str.str,
8411 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008412 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008413 found = 1;
8414 if (occ >= 0) {
8415 /* one value was returned into smp->data.str.{str,len} */
8416 smp->flags |= SMP_F_NOT_LAST;
8417 return 1;
8418 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008419 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008420 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008421 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008422 /* all cookie headers and values were scanned. If we're looking for the
8423 * last occurrence, we may return it now.
8424 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008425 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008426 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008427 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008428}
8429
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008430/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008431 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008432 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008433 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008434 */
8435static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008436acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008437 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008438{
8439 struct http_txn *txn = l7;
8440 struct hdr_idx *idx = &txn->hdr_idx;
8441 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008442 const struct http_msg *msg;
8443 const char *hdr_name;
8444 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008445 int cnt;
8446 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008447 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008448
Willy Tarreau24e32d82012-04-23 23:55:44 +02008449 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008450 return 0;
8451
Willy Tarreaue333ec92012-04-16 16:26:40 +02008452 CHECK_HTTP_MESSAGE_FIRST();
8453
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008454 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008455 msg = &txn->req;
8456 hdr_name = "Cookie";
8457 hdr_name_len = 6;
8458 } else {
8459 msg = &txn->rsp;
8460 hdr_name = "Set-Cookie";
8461 hdr_name_len = 10;
8462 }
8463
Willy Tarreau572bf902012-07-02 17:01:20 +02008464 sol = msg->buf->buf.p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008465 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008466 ctx.idx = 0;
8467 cnt = 0;
8468
8469 while (1) {
8470 /* Note: val_beg == NULL every time we need to fetch a new header */
8471 if (!val_beg) {
8472 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8473 break;
8474
Willy Tarreau24e32d82012-04-23 23:55:44 +02008475 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008476 continue;
8477
8478 val_beg = ctx.line + ctx.val;
8479 val_end = val_beg + ctx.vlen;
8480 }
8481
Willy Tarreauf853c462012-04-23 18:53:56 +02008482 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008483 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008484 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008485 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008486 &smp->data.str.str,
8487 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008488 cnt++;
8489 }
8490 }
8491
Willy Tarreauf853c462012-04-23 18:53:56 +02008492 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008493 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008494 return 1;
8495}
8496
Willy Tarreau51539362012-05-08 12:46:28 +02008497/* Fetch an cookie's integer value. The integer value is returned. It
8498 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8499 */
8500static int
8501smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8502 const struct arg *args, struct sample *smp)
8503{
8504 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8505
8506 if (ret > 0) {
8507 smp->type = SMP_T_UINT;
8508 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8509 }
8510
8511 return ret;
8512}
8513
Willy Tarreau8797c062007-05-07 00:55:35 +02008514/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008515/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008516/************************************************************************/
8517
David Cournapeau16023ee2010-12-23 20:55:41 +09008518/*
8519 * Given a path string and its length, find the position of beginning of the
8520 * query string. Returns NULL if no query string is found in the path.
8521 *
8522 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8523 *
8524 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8525 */
8526static inline char *find_query_string(char *path, size_t path_l)
8527{
8528 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008529
David Cournapeau16023ee2010-12-23 20:55:41 +09008530 p = memchr(path, '?', path_l);
8531 return p ? p + 1 : NULL;
8532}
8533
8534static inline int is_param_delimiter(char c)
8535{
8536 return c == '&' || c == ';';
8537}
8538
8539/*
8540 * Given a url parameter, find the starting position of the first occurence,
8541 * or NULL if the parameter is not found.
8542 *
8543 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8544 * the function will return query_string+8.
8545 */
8546static char*
8547find_url_param_pos(char* query_string, size_t query_string_l,
8548 char* url_param_name, size_t url_param_name_l)
8549{
8550 char *pos, *last;
8551
8552 pos = query_string;
8553 last = query_string + query_string_l - url_param_name_l - 1;
8554
8555 while (pos <= last) {
8556 if (pos[url_param_name_l] == '=') {
8557 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8558 return pos;
8559 pos += url_param_name_l + 1;
8560 }
8561 while (pos <= last && !is_param_delimiter(*pos))
8562 pos++;
8563 pos++;
8564 }
8565 return NULL;
8566}
8567
8568/*
8569 * Given a url parameter name, returns its value and size into *value and
8570 * *value_l respectively, and returns non-zero. If the parameter is not found,
8571 * zero is returned and value/value_l are not touched.
8572 */
8573static int
8574find_url_param_value(char* path, size_t path_l,
8575 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008576 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008577{
8578 char *query_string, *qs_end;
8579 char *arg_start;
8580 char *value_start, *value_end;
8581
8582 query_string = find_query_string(path, path_l);
8583 if (!query_string)
8584 return 0;
8585
8586 qs_end = path + path_l;
8587 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8588 url_param_name, url_param_name_l);
8589 if (!arg_start)
8590 return 0;
8591
8592 value_start = arg_start + url_param_name_l + 1;
8593 value_end = value_start;
8594
8595 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8596 value_end++;
8597
8598 *value = value_start;
8599 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008600 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008601}
8602
8603static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008604smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8605 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008606{
8607 struct http_txn *txn = l7;
8608 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008609
8610 if (!args || args->type != ARGT_STR)
8611 return 0;
8612
8613 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008614
Willy Tarreau572bf902012-07-02 17:01:20 +02008615 if (!find_url_param_value(msg->buf->buf.p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008616 args->data.str.str, args->data.str.len,
8617 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008618 return 0;
8619
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008620 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008621 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008622 return 1;
8623}
8624
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008625/* Return the signed integer value for the specified url parameter (see url_param
8626 * above).
8627 */
8628static int
8629smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8630 const struct arg *args, struct sample *smp)
8631{
8632 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8633
8634 if (ret > 0) {
8635 smp->type = SMP_T_UINT;
8636 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8637 }
8638
8639 return ret;
8640}
8641
Willy Tarreau185b5c42012-04-26 15:11:51 +02008642/* This function is used to validate the arguments passed to any "hdr" fetch
8643 * keyword. These keywords support an optional positive or negative occurrence
8644 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8645 * is assumed that the types are already the correct ones. Returns 0 on error,
8646 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8647 * error message in case of error, that the caller is responsible for freeing.
8648 * The initial location must either be freeable or NULL.
8649 */
8650static int val_hdr(struct arg *arg, char **err_msg)
8651{
8652 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008653 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008654 return 0;
8655 }
8656 return 1;
8657}
8658
Willy Tarreau4a568972010-05-12 08:08:50 +02008659/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008660/* All supported ACL keywords must be declared here. */
8661/************************************************************************/
8662
8663/* Note: must not be declared <const> as its list will be overwritten.
8664 * Please take care of keeping this list alphabetically sorted.
8665 */
8666static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008667 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8668 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8669 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8670 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8671 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8672 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8673 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8674 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8675
Willy Tarreau51539362012-05-08 12:46:28 +02008676 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8677 { "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 +02008678 { "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 +02008679 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8680 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8681 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8682 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8683 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8684 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8685 { "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 +02008686
Willy Tarreau185b5c42012-04-26 15:11:51 +02008687 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8688 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8689 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8690 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8691 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8692 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8693 { "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 },
8694 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8695 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8696 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8697 { "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 +02008698
8699 { "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 +02008700 { "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 +02008701 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8702
8703 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8704
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008705 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8706 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8707 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8708 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8709 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8710 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8711 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8712 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008713
8714 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8715 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8716 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8717
Willy Tarreau51539362012-05-08 12:46:28 +02008718 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8719 { "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 +02008720 { "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 +02008721 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8722 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8723 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8724 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8725 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8726 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8727 { "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 +02008728
Willy Tarreau185b5c42012-04-26 15:11:51 +02008729 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8730 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8731 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8732 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8733 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8734 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8735 { "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 },
8736 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8737 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8738 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8739 { "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 +02008740
8741 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8742
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008743 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8744 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8745 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8746 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8747 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8748 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8749 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8750 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8751 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8752 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008753
8754 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8755 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8756 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8757 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8758 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8759 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8760 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8761 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8762 { "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 +02008763 { "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 +02008764
8765 { NULL, NULL, NULL, NULL },
8766}};
8767
8768/************************************************************************/
8769/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008770/************************************************************************/
8771/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008772static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008773 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008774 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008775 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8776 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8777 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ },
8778 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008779 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008780 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8781 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008782 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008783}};
8784
Willy Tarreau8797c062007-05-07 00:55:35 +02008785
8786__attribute__((constructor))
8787static void __http_protocol_init(void)
8788{
8789 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008790 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008791}
8792
8793
Willy Tarreau58f10d72006-12-04 02:26:12 +01008794/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008795 * Local variables:
8796 * c-indent-level: 8
8797 * c-basic-offset: 8
8798 * End:
8799 */