blob: 2b96ca33652a3698fd89d0590569f984df903d5c [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>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
96const char *HTTP_401_fmt =
97 "HTTP/1.0 401 Unauthorized\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200100 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
102 "\r\n"
103 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
104
Willy Tarreau844a7e72010-01-31 21:46:18 +0100105const char *HTTP_407_fmt =
106 "HTTP/1.0 407 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
109 "Content-Type: text/html\r\n"
110 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200116 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100117 [HTTP_ERR_400] = 400,
118 [HTTP_ERR_403] = 403,
119 [HTTP_ERR_408] = 408,
120 [HTTP_ERR_500] = 500,
121 [HTTP_ERR_502] = 502,
122 [HTTP_ERR_503] = 503,
123 [HTTP_ERR_504] = 504,
124};
125
Willy Tarreau80587432006-12-24 17:47:20 +0100126static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200127 [HTTP_ERR_200] =
128 "HTTP/1.0 200 OK\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "\r\n"
133 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Cyril Bonté19979e12012-04-04 12:57:21 +0200193/* status codes available for the stats admin page (strictly 4 chars length) */
194const char *stat_status_codes[STAT_STATUS_SIZE] = {
195 [STAT_STATUS_DENY] = "DENY",
196 [STAT_STATUS_DONE] = "DONE",
197 [STAT_STATUS_ERRP] = "ERRP",
198 [STAT_STATUS_EXCD] = "EXCD",
199 [STAT_STATUS_NONE] = "NONE",
200 [STAT_STATUS_PART] = "PART",
201 [STAT_STATUS_UNKN] = "UNKN",
202};
203
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205/* We must put the messages here since GCC cannot initialize consts depending
206 * on strlen().
207 */
208struct chunk http_err_chunks[HTTP_ERR_SIZE];
209
Willy Tarreau42250582007-04-01 01:30:43 +0200210#define FD_SETS_ARE_BITFIELDS
211#ifdef FD_SETS_ARE_BITFIELDS
212/*
213 * This map is used with all the FD_* macros to check whether a particular bit
214 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
215 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
216 * byte should be encoded. Be careful to always pass bytes from 0 to 255
217 * exclusively to the macros.
218 */
219fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
221
222#else
223#error "Check if your OS uses bitfields for fd_sets"
224#endif
225
Willy Tarreau80587432006-12-24 17:47:20 +0100226void init_proto_http()
227{
Willy Tarreau42250582007-04-01 01:30:43 +0200228 int i;
229 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100230 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200231
Willy Tarreau80587432006-12-24 17:47:20 +0100232 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
233 if (!http_err_msgs[msg]) {
234 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
235 abort();
236 }
237
238 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
239 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
240 }
Willy Tarreau42250582007-04-01 01:30:43 +0200241
242 /* initialize the log header encoding map : '{|}"#' should be encoded with
243 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
244 * URL encoding only requires '"', '#' to be encoded as well as non-
245 * printable characters above.
246 */
247 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
248 memset(url_encode_map, 0, sizeof(url_encode_map));
249 for (i = 0; i < 32; i++) {
250 FD_SET(i, hdr_encode_map);
251 FD_SET(i, url_encode_map);
252 }
253 for (i = 127; i < 256; i++) {
254 FD_SET(i, hdr_encode_map);
255 FD_SET(i, url_encode_map);
256 }
257
258 tmp = "\"#{|}";
259 while (*tmp) {
260 FD_SET(*tmp, hdr_encode_map);
261 tmp++;
262 }
263
264 tmp = "\"#";
265 while (*tmp) {
266 FD_SET(*tmp, url_encode_map);
267 tmp++;
268 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200269
270 /* memory allocations */
271 pool2_requri = create_pool("requri", REQURI_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{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100392 chunk_printf(&trash,
393 "[%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",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 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);
397 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100399 chunk_printf(&trash,
400 " %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",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 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);
404 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405}
406#else
407#define http_silent_debug(l,s) do { } while (0)
408#endif
409
410/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100411 * Adds a header and its CRLF at the tail of the message's buffer, just before
412 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100417int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418{
419 int bytes, len;
420
421 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200422 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
428
429/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100430 * Adds a header and its CRLF at the tail of the message's buffer, just before
431 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100432 * the buffer is only opened and the space reserved, but nothing is copied.
433 * The header is also automatically added to the index <hdr_idx>, and the end
434 * of headers is automatically adjusted. The number of bytes added is returned
435 * on success, otherwise <0 is returned indicating an error.
436 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100437int http_header_add_tail2(struct http_msg *msg,
438 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439{
440 int bytes;
441
Willy Tarreau9b28e032012-10-12 23:49:43 +0200442 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 if (!bytes)
444 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100445 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
447}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448
449/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100450 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
451 * If so, returns the position of the first non-space character relative to
452 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
453 * to return a pointer to the place after the first space. Returns 0 if the
454 * header name does not match. Checks are case-insensitive.
455 */
456int http_header_match2(const char *hdr, const char *end,
457 const char *name, int len)
458{
459 const char *val;
460
461 if (hdr + len >= end)
462 return 0;
463 if (hdr[len] != ':')
464 return 0;
465 if (strncasecmp(hdr, name, len) != 0)
466 return 0;
467 val = hdr + len + 1;
468 while (val < end && HTTP_IS_SPHT(*val))
469 val++;
470 if ((val >= end) && (len + 2 <= end - hdr))
471 return len + 2; /* we may replace starting from second space */
472 return val - hdr;
473}
474
Willy Tarreau68085d82010-01-18 14:54:04 +0100475/* Find the end of the header value contained between <s> and <e>. See RFC2616,
476 * par 2.2 for more information. Note that it requires a valid header to return
477 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200478 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100479char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480{
481 int quoted, qdpair;
482
483 quoted = qdpair = 0;
484 for (; s < e; s++) {
485 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200486 else if (quoted) {
487 if (*s == '\\') qdpair = 1;
488 else if (*s == '"') quoted = 0;
489 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490 else if (*s == '"') quoted = 1;
491 else if (*s == ',') return s;
492 }
493 return s;
494}
495
496/* Find the first or next occurrence of header <name> in message buffer <sol>
497 * using headers index <idx>, and return it in the <ctx> structure. This
498 * structure holds everything necessary to use the header and find next
499 * occurrence. If its <idx> member is 0, the header is searched from the
500 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100501 * 1 when it finds a value, and 0 when there is no more. It is designed to work
502 * with headers defined as comma-separated lists. As a special case, if ctx->val
503 * is NULL when searching for a new values of a header, the current header is
504 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 */
506int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100507 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 struct hdr_ctx *ctx)
509{
Willy Tarreau68085d82010-01-18 14:54:04 +0100510 char *eol, *sov;
511 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200512
Willy Tarreau68085d82010-01-18 14:54:04 +0100513 cur_idx = ctx->idx;
514 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515 /* We have previously returned a value, let's search
516 * another one on the same line.
517 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200519 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100520 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 eol = sol + idx->v[cur_idx].len;
522
523 if (sov >= eol)
524 /* no more values in this header */
525 goto next_hdr;
526
Willy Tarreau68085d82010-01-18 14:54:04 +0100527 /* values remaining for this header, skip the comma but save it
528 * for later use (eg: for header deletion).
529 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 sov++;
531 while (sov < eol && http_is_lws[(unsigned char)*sov])
532 sov++;
533
534 goto return_hdr;
535 }
536
537 /* first request for this header */
538 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200540 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 while (cur_idx) {
542 eol = sol + idx->v[cur_idx].len;
543
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200544 if (len == 0) {
545 /* No argument was passed, we want any header.
546 * To achieve this, we simply build a fake request. */
547 while (sol + len < eol && sol[len] != ':')
548 len++;
549 name = sol;
550 }
551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 if ((len < eol - sol) &&
553 (sol[len] == ':') &&
554 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 sov = sol + len + 1;
557 while (sov < eol && http_is_lws[(unsigned char)*sov])
558 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 ctx->prev = old_idx;
562 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 ctx->idx = cur_idx;
564 ctx->val = sov - sol;
565
566 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200567 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200568 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 eol--;
570 ctx->tws++;
571 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 ctx->vlen = eol - sov;
573 return 1;
574 }
575 next_hdr:
576 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200578 cur_idx = idx->v[cur_idx].next;
579 }
580 return 0;
581}
582
583int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100584 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200585 struct hdr_ctx *ctx)
586{
587 return http_find_header2(name, strlen(name), sol, idx, ctx);
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Remove one value of a header. This only works on a <ctx> returned by one of
591 * the http_find_header functions. The value is removed, as well as surrounding
592 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100593 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100594 * message <msg>. The new index is returned. If it is zero, it means there is
595 * no more header, so any processing may stop. The ctx is always left in a form
596 * that can be handled by http_find_header2() to find next occurrence.
597 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100598int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100599{
600 int cur_idx = ctx->idx;
601 char *sol = ctx->line;
602 struct hdr_idx_elem *hdr;
603 int delta, skip_comma;
604
605 if (!cur_idx)
606 return 0;
607
608 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200609 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200611 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 http_msg_move_end(msg, delta);
613 idx->used--;
614 hdr->len = 0; /* unused entry */
615 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100616 if (idx->tail == ctx->idx)
617 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 ctx->idx = ctx->prev; /* walk back to the end of previous header */
619 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
620 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 return ctx->idx;
623 }
624
625 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200626 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
627 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 */
629
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200630 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200631 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 NULL, 0);
634 hdr->len += delta;
635 http_msg_move_end(msg, delta);
636 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200637 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100638 return ctx->idx;
639}
640
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641/* This function handles a server error at the stream interface level. The
642 * stream interface is assumed to be already in a closed state. An optional
643 * message is copied into the input buffer, and an HTTP status code stored.
644 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100645 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100647static void http_server_error(struct session *t, struct stream_interface *si,
648 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200650 channel_auto_read(si->ob);
651 channel_abort(si->ob);
652 channel_auto_close(si->ob);
653 channel_erase(si->ob);
654 channel_auto_close(si->ib);
655 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100656 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100657 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200658 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659 }
660 if (!(t->flags & SN_ERR_MASK))
661 t->flags |= err;
662 if (!(t->flags & SN_FINST_MASK))
663 t->flags |= finst;
664}
665
Willy Tarreau80587432006-12-24 17:47:20 +0100666/* This function returns the appropriate error location for the given session
667 * and message.
668 */
669
Willy Tarreau783f2582012-09-04 12:19:04 +0200670struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100671{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200672 if (s->be->errmsg[msgnum].str)
673 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100674 else if (s->fe->errmsg[msgnum].str)
675 return &s->fe->errmsg[msgnum];
676 else
677 return &http_err_chunks[msgnum];
678}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680/*
681 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
682 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
683 */
684static http_meth_t find_http_meth(const char *str, const int len)
685{
686 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100687 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100688
689 m = ((unsigned)*str - 'A');
690
691 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 for (h = http_methods[m]; h->len > 0; h++) {
693 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100694 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 };
698 return HTTP_METH_OTHER;
699 }
700 return HTTP_METH_NONE;
701
702}
703
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704/* Parse the URI from the given transaction (which is assumed to be in request
705 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
706 * It is returned otherwise.
707 */
708static char *
709http_get_path(struct http_txn *txn)
710{
711 char *ptr, *end;
712
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100714 end = ptr + txn->req.sl.rq.u_l;
715
716 if (ptr >= end)
717 return NULL;
718
719 /* RFC2616, par. 5.1.2 :
720 * Request-URI = "*" | absuri | abspath | authority
721 */
722
723 if (*ptr == '*')
724 return NULL;
725
726 if (isalpha((unsigned char)*ptr)) {
727 /* this is a scheme as described by RFC3986, par. 3.1 */
728 ptr++;
729 while (ptr < end &&
730 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
731 ptr++;
732 /* skip '://' */
733 if (ptr == end || *ptr++ != ':')
734 return NULL;
735 if (ptr == end || *ptr++ != '/')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 }
740 /* skip [user[:passwd]@]host[:[port]] */
741
742 while (ptr < end && *ptr != '/')
743 ptr++;
744
745 if (ptr == end)
746 return NULL;
747
748 /* OK, we got the '/' ! */
749 return ptr;
750}
751
Willy Tarreauefb453c2008-10-26 20:49:47 +0100752/* Returns a 302 for a redirectable request. This may only be called just after
753 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
754 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200755 * NOTE: this function is designed to support being called once data are scheduled
756 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200763 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100766 trash.len = strlen(HTTP_302);
767 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100768
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100769 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100770
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100772 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100775 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100777 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
778 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200781 /* 3: add the request URI. Since it was already forwarded, we need
782 * to temporarily rewind the buffer.
783 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200785 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200788 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 +0200789
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 if (!path)
793 return;
794
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100795 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 return;
797
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100798 memcpy(trash.str + trash.len, path, len);
799 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100800
801 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100802 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
803 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100804 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100805 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
806 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100807 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808
809 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200810 si_shutr(si);
811 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 si->err_type = SI_ET_NONE;
813 si->err_loc = NULL;
814 si->state = SI_ST_CLO;
815
816 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100817 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 if (srv)
821 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822}
823
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 * that the server side is closed. Note that err_type is actually a
826 * bitmask, where almost only aborts may be cumulated with other
827 * values. We consider that aborted operations are more important
828 * than timeouts or errors due to the fact that nobody else in the
829 * logs might explain incomplete retries. All others should avoid
830 * being cumulated. It should normally not be possible to have multiple
831 * aborts at once, but just in case, the first one in sequence is reported.
832 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100833void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100835 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836
837 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200839 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100841 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200842 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200845 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200848 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200851 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858}
859
Willy Tarreau42250582007-04-01 01:30:43 +0200860extern const char sess_term_cond[8];
861extern const char sess_fin_state[8];
862extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200863struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100864struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100865struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866
Willy Tarreau117f59e2007-03-04 18:17:17 +0100867/*
868 * Capture headers from message starting at <som> according to header list
869 * <cap_hdr>, and fill the <idx> structure appropriately.
870 */
871void capture_headers(char *som, struct hdr_idx *idx,
872 char **cap, struct cap_hdr *cap_hdr)
873{
874 char *eol, *sol, *col, *sov;
875 int cur_idx;
876 struct cap_hdr *h;
877 int len;
878
879 sol = som + hdr_idx_first_pos(idx);
880 cur_idx = hdr_idx_first_idx(idx);
881
882 while (cur_idx) {
883 eol = sol + idx->v[cur_idx].len;
884
885 col = sol;
886 while (col < eol && *col != ':')
887 col++;
888
889 sov = col + 1;
890 while (sov < eol && http_is_lws[(unsigned char)*sov])
891 sov++;
892
893 for (h = cap_hdr; h; h = h->next) {
894 if ((h->namelen == col - sol) &&
895 (strncasecmp(sol, h->name, h->namelen) == 0)) {
896 if (cap[h->index] == NULL)
897 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200898 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100899
900 if (cap[h->index] == NULL) {
901 Alert("HTTP capture : out of memory.\n");
902 continue;
903 }
904
905 len = eol - sov;
906 if (len > h->len)
907 len = h->len;
908
909 memcpy(cap[h->index], sov, len);
910 cap[h->index][len]=0;
911 }
912 }
913 sol = eol + idx->v[cur_idx].cr + 1;
914 cur_idx = idx->v[cur_idx].next;
915 }
916}
917
918
Willy Tarreau42250582007-04-01 01:30:43 +0200919/* either we find an LF at <ptr> or we jump to <bad>.
920 */
921#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
922
923/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
924 * otherwise to <http_msg_ood> with <state> set to <st>.
925 */
926#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
927 ptr++; \
928 if (likely(ptr < end)) \
929 goto good; \
930 else { \
931 state = (st); \
932 goto http_msg_ood; \
933 } \
934 } while (0)
935
936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100938 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100939 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
940 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
941 * will give undefined results.
942 * Note that it is upon the caller's responsibility to ensure that ptr < end,
943 * and that msg->sol points to the beginning of the response.
944 * If a complete line is found (which implies that at least one CR or LF is
945 * found before <end>, the updated <ptr> is returned, otherwise NULL is
946 * returned indicating an incomplete line (which does not mean that parts have
947 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
948 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
949 * upon next call.
950 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
953 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200954 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100955 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200956const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100957 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100958 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100959{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200960 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100961
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200964 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100965 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
967
968 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100969 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
971 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100972 state = HTTP_MSG_ERROR;
973 break;
974
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200976 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100978 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100979 goto http_msg_rpcode;
980 }
981 if (likely(HTTP_IS_SPHT(*ptr)))
982 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
983 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100984 state = HTTP_MSG_ERROR;
985 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100986
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200988 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 if (likely(!HTTP_IS_LWS(*ptr)))
990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
991
992 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
995 }
996
997 /* so it's a CR/LF, so there is no reason phrase */
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 http_msg_rsp_reason:
1000 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001001 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 msg->sl.st.r_l = 0;
1003 goto http_msg_rpline_eol;
1004
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001006 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 goto http_msg_rpreason;
1010 }
1011 if (likely(HTTP_IS_SPHT(*ptr)))
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1013 /* so it's a CR/LF, so there is no reason phrase */
1014 goto http_msg_rsp_reason;
1015
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001017 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001018 if (likely(!HTTP_IS_CRLF(*ptr)))
1019 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001020 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 http_msg_rpline_eol:
1022 /* We have seen the end of line. Note that we do not
1023 * necessarily have the \n yet, but at least we know that we
1024 * have EITHER \r OR \n, otherwise the response would not be
1025 * complete. We can then record the response length and return
1026 * to the caller which will be able to register it.
1027 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001028 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001029 return ptr;
1030
1031#ifdef DEBUG_FULL
1032 default:
1033 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1034 exit(1);
1035#endif
1036 }
1037
1038 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001039 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 if (ret_state)
1041 *ret_state = state;
1042 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001043 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001045}
1046
Willy Tarreau8973c702007-01-21 23:58:29 +01001047/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001048 * This function parses a request line between <ptr> and <end>, starting with
1049 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1050 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1051 * will give undefined results.
1052 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1053 * and that msg->sol points to the beginning of the request.
1054 * If a complete line is found (which implies that at least one CR or LF is
1055 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1056 * returned indicating an incomplete line (which does not mean that parts have
1057 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1058 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1059 * upon next call.
1060 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001061 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1063 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001064 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001066const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001067 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001068 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001070 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001074 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 if (likely(HTTP_IS_TOKEN(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001079 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_CRLF(*ptr))) {
1084 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001087 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001089 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 msg->sl.rq.v_l = 0;
1093 goto http_msg_rqline_eol;
1094 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001095 state = HTTP_MSG_ERROR;
1096 break;
1097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001099 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001101 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 goto http_msg_rquri;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1106 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1107 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001110 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001111 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001115 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001120 /* non-ASCII chars are forbidden unless option
1121 * accept-invalid-http-request is enabled in the frontend.
1122 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001124 if (msg->err_pos < -1)
1125 goto invalid_char;
1126 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001127 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1129 }
1130
1131 if (likely(HTTP_IS_CRLF(*ptr))) {
1132 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1133 goto http_msg_req09_uri_e;
1134 }
1135
1136 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001137 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001138 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001139 state = HTTP_MSG_ERROR;
1140 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001143 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001145 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 goto http_msg_rqver;
1147 }
1148 if (likely(HTTP_IS_SPHT(*ptr)))
1149 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1150 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1151 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001154 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001157
1158 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001159 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 http_msg_rqline_eol:
1161 /* We have seen the end of line. Note that we do not
1162 * necessarily have the \n yet, but at least we know that we
1163 * have EITHER \r OR \n, otherwise the request would not be
1164 * complete. We can then record the request length and return
1165 * to the caller which will be able to register it.
1166 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001167 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001168 return ptr;
1169 }
1170
1171 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001172 state = HTTP_MSG_ERROR;
1173 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175#ifdef DEBUG_FULL
1176 default:
1177 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1178 exit(1);
1179#endif
1180 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (ret_state)
1185 *ret_state = state;
1186 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001187 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001191/*
1192 * Returns the data from Authorization header. Function may be called more
1193 * than once so data is stored in txn->auth_data. When no header is found
1194 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1195 * searching again for something we are unable to find anyway.
1196 */
1197
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001198char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001199
1200int
1201get_http_auth(struct session *s)
1202{
1203
1204 struct http_txn *txn = &s->txn;
1205 struct chunk auth_method;
1206 struct hdr_ctx ctx;
1207 char *h, *p;
1208 int len;
1209
1210#ifdef DEBUG_AUTH
1211 printf("Auth for session %p: %d\n", s, txn->auth.method);
1212#endif
1213
1214 if (txn->auth.method == HTTP_AUTH_WRONG)
1215 return 0;
1216
1217 if (txn->auth.method)
1218 return 1;
1219
1220 txn->auth.method = HTTP_AUTH_WRONG;
1221
1222 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001223
1224 if (txn->flags & TX_USE_PX_CONN) {
1225 h = "Proxy-Authorization";
1226 len = strlen(h);
1227 } else {
1228 h = "Authorization";
1229 len = strlen(h);
1230 }
1231
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001233 return 0;
1234
1235 h = ctx.line + ctx.val;
1236
1237 p = memchr(h, ' ', ctx.vlen);
1238 if (!p || p == h)
1239 return 0;
1240
1241 chunk_initlen(&auth_method, h, 0, p-h);
1242 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1243
1244 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1245
1246 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001247 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248
1249 if (len < 0)
1250 return 0;
1251
1252
1253 get_http_auth_buff[len] = '\0';
1254
1255 p = strchr(get_http_auth_buff, ':');
1256
1257 if (!p)
1258 return 0;
1259
1260 txn->auth.user = get_http_auth_buff;
1261 *p = '\0';
1262 txn->auth.pass = p+1;
1263
1264 txn->auth.method = HTTP_AUTH_BASIC;
1265 return 1;
1266 }
1267
1268 return 0;
1269}
1270
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8973c702007-01-21 23:58:29 +01001272/*
1273 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001274 * depending on the initial msg->msg_state. The caller is responsible for
1275 * ensuring that the message does not wrap. The function can be preempted
1276 * everywhere when data are missing and recalled at the exact same location
1277 * with no information loss. The message may even be realigned between two
1278 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001279 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001280 * fields. Note that msg->sol will be initialized after completing the first
1281 * state, so that none of the msg pointers has to be initialized prior to the
1282 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001284void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001286 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001288 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001290 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001291 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001292 ptr = buf->p + msg->next;
1293 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (unlikely(ptr >= end))
1296 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 /*
1300 * First, states that are specific to the response only.
1301 * We check them first so that request and headers are
1302 * closer to each other (accessed more often).
1303 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 /* we have a start of message, but we have to check
1308 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001309 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001311 if (unlikely(ptr != buf->p)) {
1312 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001313 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001314 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001315 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 }
Willy Tarreau26927362012-05-18 23:22:52 +02001317 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001318 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 hdr_idx_init(idx);
1320 state = HTTP_MSG_RPVER;
1321 goto http_msg_rpver;
1322 }
1323
1324 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1325 goto http_msg_invalid;
1326
1327 if (unlikely(*ptr == '\n'))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1330 /* stop here */
1331
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001333 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 EXPECT_LF_HERE(ptr, http_msg_invalid);
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1336 /* stop here */
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 case HTTP_MSG_RPVER_SP:
1341 case HTTP_MSG_RPCODE:
1342 case HTTP_MSG_RPCODE_SP:
1343 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001344 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001345 state, ptr, end,
1346 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (unlikely(!ptr))
1348 return;
1349
1350 /* we have a full response and we know that we have either a CR
1351 * or an LF at <ptr>.
1352 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1354
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001355 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 if (likely(*ptr == '\r'))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1358 goto http_msg_rpline_end;
1359
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001361 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 /* msg->sol must point to the first of CR or LF. */
1363 EXPECT_LF_HERE(ptr, http_msg_invalid);
1364 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1365 /* stop here */
1366
1367 /*
1368 * Second, states that are specific to the request only
1369 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001373 /* we have a start of message, but we have to check
1374 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001375 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 if (likely(ptr != buf->p)) {
1378 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001380 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 }
Willy Tarreau26927362012-05-18 23:22:52 +02001383 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001384 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 state = HTTP_MSG_RQMETH;
1386 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1390 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (unlikely(*ptr == '\n'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH_SP:
1406 case HTTP_MSG_RQURI:
1407 case HTTP_MSG_RQURI_SP:
1408 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001409 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001410 state, ptr, end,
1411 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (unlikely(!ptr))
1413 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 /* we have a full request and we know that we have either a CR
1416 * or an LF at <ptr>.
1417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001420 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* check for HTTP/0.9 request : no version information available.
1428 * msg->sol must point to the first of CR or LF.
1429 */
1430 if (unlikely(msg->sl.rq.v_l == 0))
1431 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 EXPECT_LF_HERE(ptr, http_msg_invalid);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 /*
1438 * Common states below
1439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001442 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_CRLF(*ptr))) {
1444 goto http_msg_hdr_name;
1445 }
1446
1447 if (likely(*ptr == '\r'))
1448 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1449 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001452 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* assumes msg->sol points to the first char */
1454 if (likely(HTTP_IS_TOKEN(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001457 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001464 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001471 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001500 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001505 /* assumes msg->sol points to the first char, and msg->sov
1506 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
1508 if (likely(!HTTP_IS_CRLF(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001511 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* Note: we could also copy eol into ->eoh so that we have the
1513 * real header end in case it ends with lots of LWS, but is this
1514 * really needed ?
1515 */
1516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1518 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1528 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001529 for (; buf->p + msg->eol < ptr; msg->eol++)
1530 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_hdr_val;
1532 }
1533 http_msg_complete_header:
1534 /*
1535 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001536 * Assumes msg->sol points to the first char, msg->sov points
1537 * to the first character of the value and msg->eol to the
1538 * first CR or LF so we know how the line ends. We insert last
1539 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001541 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 idx, idx->tail) < 0))
1543 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001544
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (likely(!HTTP_IS_CRLF(*ptr))) {
1547 goto http_msg_hdr_name;
1548 }
1549
1550 if (likely(*ptr == '\r'))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1552 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /* Assumes msg->sol points to the first of either CR or LF */
1557 EXPECT_LF_HERE(ptr, http_msg_invalid);
1558 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001560 msg->eoh = msg->sol;
1561 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001562 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001564
1565 case HTTP_MSG_ERROR:
1566 /* this may only happen if we call http_msg_analyser() twice with an error */
1567 break;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569#ifdef DEBUG_FULL
1570 default:
1571 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1572 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
1575 http_msg_ood:
1576 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001578 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 http_msg_invalid:
1582 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
1586}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001587
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001588/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1589 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1590 * nothing is done and 1 is returned.
1591 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593{
1594 int delta;
1595 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau9b28e032012-10-12 23:49:43 +02001601 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001606 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001611 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001614 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001629 * and "keep-alive" values. If we already know that some headers may safely
1630 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001631 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1632 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001633 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1635 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1636 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001637 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001638 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001639void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001640{
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001642 const char *hdr_val = "Connection";
1643 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001644
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001645 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001646 return;
1647
Willy Tarreau88d349d2010-01-25 12:15:43 +01001648 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1649 hdr_val = "Proxy-Connection";
1650 hdr_len = 16;
1651 }
1652
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001654 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001655 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1657 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001658 if (to_del & 2)
1659 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 else
1661 txn->flags |= TX_CON_KAL_SET;
1662 }
1663 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1664 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001665 if (to_del & 1)
1666 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001667 else
1668 txn->flags |= TX_CON_CLO_SET;
1669 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001670 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1671 txn->flags |= TX_HDR_CONN_UPG;
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 Tarreau9b28e032012-10-12 23:49:43 +02001699 while (http_find_header2(hdr_val, hdr_len, msg->chn->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 Tarreau24e6d972012-10-26 00:49:52 +02001747static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001749 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001751 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001752 const char *end = buf->data + buf->size;
1753 const char *stop = bi_end(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 Tarreaucdbdd522012-10-12 22:51:15 +02001768 ptr = 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 Tarreaucdbdd522012-10-12 22:51:15 +02001780 ptr = 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 Tarreaucdbdd522012-10-12 22:51:15 +02001793 ptr = 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 Tarreaucdbdd522012-10-12 22:51:15 +02001801 ptr = 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 Tarreaucdbdd522012-10-12 22:51:15 +02001808 ptr = 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 Tarreaucdbdd522012-10-12 22:51:15 +02001814 ptr = 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 Tarreaub8ffd372012-09-27 15:08:56 +02001829 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001830 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001831 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001833 msg->chunk_len = chunk;
1834 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001838 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001839 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001840}
1841
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001842/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001844 * the trailers is found, it is automatically scheduled to be forwarded,
1845 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1846 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001847 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001848 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001849 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001850 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1851 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001852 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001853 * matches the length of trailers already parsed and not forwarded. It is also
1854 * important to note that this function is designed to be able to parse wrapped
1855 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001857static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001859 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001860
Willy Tarreaua458b672012-03-05 11:17:50 +01001861 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001863 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 const char *ptr = b_ptr(buf, msg->next);
1865 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001866 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 /* scan current line and stop at LF or CRLF */
1869 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001870 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 return 0;
1872
1873 if (*ptr == '\n') {
1874 if (!p1)
1875 p1 = ptr;
1876 p2 = ptr;
1877 break;
1878 }
1879
1880 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001881 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001884 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 p1 = ptr;
1886 }
1887
1888 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001889 if (ptr >= buf->data + buf->size)
1890 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 }
1892
1893 /* after LF; point to beginning of next line */
1894 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 if (p2 >= buf->data + buf->size)
1896 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001897
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001899 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001900 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001901
1902 /* schedule this line for forwarding */
1903 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (msg->sov >= buf->size)
1905 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001907 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001908 /* LF/CRLF at beginning of line => end of trailers at p2.
1909 * Everything was scheduled for forwarding, there's nothing
1910 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001911 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 msg->msg_state = HTTP_MSG_DONE;
1914 return 1;
1915 }
1916 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 }
1919}
1920
Willy Tarreau54d23df2012-10-25 19:04:45 +02001921/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001923 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001924 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1926 * not enough data are available, the function does not change anything and
1927 * returns zero. If a parse error is encountered, the function returns < 0 and
1928 * does not change anything. Note: this function is designed to parse wrapped
1929 * CRLF at the end of the buffer.
1930 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001931static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001933 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001934 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935 int bytes;
1936
1937 /* NB: we'll check data availabilty at the end. It's not a
1938 * problem because whatever we match first will be checked
1939 * against the correct length.
1940 */
1941 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 if (*ptr == '\r') {
1944 bytes++;
1945 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001946 if (ptr >= buf->data + buf->size)
1947 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 }
1949
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001950 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return 0;
1952
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001959 if (ptr >= buf->data + buf->size)
1960 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001961 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1962 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001963 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1965 return 1;
1966}
Willy Tarreau5b154472009-12-21 20:11:07 +01001967
William Lallemand82fe75c2012-10-23 10:25:10 +02001968
1969/*
1970 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001971 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001972int select_compression_request_header(struct session *s, struct buffer *req)
1973{
1974 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001975 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001976 struct hdr_ctx ctx;
1977 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001978 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001979
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001980 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1981 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001982 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1983 */
1984 ctx.idx = 0;
1985 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1986 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001987 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1988 (ctx.vlen < 31 ||
1989 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1990 ctx.line[ctx.val + 30] < '6' ||
1991 (ctx.line[ctx.val + 30] == '6' &&
1992 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1993 s->comp_algo = NULL;
1994 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001995 }
1996
William Lallemand82fe75c2012-10-23 10:25:10 +02001997 ctx.idx = 0;
1998 /* no compression when Cache-Control: no-transform found */
1999 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
2000 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
2001 s->comp_algo = NULL;
2002 return 0;
2003 }
2004 }
2005
2006 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002007 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002008 ctx.idx = 0;
2009 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002010 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002011 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2012 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002013
2014 /* remove all occurrences of the header when "compression offload" is set */
2015
2016 if ((s->be->comp && s->be->comp->offload) ||
2017 (s->fe->comp && s->fe->comp->offload)) {
2018 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2019 ctx.idx = 0;
2020 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2021 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2022 }
2023 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002024 return 1;
2025 }
2026 }
2027 }
2028 }
2029
2030 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002031 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2032 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002033 if (comp_algo->add_data == identity_add_data) {
2034 s->comp_algo = comp_algo;
2035 return 1;
2036 }
2037 }
2038 }
2039
2040 s->comp_algo = NULL;
2041
2042 return 0;
2043}
2044
2045/*
2046 * Selects a comression algorithm depending of the server response.
2047 */
2048int select_compression_response_header(struct session *s, struct buffer *res)
2049{
2050 struct http_txn *txn = &s->txn;
2051 struct http_msg *msg = &txn->rsp;
2052 struct hdr_ctx ctx;
2053 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002054
2055 /* no common compression algorithm was found in request header */
2056 if (s->comp_algo == NULL)
2057 goto fail;
2058
2059 /* HTTP < 1.1 should not be compressed */
2060 if (!(msg->flags & HTTP_MSGF_VER_11))
2061 goto fail;
2062
William Lallemand82fe75c2012-10-23 10:25:10 +02002063 ctx.idx = 0;
2064
2065 /* Content-Length is null */
2066 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2067 goto fail;
2068
2069 /* content is already compressed */
2070 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2071 goto fail;
2072
2073 comp_type = NULL;
2074
2075 /* if there was a compression content-type option in the backend or the frontend
2076 * The backend have priority.
2077 */
2078 if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) {
2079 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2080 for (; comp_type; comp_type = comp_type->next) {
2081 if (strncmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
2082 /* this Content-Type should be compressed */
2083 break;
2084 }
William Lallemandc04ca582012-11-12 13:56:25 +01002085 } else {
2086 /* there is no Content-Type header */
2087 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002088 }
2089 /* this Content-Type should not be compressed */
2090 if (comp_type == NULL)
2091 goto fail;
2092 }
2093
2094 ctx.idx = 0;
2095
William Lallemandd85f9172012-11-09 17:05:39 +01002096 /* limit compression rate */
2097 if (global.comp_rate_lim > 0)
2098 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2099 goto fail;
2100
William Lallemand072a2bf2012-11-20 17:01:01 +01002101 /* limit cpu usage */
2102 if (idle_pct < compress_min_idle)
2103 goto fail;
2104
William Lallemand4c49fae2012-11-07 15:00:23 +01002105 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002106 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002107 goto fail;
2108
William Lallemandec3e3892012-11-12 17:02:18 +01002109 s->flags |= SN_COMP_READY;
2110
William Lallemand82fe75c2012-10-23 10:25:10 +02002111 /* remove Content-Length header */
2112 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2113 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2114
2115 /* add Transfer-Encoding header */
2116 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2117 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2118
2119 /*
2120 * Add Content-Encoding header when it's not identity encoding.
2121 * RFC 2616 : Identity encoding: This content-coding is used only in the
2122 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2123 * header.
2124 */
2125 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002126 trash.len = 18;
2127 memcpy(trash.str, "Content-Encoding: ", trash.len);
2128 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2129 trash.len += s->comp_algo->name_len;
2130 trash.str[trash.len] = '\0';
2131 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002132 }
2133
William Lallemand82fe75c2012-10-23 10:25:10 +02002134 return 1;
2135
2136fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002137 if (s->flags & SN_COMP_READY)
William Lallemand1c2d6222012-10-30 15:52:53 +01002138 s->comp_algo->end(&s->comp_ctx);
Willy Tarreaub97b6192012-11-19 14:55:02 +01002139 s->comp_algo = NULL;
2140 s->flags &= ~SN_COMP_READY;
William Lallemand82fe75c2012-10-23 10:25:10 +02002141 return 0;
2142}
2143
2144
Willy Tarreaud787e662009-07-07 10:14:51 +02002145/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2146 * processing can continue on next analysers, or zero if it either needs more
2147 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2148 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2149 * when it has nothing left to do, and may remove any analyser when it wants to
2150 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002151 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002152int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002153{
Willy Tarreau59234e92008-11-30 23:51:27 +01002154 /*
2155 * We will parse the partial (or complete) lines.
2156 * We will check the request syntax, and also join multi-line
2157 * headers. An index of all the lines will be elaborated while
2158 * parsing.
2159 *
2160 * For the parsing, we use a 28 states FSM.
2161 *
2162 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002163 * req->buf->p = beginning of request
2164 * req->buf->p + msg->eoh = end of processed headers / start of current one
2165 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002166 * msg->eol = end of current header or line (LF or CRLF)
2167 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002168 *
2169 * At end of parsing, we may perform a capture of the error (if any), and
2170 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2171 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2172 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002174
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002176 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 struct http_txn *txn = &s->txn;
2178 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002179 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002180
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002181 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 +01002182 now_ms, __FUNCTION__,
2183 s,
2184 req,
2185 req->rex, req->wex,
2186 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002187 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002188 req->analysers);
2189
Willy Tarreau52a0c602009-08-16 22:45:38 +02002190 /* we're speaking HTTP here, so let's speak HTTP to the client */
2191 s->srv_error = http_return_srv_error;
2192
Willy Tarreau83e3af02009-12-28 17:39:57 +01002193 /* There's a protected area at the end of the buffer for rewriting
2194 * purposes. We don't want to start to parse the request if the
2195 * protected area is affected, because we may have to move processed
2196 * data later, which is much more complicated.
2197 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002198 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002199 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002200 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002201 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2202 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2203 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002204 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002205 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002206 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002207 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002208 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002209 return 0;
2210 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002211 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2212 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2213 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002214 }
2215
Willy Tarreau065e8332010-01-08 00:30:20 +01002216 /* Note that we have the same problem with the response ; we
2217 * may want to send a redirect, error or anything which requires
2218 * some spare space. So we'll ensure that we have at least
2219 * maxrewrite bytes available in the response buffer before
2220 * processing that one. This will only affect pipelined
2221 * keep-alive requests.
2222 */
2223 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002224 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002225 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2226 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2227 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002228 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002229 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002230 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002231 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002232 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002233 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002234 return 0;
2235 }
2236 }
2237
Willy Tarreau9b28e032012-10-12 23:49:43 +02002238 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002239 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002240 }
2241
Willy Tarreau59234e92008-11-30 23:51:27 +01002242 /* 1: we might have to print this header in debug mode */
2243 if (unlikely((global.mode & MODE_DEBUG) &&
2244 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002245 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002247
Willy Tarreau9b28e032012-10-12 23:49:43 +02002248 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002249 /* this is a bit complex : in case of error on the request line,
2250 * we know that rq.l is still zero, so we display only the part
2251 * up to the end of the line (truncated by debug_hdr).
2252 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002253 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002254 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002255
Willy Tarreau59234e92008-11-30 23:51:27 +01002256 sol += hdr_idx_first_pos(&txn->hdr_idx);
2257 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002258
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 while (cur_idx) {
2260 eol = sol + txn->hdr_idx.v[cur_idx].len;
2261 debug_hdr("clihdr", s, sol, eol);
2262 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2263 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002264 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 }
2266
Willy Tarreau58f10d72006-12-04 02:26:12 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 /*
2269 * Now we quickly check if we have found a full valid request.
2270 * If not so, we check the FD and buffer states before leaving.
2271 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002272 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002273 * requests are checked first. When waiting for a second request
2274 * on a keep-alive session, if we encounter and error, close, t/o,
2275 * we note the error in the session flags but don't set any state.
2276 * Since the error will be noted there, it will not be counted by
2277 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002278 * Last, we may increase some tracked counters' http request errors on
2279 * the cases that are deliberately the client's fault. For instance,
2280 * a timeout or connection reset is not counted as an error. However
2281 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Willy Tarreau655dce92009-11-08 13:10:58 +01002284 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002285 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002286 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002287 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002288 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002289 session_inc_http_req_ctr(s);
2290 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002291 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002292 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002294
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 /* 1: Since we are in header mode, if there's no space
2296 * left for headers, we won't be able to free more
2297 * later, so the session will never terminate. We
2298 * must terminate it now.
2299 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002300 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002301 /* FIXME: check if URI is set and return Status
2302 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002304 session_inc_http_req_ctr(s);
2305 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002306 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002307 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002308 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 goto return_bad_req;
2310 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002311
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002313 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002314 if (!(s->flags & SN_ERR_MASK))
2315 s->flags |= SN_ERR_CLICL;
2316
Willy Tarreaufcffa692010-01-10 14:21:19 +01002317 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002318 goto failed_keep_alive;
2319
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002321 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002322 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002323 session_inc_http_err_ctr(s);
2324 }
2325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 msg->msg_state = HTTP_MSG_ERROR;
2327 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002328
Willy Tarreauda7ff642010-06-23 11:44:09 +02002329 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002330 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002331 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002332 if (s->listener->counters)
2333 s->listener->counters->failed_req++;
2334
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 if (!(s->flags & SN_FINST_MASK))
2336 s->flags |= SN_FINST_R;
2337 return 0;
2338 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002339
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002341 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002342 if (!(s->flags & SN_ERR_MASK))
2343 s->flags |= SN_ERR_CLITO;
2344
Willy Tarreaufcffa692010-01-10 14:21:19 +01002345 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002346 goto failed_keep_alive;
2347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002349 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002350 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002351 session_inc_http_err_ctr(s);
2352 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002354 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 msg->msg_state = HTTP_MSG_ERROR;
2356 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002357
Willy Tarreauda7ff642010-06-23 11:44:09 +02002358 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002359 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002360 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002361 if (s->listener->counters)
2362 s->listener->counters->failed_req++;
2363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 if (!(s->flags & SN_FINST_MASK))
2365 s->flags |= SN_FINST_R;
2366 return 0;
2367 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002370 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002371 if (!(s->flags & SN_ERR_MASK))
2372 s->flags |= SN_ERR_CLICL;
2373
Willy Tarreaufcffa692010-01-10 14:21:19 +01002374 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002375 goto failed_keep_alive;
2376
Willy Tarreau4076a152009-04-02 15:18:36 +02002377 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002378 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002380 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 msg->msg_state = HTTP_MSG_ERROR;
2382 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002383
Willy Tarreauda7ff642010-06-23 11:44:09 +02002384 session_inc_http_err_ctr(s);
2385 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002386 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002387 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002388 if (s->listener->counters)
2389 s->listener->counters->failed_req++;
2390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 if (!(s->flags & SN_FINST_MASK))
2392 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002393 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002394 }
2395
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002396 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002397 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2398 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002399#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002400 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002401 /* We need more data, we have to re-enable quick-ack in case we
2402 * previously disabled it, otherwise we might cause the client
2403 * to delay next data.
2404 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002405 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002406 }
2407#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002408
Willy Tarreaufcffa692010-01-10 14:21:19 +01002409 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2410 /* If the client starts to talk, let's fall back to
2411 * request timeout processing.
2412 */
2413 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002414 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002415 }
2416
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002418 if (!tick_isset(req->analyse_exp)) {
2419 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2420 (txn->flags & TX_WAIT_NEXT_RQ) &&
2421 tick_isset(s->be->timeout.httpka))
2422 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2423 else
2424 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2425 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 /* we're not ready yet */
2428 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002429
2430 failed_keep_alive:
2431 /* Here we process low-level errors for keep-alive requests. In
2432 * short, if the request is not the first one and it experiences
2433 * a timeout, read error or shutdown, we just silently close so
2434 * that the client can try again.
2435 */
2436 txn->status = 0;
2437 msg->msg_state = HTTP_MSG_RQBEFORE;
2438 req->analysers = 0;
2439 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002440 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002441 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002442 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002444
Willy Tarreaud787e662009-07-07 10:14:51 +02002445 /* OK now we have a complete HTTP request with indexed headers. Let's
2446 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002447 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002448 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002449 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002450 * byte after the last LF. msg->sov points to the first byte of data.
2451 * msg->eol cannot be trusted because it may have been left uninitialized
2452 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002453 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002454
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002456 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2457
Willy Tarreaub16a5742010-01-10 14:46:16 +01002458 if (txn->flags & TX_WAIT_NEXT_RQ) {
2459 /* kill the pending keep-alive timeout */
2460 txn->flags &= ~TX_WAIT_NEXT_RQ;
2461 req->analyse_exp = TICK_ETERNITY;
2462 }
2463
2464
Willy Tarreaud787e662009-07-07 10:14:51 +02002465 /* Maybe we found in invalid header name while we were configured not
2466 * to block on that, so we have to capture it now.
2467 */
2468 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002469 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002470
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 /*
2472 * 1: identify the method
2473 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002474 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002475
2476 /* we can make use of server redirect on GET and HEAD */
2477 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2478 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 /*
2481 * 2: check if the URI matches the monitor_uri.
2482 * We have to do this for every request which gets in, because
2483 * the monitor-uri is defined by the frontend.
2484 */
2485 if (unlikely((s->fe->monitor_uri_len != 0) &&
2486 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002487 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 s->fe->monitor_uri,
2489 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002490 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002492 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002494
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002496 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002499 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002500 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 ret = acl_pass(ret);
2503 if (cond->pol == ACL_COND_UNLESS)
2504 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 if (ret) {
2507 /* we fail this request, let's return 503 service unavail */
2508 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002509 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002511 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002512 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* nothing to fail, let's reply normaly */
2515 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002516 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 goto return_prx_cond;
2518 }
2519
2520 /*
2521 * 3: Maybe we have to copy the original REQURI for the logs ?
2522 * Note: we cannot log anymore if the request has been
2523 * classified as invalid.
2524 */
2525 if (unlikely(s->logs.logwait & LW_REQ)) {
2526 /* we have a complete HTTP request that we must log */
2527 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2528 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002529
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 if (urilen >= REQURI_LEN)
2531 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002532 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 if (!(s->logs.logwait &= ~LW_REQ))
2536 s->do_log(s);
2537 } else {
2538 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002539 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 }
Willy Tarreau06619262006-12-17 08:37:22 +01002541
William Lallemanda73203e2012-03-12 12:48:57 +01002542 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2543 s->unique_id = pool_alloc2(pool2_uniqueid);
2544 }
2545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002547 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002548 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002549
Willy Tarreau5b154472009-12-21 20:11:07 +01002550 /* ... and check if the request is HTTP/1.1 or above */
2551 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2553 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2554 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002555 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002556
2557 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002558 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002559
Willy Tarreau88d349d2010-01-25 12:15:43 +01002560 /* if the frontend has "option http-use-proxy-header", we'll check if
2561 * we have what looks like a proxied connection instead of a connection,
2562 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2563 * Note that this is *not* RFC-compliant, however browsers and proxies
2564 * happen to do that despite being non-standard :-(
2565 * We consider that a request not beginning with either '/' or '*' is
2566 * a proxied connection, which covers both "scheme://location" and
2567 * CONNECT ip:port.
2568 */
2569 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002570 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002571 txn->flags |= TX_USE_PX_CONN;
2572
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002573 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002574 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002575
Willy Tarreau59234e92008-11-30 23:51:27 +01002576 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002577 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002578 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002580
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002581 /* 6: determine the transfer-length.
2582 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2583 * the presence of a message-body in a REQUEST and its transfer length
2584 * must be determined that way (in order of precedence) :
2585 * 1. The presence of a message-body in a request is signaled by the
2586 * inclusion of a Content-Length or Transfer-Encoding header field
2587 * in the request's header fields. When a request message contains
2588 * both a message-body of non-zero length and a method that does
2589 * not define any semantics for that request message-body, then an
2590 * origin server SHOULD either ignore the message-body or respond
2591 * with an appropriate error message (e.g., 413). A proxy or
2592 * gateway, when presented the same request, SHOULD either forward
2593 * the request inbound with the message- body or ignore the
2594 * message-body when determining a response.
2595 *
2596 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2597 * and the "chunked" transfer-coding (Section 6.2) is used, the
2598 * transfer-length is defined by the use of this transfer-coding.
2599 * If a Transfer-Encoding header field is present and the "chunked"
2600 * transfer-coding is not present, the transfer-length is defined
2601 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002602 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002603 * 3. If a Content-Length header field is present, its decimal value in
2604 * OCTETs represents both the entity-length and the transfer-length.
2605 * If a message is received with both a Transfer-Encoding header
2606 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002607 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002608 * 4. By the server closing the connection. (Closing the connection
2609 * cannot be used to indicate the end of a request body, since that
2610 * would leave no possibility for the server to send back a response.)
2611 *
2612 * Whenever a transfer-coding is applied to a message-body, the set of
2613 * transfer-codings MUST include "chunked", unless the message indicates
2614 * it is terminated by closing the connection. When the "chunked"
2615 * transfer-coding is used, it MUST be the last transfer-coding applied
2616 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002617 */
2618
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002619 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002620 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002621 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002622 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002623 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002624 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002625 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2626 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002627 /* bad transfer-encoding (chunked followed by something else) */
2628 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002629 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002630 break;
2631 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002632 }
2633
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002635 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002636 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002637 signed long long cl;
2638
Willy Tarreauad14f752011-09-02 20:33:27 +02002639 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002642 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002643
Willy Tarreauad14f752011-09-02 20:33:27 +02002644 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002646 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002647 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002648
Willy Tarreauad14f752011-09-02 20:33:27 +02002649 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002650 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002651 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002652 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002653
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002654 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002655 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002656 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002657 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002659 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002660 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002661 }
2662
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002663 /* bodyless requests have a known length */
2664 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002665 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002666
Willy Tarreaud787e662009-07-07 10:14:51 +02002667 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002668 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002669 req->analyse_exp = TICK_ETERNITY;
2670 return 1;
2671
2672 return_bad_req:
2673 /* We centralize bad requests processing here */
2674 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2675 /* we detected a parsing error. We want to archive this request
2676 * in the dedicated proxy area for later troubleshooting.
2677 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002678 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002679 }
2680
2681 txn->req.msg_state = HTTP_MSG_ERROR;
2682 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002683 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002684
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002685 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002686 if (s->listener->counters)
2687 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002688
2689 return_prx_cond:
2690 if (!(s->flags & SN_ERR_MASK))
2691 s->flags |= SN_ERR_PRXCOND;
2692 if (!(s->flags & SN_FINST_MASK))
2693 s->flags |= SN_FINST_R;
2694
2695 req->analysers = 0;
2696 req->analyse_exp = TICK_ETERNITY;
2697 return 0;
2698}
2699
Cyril Bonté70be45d2010-10-12 00:14:35 +02002700/* We reached the stats page through a POST request.
2701 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002702 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002703 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002704int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002705{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002706 struct proxy *px = NULL;
2707 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002708
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002709 char key[LINESIZE];
2710 int action = ST_ADM_ACTION_NONE;
2711 int reprocess = 0;
2712
2713 int total_servers = 0;
2714 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002715
2716 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002717 char *st_cur_param = NULL;
2718 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719
Willy Tarreau9b28e032012-10-12 23:49:43 +02002720 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002721 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002722
2723 cur_param = next_param = end_params;
2724
Willy Tarreau9b28e032012-10-12 23:49:43 +02002725 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002726 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002727 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002728 return 1;
2729 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002730 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002731 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002732 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002733 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002734 }
2735
2736 *end_params = '\0';
2737
Willy Tarreau295a8372011-03-10 11:25:07 +01002738 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
2740 /*
2741 * Parse the parameters in reverse order to only store the last value.
2742 * From the html form, the backend and the action are at the end.
2743 */
2744 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002745 char *value;
2746 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002747
2748 cur_param--;
2749 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002750 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002751 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002752 poffset = (cur_param != first_param ? 1 : 0);
2753 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2754 if ((plen > 0) && (plen <= sizeof(key))) {
2755 strncpy(key, cur_param + poffset, plen);
2756 key[plen - 1] = '\0';
2757 } else {
2758 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2759 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002760 }
2761
2762 /* Parse the value */
2763 value = key;
2764 while (*value != '\0' && *value != '=') {
2765 value++;
2766 }
2767 if (*value == '=') {
2768 /* Ok, a value is found, we can mark the end of the key */
2769 *value++ = '\0';
2770 }
2771
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002772 if (!url_decode(key) || !url_decode(value))
2773 break;
2774
Cyril Bonté70be45d2010-10-12 00:14:35 +02002775 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002776 if (!px && (strcmp(key, "b") == 0)) {
2777 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2778 /* the backend name is unknown or ambiguous (duplicate names) */
2779 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2780 goto out;
2781 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002782 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002783 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002784 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002785 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002786 }
2787 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002788 action = ST_ADM_ACTION_ENABLE;
2789 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002790 else if (strcmp(value, "stop") == 0) {
2791 action = ST_ADM_ACTION_STOP;
2792 }
2793 else if (strcmp(value, "start") == 0) {
2794 action = ST_ADM_ACTION_START;
2795 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002796 else if (strcmp(value, "shutdown") == 0) {
2797 action = ST_ADM_ACTION_SHUTDOWN;
2798 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002799 else {
2800 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2801 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002802 }
2803 }
2804 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002805 if (!(px && action)) {
2806 /*
2807 * Indicates that we'll need to reprocess the parameters
2808 * as soon as backend and action are known
2809 */
2810 if (!reprocess) {
2811 st_cur_param = cur_param;
2812 st_next_param = next_param;
2813 }
2814 reprocess = 1;
2815 }
2816 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002817 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002818 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002819 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002820 /* Not already in maintenance, we can change the server state */
2821 sv->state |= SRV_MAINTAIN;
2822 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002823 altered_servers++;
2824 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002825 }
2826 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002827 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002828 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002829 /* Already in maintenance, we can change the server state */
2830 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002831 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002832 altered_servers++;
2833 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002834 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002835 break;
2836 case ST_ADM_ACTION_STOP:
2837 case ST_ADM_ACTION_START:
2838 if (action == ST_ADM_ACTION_START)
2839 sv->uweight = sv->iweight;
2840 else
2841 sv->uweight = 0;
2842
2843 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2844 /* we must take care of not pushing the server to full throttle during slow starts */
2845 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2846 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2847 else
2848 sv->eweight = BE_WEIGHT_SCALE;
2849 sv->eweight *= sv->uweight;
2850 } else {
2851 sv->eweight = sv->uweight;
2852 }
2853
2854 /* static LB algorithms are a bit harder to update */
2855 if (px->lbprm.update_server_eweight)
2856 px->lbprm.update_server_eweight(sv);
2857 else if (sv->eweight) {
2858 if (px->lbprm.set_server_status_up)
2859 px->lbprm.set_server_status_up(sv);
2860 }
2861 else {
2862 if (px->lbprm.set_server_status_down)
2863 px->lbprm.set_server_status_down(sv);
2864 }
2865 altered_servers++;
2866 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002867 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002868 case ST_ADM_ACTION_SHUTDOWN:
2869 if (px->state != PR_STSTOPPED) {
2870 struct session *sess, *sess_bck;
2871
2872 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2873 if (sess->srv_conn == sv)
2874 session_shutdown(sess, SN_ERR_KILLED);
2875
2876 altered_servers++;
2877 total_servers++;
2878 }
2879 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002881 } else {
2882 /* the server name is unknown or ambiguous (duplicate names) */
2883 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002884 }
2885 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002886 if (reprocess && px && action) {
2887 /* Now, we know the backend and the action chosen by the user.
2888 * We can safely restart from the first server parameter
2889 * to reprocess them
2890 */
2891 cur_param = st_cur_param;
2892 next_param = st_next_param;
2893 reprocess = 0;
2894 goto reprocess_servers;
2895 }
2896
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897 next_param = cur_param;
2898 }
2899 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002900
2901 if (total_servers == 0) {
2902 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2903 }
2904 else if (altered_servers == 0) {
2905 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2906 }
2907 else if (altered_servers == total_servers) {
2908 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2909 }
2910 else {
2911 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2912 }
2913 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002914 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002915}
2916
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002917/* returns a pointer to the first rule which forbids access (deny or http_auth),
2918 * or NULL if everything's OK.
2919 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002920static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002921http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2922{
Willy Tarreauff011f22011-01-06 17:51:27 +01002923 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002924
Willy Tarreauff011f22011-01-06 17:51:27 +01002925 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002926 int ret = 1;
2927
Willy Tarreauff011f22011-01-06 17:51:27 +01002928 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002929 continue;
2930
2931 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002932 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002933 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002934 ret = acl_pass(ret);
2935
Willy Tarreauff011f22011-01-06 17:51:27 +01002936 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002937 ret = !ret;
2938 }
2939
2940 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002941 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002942 return NULL; /* no problem */
2943 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002944 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002945 }
2946 }
2947 return NULL;
2948}
2949
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002950/* This stream analyser runs all HTTP request processing which is common to
2951 * frontends and backends, which means blocking ACLs, filters, connection-close,
2952 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002953 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002954 * either needs more data or wants to immediately abort the request (eg: deny,
2955 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002956 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002957int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002958{
Willy Tarreaud787e662009-07-07 10:14:51 +02002959 struct http_txn *txn = &s->txn;
2960 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002962 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002963 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002964 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002965 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002966
Willy Tarreau655dce92009-11-08 13:10:58 +01002967 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002968 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002969 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002970 return 0;
2971 }
2972
Willy Tarreau3a816292009-07-07 10:55:49 +02002973 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002974 req->analyse_exp = TICK_ETERNITY;
2975
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002976 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 +02002977 now_ms, __FUNCTION__,
2978 s,
2979 req,
2980 req->rex, req->wex,
2981 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002982 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002983 req->analysers);
2984
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985 /* first check whether we have some ACLs set to block this request */
2986 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002987 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002988
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989 ret = acl_pass(ret);
2990 if (cond->pol == ACL_COND_UNLESS)
2991 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002992
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002993 if (ret) {
2994 txn->status = 403;
2995 /* let's log the request time */
2996 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002997 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003001 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003002
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003003 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003004 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003005
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003006 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003007 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003008 do_stats = stats_check_uri(s->rep->prod, txn, px);
3009 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003010 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 +01003011 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003012 else
3013 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003014
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003015 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003016 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003017 txn->status = 403;
3018 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003019 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003020 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003021 s->fe->fe_counters.denied_req++;
3022 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3023 s->be->be_counters.denied_req++;
3024 if (s->listener->counters)
3025 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003026 goto return_prx_cond;
3027 }
3028
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 /* try headers filters */
3030 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003031 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003032 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003033
Willy Tarreau59234e92008-11-30 23:51:27 +01003034 /* has the request been denied ? */
3035 if (txn->flags & TX_CLDENY) {
3036 /* no need to go further */
3037 txn->status = 403;
3038 /* let's log the request time */
3039 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003040 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003041 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003042 goto return_prx_cond;
3043 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003044
3045 /* When a connection is tarpitted, we use the tarpit timeout,
3046 * which may be the same as the connect timeout if unspecified.
3047 * If unset, then set it to zero because we really want it to
3048 * eventually expire. We build the tarpit as an analyser.
3049 */
3050 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003051 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003052 /* wipe the request out so that we can drop the connection early
3053 * if the client closes first.
3054 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003055 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003056 req->analysers = 0; /* remove switching rules etc... */
3057 req->analysers |= AN_REQ_HTTP_TARPIT;
3058 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3059 if (!req->analyse_exp)
3060 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003061 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003062 return 1;
3063 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 }
Willy Tarreau06619262006-12-17 08:37:22 +01003065
Willy Tarreau5b154472009-12-21 20:11:07 +01003066 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3067 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003068 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3069 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003070 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3071 * avoid to redo the same work if FE and BE have the same settings (common).
3072 * The method consists in checking if options changed between the two calls
3073 * (implying that either one is non-null, or one of them is non-null and we
3074 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003075 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003076
Willy Tarreaudc008c52010-02-01 16:20:08 +01003077 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3078 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3079 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3080 (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 +01003081 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003082
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003083 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3084 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003085 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003086 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3087 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003088 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003089 tmp = TX_CON_WANT_CLO;
3090
Willy Tarreau5b154472009-12-21 20:11:07 +01003091 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3092 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003093
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003094 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3095 /* parse the Connection header and possibly clean it */
3096 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003097 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003098 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3099 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003100 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003101 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003102 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003103 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003104 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003105
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003106 /* check if client or config asks for explicit close in KAL/SCL */
3107 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3108 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3109 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003110 (!(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 +01003111 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003112 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003113 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003114 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3115 }
Willy Tarreau78599912009-10-17 20:12:21 +02003116
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003117 /* we can be blocked here because the request needs to be authenticated,
3118 * either to pass or to access stats.
3119 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003120 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003121 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003122
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003123 if (!realm)
3124 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003126 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003127 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003128 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003129 /* on 401 we still count one error, because normal browsing
3130 * won't significantly increase the counter but brute force
3131 * attempts will.
3132 */
3133 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003134 goto return_prx_cond;
3135 }
3136
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003137 /* add request headers from the rule sets in the same order */
3138 list_for_each_entry(wl, &px->req_add, list) {
3139 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003140 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003141 ret = acl_pass(ret);
3142 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3143 ret = !ret;
3144 if (!ret)
3145 continue;
3146 }
3147
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003148 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003149 goto return_bad_req;
3150 }
3151
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003152 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003153 struct stats_admin_rule *stats_admin_rule;
3154
3155 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003156 * FIXME!!! that one is rather dangerous, we want to
3157 * make it follow standard rules (eg: clear req->analysers).
3158 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003159
Cyril Bonté474be412010-10-12 00:14:36 +02003160 /* now check whether we have some admin rules for this request */
3161 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3162 int ret = 1;
3163
3164 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003165 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 +02003166 ret = acl_pass(ret);
3167 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3168 ret = !ret;
3169 }
3170
3171 if (ret) {
3172 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003173 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003174 break;
3175 }
3176 }
3177
Cyril Bonté70be45d2010-10-12 00:14:35 +02003178 /* Was the status page requested with a POST ? */
3179 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003180 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003181 if (msg->msg_state < HTTP_MSG_100_SENT) {
3182 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3183 * send an HTTP/1.1 100 Continue intermediate response.
3184 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003185 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003186 struct hdr_ctx ctx;
3187 ctx.idx = 0;
3188 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003189 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003190 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003191 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003192 }
3193 }
3194 msg->msg_state = HTTP_MSG_100_SENT;
3195 s->logs.tv_request = now; /* update the request timer to reflect full request */
3196 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003197 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003198 /* we need more data */
3199 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003200 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003201 return 0;
3202 }
Cyril Bonté474be412010-10-12 00:14:36 +02003203 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003204 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003205 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003206 }
3207
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003208 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003209 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003210 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003211 s->target = s->rep->prod->conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003212 s->rep->prod->conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003213 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003214 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003215 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3216 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003217
3218 return 0;
3219
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003220 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003221
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003222 /* check whether we have some ACLs set to redirect this request */
3223 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003224 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003225
Willy Tarreauf285f542010-01-03 20:03:03 +01003226 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003227 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003228 ret = acl_pass(ret);
3229 if (rule->cond->pol == ACL_COND_UNLESS)
3230 ret = !ret;
3231 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003232
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 if (ret) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003234 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003235
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003236 /* build redirect message */
3237 switch(rule->code) {
3238 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003239 msg_fmt = HTTP_303;
3240 break;
3241 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003242 msg_fmt = HTTP_301;
3243 break;
3244 case 302:
3245 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003246 msg_fmt = HTTP_302;
3247 break;
3248 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003249
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003250 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003251 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003252
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003253 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003254 case REDIRECT_TYPE_SCHEME: {
3255 const char *path;
3256 const char *host;
3257 struct hdr_ctx ctx;
3258 int pathlen;
3259 int hostlen;
3260
3261 host = "";
3262 hostlen = 0;
3263 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003264 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003265 host = ctx.line + ctx.val;
3266 hostlen = ctx.vlen;
3267 }
3268
3269 path = http_get_path(txn);
3270 /* build message using path */
3271 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003272 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003273 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3274 int qs = 0;
3275 while (qs < pathlen) {
3276 if (path[qs] == '?') {
3277 pathlen = qs;
3278 break;
3279 }
3280 qs++;
3281 }
3282 }
3283 } else {
3284 path = "/";
3285 pathlen = 1;
3286 }
3287
3288 /* check if we can add scheme + "://" + host + path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003289 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003290 goto return_bad_req;
3291
3292 /* add scheme */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003293 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3294 trash.len += rule->rdr_len;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003295
3296 /* add "://" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003297 memcpy(trash.str + trash.len, "://", 3);
3298 trash.len += 3;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003299
3300 /* add host */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003301 memcpy(trash.str + trash.len, host, hostlen);
3302 trash.len += hostlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003303
3304 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003305 memcpy(trash.str + trash.len, path, pathlen);
3306 trash.len += pathlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003307
3308 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003309 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003310 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003311 if (trash.len > trash.size - 5)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003312 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003313 trash.str[trash.len] = '/';
3314 trash.len++;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003315 }
3316
3317 break;
3318 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003319 case REDIRECT_TYPE_PREFIX: {
3320 const char *path;
3321 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003322
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 path = http_get_path(txn);
3324 /* build message using path */
3325 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003326 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003327 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3328 int qs = 0;
3329 while (qs < pathlen) {
3330 if (path[qs] == '?') {
3331 pathlen = qs;
3332 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003333 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003334 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003335 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003336 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003337 } else {
3338 path = "/";
3339 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003340 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003341
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003342 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003343 goto return_bad_req;
3344
3345 /* add prefix. Note that if prefix == "/", we don't want to
3346 * add anything, otherwise it makes it hard for the user to
3347 * configure a self-redirection.
3348 */
3349 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003350 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3351 trash.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003352 }
3353
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003354 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003355 memcpy(trash.str + trash.len, path, pathlen);
3356 trash.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003357
3358 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003359 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003360 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003361 if (trash.len > trash.size - 5)
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003362 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003363 trash.str[trash.len] = '/';
3364 trash.len++;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003365 }
3366
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003367 break;
3368 }
3369 case REDIRECT_TYPE_LOCATION:
3370 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003371 if (trash.len + rule->rdr_len > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003372 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003373
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003374 /* add location */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003375 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3376 trash.len += rule->rdr_len;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003377 break;
3378 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003379
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003380 if (rule->cookie_len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003381 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3382 trash.len += 14;
3383 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3384 trash.len += rule->cookie_len;
3385 memcpy(trash.str + trash.len, "\r\n", 2);
3386 trash.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003387 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003388
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003389 /* add end of headers and the keep-alive/close status.
3390 * We may choose to set keep-alive if the Location begins
3391 * with a slash, because the client will come back to the
3392 * same server.
3393 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003394 txn->status = rule->code;
3395 /* let's log the request time */
3396 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003397
3398 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003399 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3400 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003401 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3402 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3403 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003404 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003405 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003406 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3407 trash.len += 30;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003408 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003409 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3410 trash.len += 24;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003411 }
Willy Tarreau75661452010-01-10 10:35:01 +01003412 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003413 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3414 trash.len += 4;
3415 bo_inject(req->prod->ob, trash.str, trash.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003416 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003417 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003418 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003419 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003420 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3421 txn->req.msg_state = HTTP_MSG_CLOSED;
3422 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003423 break;
3424 } else {
3425 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003426 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003427 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3428 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003429 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003430 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3431 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003432 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003433 stream_int_retnclose(req->prod, &trash);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003434 goto return_prx_cond;
3435 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003436 }
3437 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003438
Willy Tarreau2be39392010-01-03 17:24:51 +01003439 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3440 * If this happens, then the data will not come immediately, so we must
3441 * send all what we have without waiting. Note that due to the small gain
3442 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003443 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003444 * itself once used.
3445 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003446 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003447
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003448 /* that's OK for us now, let's move on to next analysers */
3449 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003450
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003451 return_bad_req:
3452 /* We centralize bad requests processing here */
3453 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3454 /* we detected a parsing error. We want to archive this request
3455 * in the dedicated proxy area for later troubleshooting.
3456 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003457 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003459
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 txn->req.msg_state = HTTP_MSG_ERROR;
3461 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003462 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003463
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003464 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003465 if (s->listener->counters)
3466 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003467
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003468 return_prx_cond:
3469 if (!(s->flags & SN_ERR_MASK))
3470 s->flags |= SN_ERR_PRXCOND;
3471 if (!(s->flags & SN_FINST_MASK))
3472 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003473
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003474 req->analysers = 0;
3475 req->analyse_exp = TICK_ETERNITY;
3476 return 0;
3477}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003478
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003479/* This function performs all the processing enabled for the current request.
3480 * It returns 1 if the processing can continue on next analysers, or zero if it
3481 * needs more data, encounters an error, or wants to immediately abort the
3482 * request. It relies on buffers flags, and updates s->req->analysers.
3483 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003484int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003485{
3486 struct http_txn *txn = &s->txn;
3487 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003488
Willy Tarreau655dce92009-11-08 13:10:58 +01003489 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003490 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003491 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003492 return 0;
3493 }
3494
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003495 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 +02003496 now_ms, __FUNCTION__,
3497 s,
3498 req,
3499 req->rex, req->wex,
3500 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003501 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003502 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003503
William Lallemand82fe75c2012-10-23 10:25:10 +02003504 if (s->fe->comp || s->be->comp)
3505 select_compression_request_header(s, req->buf);
3506
Willy Tarreau59234e92008-11-30 23:51:27 +01003507 /*
3508 * Right now, we know that we have processed the entire headers
3509 * and that unwanted requests have been filtered out. We can do
3510 * whatever we want with the remaining request. Also, now we
3511 * may have separate values for ->fe, ->be.
3512 */
Willy Tarreau06619262006-12-17 08:37:22 +01003513
Willy Tarreau59234e92008-11-30 23:51:27 +01003514 /*
3515 * If HTTP PROXY is set we simply get remote server address
3516 * parsing incoming request.
3517 */
3518 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003519 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 +01003520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003521
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003523 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003524 * Note that doing so might move headers in the request, but
3525 * the fields will stay coherent and the URI will not move.
3526 * This should only be performed in the backend.
3527 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003528 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003529 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3530 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003531
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003533 * 8: the appsession cookie was looked up very early in 1.2,
3534 * so let's do the same now.
3535 */
3536
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003537 /* It needs to look into the URI unless persistence must be ignored */
3538 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003539 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 +01003540 }
3541
William Lallemanda73203e2012-03-12 12:48:57 +01003542 /* add unique-id if "header-unique-id" is specified */
3543
3544 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3545 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3546
3547 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003548 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3549 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003550 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003551 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003552 goto return_bad_req;
3553 }
3554
Cyril Bontéb21570a2009-11-29 20:04:48 +01003555 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003556 * 9: add X-Forwarded-For if either the frontend or the backend
3557 * asks for it.
3558 */
3559 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003560 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003561 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003562 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3563 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003564 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003565 /* The header is set to be added only if none is present
3566 * and we found it, so don't do anything.
3567 */
3568 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003569 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003570 /* Add an X-Forwarded-For header unless the source IP is
3571 * in the 'except' network range.
3572 */
3573 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003574 (((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 +01003575 != s->fe->except_net.s_addr) &&
3576 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003577 (((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 +01003578 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003579 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003580 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003581 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003582
3583 /* Note: we rely on the backend to get the header name to be used for
3584 * x-forwarded-for, because the header is really meant for the backends.
3585 * However, if the backend did not specify any option, we have to rely
3586 * on the frontend's header name.
3587 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003588 if (s->be->fwdfor_hdr_len) {
3589 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003590 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003591 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003592 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003593 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003594 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003595 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003596
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003597 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003598 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003599 }
3600 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003601 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003602 /* FIXME: for the sake of completeness, we should also support
3603 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003604 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003605 int len;
3606 char pn[INET6_ADDRSTRLEN];
3607 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003608 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003609 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003610
Willy Tarreau59234e92008-11-30 23:51:27 +01003611 /* Note: we rely on the backend to get the header name to be used for
3612 * x-forwarded-for, because the header is really meant for the backends.
3613 * However, if the backend did not specify any option, we have to rely
3614 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003615 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003616 if (s->be->fwdfor_hdr_len) {
3617 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003618 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003619 } else {
3620 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003621 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003622 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003623 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003625 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003626 goto return_bad_req;
3627 }
3628 }
3629
3630 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003631 * 10: add X-Original-To if either the frontend or the backend
3632 * asks for it.
3633 */
3634 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3635
3636 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003637 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003638 /* Add an X-Original-To header unless the destination IP is
3639 * in the 'except' network range.
3640 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003641 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003642
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003643 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003644 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003645 (((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 +02003646 != s->fe->except_to.s_addr) &&
3647 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003648 (((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 +02003649 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003650 int len;
3651 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003652 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003653
3654 /* Note: we rely on the backend to get the header name to be used for
3655 * x-original-to, because the header is really meant for the backends.
3656 * However, if the backend did not specify any option, we have to rely
3657 * on the frontend's header name.
3658 */
3659 if (s->be->orgto_hdr_len) {
3660 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003661 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003662 } else {
3663 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003664 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003665 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003666 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003667
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003668 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003669 goto return_bad_req;
3670 }
3671 }
3672 }
3673
Willy Tarreau50fc7772012-11-11 22:19:57 +01003674 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3675 * If an "Upgrade" token is found, the header is left untouched in order not to have
3676 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3677 * "Upgrade" is present in the Connection header.
3678 */
3679 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3680 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3681 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003682 unsigned int want_flags = 0;
3683
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003684 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003685 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3686 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3687 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003688 want_flags |= TX_CON_CLO_SET;
3689 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003690 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3691 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3692 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003693 want_flags |= TX_CON_KAL_SET;
3694 }
3695
3696 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003697 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003698 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003699
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003700
Willy Tarreau522d6c02009-12-06 18:49:18 +01003701 /* If we have no server assigned yet and we're balancing on url_param
3702 * with a POST request, we may be interested in checking the body for
3703 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003704 */
3705 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3706 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003707 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003708 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003709 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003710 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003711 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003712
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003713 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003714 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003715#ifdef TCP_QUICKACK
3716 /* We expect some data from the client. Unless we know for sure
3717 * we already have a full request, we have to re-enable quick-ack
3718 * in case we previously disabled it, otherwise we might cause
3719 * the client to delay further data.
3720 */
3721 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003722 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003723 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003724 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003725#endif
3726 }
Willy Tarreau03945942009-12-22 16:50:27 +01003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 /*************************************************************
3729 * OK, that's finished for the headers. We have done what we *
3730 * could. Let's switch to the DATA state. *
3731 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003732 req->analyse_exp = TICK_ETERNITY;
3733 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003734
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003735 /* if the server closes the connection, we want to immediately react
3736 * and close the socket to save packets and syscalls.
3737 */
3738 req->cons->flags |= SI_FL_NOHALF;
3739
Willy Tarreau59234e92008-11-30 23:51:27 +01003740 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003741 /* OK let's go on with the BODY now */
3742 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003743
Willy Tarreau59234e92008-11-30 23:51:27 +01003744 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003745 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003746 /* we detected a parsing error. We want to archive this request
3747 * in the dedicated proxy area for later troubleshooting.
3748 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003749 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003750 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003751
Willy Tarreau59234e92008-11-30 23:51:27 +01003752 txn->req.msg_state = HTTP_MSG_ERROR;
3753 txn->status = 400;
3754 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003755 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003756
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003757 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003758 if (s->listener->counters)
3759 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003760
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 if (!(s->flags & SN_ERR_MASK))
3762 s->flags |= SN_ERR_PRXCOND;
3763 if (!(s->flags & SN_FINST_MASK))
3764 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003765 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003766}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003767
Willy Tarreau60b85b02008-11-30 23:28:40 +01003768/* This function is an analyser which processes the HTTP tarpit. It always
3769 * returns zero, at the beginning because it prevents any other processing
3770 * from occurring, and at the end because it terminates the request.
3771 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003772int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003773{
3774 struct http_txn *txn = &s->txn;
3775
3776 /* This connection is being tarpitted. The CLIENT side has
3777 * already set the connect expiration date to the right
3778 * timeout. We just have to check that the client is still
3779 * there and that the timeout has not expired.
3780 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003781 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003782 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003783 !tick_is_expired(req->analyse_exp, now_ms))
3784 return 0;
3785
3786 /* We will set the queue timer to the time spent, just for
3787 * logging purposes. We fake a 500 server error, so that the
3788 * attacker will not suspect his connection has been tarpitted.
3789 * It will not cause trouble to the logs because we can exclude
3790 * the tarpitted connections by filtering on the 'PT' status flags.
3791 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003792 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3793
3794 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003795 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003796 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003797
3798 req->analysers = 0;
3799 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003800
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003801 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003802 if (s->listener->counters)
3803 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003804
Willy Tarreau60b85b02008-11-30 23:28:40 +01003805 if (!(s->flags & SN_ERR_MASK))
3806 s->flags |= SN_ERR_PRXCOND;
3807 if (!(s->flags & SN_FINST_MASK))
3808 s->flags |= SN_FINST_T;
3809 return 0;
3810}
3811
Willy Tarreaud34af782008-11-30 23:36:37 +01003812/* This function is an analyser which processes the HTTP request body. It looks
3813 * for parameters to be used for the load balancing algorithm (url_param). It
3814 * must only be called after the standard HTTP request processing has occurred,
3815 * because it expects the request to be parsed. It returns zero if it needs to
3816 * read more data, or 1 once it has completed its analysis.
3817 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003818int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003819{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003820 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003821 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003822 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003823
3824 /* We have to parse the HTTP request body to find any required data.
3825 * "balance url_param check_post" should have been the only way to get
3826 * into this. We were brought here after HTTP header analysis, so all
3827 * related structures are ready.
3828 */
3829
Willy Tarreau522d6c02009-12-06 18:49:18 +01003830 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3831 goto missing_data;
3832
3833 if (msg->msg_state < HTTP_MSG_100_SENT) {
3834 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3835 * send an HTTP/1.1 100 Continue intermediate response.
3836 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003837 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003838 struct hdr_ctx ctx;
3839 ctx.idx = 0;
3840 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003841 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003842 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003843 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003844 }
3845 }
3846 msg->msg_state = HTTP_MSG_100_SENT;
3847 }
3848
3849 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003850 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003851 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003852 * is still null. We must save the body in msg->next because it
3853 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003854 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003855 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003856
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003857 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003858 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3859 else
3860 msg->msg_state = HTTP_MSG_DATA;
3861 }
3862
3863 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003864 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003865 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003866 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003867 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003868 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003869
Willy Tarreau115acb92009-12-26 13:56:06 +01003870 if (!ret)
3871 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003872 else if (ret < 0) {
3873 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003874 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003875 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003876 }
3877
Willy Tarreaud98cf932009-12-27 22:54:55 +01003878 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003879 * We have the first data byte is in msg->sov. We're waiting for at
3880 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003881 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882
Willy Tarreau124d9912011-03-01 20:30:48 +01003883 if (msg->body_len < limit)
3884 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003885
Willy Tarreau9b28e032012-10-12 23:49:43 +02003886 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003887 goto http_end;
3888
3889 missing_data:
3890 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003891 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003892 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003893 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003894 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003895
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003896 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003897 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003898 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003899
3900 if (!(s->flags & SN_ERR_MASK))
3901 s->flags |= SN_ERR_CLITO;
3902 if (!(s->flags & SN_FINST_MASK))
3903 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003904 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003905 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003906
3907 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003908 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003909 /* Not enough data. We'll re-use the http-request
3910 * timeout here. Ideally, we should set the timeout
3911 * relative to the accept() date. We just set the
3912 * request timeout once at the beginning of the
3913 * request.
3914 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003915 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003916 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003917 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003918 return 0;
3919 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003920
3921 http_end:
3922 /* The situation will not evolve, so let's give up on the analysis. */
3923 s->logs.tv_request = now; /* update the request timer to reflect full request */
3924 req->analysers &= ~an_bit;
3925 req->analyse_exp = TICK_ETERNITY;
3926 return 1;
3927
3928 return_bad_req: /* let's centralize all bad requests */
3929 txn->req.msg_state = HTTP_MSG_ERROR;
3930 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003931 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003932
Willy Tarreau79ebac62010-06-07 13:47:49 +02003933 if (!(s->flags & SN_ERR_MASK))
3934 s->flags |= SN_ERR_PRXCOND;
3935 if (!(s->flags & SN_FINST_MASK))
3936 s->flags |= SN_FINST_R;
3937
Willy Tarreau522d6c02009-12-06 18:49:18 +01003938 return_err_msg:
3939 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003940 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003941 if (s->listener->counters)
3942 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003943 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003944}
3945
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003946/* send a server's name with an outgoing request over an established connection.
3947 * Note: this function is designed to be called once the request has been scheduled
3948 * for being forwarded. This is the reason why it rewinds the buffer before
3949 * proceeding.
3950 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003951int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003952
3953 struct hdr_ctx ctx;
3954
Mark Lamourinec2247f02012-01-04 13:02:01 -05003955 char *hdr_name = be->server_id_hdr_name;
3956 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003957 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003958 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003959 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003960
William Lallemandd9e90662012-01-30 17:27:17 +01003961 ctx.idx = 0;
3962
Willy Tarreau9b28e032012-10-12 23:49:43 +02003963 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003964 if (old_o) {
3965 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003966 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003967 }
3968
Willy Tarreau9b28e032012-10-12 23:49:43 +02003969 old_i = chn->buf->i;
3970 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003971 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003972 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003973 }
3974
3975 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003976 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003977 memcpy(hdr_val, hdr_name, hdr_name_len);
3978 hdr_val += hdr_name_len;
3979 *hdr_val++ = ':';
3980 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003981 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
3982 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003983
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003984 if (old_o) {
3985 /* If this was a forwarded request, we must readjust the amount of
3986 * data to be forwarded in order to take into account the size
3987 * variations.
3988 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003989 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003990 }
3991
Mark Lamourinec2247f02012-01-04 13:02:01 -05003992 return 0;
3993}
3994
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995/* Terminate current transaction and prepare a new one. This is very tricky
3996 * right now but it works.
3997 */
3998void http_end_txn_clean_session(struct session *s)
3999{
4000 /* FIXME: We need a more portable way of releasing a backend's and a
4001 * server's connections. We need a safer way to reinitialize buffer
4002 * flags. We also need a more accurate method for computing per-request
4003 * data.
4004 */
4005 http_silent_debug(__LINE__, s);
4006
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004007 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004008 si_shutr(s->req->cons);
4009 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010
4011 http_silent_debug(__LINE__, s);
4012
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004013 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004015 if (unlikely(s->srv_conn))
4016 sess_change_server(s, NULL);
4017 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018
4019 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4020 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004021 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022
4023 if (s->txn.status) {
4024 int n;
4025
4026 n = s->txn.status / 100;
4027 if (n < 1 || n > 5)
4028 n = 0;
4029
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004030 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004031 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004032 if (s->comp_algo)
4033 s->fe->fe_counters.p.http.comp_rsp++;
4034 }
Willy Tarreau24657792010-02-26 10:30:28 +01004035 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004036 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004037 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004038 s->be->be_counters.p.http.cum_req++;
4039 if (s->comp_algo)
4040 s->be->be_counters.p.http.comp_rsp++;
4041 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 }
4043
4044 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004045 s->logs.bytes_in -= s->req->buf->i;
4046 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047
4048 /* let's do a final log if we need it */
4049 if (s->logs.logwait &&
4050 !(s->flags & SN_MONITOR) &&
4051 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4052 s->do_log(s);
4053 }
4054
4055 s->logs.accept_date = date; /* user-visible date for logging */
4056 s->logs.tv_accept = now; /* corrected date for internal use */
4057 tv_zero(&s->logs.tv_request);
4058 s->logs.t_queue = -1;
4059 s->logs.t_connect = -1;
4060 s->logs.t_data = -1;
4061 s->logs.t_close = 0;
4062 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4063 s->logs.srv_queue_size = 0; /* we will get this number soon */
4064
Willy Tarreau9b28e032012-10-12 23:49:43 +02004065 s->logs.bytes_in = s->req->total = s->req->buf->i;
4066 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004067
4068 if (s->pend_pos)
4069 pendconn_free(s->pend_pos);
4070
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004071 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 if (s->flags & SN_CURR_SESS) {
4073 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004074 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004076 if (may_dequeue_tasks(objt_server(s->target), s->be))
4077 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 }
4079
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004080 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081
4082 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004083 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4084 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004085 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004086 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 s->req->cons->err_loc = NULL;
4088 s->req->cons->exp = TICK_ETERNITY;
4089 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004090 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4091 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 +02004092 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 +01004093 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004094
4095 if (s->flags & SN_COMP_READY)
4096 s->comp_algo->end(&s->comp_ctx);
4097 s->comp_algo = NULL;
4098 s->flags &= ~SN_COMP_READY;
4099
Willy Tarreau610ecce2010-01-04 21:15:02 +01004100 s->txn.meth = 0;
4101 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004102 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004103 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 s->req->cons->flags |= SI_FL_INDEP_STR;
4105
Willy Tarreau96e31212011-05-30 18:10:30 +02004106 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004107 s->req->flags |= CF_NEVER_WAIT;
4108 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004109 }
4110
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 /* if the request buffer is not empty, it means we're
4112 * about to process another request, so send pending
4113 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004114 * Just don't do this if the buffer is close to be full,
4115 * because the request will wait for it to flush a little
4116 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004118 if (s->req->buf->i) {
4119 if (s->rep->buf->o &&
4120 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4121 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004122 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004123 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004124
4125 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004126 channel_auto_read(s->req);
4127 channel_auto_close(s->req);
4128 channel_auto_read(s->rep);
4129 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130
Willy Tarreau342b11c2010-11-24 16:22:09 +01004131 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 s->rep->analysers = 0;
4133
4134 http_silent_debug(__LINE__, s);
4135}
4136
4137
4138/* This function updates the request state machine according to the response
4139 * state machine and buffer flags. It returns 1 if it changes anything (flag
4140 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4141 * it is only used to find when a request/response couple is complete. Both
4142 * this function and its equivalent should loop until both return zero. It
4143 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4144 */
4145int http_sync_req_state(struct session *s)
4146{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004147 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004149 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 unsigned int old_state = txn->req.msg_state;
4151
4152 http_silent_debug(__LINE__, s);
4153 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4154 return 0;
4155
4156 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004157 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004158 * We can shut the read side unless we want to abort_on_close,
4159 * or we have a POST request. The issue with POST requests is
4160 * that some browsers still send a CRLF after the request, and
4161 * this CRLF must be read so that it does not remain in the kernel
4162 * buffers, otherwise a close could cause an RST on some systems
4163 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004164 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004165 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004166 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167
4168 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4169 goto wait_other_side;
4170
4171 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4172 /* The server has not finished to respond, so we
4173 * don't want to move in order not to upset it.
4174 */
4175 goto wait_other_side;
4176 }
4177
4178 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4179 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004180 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004182 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 goto wait_other_side;
4184 }
4185
4186 /* When we get here, it means that both the request and the
4187 * response have finished receiving. Depending on the connection
4188 * mode, we'll have to wait for the last bytes to leave in either
4189 * direction, and sometimes for a close to be effective.
4190 */
4191
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004192 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4193 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004194 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4195 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004196 }
4197 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4198 /* Option forceclose is set, or either side wants to close,
4199 * let's enforce it now that we're not expecting any new
4200 * data to come. The caller knows the session is complete
4201 * once both states are CLOSED.
4202 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004203 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4204 channel_shutr_now(chn);
4205 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004207 }
4208 else {
4209 /* The last possible modes are keep-alive and tunnel. Since tunnel
4210 * mode does not set the body analyser, we can't reach this place
4211 * in tunnel mode, so we're left with keep-alive only.
4212 * This mode is currently not implemented, we switch to tunnel mode.
4213 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004214 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004215 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004216 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 }
4218
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004219 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004221 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004222
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004223 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224 txn->req.msg_state = HTTP_MSG_CLOSING;
4225 goto http_msg_closing;
4226 }
4227 else {
4228 txn->req.msg_state = HTTP_MSG_CLOSED;
4229 goto http_msg_closed;
4230 }
4231 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004232 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004233 }
4234
4235 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4236 http_msg_closing:
4237 /* nothing else to forward, just waiting for the output buffer
4238 * to be empty and for the shutw_now to take effect.
4239 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004240 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 txn->req.msg_state = HTTP_MSG_CLOSED;
4242 goto http_msg_closed;
4243 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004244 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 txn->req.msg_state = HTTP_MSG_ERROR;
4246 goto wait_other_side;
4247 }
4248 }
4249
4250 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4251 http_msg_closed:
4252 goto wait_other_side;
4253 }
4254
4255 wait_other_side:
4256 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004257 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258}
4259
4260
4261/* This function updates the response state machine according to the request
4262 * state machine and buffer flags. It returns 1 if it changes anything (flag
4263 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4264 * it is only used to find when a request/response couple is complete. Both
4265 * this function and its equivalent should loop until both return zero. It
4266 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4267 */
4268int http_sync_res_state(struct session *s)
4269{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004270 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004272 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 unsigned int old_state = txn->rsp.msg_state;
4274
4275 http_silent_debug(__LINE__, s);
4276 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4277 return 0;
4278
4279 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4280 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004281 * still monitor the server connection for a possible close
4282 * while the request is being uploaded, so we don't disable
4283 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004285 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286
4287 if (txn->req.msg_state == HTTP_MSG_ERROR)
4288 goto wait_other_side;
4289
4290 if (txn->req.msg_state < HTTP_MSG_DONE) {
4291 /* The client seems to still be sending data, probably
4292 * because we got an error response during an upload.
4293 * We have the choice of either breaking the connection
4294 * or letting it pass through. Let's do the later.
4295 */
4296 goto wait_other_side;
4297 }
4298
4299 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4300 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004301 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004303 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 goto wait_other_side;
4305 }
4306
4307 /* When we get here, it means that both the request and the
4308 * response have finished receiving. Depending on the connection
4309 * mode, we'll have to wait for the last bytes to leave in either
4310 * direction, and sometimes for a close to be effective.
4311 */
4312
4313 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4314 /* Server-close mode : shut read and wait for the request
4315 * side to close its output buffer. The caller will detect
4316 * when we're in DONE and the other is in CLOSED and will
4317 * catch that for the final cleanup.
4318 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004319 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4320 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004322 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4323 /* Option forceclose is set, or either side wants to close,
4324 * let's enforce it now that we're not expecting any new
4325 * data to come. The caller knows the session is complete
4326 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004328 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4329 channel_shutr_now(chn);
4330 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004331 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332 }
4333 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004334 /* The last possible modes are keep-alive and tunnel. Since tunnel
4335 * mode does not set the body analyser, we can't reach this place
4336 * in tunnel mode, so we're left with keep-alive only.
4337 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004339 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004341 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 }
4343
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004344 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004346 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4348 goto http_msg_closing;
4349 }
4350 else {
4351 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4352 goto http_msg_closed;
4353 }
4354 }
4355 goto wait_other_side;
4356 }
4357
4358 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4359 http_msg_closing:
4360 /* nothing else to forward, just waiting for the output buffer
4361 * to be empty and for the shutw_now to take effect.
4362 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004363 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004364 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4365 goto http_msg_closed;
4366 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004367 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004368 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004369 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004370 if (objt_server(s->target))
4371 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372 goto wait_other_side;
4373 }
4374 }
4375
4376 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4377 http_msg_closed:
4378 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004379 bi_erase(chn);
4380 channel_auto_close(chn);
4381 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004382 goto wait_other_side;
4383 }
4384
4385 wait_other_side:
4386 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004387 /* We force the response to leave immediately if we're waiting for the
4388 * other side, since there is no pending shutdown to push it out.
4389 */
4390 if (!channel_is_empty(chn))
4391 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004392 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004393}
4394
4395
4396/* Resync the request and response state machines. Return 1 if either state
4397 * changes.
4398 */
4399int http_resync_states(struct session *s)
4400{
4401 struct http_txn *txn = &s->txn;
4402 int old_req_state = txn->req.msg_state;
4403 int old_res_state = txn->rsp.msg_state;
4404
4405 http_silent_debug(__LINE__, s);
4406 http_sync_req_state(s);
4407 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004408 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409 if (!http_sync_res_state(s))
4410 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004411 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412 if (!http_sync_req_state(s))
4413 break;
4414 }
4415 http_silent_debug(__LINE__, s);
4416 /* OK, both state machines agree on a compatible state.
4417 * There are a few cases we're interested in :
4418 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4419 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4420 * directions, so let's simply disable both analysers.
4421 * - HTTP_MSG_CLOSED on the response only means we must abort the
4422 * request.
4423 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4424 * with server-close mode means we've completed one request and we
4425 * must re-initialize the server connection.
4426 */
4427
4428 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4429 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4430 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4431 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4432 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004433 channel_auto_close(s->req);
4434 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004436 channel_auto_close(s->rep);
4437 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004439 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4440 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004441 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004442 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004443 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004444 channel_auto_close(s->rep);
4445 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004446 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004447 channel_abort(s->req);
4448 channel_auto_close(s->req);
4449 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004450 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 }
4452 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4453 txn->rsp.msg_state == HTTP_MSG_DONE &&
4454 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4455 /* server-close: terminate this server connection and
4456 * reinitialize a fresh-new transaction.
4457 */
4458 http_end_txn_clean_session(s);
4459 }
4460
4461 http_silent_debug(__LINE__, s);
4462 return txn->req.msg_state != old_req_state ||
4463 txn->rsp.msg_state != old_res_state;
4464}
4465
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466/* This function is an analyser which forwards request body (including chunk
4467 * sizes if any). It is called as soon as we must forward, even if we forward
4468 * zero byte. The only situation where it must not be called is when we're in
4469 * tunnel mode and we want to forward till the close. It's used both to forward
4470 * remaining data and to resync after end of body. It expects the msg_state to
4471 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4472 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004473 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004474 * bytes of pending data + the headers if not already done (between sol and sov).
4475 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004476 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004477int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004478{
4479 struct http_txn *txn = &s->txn;
4480 struct http_msg *msg = &s->txn.req;
4481
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004482 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4483 return 0;
4484
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004485 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004486 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004487 /* Output closed while we were sending data. We must abort and
4488 * wake the other side up.
4489 */
4490 msg->msg_state = HTTP_MSG_ERROR;
4491 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004492 return 1;
4493 }
4494
Willy Tarreau4fe41902010-06-07 22:27:41 +02004495 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004496 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004497
4498 /* Note that we don't have to send 100-continue back because we don't
4499 * need the data to complete our job, and it's up to the server to
4500 * decide whether to return 100, 417 or anything else in return of
4501 * an "Expect: 100-continue" header.
4502 */
4503
4504 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004505 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004506 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004507 * is still null. We must save the body in msg->next because it
4508 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004510 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004511
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004512 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004513 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004514 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004515 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004516 }
4517
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004519 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004520
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004522 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004523 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004524 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004525 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004526 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004527 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004528 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004529 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004530
Willy Tarreaucaabe412010-01-03 23:08:28 +01004531 if (msg->msg_state == HTTP_MSG_DATA) {
4532 /* must still forward */
4533 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004534 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004535
4536 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004537 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004538 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004539 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004540 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004541 }
4542 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004543 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004544 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004545 * TRAILERS state.
4546 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004547 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004548
Willy Tarreau54d23df2012-10-25 19:04:45 +02004549 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004550 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004551 else if (ret < 0) {
4552 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004553 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004554 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004555 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004556 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004557 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004558 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004559 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004560 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004561 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004562
4563 if (ret == 0)
4564 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004565 else if (ret < 0) {
4566 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004567 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004568 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004569 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004570 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004571 /* we're in MSG_CHUNK_SIZE now */
4572 }
4573 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004574 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004575
4576 if (ret == 0)
4577 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004578 else if (ret < 0) {
4579 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004580 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004581 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004582 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004583 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004584 /* we're in HTTP_MSG_DONE now */
4585 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004587 int old_state = msg->msg_state;
4588
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004590 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004591 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4592 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004593 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004594 if (http_resync_states(s)) {
4595 /* some state changes occurred, maybe the analyser
4596 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004597 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004598 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004599 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004600 /* request errors are most likely due to
4601 * the server aborting the transfer.
4602 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004603 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004604 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004605 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004606 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004607 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004608 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004609 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004610 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004611
4612 /* If "option abortonclose" is set on the backend, we
4613 * want to monitor the client's connection and forward
4614 * any shutdown notification to the server, which will
4615 * decide whether to close or to go on processing the
4616 * request.
4617 */
4618 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004619 channel_auto_read(req);
4620 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004621 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004622 else if (s->txn.meth == HTTP_METH_POST) {
4623 /* POST requests may require to read extra CRLF
4624 * sent by broken browsers and which could cause
4625 * an RST to be sent upon close on some systems
4626 * (eg: Linux).
4627 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004628 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004629 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004630
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004632 }
4633 }
4634
Willy Tarreaud98cf932009-12-27 22:54:55 +01004635 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004636 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004637 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004638 if (!(s->flags & SN_ERR_MASK))
4639 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004640 if (!(s->flags & SN_FINST_MASK)) {
4641 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4642 s->flags |= SN_FINST_H;
4643 else
4644 s->flags |= SN_FINST_D;
4645 }
4646
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004647 s->fe->fe_counters.cli_aborts++;
4648 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004649 if (objt_server(s->target))
4650 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004651
4652 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004653 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004654
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004655 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004656 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004657 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004659 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004660 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004661 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004662 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004663 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004664
Willy Tarreau5c620922011-05-11 19:56:11 +02004665 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004666 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004667 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004668 * modes are already handled by the stream sock layer. We must not do
4669 * this in content-length mode because it could present the MSG_MORE
4670 * flag with the last block of forwarded data, which would cause an
4671 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004672 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004673 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004674 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004675
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004677 return 0;
4678
Willy Tarreaud98cf932009-12-27 22:54:55 +01004679 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004680 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004681 if (s->listener->counters)
4682 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004683 return_bad_req_stats_ok:
4684 txn->req.msg_state = HTTP_MSG_ERROR;
4685 if (txn->status) {
4686 /* Note: we don't send any error if some data were already sent */
4687 stream_int_retnclose(req->prod, NULL);
4688 } else {
4689 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004690 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004691 }
4692 req->analysers = 0;
4693 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004694
4695 if (!(s->flags & SN_ERR_MASK))
4696 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004697 if (!(s->flags & SN_FINST_MASK)) {
4698 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4699 s->flags |= SN_FINST_H;
4700 else
4701 s->flags |= SN_FINST_D;
4702 }
4703 return 0;
4704
4705 aborted_xfer:
4706 txn->req.msg_state = HTTP_MSG_ERROR;
4707 if (txn->status) {
4708 /* Note: we don't send any error if some data were already sent */
4709 stream_int_retnclose(req->prod, NULL);
4710 } else {
4711 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004712 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004713 }
4714 req->analysers = 0;
4715 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4716
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004717 s->fe->fe_counters.srv_aborts++;
4718 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004719 if (objt_server(s->target))
4720 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004721
4722 if (!(s->flags & SN_ERR_MASK))
4723 s->flags |= SN_ERR_SRVCL;
4724 if (!(s->flags & SN_FINST_MASK)) {
4725 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4726 s->flags |= SN_FINST_H;
4727 else
4728 s->flags |= SN_FINST_D;
4729 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004730 return 0;
4731}
4732
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004733/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4734 * processing can continue on next analysers, or zero if it either needs more
4735 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4736 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4737 * when it has nothing left to do, and may remove any analyser when it wants to
4738 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004739 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004740int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004741{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004742 struct http_txn *txn = &s->txn;
4743 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004744 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004745 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004747 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004748
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004749 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 +02004750 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004752 rep,
4753 rep->rex, rep->wex,
4754 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004755 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004756 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004757
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 /*
4759 * Now parse the partial (or complete) lines.
4760 * We will check the response syntax, and also join multi-line
4761 * headers. An index of all the lines will be elaborated while
4762 * parsing.
4763 *
4764 * For the parsing, we use a 28 states FSM.
4765 *
4766 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004767 * rep->buf->p = beginning of response
4768 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4769 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004770 * msg->eol = end of current header or line (LF or CRLF)
4771 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 */
4773
Willy Tarreau83e3af02009-12-28 17:39:57 +01004774 /* There's a protected area at the end of the buffer for rewriting
4775 * purposes. We don't want to start to parse the request if the
4776 * protected area is affected, because we may have to move processed
4777 * data later, which is much more complicated.
4778 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004779 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004780 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004781 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4782 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4783 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004784 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004785 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004786 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004787 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004788 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004789 return 0;
4790 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004791 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4792 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004793 }
4794
Willy Tarreau9b28e032012-10-12 23:49:43 +02004795 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004796 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004797 }
4798
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799 /* 1: we might have to print this header in debug mode */
4800 if (unlikely((global.mode & MODE_DEBUG) &&
4801 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004802 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004804
Willy Tarreau9b28e032012-10-12 23:49:43 +02004805 sol = rep->buf->p;
4806 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004808
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 sol += hdr_idx_first_pos(&txn->hdr_idx);
4810 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004811
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004812 while (cur_idx) {
4813 eol = sol + txn->hdr_idx.v[cur_idx].len;
4814 debug_hdr("srvhdr", s, sol, eol);
4815 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4816 cur_idx = txn->hdr_idx.v[cur_idx].next;
4817 }
4818 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004819
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004820 /*
4821 * Now we quickly check if we have found a full valid response.
4822 * If not so, we check the FD and buffer states before leaving.
4823 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004824 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 * responses are checked first.
4826 *
4827 * Depending on whether the client is still there or not, we
4828 * may send an error response back or not. Note that normally
4829 * we should only check for HTTP status there, and check I/O
4830 * errors somewhere else.
4831 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004832
Willy Tarreau655dce92009-11-08 13:10:58 +01004833 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 /* Invalid response */
4835 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4836 /* we detected a parsing error. We want to archive this response
4837 * in the dedicated proxy area for later troubleshooting.
4838 */
4839 hdr_response_bad:
4840 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004841 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004843 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004844 if (objt_server(s->target)) {
4845 objt_server(s->target)->counters.failed_resp++;
4846 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004847 }
Willy Tarreau64648412010-03-05 10:41:54 +01004848 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004849 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 rep->analysers = 0;
4851 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004852 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004853 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004854 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855
4856 if (!(s->flags & SN_ERR_MASK))
4857 s->flags |= SN_ERR_PRXCOND;
4858 if (!(s->flags & SN_FINST_MASK))
4859 s->flags |= SN_FINST_H;
4860
4861 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004862 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004865 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004866 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004867 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868 goto hdr_response_bad;
4869 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004870
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004872 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004874 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004875
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004876 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004877 if (objt_server(s->target)) {
4878 objt_server(s->target)->counters.failed_resp++;
4879 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004880 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004881
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004882 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883 rep->analysers = 0;
4884 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004885 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004886 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004887 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004888
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889 if (!(s->flags & SN_ERR_MASK))
4890 s->flags |= SN_ERR_SRVCL;
4891 if (!(s->flags & SN_FINST_MASK))
4892 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004893 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004894 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004895
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004896 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004897 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004898 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004899 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004900
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004901 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004902 if (objt_server(s->target)) {
4903 objt_server(s->target)->counters.failed_resp++;
4904 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004905 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004906
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004907 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004908 rep->analysers = 0;
4909 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004910 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004911 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004912 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004914 if (!(s->flags & SN_ERR_MASK))
4915 s->flags |= SN_ERR_SRVTO;
4916 if (!(s->flags & SN_FINST_MASK))
4917 s->flags |= SN_FINST_H;
4918 return 0;
4919 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004920
Willy Tarreauf003d372012-11-26 13:35:37 +01004921 /* client abort with an abortonclose */
4922 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
4923 s->fe->fe_counters.cli_aborts++;
4924 s->be->be_counters.cli_aborts++;
4925 if (objt_server(s->target))
4926 objt_server(s->target)->counters.cli_aborts++;
4927
4928 rep->analysers = 0;
4929 channel_auto_close(rep);
4930
4931 txn->status = 400;
4932 bi_erase(rep);
4933 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
4934
4935 if (!(s->flags & SN_ERR_MASK))
4936 s->flags |= SN_ERR_CLICL;
4937 if (!(s->flags & SN_FINST_MASK))
4938 s->flags |= SN_FINST_H;
4939
4940 /* process_session() will take care of the error */
4941 return 0;
4942 }
4943
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004944 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004945 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004946 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004947 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004948
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004949 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004950 if (objt_server(s->target)) {
4951 objt_server(s->target)->counters.failed_resp++;
4952 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004953 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004954
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004955 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004956 rep->analysers = 0;
4957 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004958 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004959 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004960 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004961
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004962 if (!(s->flags & SN_ERR_MASK))
4963 s->flags |= SN_ERR_SRVCL;
4964 if (!(s->flags & SN_FINST_MASK))
4965 s->flags |= SN_FINST_H;
4966 return 0;
4967 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004969 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004970 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004971 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004972 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004973
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004974 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004975 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004976 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004977
4978 if (!(s->flags & SN_ERR_MASK))
4979 s->flags |= SN_ERR_CLICL;
4980 if (!(s->flags & SN_FINST_MASK))
4981 s->flags |= SN_FINST_H;
4982
4983 /* process_session() will take care of the error */
4984 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004985 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004986
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004987 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004988 return 0;
4989 }
4990
4991 /* More interesting part now : we know that we have a complete
4992 * response which at least looks like HTTP. We have an indicator
4993 * of each header's length, so we can parse them quickly.
4994 */
4995
4996 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004997 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004998
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 /*
5000 * 1: get the status code
5001 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005002 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005003 if (n < 1 || n > 5)
5004 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005005 /* when the client triggers a 4xx from the server, it's most often due
5006 * to a missing object or permission. These events should be tracked
5007 * because if they happen often, it may indicate a brute force or a
5008 * vulnerability scan.
5009 */
5010 if (n == 4)
5011 session_inc_http_err_ctr(s);
5012
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005013 if (objt_server(s->target))
5014 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015
Willy Tarreau5b154472009-12-21 20:11:07 +01005016 /* check if the response is HTTP/1.1 or above */
5017 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005018 ((rep->buf->p[5] > '1') ||
5019 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005020 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005021
5022 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005023 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005024
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005025 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005026 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005027
Willy Tarreau9b28e032012-10-12 23:49:43 +02005028 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005029
Willy Tarreau39650402010-03-15 19:44:39 +01005030 /* Adjust server's health based on status code. Note: status codes 501
5031 * and 505 are triggered on demand by client request, so we must not
5032 * count them as server failures.
5033 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005034 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005035 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005036 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005037 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005038 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005039 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 /*
5042 * 2: check for cacheability.
5043 */
5044
5045 switch (txn->status) {
5046 case 200:
5047 case 203:
5048 case 206:
5049 case 300:
5050 case 301:
5051 case 410:
5052 /* RFC2616 @13.4:
5053 * "A response received with a status code of
5054 * 200, 203, 206, 300, 301 or 410 MAY be stored
5055 * by a cache (...) unless a cache-control
5056 * directive prohibits caching."
5057 *
5058 * RFC2616 @9.5: POST method :
5059 * "Responses to this method are not cacheable,
5060 * unless the response includes appropriate
5061 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005062 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005064 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5066 break;
5067 default:
5068 break;
5069 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005070
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005071 /*
5072 * 3: we may need to capture headers
5073 */
5074 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005075 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005076 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 txn->rsp.cap, s->fe->rsp_cap);
5078
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005079 /* 4: determine the transfer-length.
5080 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5081 * the presence of a message-body in a RESPONSE and its transfer length
5082 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005083 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005084 * All responses to the HEAD request method MUST NOT include a
5085 * message-body, even though the presence of entity-header fields
5086 * might lead one to believe they do. All 1xx (informational), 204
5087 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5088 * message-body. All other responses do include a message-body,
5089 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005090 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005091 * 1. Any response which "MUST NOT" include a message-body (such as the
5092 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5093 * always terminated by the first empty line after the header fields,
5094 * regardless of the entity-header fields present in the message.
5095 *
5096 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5097 * the "chunked" transfer-coding (Section 6.2) is used, the
5098 * transfer-length is defined by the use of this transfer-coding.
5099 * If a Transfer-Encoding header field is present and the "chunked"
5100 * transfer-coding is not present, the transfer-length is defined by
5101 * the sender closing the connection.
5102 *
5103 * 3. If a Content-Length header field is present, its decimal value in
5104 * OCTETs represents both the entity-length and the transfer-length.
5105 * If a message is received with both a Transfer-Encoding header
5106 * field and a Content-Length header field, the latter MUST be ignored.
5107 *
5108 * 4. If the message uses the media type "multipart/byteranges", and
5109 * the transfer-length is not otherwise specified, then this self-
5110 * delimiting media type defines the transfer-length. This media
5111 * type MUST NOT be used unless the sender knows that the recipient
5112 * can parse it; the presence in a request of a Range header with
5113 * multiple byte-range specifiers from a 1.1 client implies that the
5114 * client can parse multipart/byteranges responses.
5115 *
5116 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005117 */
5118
5119 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005120 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005121 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005122 * FIXME: should we parse anyway and return an error on chunked encoding ?
5123 */
5124 if (txn->meth == HTTP_METH_HEAD ||
5125 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005126 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005127 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005128 goto skip_content_length;
5129 }
5130
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005131 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005132 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005133 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005134 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005135 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005136 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5137 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005138 /* bad transfer-encoding (chunked followed by something else) */
5139 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005140 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005141 break;
5142 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005143 }
5144
5145 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5146 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005147 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005148 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005149 signed long long cl;
5150
Willy Tarreauad14f752011-09-02 20:33:27 +02005151 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005152 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005153 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005154 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005155
Willy Tarreauad14f752011-09-02 20:33:27 +02005156 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005157 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005158 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005159 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005160
Willy Tarreauad14f752011-09-02 20:33:27 +02005161 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005162 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005163 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005164 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005165
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005166 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005167 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005168 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005169 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005170
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005171 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005172 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005173 }
5174
William Lallemand82fe75c2012-10-23 10:25:10 +02005175 if (s->fe->comp || s->be->comp)
5176 select_compression_response_header(s, rep->buf);
5177
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005178 /* FIXME: we should also implement the multipart/byterange method.
5179 * For now on, we resort to close mode in this case (unknown length).
5180 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005181skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005182
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183 /* end of job, return OK */
5184 rep->analysers &= ~an_bit;
5185 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005186 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005187 return 1;
5188}
5189
5190/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005191 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5192 * and updates t->rep->analysers. It might make sense to explode it into several
5193 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005195int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005196{
5197 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 struct http_msg *msg = &txn->rsp;
5199 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005200 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005201
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005202 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 +02005203 now_ms, __FUNCTION__,
5204 t,
5205 rep,
5206 rep->rex, rep->wex,
5207 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005208 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 rep->analysers);
5210
Willy Tarreau655dce92009-11-08 13:10:58 +01005211 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 return 0;
5213
5214 rep->analysers &= ~an_bit;
5215 rep->analyse_exp = TICK_ETERNITY;
5216
Willy Tarreau5b154472009-12-21 20:11:07 +01005217 /* Now we have to check if we need to modify the Connection header.
5218 * This is more difficult on the response than it is on the request,
5219 * because we can have two different HTTP versions and we don't know
5220 * how the client will interprete a response. For instance, let's say
5221 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5222 * HTTP/1.1 response without any header. Maybe it will bound itself to
5223 * HTTP/1.0 because it only knows about it, and will consider the lack
5224 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5225 * the lack of header as a keep-alive. Thus we will use two flags
5226 * indicating how a request MAY be understood by the client. In case
5227 * of multiple possibilities, we'll fix the header to be explicit. If
5228 * ambiguous cases such as both close and keepalive are seen, then we
5229 * will fall back to explicit close. Note that we won't take risks with
5230 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005231 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005232 */
5233
Willy Tarreaudc008c52010-02-01 16:20:08 +01005234 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5235 txn->status == 101)) {
5236 /* Either we've established an explicit tunnel, or we're
5237 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005238 * to understand the next protocols. We have to switch to tunnel
5239 * mode, so that we transfer the request and responses then let
5240 * this protocol pass unmodified. When we later implement specific
5241 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005242 * header which contains information about that protocol for
5243 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005244 */
5245 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5246 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005247 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5248 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5249 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005250 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005251
Willy Tarreau60466522010-01-18 19:08:45 +01005252 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005253 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005254 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5255 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005256
Willy Tarreau60466522010-01-18 19:08:45 +01005257 /* now adjust header transformations depending on current state */
5258 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5259 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5260 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005261 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005262 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005263 }
Willy Tarreau60466522010-01-18 19:08:45 +01005264 else { /* SCL / KAL */
5265 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005266 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005267 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005268 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005269
Willy Tarreau60466522010-01-18 19:08:45 +01005270 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005271 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005272
Willy Tarreau60466522010-01-18 19:08:45 +01005273 /* Some keep-alive responses are converted to Server-close if
5274 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005275 */
Willy Tarreau60466522010-01-18 19:08:45 +01005276 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5277 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005278 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005279 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005280 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005281 }
5282
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005283 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005284 /*
5285 * 3: we will have to evaluate the filters.
5286 * As opposed to version 1.2, now they will be evaluated in the
5287 * filters order and not in the header order. This means that
5288 * each filter has to be validated among all headers.
5289 *
5290 * Filters are tried with ->be first, then with ->fe if it is
5291 * different from ->be.
5292 */
5293
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005294 cur_proxy = t->be;
5295 while (1) {
5296 struct proxy *rule_set = cur_proxy;
5297
5298 /* try headers filters */
5299 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005300 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005301 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005302 if (objt_server(t->target)) {
5303 objt_server(t->target)->counters.failed_resp++;
5304 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005305 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005306 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005308 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005309 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005310 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005311 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005312 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005313 if (!(t->flags & SN_ERR_MASK))
5314 t->flags |= SN_ERR_PRXCOND;
5315 if (!(t->flags & SN_FINST_MASK))
5316 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005317 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005318 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005320
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005321 /* has the response been denied ? */
5322 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005323 if (objt_server(t->target))
5324 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005325
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005326 t->be->be_counters.denied_resp++;
5327 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005328 if (t->listener->counters)
5329 t->listener->counters->denied_resp++;
5330
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005331 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005332 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005333
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005334 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005335 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005336 if (txn->status < 200)
5337 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005338 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005339 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005340 ret = acl_pass(ret);
5341 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5342 ret = !ret;
5343 if (!ret)
5344 continue;
5345 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005346 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005347 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005348 }
5349
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005350 /* check whether we're already working on the frontend */
5351 if (cur_proxy == t->fe)
5352 break;
5353 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005354 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005355
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005356 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005357 * We may be facing a 100-continue response, in which case this
5358 * is not the right response, and we're waiting for the next one.
5359 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005360 * next one.
5361 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005362 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005363 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005364 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005365 msg->msg_state = HTTP_MSG_RPBEFORE;
5366 txn->status = 0;
5367 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5368 return 1;
5369 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005370 else if (unlikely(txn->status < 200))
5371 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005372
5373 /* we don't have any 1xx status code now */
5374
5375 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005376 * 4: check for server cookie.
5377 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005378 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5379 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005381
Willy Tarreaubaaee002006-06-26 02:48:02 +02005382
Willy Tarreaua15645d2007-03-18 16:22:39 +01005383 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005384 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005385 */
Willy Tarreau67402132012-05-31 20:40:20 +02005386 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005387 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005388
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005389 /*
5390 * 6: add server cookie in the response if needed
5391 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005392 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005393 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005394 (!(t->flags & SN_DIRECT) ||
5395 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5396 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5397 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5398 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005399 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005400 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005401 /* the server is known, it's not the one the client requested, or the
5402 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005403 * insert a set-cookie here, except if we want to insert only on POST
5404 * requests and this one isn't. Note that servers which don't have cookies
5405 * (eg: some backup servers) will return a full cookie removal request.
5406 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005407 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005408 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005409 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5410 t->be->cookie_name);
5411 }
5412 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005413 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005414
5415 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5416 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005417 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5418 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5419 trash.len += 5;
5420
Willy Tarreauef4f3912010-10-07 21:00:29 +02005421 if (t->be->cookie_maxlife) {
5422 /* emit first_date, which is either the original one or
5423 * the current date.
5424 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005425 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005426 s30tob64(txn->cookie_first_date ?
5427 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005428 (date.tv_sec+3) >> 2, trash.str + trash.len);
5429 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005430 }
5431 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005432 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005433 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005434
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005436 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005437
Willy Tarreau4992dd22012-05-31 21:02:17 +02005438 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005439 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005440
5441 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005442 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005443
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005444 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005446
Willy Tarreauf1348312010-10-07 15:54:11 +02005447 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005448 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005449 /* the server did not change, only the date was updated */
5450 txn->flags |= TX_SCK_UPDATED;
5451 else
5452 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005453
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005454 /* Here, we will tell an eventual cache on the client side that we don't
5455 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5456 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5457 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5458 */
Willy Tarreau67402132012-05-31 20:40:20 +02005459 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005460
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005461 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5462
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005463 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005464 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005466 }
5467 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005468
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005469 /*
5470 * 7: check if result will be cacheable with a cookie.
5471 * We'll block the response if security checks have caught
5472 * nasty things such as a cacheable cookie.
5473 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005474 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5475 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005476 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005477
5478 /* we're in presence of a cacheable response containing
5479 * a set-cookie header. We'll block it as requested by
5480 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005481 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005482 if (objt_server(t->target))
5483 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005484
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005485 t->be->be_counters.denied_resp++;
5486 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005487 if (t->listener->counters)
5488 t->listener->counters->denied_resp++;
5489
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005490 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005491 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005492 send_log(t->be, LOG_ALERT,
5493 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005494 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005495 goto return_srv_prx_502;
5496 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497
5498 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005499 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005500 * If an "Upgrade" token is found, the header is left untouched in order
5501 * not to have to deal with some client bugs : some of them fail an upgrade
5502 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005503 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005504 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5505 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5506 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005507 unsigned int want_flags = 0;
5508
5509 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5510 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5511 /* we want a keep-alive response here. Keep-alive header
5512 * required if either side is not 1.1.
5513 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005514 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005515 want_flags |= TX_CON_KAL_SET;
5516 }
5517 else {
5518 /* we want a close response here. Close header required if
5519 * the server is 1.1, regardless of the client.
5520 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005521 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005522 want_flags |= TX_CON_CLO_SET;
5523 }
5524
5525 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005526 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005527 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005528
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005529 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005530 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005531 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005533
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005534 /*************************************************************
5535 * OK, that's finished for the headers. We have done what we *
5536 * could. Let's switch to the DATA state. *
5537 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005538
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005539 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 /* if the user wants to log as soon as possible, without counting
5542 * bytes from the server, then this is the right moment. We have
5543 * to temporarily assign bytes_out to log what we currently have.
5544 */
5545 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5546 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5547 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005548 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005549 t->logs.bytes_out = 0;
5550 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005551
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005552 /* Note: we must not try to cheat by jumping directly to DATA,
5553 * otherwise we would not let the client side wake up.
5554 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005555
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005556 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005557 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005558 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005559}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005560
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561/* This function is an analyser which forwards response body (including chunk
5562 * sizes if any). It is called as soon as we must forward, even if we forward
5563 * zero byte. The only situation where it must not be called is when we're in
5564 * tunnel mode and we want to forward till the close. It's used both to forward
5565 * remaining data and to resync after end of body. It expects the msg_state to
5566 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5567 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005568 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005569 * bytes of pending data + the headers if not already done (between sol and sov).
5570 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005572int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573{
5574 struct http_txn *txn = &s->txn;
5575 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005576 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005577 static struct buffer *tmpbuf = NULL;
5578 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005579 int consumed_data = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005580
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005581 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5582 return 0;
5583
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005584 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005585 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005586 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005587 /* Output closed while we were sending data. We must abort and
5588 * wake the other side up.
5589 */
5590 msg->msg_state = HTTP_MSG_ERROR;
5591 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005592 return 1;
5593 }
5594
Willy Tarreau4fe41902010-06-07 22:27:41 +02005595 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005596 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005597
William Lallemand82fe75c2012-10-23 10:25:10 +02005598 /* this is the first time we need the compression buffer */
5599 if (s->comp_algo != NULL && tmpbuf == NULL) {
5600 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5601 goto aborted_xfer; /* no memory */
5602 }
5603
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005605 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005606 * rep->buf.p still points to the beginning of the message and msg->sol
5607 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005609 channel_forward(res, msg->sov);
5610 msg->next = 0;
5611 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005612
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005613 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005614 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005615 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005616 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005617 }
5618
William Lallemand82fe75c2012-10-23 10:25:10 +02005619 if (s->comp_algo != NULL) {
5620 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5621 if (ret < 0)
5622 goto missing_data; /* not enough spaces in buffers */
5623 compressing = 1;
5624 }
5625
Willy Tarreaud98cf932009-12-27 22:54:55 +01005626 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005628 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005629 if (s->comp_algo == NULL) {
5630 bytes = msg->sov - msg->sol;
5631 if (msg->chunk_len || bytes) {
5632 msg->sol = msg->sov;
5633 msg->next -= bytes; /* will be forwarded */
5634 msg->chunk_len += bytes;
5635 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5636 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005637 }
5638
Willy Tarreaucaabe412010-01-03 23:08:28 +01005639 if (msg->msg_state == HTTP_MSG_DATA) {
William Lallemandbf3ae612012-11-19 12:35:37 +01005640 int ret;
5641
Willy Tarreaucaabe412010-01-03 23:08:28 +01005642 /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005643 if (compressing) {
5644 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5645 if (ret < 0)
5646 goto aborted_xfer;
5647 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005648
5649 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005650 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005651
5652 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005653 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005654 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005655 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005656 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005657 if (compressing && consumed_data) {
5658 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5659 compressing = 0;
5660 }
5661 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005662 }
5663 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005664 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005665 * set ->sov and ->next to point to the body and switch to DATA or
5666 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005668 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669
Willy Tarreau54d23df2012-10-25 19:04:45 +02005670 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005672 else if (ret < 0) {
5673 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005674 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005676 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005677 if (compressing) {
5678 if (likely(msg->chunk_len > 0)) {
5679 /* skipping data if we are in compression mode */
5680 b_adv(res->buf, msg->next);
5681 msg->next = 0;
5682 msg->sov = 0;
5683 msg->sol = 0;
5684 } else {
5685 if (consumed_data) {
5686 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5687 compressing = 0;
5688 }
5689 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005690 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005691 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005693 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005694 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005695 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005696
Willy Tarreau54d23df2012-10-25 19:04:45 +02005697 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005698 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005699 else if (ret < 0) {
5700 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005701 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005702 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005703 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005704 /* skipping data in buffer for compression */
5705 if (compressing) {
5706 b_adv(res->buf, msg->next);
5707 msg->next = 0;
5708 msg->sov = 0;
5709 msg->sol = 0;
5710 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005711 /* we're in MSG_CHUNK_SIZE now */
5712 }
5713 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005714 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005715
Willy Tarreaud98cf932009-12-27 22:54:55 +01005716 if (ret == 0)
5717 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005718 else if (ret < 0) {
5719 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005720 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005721 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005722 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005723 if (s->comp_algo != NULL) {
5724 /* forwarding trailers */
5725 channel_forward(res, msg->next);
5726 msg->next = 0;
5727 }
5728 /* we're in HTTP_MSG_DONE now */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005729 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005730 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005731 int old_state = msg->msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005732 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005733 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005734 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5735 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005736 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005737 if (http_resync_states(s)) {
5738 http_silent_debug(__LINE__, s);
5739 /* some state changes occurred, maybe the analyser
5740 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005741 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005742 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005743 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005744 /* response errors are most likely due to
5745 * the client aborting the transfer.
5746 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005747 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005748 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005749 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005750 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005751 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005752 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005753 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005754 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005755 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 }
5757 }
5758
Willy Tarreaud98cf932009-12-27 22:54:55 +01005759 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005760 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005761 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5762 compressing = 0;
5763 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005764
5765 if (res->flags & CF_SHUTW)
5766 goto aborted_xfer;
5767
5768 /* stop waiting for data if the input is closed before the end. If the
5769 * client side was already closed, it means that the client has aborted,
5770 * so we don't want to count this as a server abort. Otherwise it's a
5771 * server abort.
5772 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005773 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005774 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5775 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005776 if (!(s->flags & SN_ERR_MASK))
5777 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005778 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005779 if (objt_server(s->target))
5780 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005781 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005782 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005783
Willy Tarreau40dba092010-03-04 18:14:51 +01005784 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005785 if (!s->req->analysers)
5786 goto return_bad_res;
5787
Willy Tarreauea953162012-05-18 23:41:28 +02005788 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005789 if (s->comp_algo == NULL) {
5790 bytes = msg->sov - msg->sol;
5791 if (msg->chunk_len || bytes) {
5792 msg->sol = msg->sov;
5793 msg->next -= bytes; /* will be forwarded */
5794 msg->chunk_len += bytes;
5795 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5796 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005797 }
5798
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005799 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005800 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005801 * Similarly, with keep-alive on the client side, we don't want to forward a
5802 * close.
5803 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005804 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005805 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5806 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005807 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005808
Willy Tarreau5c620922011-05-11 19:56:11 +02005809 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005810 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005811 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005812 * modes are already handled by the stream sock layer. We must not do
5813 * this in content-length mode because it could present the MSG_MORE
5814 * flag with the last block of forwarded data, which would cause an
5815 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005816 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005817 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005818 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005819
Willy Tarreaud98cf932009-12-27 22:54:55 +01005820 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005821 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005822 return 0;
5823
Willy Tarreau40dba092010-03-04 18:14:51 +01005824 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005825 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005826 if (objt_server(s->target))
5827 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005828
5829 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005830 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005831 /* don't send any error message as we're in the body */
5832 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005833 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005834 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005835 if (objt_server(s->target))
5836 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005837
5838 if (!(s->flags & SN_ERR_MASK))
5839 s->flags |= SN_ERR_PRXCOND;
5840 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005841 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005842 return 0;
5843
5844 aborted_xfer:
5845 txn->rsp.msg_state = HTTP_MSG_ERROR;
5846 /* don't send any error message as we're in the body */
5847 stream_int_retnclose(res->cons, NULL);
5848 res->analysers = 0;
5849 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5850
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005851 s->fe->fe_counters.cli_aborts++;
5852 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005853 if (objt_server(s->target))
5854 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855
5856 if (!(s->flags & SN_ERR_MASK))
5857 s->flags |= SN_ERR_CLICL;
5858 if (!(s->flags & SN_FINST_MASK))
5859 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005860 return 0;
5861}
5862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005863/* Iterate the same filter through all request headers.
5864 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865 * Since it can manage the switch to another backend, it updates the per-proxy
5866 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005867 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005868int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005869{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005870 char term;
5871 char *cur_ptr, *cur_end, *cur_next;
5872 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005873 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005875 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005876
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005877 last_hdr = 0;
5878
Willy Tarreau9b28e032012-10-12 23:49:43 +02005879 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880 old_idx = 0;
5881
5882 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005883 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005884 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005885 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005886 (exp->action == ACT_ALLOW ||
5887 exp->action == ACT_DENY ||
5888 exp->action == ACT_TARPIT))
5889 return 0;
5890
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005891 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005892 if (!cur_idx)
5893 break;
5894
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005895 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896 cur_ptr = cur_next;
5897 cur_end = cur_ptr + cur_hdr->len;
5898 cur_next = cur_end + cur_hdr->cr + 1;
5899
5900 /* Now we have one header between cur_ptr and cur_end,
5901 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902 */
5903
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 /* The annoying part is that pattern matching needs
5905 * that we modify the contents to null-terminate all
5906 * strings before testing them.
5907 */
5908
5909 term = *cur_end;
5910 *cur_end = '\0';
5911
5912 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5913 switch (exp->action) {
5914 case ACT_SETBE:
5915 /* It is not possible to jump a second time.
5916 * FIXME: should we return an HTTP/500 here so that
5917 * the admin knows there's a problem ?
5918 */
5919 if (t->be != t->fe)
5920 break;
5921
5922 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005923 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 last_hdr = 1;
5925 break;
5926
5927 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 last_hdr = 1;
5930 break;
5931
5932 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005933 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005935
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005936 t->fe->fe_counters.denied_req++;
5937 if (t->fe != t->be)
5938 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005939 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005940 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 break;
5943
5944 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005945 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005947
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005948 t->fe->fe_counters.denied_req++;
5949 if (t->fe != t->be)
5950 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005951 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005952 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005954 break;
5955
5956 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005957 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
5958 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 /* FIXME: if the user adds a newline in the replacement, the
5960 * index will not be recalculated for now, and the new line
5961 * will not be counted as a new header.
5962 */
5963
5964 cur_end += delta;
5965 cur_next += delta;
5966 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005967 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 break;
5969
5970 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005971 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005972 cur_next += delta;
5973
Willy Tarreaufa355d42009-11-29 18:12:29 +01005974 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005975 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5976 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005977 cur_hdr->len = 0;
5978 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005979 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 break;
5981
5982 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005983 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 if (cur_end)
5985 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 /* keep the link from this header to next one in case of later
5988 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005989 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 old_idx = cur_idx;
5991 }
5992 return 0;
5993}
5994
5995
5996/* Apply the filter to the request line.
5997 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5998 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999 * Since it can manage the switch to another backend, it updates the per-proxy
6000 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006001 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006002int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003{
6004 char term;
6005 char *cur_ptr, *cur_end;
6006 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006007 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006008 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009
Willy Tarreau3d300592007-03-18 18:34:41 +01006010 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006011 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006012 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 (exp->action == ACT_ALLOW ||
6014 exp->action == ACT_DENY ||
6015 exp->action == ACT_TARPIT))
6016 return 0;
6017 else if (exp->action == ACT_REMOVE)
6018 return 0;
6019
6020 done = 0;
6021
Willy Tarreau9b28e032012-10-12 23:49:43 +02006022 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006023 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006024
6025 /* Now we have the request line between cur_ptr and cur_end */
6026
6027 /* The annoying part is that pattern matching needs
6028 * that we modify the contents to null-terminate all
6029 * strings before testing them.
6030 */
6031
6032 term = *cur_end;
6033 *cur_end = '\0';
6034
6035 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6036 switch (exp->action) {
6037 case ACT_SETBE:
6038 /* It is not possible to jump a second time.
6039 * FIXME: should we return an HTTP/500 here so that
6040 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 if (t->be != t->fe)
6043 break;
6044
6045 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006046 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006047 done = 1;
6048 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006051 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 done = 1;
6053 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006056 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006057
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006058 t->fe->fe_counters.denied_req++;
6059 if (t->fe != t->be)
6060 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006061 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006062 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 done = 1;
6065 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006067 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006068 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006069
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006070 t->fe->fe_counters.denied_req++;
6071 if (t->fe != t->be)
6072 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006073 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006074 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006076 done = 1;
6077 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 case ACT_REPLACE:
6080 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006081 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6082 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 /* FIXME: if the user adds a newline in the replacement, the
6084 * index will not be recalculated for now, and the new line
6085 * will not be counted as a new header.
6086 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006087
Willy Tarreaufa355d42009-11-29 18:12:29 +01006088 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006090 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 HTTP_MSG_RQMETH,
6092 cur_ptr, cur_end + 1,
6093 NULL, NULL);
6094 if (unlikely(!cur_end))
6095 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006097 /* we have a full request and we know that we have either a CR
6098 * or an LF at <ptr>.
6099 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006100 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6101 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006102 /* there is no point trying this regex on headers */
6103 return 1;
6104 }
6105 }
6106 *cur_end = term; /* restore the string terminator */
6107 return done;
6108}
Willy Tarreau97de6242006-12-27 17:18:38 +01006109
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006112/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006113 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006115 * unparsable request. Since it can manage the switch to another backend, it
6116 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006117 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006118int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006120 struct http_txn *txn = &s->txn;
6121 struct hdr_exp *exp;
6122
6123 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126 /*
6127 * The interleaving of transformations and verdicts
6128 * makes it difficult to decide to continue or stop
6129 * the evaluation.
6130 */
6131
Willy Tarreau6c123b12010-01-28 20:22:06 +01006132 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6133 break;
6134
Willy Tarreau3d300592007-03-18 18:34:41 +01006135 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006137 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006139
6140 /* if this filter had a condition, evaluate it now and skip to
6141 * next filter if the condition does not match.
6142 */
6143 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006144 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006145 ret = acl_pass(ret);
6146 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6147 ret = !ret;
6148
6149 if (!ret)
6150 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 }
6152
6153 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006154 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 if (unlikely(ret < 0))
6156 return -1;
6157
6158 if (likely(ret == 0)) {
6159 /* The filter did not match the request, it can be
6160 * iterated through all headers.
6161 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006162 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006165 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006166}
6167
6168
Willy Tarreaua15645d2007-03-18 16:22:39 +01006169
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006171 * Try to retrieve the server associated to the appsession.
6172 * If the server is found, it's assigned to the session.
6173 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006174void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006175 struct http_txn *txn = &t->txn;
6176 appsess *asession = NULL;
6177 char *sessid_temp = NULL;
6178
Cyril Bontéb21570a2009-11-29 20:04:48 +01006179 if (len > t->be->appsession_len) {
6180 len = t->be->appsession_len;
6181 }
6182
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006183 if (t->be->options2 & PR_O2_AS_REQL) {
6184 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006185 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006186 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006187 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006188 }
6189
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006190 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006191 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6192 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6193 return;
6194 }
6195
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006196 memcpy(txn->sessid, buf, len);
6197 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006198 }
6199
6200 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6201 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6202 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6203 return;
6204 }
6205
Cyril Bontéb21570a2009-11-29 20:04:48 +01006206 memcpy(sessid_temp, buf, len);
6207 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006208
6209 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6210 /* free previously allocated memory */
6211 pool_free2(apools.sessid, sessid_temp);
6212
6213 if (asession != NULL) {
6214 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6215 if (!(t->be->options2 & PR_O2_AS_REQL))
6216 asession->request_count++;
6217
6218 if (asession->serverid != NULL) {
6219 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006220
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006221 while (srv) {
6222 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006223 if ((srv->state & SRV_RUNNING) ||
6224 (t->be->options & PR_O_PERSIST) ||
6225 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006226 /* we found the server and it's usable */
6227 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006228 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006229 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006230 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006231
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006232 break;
6233 } else {
6234 txn->flags &= ~TX_CK_MASK;
6235 txn->flags |= TX_CK_DOWN;
6236 }
6237 }
6238 srv = srv->next;
6239 }
6240 }
6241 }
6242}
6243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244/* Find the end of a cookie value contained between <s> and <e>. It works the
6245 * same way as with headers above except that the semi-colon also ends a token.
6246 * See RFC2965 for more information. Note that it requires a valid header to
6247 * return a valid result.
6248 */
6249char *find_cookie_value_end(char *s, const char *e)
6250{
6251 int quoted, qdpair;
6252
6253 quoted = qdpair = 0;
6254 for (; s < e; s++) {
6255 if (qdpair) qdpair = 0;
6256 else if (quoted) {
6257 if (*s == '\\') qdpair = 1;
6258 else if (*s == '"') quoted = 0;
6259 }
6260 else if (*s == '"') quoted = 1;
6261 else if (*s == ',' || *s == ';') return s;
6262 }
6263 return s;
6264}
6265
6266/* Delete a value in a header between delimiters <from> and <next> in buffer
6267 * <buf>. The number of characters displaced is returned, and the pointer to
6268 * the first delimiter is updated if required. The function tries as much as
6269 * possible to respect the following principles :
6270 * - replace <from> delimiter by the <next> one unless <from> points to a
6271 * colon, in which case <next> is simply removed
6272 * - set exactly one space character after the new first delimiter, unless
6273 * there are not enough characters in the block being moved to do so.
6274 * - remove unneeded spaces before the previous delimiter and after the new
6275 * one.
6276 *
6277 * It is the caller's responsibility to ensure that :
6278 * - <from> points to a valid delimiter or the colon ;
6279 * - <next> points to a valid delimiter or the final CR/LF ;
6280 * - there are non-space chars before <from> ;
6281 * - there is a CR/LF at or after <next>.
6282 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006283int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284{
6285 char *prev = *from;
6286
6287 if (*prev == ':') {
6288 /* We're removing the first value, preserve the colon and add a
6289 * space if possible.
6290 */
6291 if (!http_is_crlf[(unsigned char)*next])
6292 next++;
6293 prev++;
6294 if (prev < next)
6295 *prev++ = ' ';
6296
6297 while (http_is_spht[(unsigned char)*next])
6298 next++;
6299 } else {
6300 /* Remove useless spaces before the old delimiter. */
6301 while (http_is_spht[(unsigned char)*(prev-1)])
6302 prev--;
6303 *from = prev;
6304
6305 /* copy the delimiter and if possible a space if we're
6306 * not at the end of the line.
6307 */
6308 if (!http_is_crlf[(unsigned char)*next]) {
6309 *prev++ = *next++;
6310 if (prev + 1 < next)
6311 *prev++ = ' ';
6312 while (http_is_spht[(unsigned char)*next])
6313 next++;
6314 }
6315 }
6316 return buffer_replace2(buf, prev, next, NULL, 0);
6317}
6318
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006319/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006320 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 * desirable to call it only when needed. This code is quite complex because
6322 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6323 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006324 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006325void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006327 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006329 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6331 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006334 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006335 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006337 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006339 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006341 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 hdr_beg = hdr_next;
6343 hdr_end = hdr_beg + cur_hdr->len;
6344 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 /* We have one full header between hdr_beg and hdr_end, and the
6347 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 * "Cookie:" headers.
6349 */
6350
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006352 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353 old_idx = cur_idx;
6354 continue;
6355 }
6356
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357 del_from = NULL; /* nothing to be deleted */
6358 preserve_hdr = 0; /* assume we may kill the whole header */
6359
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360 /* Now look for cookies. Conforming to RFC2109, we have to support
6361 * attributes whose name begin with a '$', and associate them with
6362 * the right cookie, if we want to delete this cookie.
6363 * So there are 3 cases for each cookie read :
6364 * 1) it's a special attribute, beginning with a '$' : ignore it.
6365 * 2) it's a server id cookie that we *MAY* want to delete : save
6366 * some pointers on it (last semi-colon, beginning of cookie...)
6367 * 3) it's an application cookie : we *MAY* have to delete a previous
6368 * "special" cookie.
6369 * At the end of loop, if a "special" cookie remains, we may have to
6370 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006371 * *MUST* delete it.
6372 *
6373 * Note: RFC2965 is unclear about the processing of spaces around
6374 * the equal sign in the ATTR=VALUE form. A careful inspection of
6375 * the RFC explicitly allows spaces before it, and not within the
6376 * tokens (attrs or values). An inspection of RFC2109 allows that
6377 * too but section 10.1.3 lets one think that spaces may be allowed
6378 * after the equal sign too, resulting in some (rare) buggy
6379 * implementations trying to do that. So let's do what servers do.
6380 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6381 * allowed quoted strings in values, with any possible character
6382 * after a backslash, including control chars and delimitors, which
6383 * causes parsing to become ambiguous. Browsers also allow spaces
6384 * within values even without quotes.
6385 *
6386 * We have to keep multiple pointers in order to support cookie
6387 * removal at the beginning, middle or end of header without
6388 * corrupting the header. All of these headers are valid :
6389 *
6390 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6391 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6392 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6393 * | | | | | | | | |
6394 * | | | | | | | | hdr_end <--+
6395 * | | | | | | | +--> next
6396 * | | | | | | +----> val_end
6397 * | | | | | +-----------> val_beg
6398 * | | | | +--------------> equal
6399 * | | | +----------------> att_end
6400 * | | +---------------------> att_beg
6401 * | +--------------------------> prev
6402 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403 */
6404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6406 /* Iterate through all cookies on this line */
6407
6408 /* find att_beg */
6409 att_beg = prev + 1;
6410 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6411 att_beg++;
6412
6413 /* find att_end : this is the first character after the last non
6414 * space before the equal. It may be equal to hdr_end.
6415 */
6416 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 while (equal < hdr_end) {
6419 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006421 if (http_is_spht[(unsigned char)*equal++])
6422 continue;
6423 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424 }
6425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6427 * is between <att_beg> and <equal>, both may be identical.
6428 */
6429
6430 /* look for end of cookie if there is an equal sign */
6431 if (equal < hdr_end && *equal == '=') {
6432 /* look for the beginning of the value */
6433 val_beg = equal + 1;
6434 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6435 val_beg++;
6436
6437 /* find the end of the value, respecting quotes */
6438 next = find_cookie_value_end(val_beg, hdr_end);
6439
6440 /* make val_end point to the first white space or delimitor after the value */
6441 val_end = next;
6442 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6443 val_end--;
6444 } else {
6445 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 /* We have nothing to do with attributes beginning with '$'. However,
6449 * they will automatically be removed if a header before them is removed,
6450 * since they're supposed to be linked together.
6451 */
6452 if (*att_beg == '$')
6453 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 /* Ignore cookies with no equal sign */
6456 if (equal == next) {
6457 /* This is not our cookie, so we must preserve it. But if we already
6458 * scheduled another cookie for removal, we cannot remove the
6459 * complete header, but we can remove the previous block itself.
6460 */
6461 preserve_hdr = 1;
6462 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006463 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 val_end += delta;
6465 next += delta;
6466 hdr_end += delta;
6467 hdr_next += delta;
6468 cur_hdr->len += delta;
6469 http_msg_move_end(&txn->req, delta);
6470 prev = del_from;
6471 del_from = NULL;
6472 }
6473 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006476 /* if there are spaces around the equal sign, we need to
6477 * strip them otherwise we'll get trouble for cookie captures,
6478 * or even for rewrites. Since this happens extremely rarely,
6479 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006480 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006481 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6482 int stripped_before = 0;
6483 int stripped_after = 0;
6484
6485 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006486 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006487 equal += stripped_before;
6488 val_beg += stripped_before;
6489 }
6490
6491 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006492 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 val_beg += stripped_after;
6494 stripped_before += stripped_after;
6495 }
6496
6497 val_end += stripped_before;
6498 next += stripped_before;
6499 hdr_end += stripped_before;
6500 hdr_next += stripped_before;
6501 cur_hdr->len += stripped_before;
6502 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 /* First, let's see if we want to capture this cookie. We check
6507 * that we don't already have a client side cookie, because we
6508 * can only capture one. Also as an optimisation, we ignore
6509 * cookies shorter than the declared name.
6510 */
6511 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6512 (val_end - att_beg >= t->fe->capture_namelen) &&
6513 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6514 int log_len = val_end - att_beg;
6515
6516 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6517 Alert("HTTP logging : out of memory.\n");
6518 } else {
6519 if (log_len > t->fe->capture_len)
6520 log_len = t->fe->capture_len;
6521 memcpy(txn->cli_cookie, att_beg, log_len);
6522 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreaubca99692010-10-06 19:25:55 +02006526 /* Persistence cookies in passive, rewrite or insert mode have the
6527 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006528 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006529 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006531 * For cookies in prefix mode, the form is :
6532 *
6533 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006534 */
6535 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6536 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6537 struct server *srv = t->be->srv;
6538 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006540 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6541 * have the server ID between val_beg and delim, and the original cookie between
6542 * delim+1 and val_end. Otherwise, delim==val_end :
6543 *
6544 * Cookie: NAME=SRV; # in all but prefix modes
6545 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6546 * | || || | |+-> next
6547 * | || || | +--> val_end
6548 * | || || +---------> delim
6549 * | || |+------------> val_beg
6550 * | || +-------------> att_end = equal
6551 * | |+-----------------> att_beg
6552 * | +------------------> prev
6553 * +-------------------------> hdr_beg
6554 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555
Willy Tarreau67402132012-05-31 20:40:20 +02006556 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006557 for (delim = val_beg; delim < val_end; delim++)
6558 if (*delim == COOKIE_DELIM)
6559 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006560 } else {
6561 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006563 /* Now check if the cookie contains a date field, which would
6564 * appear after a vertical bar ('|') just after the server name
6565 * and before the delimiter.
6566 */
6567 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6568 if (vbar1) {
6569 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006570 * right is the last seen date. It is a base64 encoded
6571 * 30-bit value representing the UNIX date since the
6572 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006573 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006574 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006575 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006576 if (val_end - vbar1 >= 5) {
6577 val = b64tos30(vbar1);
6578 if (val > 0)
6579 txn->cookie_last_date = val << 2;
6580 }
6581 /* look for a second vertical bar */
6582 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6583 if (vbar1 && (val_end - vbar1 > 5)) {
6584 val = b64tos30(vbar1 + 1);
6585 if (val > 0)
6586 txn->cookie_first_date = val << 2;
6587 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006588 }
6589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreauf64d1412010-10-07 20:06:11 +02006591 /* if the cookie has an expiration date and the proxy wants to check
6592 * it, then we do that now. We first check if the cookie is too old,
6593 * then only if it has expired. We detect strict overflow because the
6594 * time resolution here is not great (4 seconds). Cookies with dates
6595 * in the future are ignored if their offset is beyond one day. This
6596 * allows an admin to fix timezone issues without expiring everyone
6597 * and at the same time avoids keeping unwanted side effects for too
6598 * long.
6599 */
6600 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006601 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6602 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006603 txn->flags &= ~TX_CK_MASK;
6604 txn->flags |= TX_CK_OLD;
6605 delim = val_beg; // let's pretend we have not found the cookie
6606 txn->cookie_first_date = 0;
6607 txn->cookie_last_date = 0;
6608 }
6609 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006610 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6611 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006612 txn->flags &= ~TX_CK_MASK;
6613 txn->flags |= TX_CK_EXPIRED;
6614 delim = val_beg; // let's pretend we have not found the cookie
6615 txn->cookie_first_date = 0;
6616 txn->cookie_last_date = 0;
6617 }
6618
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 /* Here, we'll look for the first running server which supports the cookie.
6620 * This allows to share a same cookie between several servers, for example
6621 * to dedicate backup servers to specific servers only.
6622 * However, to prevent clients from sticking to cookie-less backup server
6623 * when they have incidentely learned an empty cookie, we simply ignore
6624 * empty cookies and mark them as invalid.
6625 * The same behaviour is applied when persistence must be ignored.
6626 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006627 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630 while (srv) {
6631 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6632 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6633 if ((srv->state & SRV_RUNNING) ||
6634 (t->be->options & PR_O_PERSIST) ||
6635 (t->flags & SN_FORCE_PRST)) {
6636 /* we found the server and we can use it */
6637 txn->flags &= ~TX_CK_MASK;
6638 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6639 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006640 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006641 break;
6642 } else {
6643 /* we found a server, but it's down,
6644 * mark it as such and go on in case
6645 * another one is available.
6646 */
6647 txn->flags &= ~TX_CK_MASK;
6648 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006651 srv = srv->next;
6652 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006653
Willy Tarreauf64d1412010-10-07 20:06:11 +02006654 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006655 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006657 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6658 txn->flags |= TX_CK_UNUSED;
6659 else
6660 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006663 /* depending on the cookie mode, we may have to either :
6664 * - delete the complete cookie if we're in insert+indirect mode, so that
6665 * the server never sees it ;
6666 * - remove the server id from the cookie value, and tag the cookie as an
6667 * application cookie so that it does not get accidentely removed later,
6668 * if we're in cookie prefix mode
6669 */
Willy Tarreau67402132012-05-31 20:40:20 +02006670 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006671 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006672
Willy Tarreau9b28e032012-10-12 23:49:43 +02006673 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 val_end += delta;
6675 next += delta;
6676 hdr_end += delta;
6677 hdr_next += delta;
6678 cur_hdr->len += delta;
6679 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 del_from = NULL;
6682 preserve_hdr = 1; /* we want to keep this cookie */
6683 }
6684 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006685 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006686 del_from = prev;
6687 }
6688 } else {
6689 /* This is not our cookie, so we must preserve it. But if we already
6690 * scheduled another cookie for removal, we cannot remove the
6691 * complete header, but we can remove the previous block itself.
6692 */
6693 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006696 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006697 if (att_beg >= del_from)
6698 att_beg += delta;
6699 if (att_end >= del_from)
6700 att_end += delta;
6701 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 val_end += delta;
6703 next += delta;
6704 hdr_end += delta;
6705 hdr_next += delta;
6706 cur_hdr->len += delta;
6707 http_msg_move_end(&txn->req, delta);
6708 prev = del_from;
6709 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 /* Look for the appsession cookie unless persistence must be ignored */
6714 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6715 int cmp_len, value_len;
6716 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006717
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006718 if (t->be->options2 & PR_O2_AS_PFX) {
6719 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6720 value_begin = att_beg + t->be->appsession_name_len;
6721 value_len = val_end - att_beg - t->be->appsession_name_len;
6722 } else {
6723 cmp_len = att_end - att_beg;
6724 value_begin = val_beg;
6725 value_len = val_end - val_beg;
6726 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006727
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 /* let's see if the cookie is our appcookie */
6729 if (cmp_len == t->be->appsession_name_len &&
6730 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6731 manage_client_side_appsession(t, value_begin, value_len);
6732 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733 }
6734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 /* continue with next cookie on this header line */
6736 att_beg = next;
6737 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 /* There are no more cookies on this line.
6740 * We may still have one (or several) marked for deletion at the
6741 * end of the line. We must do this now in two ways :
6742 * - if some cookies must be preserved, we only delete from the
6743 * mark to the end of line ;
6744 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006747 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006749 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006751 cur_hdr->len += delta;
6752 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006753 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754
6755 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006756 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6757 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006759 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006762 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763 }
6764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006766 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006768}
6769
6770
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771/* Iterate the same filter through all response headers contained in <rtr>.
6772 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6773 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006774int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775{
6776 char term;
6777 char *cur_ptr, *cur_end, *cur_next;
6778 int cur_idx, old_idx, last_hdr;
6779 struct http_txn *txn = &t->txn;
6780 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006781 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782
6783 last_hdr = 0;
6784
Willy Tarreau9b28e032012-10-12 23:49:43 +02006785 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 old_idx = 0;
6787
6788 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006789 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006791 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 (exp->action == ACT_ALLOW ||
6793 exp->action == ACT_DENY))
6794 return 0;
6795
6796 cur_idx = txn->hdr_idx.v[old_idx].next;
6797 if (!cur_idx)
6798 break;
6799
6800 cur_hdr = &txn->hdr_idx.v[cur_idx];
6801 cur_ptr = cur_next;
6802 cur_end = cur_ptr + cur_hdr->len;
6803 cur_next = cur_end + cur_hdr->cr + 1;
6804
6805 /* Now we have one header between cur_ptr and cur_end,
6806 * and the next header starts at cur_next.
6807 */
6808
6809 /* The annoying part is that pattern matching needs
6810 * that we modify the contents to null-terminate all
6811 * strings before testing them.
6812 */
6813
6814 term = *cur_end;
6815 *cur_end = '\0';
6816
6817 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6818 switch (exp->action) {
6819 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006820 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821 last_hdr = 1;
6822 break;
6823
6824 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006825 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 last_hdr = 1;
6827 break;
6828
6829 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006830 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6831 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 /* FIXME: if the user adds a newline in the replacement, the
6833 * index will not be recalculated for now, and the new line
6834 * will not be counted as a new header.
6835 */
6836
6837 cur_end += delta;
6838 cur_next += delta;
6839 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006840 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 break;
6842
6843 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006844 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845 cur_next += delta;
6846
Willy Tarreaufa355d42009-11-29 18:12:29 +01006847 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6849 txn->hdr_idx.used--;
6850 cur_hdr->len = 0;
6851 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006852 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 break;
6854
6855 }
6856 }
6857 if (cur_end)
6858 *cur_end = term; /* restore the string terminator */
6859
6860 /* keep the link from this header to next one in case of later
6861 * removal of next header.
6862 */
6863 old_idx = cur_idx;
6864 }
6865 return 0;
6866}
6867
6868
6869/* Apply the filter to the status line in the response buffer <rtr>.
6870 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6871 * or -1 if a replacement resulted in an invalid status line.
6872 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006873int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874{
6875 char term;
6876 char *cur_ptr, *cur_end;
6877 int done;
6878 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006879 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
6881
Willy Tarreau3d300592007-03-18 18:34:41 +01006882 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006884 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885 (exp->action == ACT_ALLOW ||
6886 exp->action == ACT_DENY))
6887 return 0;
6888 else if (exp->action == ACT_REMOVE)
6889 return 0;
6890
6891 done = 0;
6892
Willy Tarreau9b28e032012-10-12 23:49:43 +02006893 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006894 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
6896 /* Now we have the status line between cur_ptr and cur_end */
6897
6898 /* The annoying part is that pattern matching needs
6899 * that we modify the contents to null-terminate all
6900 * strings before testing them.
6901 */
6902
6903 term = *cur_end;
6904 *cur_end = '\0';
6905
6906 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6907 switch (exp->action) {
6908 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006909 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 done = 1;
6911 break;
6912
6913 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006914 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915 done = 1;
6916 break;
6917
6918 case ACT_REPLACE:
6919 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006920 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6921 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006922 /* FIXME: if the user adds a newline in the replacement, the
6923 * index will not be recalculated for now, and the new line
6924 * will not be counted as a new header.
6925 */
6926
Willy Tarreaufa355d42009-11-29 18:12:29 +01006927 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006929 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006930 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931 cur_ptr, cur_end + 1,
6932 NULL, NULL);
6933 if (unlikely(!cur_end))
6934 return -1;
6935
6936 /* we have a full respnse and we know that we have either a CR
6937 * or an LF at <ptr>.
6938 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006939 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006940 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006941 /* there is no point trying this regex on headers */
6942 return 1;
6943 }
6944 }
6945 *cur_end = term; /* restore the string terminator */
6946 return done;
6947}
6948
6949
6950
6951/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006952 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006953 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6954 * unparsable response.
6955 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006956int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006957{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006958 struct http_txn *txn = &s->txn;
6959 struct hdr_exp *exp;
6960
6961 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962 int ret;
6963
6964 /*
6965 * The interleaving of transformations and verdicts
6966 * makes it difficult to decide to continue or stop
6967 * the evaluation.
6968 */
6969
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006970 if (txn->flags & TX_SVDENY)
6971 break;
6972
Willy Tarreau3d300592007-03-18 18:34:41 +01006973 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6975 exp->action == ACT_PASS)) {
6976 exp = exp->next;
6977 continue;
6978 }
6979
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006980 /* if this filter had a condition, evaluate it now and skip to
6981 * next filter if the condition does not match.
6982 */
6983 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006984 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006985 ret = acl_pass(ret);
6986 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6987 ret = !ret;
6988 if (!ret)
6989 continue;
6990 }
6991
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006993 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 if (unlikely(ret < 0))
6995 return -1;
6996
6997 if (likely(ret == 0)) {
6998 /* The filter did not match the response, it can be
6999 * iterated through all headers.
7000 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007001 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 }
7004 return 0;
7005}
7006
7007
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007009 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 * desirable to call it only when needed. This function is also used when we
7011 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007013void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014{
7015 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007016 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007019 char *hdr_beg, *hdr_end, *hdr_next;
7020 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022 /* Iterate through the headers.
7023 * we start with the start line.
7024 */
7025 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007026 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027
7028 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7029 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007030 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031
7032 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 hdr_beg = hdr_next;
7034 hdr_end = hdr_beg + cur_hdr->len;
7035 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036
Willy Tarreau24581ba2010-08-31 22:39:35 +02007037 /* We have one full header between hdr_beg and hdr_end, and the
7038 * next header starts at hdr_next. We're only interested in
7039 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007040 */
7041
Willy Tarreau24581ba2010-08-31 22:39:35 +02007042 is_cookie2 = 0;
7043 prev = hdr_beg + 10;
7044 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007045 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007046 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7047 if (!val) {
7048 old_idx = cur_idx;
7049 continue;
7050 }
7051 is_cookie2 = 1;
7052 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 }
7054
Willy Tarreau24581ba2010-08-31 22:39:35 +02007055 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7056 * <prev> points to the colon.
7057 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007058 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059
Willy Tarreau24581ba2010-08-31 22:39:35 +02007060 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7061 * check-cache is enabled) and we are not interested in checking
7062 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007063 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007064 if (t->be->cookie_name == NULL &&
7065 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007066 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 return;
7068
Willy Tarreau24581ba2010-08-31 22:39:35 +02007069 /* OK so now we know we have to process this response cookie.
7070 * The format of the Set-Cookie header is slightly different
7071 * from the format of the Cookie header in that it does not
7072 * support the comma as a cookie delimiter (thus the header
7073 * cannot be folded) because the Expires attribute described in
7074 * the original Netscape's spec may contain an unquoted date
7075 * with a comma inside. We have to live with this because
7076 * many browsers don't support Max-Age and some browsers don't
7077 * support quoted strings. However the Set-Cookie2 header is
7078 * clean.
7079 *
7080 * We have to keep multiple pointers in order to support cookie
7081 * removal at the beginning, middle or end of header without
7082 * corrupting the header (in case of set-cookie2). A special
7083 * pointer, <scav> points to the beginning of the set-cookie-av
7084 * fields after the first semi-colon. The <next> pointer points
7085 * either to the end of line (set-cookie) or next unquoted comma
7086 * (set-cookie2). All of these headers are valid :
7087 *
7088 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7089 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7090 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7091 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7092 * | | | | | | | | | |
7093 * | | | | | | | | +-> next hdr_end <--+
7094 * | | | | | | | +------------> scav
7095 * | | | | | | +--------------> val_end
7096 * | | | | | +--------------------> val_beg
7097 * | | | | +----------------------> equal
7098 * | | | +------------------------> att_end
7099 * | | +----------------------------> att_beg
7100 * | +------------------------------> prev
7101 * +-----------------------------------------> hdr_beg
7102 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103
Willy Tarreau24581ba2010-08-31 22:39:35 +02007104 for (; prev < hdr_end; prev = next) {
7105 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106
Willy Tarreau24581ba2010-08-31 22:39:35 +02007107 /* find att_beg */
7108 att_beg = prev + 1;
7109 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7110 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111
Willy Tarreau24581ba2010-08-31 22:39:35 +02007112 /* find att_end : this is the first character after the last non
7113 * space before the equal. It may be equal to hdr_end.
7114 */
7115 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116
Willy Tarreau24581ba2010-08-31 22:39:35 +02007117 while (equal < hdr_end) {
7118 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7119 break;
7120 if (http_is_spht[(unsigned char)*equal++])
7121 continue;
7122 att_end = equal;
7123 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124
Willy Tarreau24581ba2010-08-31 22:39:35 +02007125 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7126 * is between <att_beg> and <equal>, both may be identical.
7127 */
7128
7129 /* look for end of cookie if there is an equal sign */
7130 if (equal < hdr_end && *equal == '=') {
7131 /* look for the beginning of the value */
7132 val_beg = equal + 1;
7133 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7134 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007135
Willy Tarreau24581ba2010-08-31 22:39:35 +02007136 /* find the end of the value, respecting quotes */
7137 next = find_cookie_value_end(val_beg, hdr_end);
7138
7139 /* make val_end point to the first white space or delimitor after the value */
7140 val_end = next;
7141 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7142 val_end--;
7143 } else {
7144 /* <equal> points to next comma, semi-colon or EOL */
7145 val_beg = val_end = next = equal;
7146 }
7147
7148 if (next < hdr_end) {
7149 /* Set-Cookie2 supports multiple cookies, and <next> points to
7150 * a colon or semi-colon before the end. So skip all attr-value
7151 * pairs and look for the next comma. For Set-Cookie, since
7152 * commas are permitted in values, skip to the end.
7153 */
7154 if (is_cookie2)
7155 next = find_hdr_value_end(next, hdr_end);
7156 else
7157 next = hdr_end;
7158 }
7159
7160 /* Now everything is as on the diagram above */
7161
7162 /* Ignore cookies with no equal sign */
7163 if (equal == val_end)
7164 continue;
7165
7166 /* If there are spaces around the equal sign, we need to
7167 * strip them otherwise we'll get trouble for cookie captures,
7168 * or even for rewrites. Since this happens extremely rarely,
7169 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7172 int stripped_before = 0;
7173 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174
Willy Tarreau24581ba2010-08-31 22:39:35 +02007175 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007176 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007177 equal += stripped_before;
7178 val_beg += stripped_before;
7179 }
7180
7181 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007182 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007183 val_beg += stripped_after;
7184 stripped_before += stripped_after;
7185 }
7186
7187 val_end += stripped_before;
7188 next += stripped_before;
7189 hdr_end += stripped_before;
7190 hdr_next += stripped_before;
7191 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007192 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007193 }
7194
7195 /* First, let's see if we want to capture this cookie. We check
7196 * that we don't already have a server side cookie, because we
7197 * can only capture one. Also as an optimisation, we ignore
7198 * cookies shorter than the declared name.
7199 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007200 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007201 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007202 (val_end - att_beg >= t->fe->capture_namelen) &&
7203 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7204 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007205 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 Alert("HTTP logging : out of memory.\n");
7207 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007208 else {
7209 if (log_len > t->fe->capture_len)
7210 log_len = t->fe->capture_len;
7211 memcpy(txn->srv_cookie, att_beg, log_len);
7212 txn->srv_cookie[log_len] = 0;
7213 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 }
7215
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007216 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007218 if (!(t->flags & SN_IGNORE_PRST) &&
7219 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7220 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007221 /* assume passive cookie by default */
7222 txn->flags &= ~TX_SCK_MASK;
7223 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224
7225 /* If the cookie is in insert mode on a known server, we'll delete
7226 * this occurrence because we'll insert another one later.
7227 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007228 * a direct access.
7229 */
Willy Tarreau67402132012-05-31 20:40:20 +02007230 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007231 /* The "preserve" flag was set, we don't want to touch the
7232 * server's cookie.
7233 */
7234 }
Willy Tarreau67402132012-05-31 20:40:20 +02007235 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7236 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007237 /* this cookie must be deleted */
7238 if (*prev == ':' && next == hdr_end) {
7239 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007240 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007241 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7242 txn->hdr_idx.used--;
7243 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007244 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 hdr_next += delta;
7246 http_msg_move_end(&txn->rsp, delta);
7247 /* note: while both invalid now, <next> and <hdr_end>
7248 * are still equal, so the for() will stop as expected.
7249 */
7250 } else {
7251 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007252 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007253 next = prev;
7254 hdr_end += delta;
7255 hdr_next += delta;
7256 cur_hdr->len += delta;
7257 http_msg_move_end(&txn->rsp, delta);
7258 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007259 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007260 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 }
Willy Tarreau67402132012-05-31 20:40:20 +02007263 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007264 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265 * with this server since we know it.
7266 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007267 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007268 next += delta;
7269 hdr_end += delta;
7270 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007272 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273
Willy Tarreauf1348312010-10-07 15:54:11 +02007274 txn->flags &= ~TX_SCK_MASK;
7275 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007277 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007278 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007279 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007281 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007282 next += delta;
7283 hdr_end += delta;
7284 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007286 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287
Willy Tarreau827aee92011-03-10 16:55:02 +01007288 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007289 txn->flags &= ~TX_SCK_MASK;
7290 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 }
7292 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007293 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7294 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007295 int cmp_len, value_len;
7296 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297
Cyril Bontéb21570a2009-11-29 20:04:48 +01007298 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007299 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7300 value_begin = att_beg + t->be->appsession_name_len;
7301 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007302 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007303 cmp_len = att_end - att_beg;
7304 value_begin = val_beg;
7305 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007306 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007307
Cyril Bonté17530c32010-04-06 21:11:10 +02007308 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007309 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7310 /* free a possibly previously allocated memory */
7311 pool_free2(apools.sessid, txn->sessid);
7312
Cyril Bontéb21570a2009-11-29 20:04:48 +01007313 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007314 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007315 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7316 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7317 return;
7318 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007319 memcpy(txn->sessid, value_begin, value_len);
7320 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007322 }
7323 /* that's done for this cookie, check the next one on the same
7324 * line when next != hdr_end (only if is_cookie2).
7325 */
7326 }
7327 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007329 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007330
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007331 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007332 appsess *asession = NULL;
7333 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007334 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007335 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007336 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007337 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7338 Alert("Not enough Memory process_srv():asession:calloc().\n");
7339 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7340 return;
7341 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007342 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7343
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007344 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7345 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7346 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007347 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007348 return;
7349 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007350 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007351 asession->sessid[t->be->appsession_len] = 0;
7352
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007353 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007354 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007355 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007356 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007357 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007358 return;
7359 }
7360 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007361 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007362
7363 asession->request_count = 0;
7364 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7365 }
7366
7367 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7368 asession->request_count++;
7369 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370}
7371
7372
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373/*
7374 * Check if response is cacheable or not. Updates t->flags.
7375 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007376void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377{
7378 struct http_txn *txn = &t->txn;
7379 char *p1, *p2;
7380
7381 char *cur_ptr, *cur_end, *cur_next;
7382 int cur_idx;
7383
Willy Tarreau5df51872007-11-25 16:20:08 +01007384 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 return;
7386
7387 /* Iterate through the headers.
7388 * we start with the start line.
7389 */
7390 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007391 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392
7393 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7394 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007395 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396
7397 cur_hdr = &txn->hdr_idx.v[cur_idx];
7398 cur_ptr = cur_next;
7399 cur_end = cur_ptr + cur_hdr->len;
7400 cur_next = cur_end + cur_hdr->cr + 1;
7401
7402 /* We have one full header between cur_ptr and cur_end, and the
7403 * next header starts at cur_next. We're only interested in
7404 * "Cookie:" headers.
7405 */
7406
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007407 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7408 if (val) {
7409 if ((cur_end - (cur_ptr + val) >= 8) &&
7410 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7411 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7412 return;
7413 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 }
7415
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007416 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7417 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 continue;
7419
7420 /* OK, right now we know we have a cache-control header at cur_ptr */
7421
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007422 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423
7424 if (p1 >= cur_end) /* no more info */
7425 continue;
7426
7427 /* p1 is at the beginning of the value */
7428 p2 = p1;
7429
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007430 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007431 p2++;
7432
7433 /* we have a complete value between p1 and p2 */
7434 if (p2 < cur_end && *p2 == '=') {
7435 /* we have something of the form no-cache="set-cookie" */
7436 if ((cur_end - p1 >= 21) &&
7437 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7438 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007439 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 continue;
7441 }
7442
7443 /* OK, so we know that either p2 points to the end of string or to a comma */
7444 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7445 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7446 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7447 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007448 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449 return;
7450 }
7451
7452 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007453 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 continue;
7455 }
7456 }
7457}
7458
7459
Willy Tarreau58f10d72006-12-04 02:26:12 +01007460/*
7461 * Try to retrieve a known appsession in the URI, then the associated server.
7462 * If the server is found, it's assigned to the session.
7463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007466 char *end_params, *first_param, *cur_param, *next_param;
7467 char separator;
7468 int value_len;
7469
7470 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007471
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007472 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007473 (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 +01007474 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476
Cyril Bontéb21570a2009-11-29 20:04:48 +01007477 first_param = NULL;
7478 switch (mode) {
7479 case PR_O2_AS_M_PP:
7480 first_param = memchr(begin, ';', len);
7481 break;
7482 case PR_O2_AS_M_QS:
7483 first_param = memchr(begin, '?', len);
7484 break;
7485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007486
Cyril Bontéb21570a2009-11-29 20:04:48 +01007487 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007489 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490
Cyril Bontéb21570a2009-11-29 20:04:48 +01007491 switch (mode) {
7492 case PR_O2_AS_M_PP:
7493 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7494 end_params = (char *) begin + len;
7495 }
7496 separator = ';';
7497 break;
7498 case PR_O2_AS_M_QS:
7499 end_params = (char *) begin + len;
7500 separator = '&';
7501 break;
7502 default:
7503 /* unknown mode, shouldn't happen */
7504 return;
7505 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506
Cyril Bontéb21570a2009-11-29 20:04:48 +01007507 cur_param = next_param = end_params;
7508 while (cur_param > first_param) {
7509 cur_param--;
7510 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7511 /* let's see if this is the appsession parameter */
7512 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7513 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7514 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7515 /* Cool... it's the right one */
7516 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7517 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7518 if (value_len > 0) {
7519 manage_client_side_appsession(t, cur_param, value_len);
7520 }
7521 break;
7522 }
7523 next_param = cur_param;
7524 }
7525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007526#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007527 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007528 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007529#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007530}
7531
Willy Tarreaub2513902006-12-17 14:52:38 +01007532/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007533 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007534 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007535 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007536 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007537 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007538 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007539 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007540 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007541int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007542{
7543 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007544 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007545 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007546 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007547
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007548 if (!uri_auth)
7549 return 0;
7550
Cyril Bonté70be45d2010-10-12 00:14:35 +02007551 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007552 return 0;
7553
Willy Tarreau295a8372011-03-10 11:25:07 +01007554 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007555 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007557 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007558 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007559 return 0;
7560
Willy Tarreau3a215be2012-03-09 21:39:51 +01007561 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007562 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007563 return 0;
7564
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007565 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007566 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007567 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007568 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007569 break;
7570 }
7571 h++;
7572 }
7573
7574 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007575 h = uri + uri_auth->uri_len;
7576 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007577 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007578 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007579 break;
7580 }
7581 h++;
7582 }
7583 }
7584
Willy Tarreau3a215be2012-03-09 21:39:51 +01007585 h = uri + uri_auth->uri_len;
7586 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007587 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007588 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007589 break;
7590 }
7591 h++;
7592 }
7593
Willy Tarreau3a215be2012-03-09 21:39:51 +01007594 h = uri + uri_auth->uri_len;
7595 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007596 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007597 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007598 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007599 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007600 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7601 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7602 si->applet.ctx.stats.st_code = i;
7603 break;
7604 }
7605 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007606 break;
7607 }
7608 h++;
7609 }
7610
Willy Tarreau295a8372011-03-10 11:25:07 +01007611 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007612
Willy Tarreaub2513902006-12-17 14:52:38 +01007613 return 1;
7614}
7615
Willy Tarreau4076a152009-04-02 15:18:36 +02007616/*
7617 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007618 * By default it tries to report the error position as msg->err_pos. However if
7619 * this one is not set, it will then report msg->next, which is the last known
7620 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007621 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007622 */
7623void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007624 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007625 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007626{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007627 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007628 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007629
Willy Tarreau9b28e032012-10-12 23:49:43 +02007630 es->len = MIN(chn->buf->i, sizeof(es->buf));
7631 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007632 len1 = MIN(len1, es->len);
7633 len2 = es->len - len1; /* remaining data if buffer wraps */
7634
Willy Tarreau9b28e032012-10-12 23:49:43 +02007635 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007636 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007637 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007638
Willy Tarreau4076a152009-04-02 15:18:36 +02007639 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007640 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007641 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007642 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007643
Willy Tarreau4076a152009-04-02 15:18:36 +02007644 es->when = date; // user-visible date
7645 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007646 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007647 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007648 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007649 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007650 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007651 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007652 es->s_flags = s->flags;
7653 es->t_flags = s->txn.flags;
7654 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007655 es->b_out = chn->buf->o;
7656 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007657 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007658 es->m_clen = msg->chunk_len;
7659 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007660}
Willy Tarreaub2513902006-12-17 14:52:38 +01007661
Willy Tarreau294c4732011-12-16 21:35:50 +01007662/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7663 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7664 * performed over the whole headers. Otherwise it must contain a valid header
7665 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7666 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7667 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7668 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7669 * -1.
7670 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007671 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007672unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007673 struct hdr_idx *idx, int occ,
7674 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007675{
Willy Tarreau294c4732011-12-16 21:35:50 +01007676 struct hdr_ctx local_ctx;
7677 char *ptr_hist[MAX_HDR_HISTORY];
7678 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007679 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007680 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007681
Willy Tarreau294c4732011-12-16 21:35:50 +01007682 if (!ctx) {
7683 local_ctx.idx = 0;
7684 ctx = &local_ctx;
7685 }
7686
Willy Tarreaubce70882009-09-07 11:51:47 +02007687 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007688 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007689 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007690 occ--;
7691 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007692 *vptr = ctx->line + ctx->val;
7693 *vlen = ctx->vlen;
7694 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007695 }
7696 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007697 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007698 }
7699
7700 /* negative occurrence, we scan all the list then walk back */
7701 if (-occ > MAX_HDR_HISTORY)
7702 return 0;
7703
Willy Tarreau294c4732011-12-16 21:35:50 +01007704 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007705 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007706 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7707 len_hist[hist_ptr] = ctx->vlen;
7708 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007709 hist_ptr = 0;
7710 found++;
7711 }
7712 if (-occ > found)
7713 return 0;
7714 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7715 * find occurrence -occ, so we have to check [hist_ptr+occ].
7716 */
7717 hist_ptr += occ;
7718 if (hist_ptr >= MAX_HDR_HISTORY)
7719 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007720 *vptr = ptr_hist[hist_ptr];
7721 *vlen = len_hist[hist_ptr];
7722 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007723}
7724
Willy Tarreaubaaee002006-06-26 02:48:02 +02007725/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007726 * Print a debug line with a header. Always stop at the first CR or LF char,
7727 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7728 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729 */
7730void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7731{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007732 int max;
7733 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007734 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7735 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007736
7737 for (max = 0; start + max < end; max++)
7738 if (start[max] == '\r' || start[max] == '\n')
7739 break;
7740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007741 UBOUND(max, trash.size - trash.len - 3);
7742 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7743 trash.str[trash.len++] = '\n';
7744 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745}
7746
Willy Tarreau0937bc42009-12-22 15:03:09 +01007747/*
7748 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7749 * the required fields are properly allocated and that we only need to (re)init
7750 * them. This should be used before processing any new request.
7751 */
7752void http_init_txn(struct session *s)
7753{
7754 struct http_txn *txn = &s->txn;
7755 struct proxy *fe = s->fe;
7756
7757 txn->flags = 0;
7758 txn->status = -1;
7759
William Lallemand5f232402012-04-05 18:02:55 +02007760 global.req_count++;
7761
Willy Tarreauf64d1412010-10-07 20:06:11 +02007762 txn->cookie_first_date = 0;
7763 txn->cookie_last_date = 0;
7764
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007765 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007766 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007767 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007768 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007769 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007770 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007771 txn->req.chunk_len = 0LL;
7772 txn->req.body_len = 0LL;
7773 txn->rsp.chunk_len = 0LL;
7774 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007775 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7776 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007777 txn->req.chn = s->req;
7778 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007779
7780 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007781
7782 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7783 if (fe->options2 & PR_O2_REQBUG_OK)
7784 txn->req.err_pos = -1; /* let buggy requests pass */
7785
Willy Tarreau46023632010-01-07 22:51:47 +01007786 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007787 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7788
Willy Tarreau46023632010-01-07 22:51:47 +01007789 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007790 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7791
7792 if (txn->hdr_idx.v)
7793 hdr_idx_init(&txn->hdr_idx);
7794}
7795
7796/* to be used at the end of a transaction */
7797void http_end_txn(struct session *s)
7798{
7799 struct http_txn *txn = &s->txn;
7800
7801 /* these ones will have been dynamically allocated */
7802 pool_free2(pool2_requri, txn->uri);
7803 pool_free2(pool2_capture, txn->cli_cookie);
7804 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007805 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007806 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007807
William Lallemanda73203e2012-03-12 12:48:57 +01007808 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007809 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007810 txn->uri = NULL;
7811 txn->srv_cookie = NULL;
7812 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007813
7814 if (txn->req.cap) {
7815 struct cap_hdr *h;
7816 for (h = s->fe->req_cap; h; h = h->next)
7817 pool_free2(h->pool, txn->req.cap[h->index]);
7818 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7819 }
7820
7821 if (txn->rsp.cap) {
7822 struct cap_hdr *h;
7823 for (h = s->fe->rsp_cap; h; h = h->next)
7824 pool_free2(h->pool, txn->rsp.cap[h->index]);
7825 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7826 }
7827
Willy Tarreau0937bc42009-12-22 15:03:09 +01007828}
7829
7830/* to be used at the end of a transaction to prepare a new one */
7831void http_reset_txn(struct session *s)
7832{
7833 http_end_txn(s);
7834 http_init_txn(s);
7835
7836 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007837 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007838 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007839 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007840 /* re-init store persistence */
7841 s->store_count = 0;
7842
Willy Tarreau0937bc42009-12-22 15:03:09 +01007843 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007844
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007845 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007846
Willy Tarreau739cfba2010-01-25 23:11:14 +01007847 /* We must trim any excess data from the response buffer, because we
7848 * may have blocked an invalid response from a server that we don't
7849 * want to accidentely forward once we disable the analysers, nor do
7850 * we want those data to come along with next response. A typical
7851 * example of such data would be from a buggy server responding to
7852 * a HEAD with some data, or sending more than the advertised
7853 * content-length.
7854 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007855 if (unlikely(s->rep->buf->i))
7856 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007857
Willy Tarreau0937bc42009-12-22 15:03:09 +01007858 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007859 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007860
Willy Tarreaud04e8582010-05-31 12:31:35 +02007861 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007862 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007863
7864 s->req->rex = TICK_ETERNITY;
7865 s->req->wex = TICK_ETERNITY;
7866 s->req->analyse_exp = TICK_ETERNITY;
7867 s->rep->rex = TICK_ETERNITY;
7868 s->rep->wex = TICK_ETERNITY;
7869 s->rep->analyse_exp = TICK_ETERNITY;
7870}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007871
Willy Tarreauff011f22011-01-06 17:51:27 +01007872void free_http_req_rules(struct list *r) {
7873 struct http_req_rule *tr, *pr;
7874
7875 list_for_each_entry_safe(pr, tr, r, list) {
7876 LIST_DEL(&pr->list);
7877 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7878 free(pr->http_auth.realm);
7879
7880 free(pr);
7881 }
7882}
7883
7884struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7885{
7886 struct http_req_rule *rule;
7887 int cur_arg;
7888
7889 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7890 if (!rule) {
7891 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7892 return NULL;
7893 }
7894
7895 if (!*args[0]) {
7896 goto req_error_parsing;
7897 } else if (!strcmp(args[0], "allow")) {
7898 rule->action = HTTP_REQ_ACT_ALLOW;
7899 cur_arg = 1;
7900 } else if (!strcmp(args[0], "deny")) {
7901 rule->action = HTTP_REQ_ACT_DENY;
7902 cur_arg = 1;
7903 } else if (!strcmp(args[0], "auth")) {
7904 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7905 cur_arg = 1;
7906
7907 while(*args[cur_arg]) {
7908 if (!strcmp(args[cur_arg], "realm")) {
7909 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7910 cur_arg+=2;
7911 continue;
7912 } else
7913 break;
7914 }
7915 } else {
7916req_error_parsing:
7917 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7918 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7919 return NULL;
7920 }
7921
7922 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7923 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007924 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007925
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007926 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7927 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7928 file, linenum, args[0], errmsg);
7929 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007930 return NULL;
7931 }
7932 rule->cond = cond;
7933 }
7934 else if (*args[cur_arg]) {
7935 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7936 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7937 file, linenum, args[0], args[cur_arg]);
7938 return NULL;
7939 }
7940
7941 return rule;
7942}
7943
Willy Tarreau8797c062007-05-07 00:55:35 +02007944/************************************************************************/
7945/* The code below is dedicated to ACL parsing and matching */
7946/************************************************************************/
7947
7948
Willy Tarreau14174bc2012-04-16 14:34:04 +02007949/* This function ensures that the prerequisites for an L7 fetch are ready,
7950 * which means that a request or response is ready. If some data is missing,
7951 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007952 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7953 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007954 *
7955 * The function returns :
7956 * 0 if some data is missing or if the requested data cannot be fetched
7957 * -1 if it is certain that we'll never have any HTTP message there
7958 * 1 if an HTTP message is ready
7959 */
7960static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007961acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007962 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007963{
7964 struct http_txn *txn = l7;
7965 struct http_msg *msg = &txn->req;
7966
7967 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7968 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7969 */
7970
7971 if (unlikely(!s || !txn))
7972 return 0;
7973
7974 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007975 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007976
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007977 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007978 if (unlikely(!s->req))
7979 return 0;
7980
7981 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007982 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007983 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007984 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007985 return -1;
7986 }
7987
7988 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007989 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007990 http_msg_analyzer(msg, &txn->hdr_idx);
7991
7992 /* Still no valid request ? */
7993 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007994 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007995 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007996 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007997 return -1;
7998 }
7999 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008000 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008001 return 0;
8002 }
8003
8004 /* OK we just got a valid HTTP request. We have some minor
8005 * preparation to perform so that further checks can rely
8006 * on HTTP tests.
8007 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008008 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008009 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8010 s->flags |= SN_REDIRECTABLE;
8011
8012 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008013 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008014 return -1;
8015 }
8016 }
8017
Willy Tarreau24e32d82012-04-23 23:55:44 +02008018 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008019 return 0; /* data might have moved and indexes changed */
8020
8021 /* otherwise everything's ready for the request */
8022 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008023 else {
8024 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008025 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8026 return 0;
8027 }
8028
8029 /* everything's OK */
8030 return 1;
8031}
Willy Tarreau8797c062007-05-07 00:55:35 +02008032
Willy Tarreauc0239e02012-04-16 14:42:55 +02008033#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008034 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 +02008035
Willy Tarreau24e32d82012-04-23 23:55:44 +02008036#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008037 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 +02008038
Willy Tarreau8797c062007-05-07 00:55:35 +02008039
8040/* 1. Check on METHOD
8041 * We use the pre-parsed method if it is known, and store its number as an
8042 * integer. If it is unknown, we use the pointer and the length.
8043 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008044static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008045{
8046 int len, meth;
8047
Willy Tarreauae8b7962007-06-09 23:10:04 +02008048 len = strlen(*text);
8049 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008050
8051 pattern->val.i = meth;
8052 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008053 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008054 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008055 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008056 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008057 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008058 pattern->len = len;
8059 }
8060 return 1;
8061}
8062
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008063/* This function fetches the method of current HTTP request and stores
8064 * it in the global pattern struct as a chunk. There are two possibilities :
8065 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8066 * in <len> and <ptr> is NULL ;
8067 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8068 * <len> to its length.
8069 * This is intended to be used with acl_match_meth() only.
8070 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008071static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008072acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008073 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008074{
8075 int meth;
8076 struct http_txn *txn = l7;
8077
Willy Tarreau24e32d82012-04-23 23:55:44 +02008078 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008079
Willy Tarreau8797c062007-05-07 00:55:35 +02008080 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008081 smp->type = SMP_T_UINT;
8082 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008083 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008084 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8085 /* ensure the indexes are not affected */
8086 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008087 smp->type = SMP_T_CSTR;
8088 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008089 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008090 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008091 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008092 return 1;
8093}
8094
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008095/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008096static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008097{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008098 int icase;
8099
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008100
Willy Tarreauf853c462012-04-23 18:53:56 +02008101 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008102 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008103 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008104 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008105 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008106 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008107
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008108 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8109 if (pattern->val.i != HTTP_METH_OTHER)
8110 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008111
8112 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008113 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008114 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008115
8116 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008117 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8118 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008119 return ACL_PAT_FAIL;
8120 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008121}
8122
8123/* 2. Check on Request/Status Version
8124 * We simply compare strings here.
8125 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008126static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008127{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008128 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008129 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008130 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008131 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008132 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008133 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008134 return 1;
8135}
8136
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008137static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008138acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008139 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008140{
8141 struct http_txn *txn = l7;
8142 char *ptr;
8143 int len;
8144
Willy Tarreauc0239e02012-04-16 14:42:55 +02008145 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008146
Willy Tarreau8797c062007-05-07 00:55:35 +02008147 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008148 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008149
8150 while ((len-- > 0) && (*ptr++ != '/'));
8151 if (len <= 0)
8152 return 0;
8153
Willy Tarreauf853c462012-04-23 18:53:56 +02008154 smp->type = SMP_T_CSTR;
8155 smp->data.str.str = ptr;
8156 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008157
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008158 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008159 return 1;
8160}
8161
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008162static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008163acl_fetch_stver(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 Tarreau8797c062007-05-07 00:55:35 +02008165{
8166 struct http_txn *txn = l7;
8167 char *ptr;
8168 int len;
8169
Willy Tarreauc0239e02012-04-16 14:42:55 +02008170 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008171
Willy Tarreau8797c062007-05-07 00:55:35 +02008172 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008173 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008174
8175 while ((len-- > 0) && (*ptr++ != '/'));
8176 if (len <= 0)
8177 return 0;
8178
Willy Tarreauf853c462012-04-23 18:53:56 +02008179 smp->type = SMP_T_CSTR;
8180 smp->data.str.str = ptr;
8181 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008182
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008183 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008184 return 1;
8185}
8186
8187/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008188static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008189acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008190 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008191{
8192 struct http_txn *txn = l7;
8193 char *ptr;
8194 int len;
8195
Willy Tarreauc0239e02012-04-16 14:42:55 +02008196 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008197
Willy Tarreau8797c062007-05-07 00:55:35 +02008198 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008199 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008200
Willy Tarreauf853c462012-04-23 18:53:56 +02008201 smp->type = SMP_T_UINT;
8202 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008203 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008204 return 1;
8205}
8206
8207/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008208static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008209smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008210 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008211{
8212 struct http_txn *txn = l7;
8213
Willy Tarreauc0239e02012-04-16 14:42:55 +02008214 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008215
Willy Tarreauf853c462012-04-23 18:53:56 +02008216 smp->type = SMP_T_CSTR;
8217 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008218 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008219 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008220 return 1;
8221}
8222
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008223static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008224smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008225 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008226{
8227 struct http_txn *txn = l7;
8228
Willy Tarreauc0239e02012-04-16 14:42:55 +02008229 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008230
8231 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008232 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8233 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008234 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008235 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008236 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008237
8238 /*
8239 * If we are parsing url in frontend space, we prepare backend stage
8240 * to not parse again the same url ! optimization lazyness...
8241 */
8242 if (px->options & PR_O_HTTP_PROXY)
8243 l4->flags |= SN_ADDR_SET;
8244
Willy Tarreau37406352012-04-23 16:16:37 +02008245 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008246 return 1;
8247}
8248
8249static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008250smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008251 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008252{
8253 struct http_txn *txn = l7;
8254
Willy Tarreauc0239e02012-04-16 14:42:55 +02008255 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008256
8257 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008258 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02008259 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008260 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008261
8262 if (px->options & PR_O_HTTP_PROXY)
8263 l4->flags |= SN_ADDR_SET;
8264
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008265 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008266 return 1;
8267}
8268
Willy Tarreau185b5c42012-04-26 15:11:51 +02008269/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8270 * Accepts an optional argument of type string containing the header field name,
8271 * and an optional argument of type signed or unsigned integer to request an
8272 * explicit occurrence of the header. Note that in the event of a missing name,
8273 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008274 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008275static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008276smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008277 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008278{
8279 struct http_txn *txn = l7;
8280 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008281 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008282 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008283 int occ = 0;
8284 const char *name_str = NULL;
8285 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008286
Willy Tarreau185b5c42012-04-26 15:11:51 +02008287 if (args) {
8288 if (args[0].type != ARGT_STR)
8289 return 0;
8290 name_str = args[0].data.str.str;
8291 name_len = args[0].data.str.len;
8292
8293 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8294 occ = args[1].data.uint;
8295 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008296
Willy Tarreaue333ec92012-04-16 16:26:40 +02008297 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008298
Willy Tarreau185b5c42012-04-26 15:11:51 +02008299 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008300 /* search for header from the beginning */
8301 ctx->idx = 0;
8302
Willy Tarreau185b5c42012-04-26 15:11:51 +02008303 if (!occ && !(opt & SMP_OPT_ITERATE))
8304 /* no explicit occurrence and single fetch => last header by default */
8305 occ = -1;
8306
8307 if (!occ)
8308 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008309 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008310
Willy Tarreau185b5c42012-04-26 15:11:51 +02008311 smp->type = SMP_T_CSTR;
8312 smp->flags |= SMP_F_VOL_HDR;
8313 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 +02008314 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008315
Willy Tarreau37406352012-04-23 16:16:37 +02008316 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008317 return 0;
8318}
8319
Willy Tarreauc11416f2007-06-17 16:58:38 +02008320/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008321 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008322 */
8323static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008324smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008325 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008326{
8327 struct http_txn *txn = l7;
8328 struct hdr_idx *idx = &txn->hdr_idx;
8329 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008330 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008331 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008332
Willy Tarreau24e32d82012-04-23 23:55:44 +02008333 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008334 return 0;
8335
Willy Tarreaue333ec92012-04-16 16:26:40 +02008336 CHECK_HTTP_MESSAGE_FIRST();
8337
Willy Tarreau33a7e692007-06-10 19:45:56 +02008338 ctx.idx = 0;
8339 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008340 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008341 cnt++;
8342
Willy Tarreauf853c462012-04-23 18:53:56 +02008343 smp->type = SMP_T_UINT;
8344 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008345 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008346 return 1;
8347}
8348
Willy Tarreau185b5c42012-04-26 15:11:51 +02008349/* Fetch an HTTP header's integer value. The integer value is returned. It
8350 * takes a mandatory argument of type string and an optional one of type int
8351 * to designate a specific occurrence. It returns an unsigned integer, which
8352 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008353 */
8354static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008355smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008356 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008357{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008358 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008359
Willy Tarreauf853c462012-04-23 18:53:56 +02008360 if (ret > 0) {
8361 smp->type = SMP_T_UINT;
8362 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8363 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008364
Willy Tarreaud53e2422012-04-16 17:21:11 +02008365 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008366}
8367
Cyril Bonté69fa9922012-10-25 00:01:06 +02008368/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8369 * and an optional one of type int to designate a specific occurrence.
8370 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008371 */
8372static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008373smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008374 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008375{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008376 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008377
Willy Tarreau185b5c42012-04-26 15:11:51 +02008378 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008379 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8380 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008381 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008382 } else {
8383 struct chunk *temp = sample_get_trash_chunk();
8384 if (smp->data.str.len < temp->size - 1) {
8385 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8386 temp->str[smp->data.str.len] = '\0';
8387 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8388 smp->type = SMP_T_IPV6;
8389 break;
8390 }
8391 }
8392 }
8393
Willy Tarreaud53e2422012-04-16 17:21:11 +02008394 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008395 if (!(smp->flags & SMP_F_NOT_LAST))
8396 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008397 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008398 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008399}
8400
Willy Tarreau737b0c12007-06-10 21:28:46 +02008401/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8402 * the first '/' after the possible hostname, and ends before the possible '?'.
8403 */
8404static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008405smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008406 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008407{
8408 struct http_txn *txn = l7;
8409 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008410
Willy Tarreauc0239e02012-04-16 14:42:55 +02008411 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008412
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008414 ptr = http_get_path(txn);
8415 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008416 return 0;
8417
8418 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008419 smp->type = SMP_T_CSTR;
8420 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008421
8422 while (ptr < end && *ptr != '?')
8423 ptr++;
8424
Willy Tarreauf853c462012-04-23 18:53:56 +02008425 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008426 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008427 return 1;
8428}
8429
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008430/* This produces a concatenation of the first occurrence of the Host header
8431 * followed by the path component if it begins with a slash ('/'). This means
8432 * that '*' will not be added, resulting in exactly the first Host entry.
8433 * If no Host header is found, then the path is returned as-is. The returned
8434 * value is stored in the trash so it does not need to be marked constant.
8435 */
8436static int
8437smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8438 const struct arg *args, struct sample *smp)
8439{
8440 struct http_txn *txn = l7;
8441 char *ptr, *end, *beg;
8442 struct hdr_ctx ctx;
8443
8444 CHECK_HTTP_MESSAGE_FIRST();
8445
8446 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008447 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008448 !ctx.vlen)
8449 return smp_fetch_path(px, l4, l7, opt, args, smp);
8450
8451 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008452 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008453 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008454 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008455 smp->data.str.len = ctx.vlen;
8456
8457 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008458 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008459 beg = http_get_path(txn);
8460 if (!beg)
8461 beg = end;
8462
8463 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8464
8465 if (beg < ptr && *beg == '/') {
8466 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8467 smp->data.str.len += ptr - beg;
8468 }
8469
8470 smp->flags = SMP_F_VOL_1ST;
8471 return 1;
8472}
8473
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008474static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008475acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008476 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008477{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008478 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8479 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8480 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008481
Willy Tarreau24e32d82012-04-23 23:55:44 +02008482 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008483
Willy Tarreauf853c462012-04-23 18:53:56 +02008484 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008485 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008486 return 1;
8487}
8488
Willy Tarreau7f18e522010-10-22 20:04:13 +02008489/* return a valid test if the current request is the first one on the connection */
8490static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008491acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008492 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008493{
8494 if (!s)
8495 return 0;
8496
Willy Tarreauf853c462012-04-23 18:53:56 +02008497 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008498 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008499 return 1;
8500}
8501
Willy Tarreau34db1082012-04-19 17:16:54 +02008502/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008503static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008504acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008505 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008506{
8507
Willy Tarreau24e32d82012-04-23 23:55:44 +02008508 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008509 return 0;
8510
Willy Tarreauc0239e02012-04-16 14:42:55 +02008511 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008512
Willy Tarreauc0239e02012-04-16 14:42:55 +02008513 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008514 return 0;
8515
Willy Tarreauf853c462012-04-23 18:53:56 +02008516 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008517 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 +01008518 return 1;
8519}
Willy Tarreau8797c062007-05-07 00:55:35 +02008520
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008521/* Accepts exactly 1 argument of type userlist */
8522static int
8523acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8524 const struct arg *args, struct sample *smp)
8525{
8526
8527 if (!args || args->type != ARGT_USR)
8528 return 0;
8529
8530 CHECK_HTTP_MESSAGE_FIRST();
8531
8532 if (!get_http_auth(l4))
8533 return 0;
8534
8535 /* acl_match_auth() will need several information at once */
8536 smp->ctx.a[0] = args->data.usr; /* user list */
8537 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8538 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8539
8540 /* if the user does not belong to the userlist or has a wrong password,
8541 * report that it unconditionally does not match. Otherwise we return
8542 * a non-zero integer which will be ignored anyway since all the params
8543 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8544 */
8545 smp->type = SMP_T_BOOL;
8546 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8547 if (smp->data.uint)
8548 smp->type = SMP_T_UINT;
8549
8550 return 1;
8551}
8552
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008553/* Try to find the next occurrence of a cookie name in a cookie header value.
8554 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8555 * the cookie value is returned into *value and *value_l, and the function
8556 * returns a pointer to the next pointer to search from if the value was found.
8557 * Otherwise if the cookie was not found, NULL is returned and neither value
8558 * nor value_l are touched. The input <hdr> string should first point to the
8559 * header's value, and the <hdr_end> pointer must point to the first character
8560 * not part of the value. <list> must be non-zero if value may represent a list
8561 * of values (cookie headers). This makes it faster to abort parsing when no
8562 * list is expected.
8563 */
8564static char *
8565extract_cookie_value(char *hdr, const char *hdr_end,
8566 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008567 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008568{
8569 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8570 char *next;
8571
8572 /* we search at least a cookie name followed by an equal, and more
8573 * generally something like this :
8574 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8575 */
8576 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8577 /* Iterate through all cookies on this line */
8578
8579 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8580 att_beg++;
8581
8582 /* find att_end : this is the first character after the last non
8583 * space before the equal. It may be equal to hdr_end.
8584 */
8585 equal = att_end = att_beg;
8586
8587 while (equal < hdr_end) {
8588 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8589 break;
8590 if (http_is_spht[(unsigned char)*equal++])
8591 continue;
8592 att_end = equal;
8593 }
8594
8595 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8596 * is between <att_beg> and <equal>, both may be identical.
8597 */
8598
8599 /* look for end of cookie if there is an equal sign */
8600 if (equal < hdr_end && *equal == '=') {
8601 /* look for the beginning of the value */
8602 val_beg = equal + 1;
8603 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8604 val_beg++;
8605
8606 /* find the end of the value, respecting quotes */
8607 next = find_cookie_value_end(val_beg, hdr_end);
8608
8609 /* make val_end point to the first white space or delimitor after the value */
8610 val_end = next;
8611 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8612 val_end--;
8613 } else {
8614 val_beg = val_end = next = equal;
8615 }
8616
8617 /* We have nothing to do with attributes beginning with '$'. However,
8618 * they will automatically be removed if a header before them is removed,
8619 * since they're supposed to be linked together.
8620 */
8621 if (*att_beg == '$')
8622 continue;
8623
8624 /* Ignore cookies with no equal sign */
8625 if (equal == next)
8626 continue;
8627
8628 /* Now we have the cookie name between att_beg and att_end, and
8629 * its value between val_beg and val_end.
8630 */
8631
8632 if (att_end - att_beg == cookie_name_l &&
8633 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8634 /* let's return this value and indicate where to go on from */
8635 *value = val_beg;
8636 *value_l = val_end - val_beg;
8637 return next + 1;
8638 }
8639
8640 /* Set-Cookie headers only have the name in the first attr=value part */
8641 if (!list)
8642 break;
8643 }
8644
8645 return NULL;
8646}
8647
Willy Tarreaue333ec92012-04-16 16:26:40 +02008648/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008649 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8650 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008651 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008652 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008653 * Accepts exactly 1 argument of type string. If the input options indicate
8654 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008655 */
8656static int
Willy Tarreau51539362012-05-08 12:46:28 +02008657smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8658 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008659{
8660 struct http_txn *txn = l7;
8661 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008662 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008663 const struct http_msg *msg;
8664 const char *hdr_name;
8665 int hdr_name_len;
8666 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008667 int occ = 0;
8668 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008669
Willy Tarreau24e32d82012-04-23 23:55:44 +02008670 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008671 return 0;
8672
Willy Tarreaue333ec92012-04-16 16:26:40 +02008673 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008674
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008675 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008676 msg = &txn->req;
8677 hdr_name = "Cookie";
8678 hdr_name_len = 6;
8679 } else {
8680 msg = &txn->rsp;
8681 hdr_name = "Set-Cookie";
8682 hdr_name_len = 10;
8683 }
8684
Willy Tarreau28376d62012-04-26 21:26:10 +02008685 if (!occ && !(opt & SMP_OPT_ITERATE))
8686 /* no explicit occurrence and single fetch => last cookie by default */
8687 occ = -1;
8688
8689 /* OK so basically here, either we want only one value and it's the
8690 * last one, or we want to iterate over all of them and we fetch the
8691 * next one.
8692 */
8693
Willy Tarreau9b28e032012-10-12 23:49:43 +02008694 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008695 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008696 /* search for the header from the beginning, we must first initialize
8697 * the search parameters.
8698 */
Willy Tarreau37406352012-04-23 16:16:37 +02008699 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008700 ctx->idx = 0;
8701 }
8702
Willy Tarreau28376d62012-04-26 21:26:10 +02008703 smp->flags |= SMP_F_VOL_HDR;
8704
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008705 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008706 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8707 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008708 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8709 goto out;
8710
Willy Tarreau24e32d82012-04-23 23:55:44 +02008711 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008712 continue;
8713
Willy Tarreau37406352012-04-23 16:16:37 +02008714 smp->ctx.a[0] = ctx->line + ctx->val;
8715 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008716 }
8717
Willy Tarreauf853c462012-04-23 18:53:56 +02008718 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008719 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008720 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008721 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008722 &smp->data.str.str,
8723 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008724 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008725 found = 1;
8726 if (occ >= 0) {
8727 /* one value was returned into smp->data.str.{str,len} */
8728 smp->flags |= SMP_F_NOT_LAST;
8729 return 1;
8730 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008731 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008732 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008733 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008734 /* all cookie headers and values were scanned. If we're looking for the
8735 * last occurrence, we may return it now.
8736 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008737 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008738 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008739 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008740}
8741
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008742/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008743 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008744 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008745 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008746 */
8747static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008748acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008749 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008750{
8751 struct http_txn *txn = l7;
8752 struct hdr_idx *idx = &txn->hdr_idx;
8753 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008754 const struct http_msg *msg;
8755 const char *hdr_name;
8756 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008757 int cnt;
8758 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008759 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008760
Willy Tarreau24e32d82012-04-23 23:55:44 +02008761 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008762 return 0;
8763
Willy Tarreaue333ec92012-04-16 16:26:40 +02008764 CHECK_HTTP_MESSAGE_FIRST();
8765
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008766 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008767 msg = &txn->req;
8768 hdr_name = "Cookie";
8769 hdr_name_len = 6;
8770 } else {
8771 msg = &txn->rsp;
8772 hdr_name = "Set-Cookie";
8773 hdr_name_len = 10;
8774 }
8775
Willy Tarreau9b28e032012-10-12 23:49:43 +02008776 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008777 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008778 ctx.idx = 0;
8779 cnt = 0;
8780
8781 while (1) {
8782 /* Note: val_beg == NULL every time we need to fetch a new header */
8783 if (!val_beg) {
8784 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8785 break;
8786
Willy Tarreau24e32d82012-04-23 23:55:44 +02008787 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008788 continue;
8789
8790 val_beg = ctx.line + ctx.val;
8791 val_end = val_beg + ctx.vlen;
8792 }
8793
Willy Tarreauf853c462012-04-23 18:53:56 +02008794 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008795 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008796 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008797 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008798 &smp->data.str.str,
8799 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008800 cnt++;
8801 }
8802 }
8803
Willy Tarreauf853c462012-04-23 18:53:56 +02008804 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008805 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008806 return 1;
8807}
8808
Willy Tarreau51539362012-05-08 12:46:28 +02008809/* Fetch an cookie's integer value. The integer value is returned. It
8810 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8811 */
8812static int
8813smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8814 const struct arg *args, struct sample *smp)
8815{
8816 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8817
8818 if (ret > 0) {
8819 smp->type = SMP_T_UINT;
8820 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8821 }
8822
8823 return ret;
8824}
8825
Willy Tarreau8797c062007-05-07 00:55:35 +02008826/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008827/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008828/************************************************************************/
8829
David Cournapeau16023ee2010-12-23 20:55:41 +09008830/*
8831 * Given a path string and its length, find the position of beginning of the
8832 * query string. Returns NULL if no query string is found in the path.
8833 *
8834 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8835 *
8836 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8837 */
bedis4c75cca2012-10-05 08:38:24 +02008838static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008839{
8840 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008841
bedis4c75cca2012-10-05 08:38:24 +02008842 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008843 return p ? p + 1 : NULL;
8844}
8845
bedis4c75cca2012-10-05 08:38:24 +02008846static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008847{
bedis4c75cca2012-10-05 08:38:24 +02008848 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008849}
8850
8851/*
8852 * Given a url parameter, find the starting position of the first occurence,
8853 * or NULL if the parameter is not found.
8854 *
8855 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8856 * the function will return query_string+8.
8857 */
8858static char*
8859find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008860 char* url_param_name, size_t url_param_name_l,
8861 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008862{
8863 char *pos, *last;
8864
8865 pos = query_string;
8866 last = query_string + query_string_l - url_param_name_l - 1;
8867
8868 while (pos <= last) {
8869 if (pos[url_param_name_l] == '=') {
8870 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8871 return pos;
8872 pos += url_param_name_l + 1;
8873 }
bedis4c75cca2012-10-05 08:38:24 +02008874 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008875 pos++;
8876 pos++;
8877 }
8878 return NULL;
8879}
8880
8881/*
8882 * Given a url parameter name, returns its value and size into *value and
8883 * *value_l respectively, and returns non-zero. If the parameter is not found,
8884 * zero is returned and value/value_l are not touched.
8885 */
8886static int
8887find_url_param_value(char* path, size_t path_l,
8888 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008889 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008890{
8891 char *query_string, *qs_end;
8892 char *arg_start;
8893 char *value_start, *value_end;
8894
bedis4c75cca2012-10-05 08:38:24 +02008895 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008896 if (!query_string)
8897 return 0;
8898
8899 qs_end = path + path_l;
8900 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008901 url_param_name, url_param_name_l,
8902 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008903 if (!arg_start)
8904 return 0;
8905
8906 value_start = arg_start + url_param_name_l + 1;
8907 value_end = value_start;
8908
bedis4c75cca2012-10-05 08:38:24 +02008909 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008910 value_end++;
8911
8912 *value = value_start;
8913 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008914 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008915}
8916
8917static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008918smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8919 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008920{
bedis4c75cca2012-10-05 08:38:24 +02008921 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008922 struct http_txn *txn = l7;
8923 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008924
bedis4c75cca2012-10-05 08:38:24 +02008925 if (!args || args[0].type != ARGT_STR ||
8926 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008927 return 0;
8928
8929 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008930
bedis4c75cca2012-10-05 08:38:24 +02008931 if (args[1].type)
8932 delim = *args[1].data.str.str;
8933
Willy Tarreau9b28e032012-10-12 23:49:43 +02008934 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008935 args->data.str.str, args->data.str.len,
8936 &smp->data.str.str, &smp->data.str.len,
8937 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008938 return 0;
8939
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008940 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008941 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008942 return 1;
8943}
8944
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008945/* Return the signed integer value for the specified url parameter (see url_param
8946 * above).
8947 */
8948static int
8949smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8950 const struct arg *args, struct sample *smp)
8951{
8952 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8953
8954 if (ret > 0) {
8955 smp->type = SMP_T_UINT;
8956 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8957 }
8958
8959 return ret;
8960}
8961
Willy Tarreau185b5c42012-04-26 15:11:51 +02008962/* This function is used to validate the arguments passed to any "hdr" fetch
8963 * keyword. These keywords support an optional positive or negative occurrence
8964 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8965 * is assumed that the types are already the correct ones. Returns 0 on error,
8966 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8967 * error message in case of error, that the caller is responsible for freeing.
8968 * The initial location must either be freeable or NULL.
8969 */
8970static int val_hdr(struct arg *arg, char **err_msg)
8971{
8972 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008973 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008974 return 0;
8975 }
8976 return 1;
8977}
8978
Willy Tarreau4a568972010-05-12 08:08:50 +02008979/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008980/* All supported ACL keywords must be declared here. */
8981/************************************************************************/
8982
8983/* Note: must not be declared <const> as its list will be overwritten.
8984 * Please take care of keeping this list alphabetically sorted.
8985 */
8986static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008987 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8988 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8989 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8990 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8991 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8992 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8993 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8994 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8995
Willy Tarreau51539362012-05-08 12:46:28 +02008996 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8997 { "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 +02008998 { "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 +02008999 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9000 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9001 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9002 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9003 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9004 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9005 { "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 +02009006
Willy Tarreau185b5c42012-04-26 15:11:51 +02009007 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9008 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9009 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9010 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9011 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9012 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9013 { "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 },
9014 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9015 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9016 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9017 { "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 +02009018
9019 { "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 +02009020 { "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 +02009021 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9022
9023 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9024
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009025 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9026 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9027 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9028 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9029 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9030 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9031 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9032 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009033
9034 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9035 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9036 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
9037
Willy Tarreau51539362012-05-08 12:46:28 +02009038 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9039 { "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 +02009040 { "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 +02009041 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9042 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9043 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9044 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9045 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9046 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9047 { "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 +02009048
Willy Tarreau185b5c42012-04-26 15:11:51 +02009049 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9050 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9051 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9052 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9053 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9054 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9055 { "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 },
9056 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9057 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9058 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9059 { "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 +02009060
9061 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9062
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009063 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9064 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9065 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9066 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9067 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9068 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9069 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9070 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9071 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9072 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009073
9074 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9075 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9076 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9077 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9078 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9079 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9080 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9081 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9082 { "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 +02009083 { "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 +02009084
9085 { NULL, NULL, NULL, NULL },
9086}};
9087
9088/************************************************************************/
9089/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009090/************************************************************************/
9091/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009092static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009093 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9094 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9095 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9096 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9097 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9098 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9099 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9100 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9101 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02009102 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009103}};
9104
Willy Tarreau8797c062007-05-07 00:55:35 +02009105
9106__attribute__((constructor))
9107static void __http_protocol_init(void)
9108{
9109 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009110 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009111}
9112
9113
Willy Tarreau58f10d72006-12-04 02:26:12 +01009114/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009115 * Local variables:
9116 * c-indent-level: 8
9117 * c-basic-offset: 8
9118 * End:
9119 */