blob: ab097a9dd112e67ed2dac5a9844fd597d54fcf94 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010045#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/backend.h>
47#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010048#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020051#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010053#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020054#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010061#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020062#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/task.h>
64
Willy Tarreau522d6c02009-12-06 18:49:18 +010065const char HTTP_100[] =
66 "HTTP/1.1 100 Continue\r\n\r\n";
67
68const struct chunk http_100_chunk = {
69 .str = (char *)&HTTP_100,
70 .len = sizeof(HTTP_100)-1
71};
72
Willy Tarreaua9679ac2010-01-03 17:32:57 +010073/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Location: "; /* not terminated since it will be concatenated with the URL */
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
86/* same as 302 except that the browser MUST retry with the GET method */
87const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
94const char *HTTP_401_fmt =
95 "HTTP/1.0 401 Unauthorized\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020098 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
100 "\r\n"
101 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
102
Willy Tarreau844a7e72010-01-31 21:46:18 +0100103const char *HTTP_407_fmt =
104 "HTTP/1.0 407 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Content-Type: text/html\r\n"
108 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200114 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100115 [HTTP_ERR_400] = 400,
116 [HTTP_ERR_403] = 403,
117 [HTTP_ERR_408] = 408,
118 [HTTP_ERR_500] = 500,
119 [HTTP_ERR_502] = 502,
120 [HTTP_ERR_503] = 503,
121 [HTTP_ERR_504] = 504,
122};
123
Willy Tarreau80587432006-12-24 17:47:20 +0100124static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200125 [HTTP_ERR_200] =
126 "HTTP/1.0 200 OK\r\n"
127 "Cache-Control: no-cache\r\n"
128 "Connection: close\r\n"
129 "Content-Type: text/html\r\n"
130 "\r\n"
131 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
132
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100134 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 "Cache-Control: no-cache\r\n"
136 "Connection: close\r\n"
137 "Content-Type: text/html\r\n"
138 "\r\n"
139 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
140
141 [HTTP_ERR_403] =
142 "HTTP/1.0 403 Forbidden\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
148
149 [HTTP_ERR_408] =
150 "HTTP/1.0 408 Request Time-out\r\n"
151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
156
157 [HTTP_ERR_500] =
158 "HTTP/1.0 500 Server Error\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
164
165 [HTTP_ERR_502] =
166 "HTTP/1.0 502 Bad Gateway\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
172
173 [HTTP_ERR_503] =
174 "HTTP/1.0 503 Service Unavailable\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
180
181 [HTTP_ERR_504] =
182 "HTTP/1.0 504 Gateway Time-out\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
188
189};
190
Cyril Bonté19979e12012-04-04 12:57:21 +0200191/* status codes available for the stats admin page (strictly 4 chars length) */
192const char *stat_status_codes[STAT_STATUS_SIZE] = {
193 [STAT_STATUS_DENY] = "DENY",
194 [STAT_STATUS_DONE] = "DONE",
195 [STAT_STATUS_ERRP] = "ERRP",
196 [STAT_STATUS_EXCD] = "EXCD",
197 [STAT_STATUS_NONE] = "NONE",
198 [STAT_STATUS_PART] = "PART",
199 [STAT_STATUS_UNKN] = "UNKN",
200};
201
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203/* We must put the messages here since GCC cannot initialize consts depending
204 * on strlen().
205 */
206struct chunk http_err_chunks[HTTP_ERR_SIZE];
207
Willy Tarreau42250582007-04-01 01:30:43 +0200208#define FD_SETS_ARE_BITFIELDS
209#ifdef FD_SETS_ARE_BITFIELDS
210/*
211 * This map is used with all the FD_* macros to check whether a particular bit
212 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
213 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
214 * byte should be encoded. Be careful to always pass bytes from 0 to 255
215 * exclusively to the macros.
216 */
217fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
218fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219
220#else
221#error "Check if your OS uses bitfields for fd_sets"
222#endif
223
Willy Tarreau80587432006-12-24 17:47:20 +0100224void init_proto_http()
225{
Willy Tarreau42250582007-04-01 01:30:43 +0200226 int i;
227 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100228 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200229
Willy Tarreau80587432006-12-24 17:47:20 +0100230 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
231 if (!http_err_msgs[msg]) {
232 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
233 abort();
234 }
235
236 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
237 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
238 }
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240 /* initialize the log header encoding map : '{|}"#' should be encoded with
241 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
242 * URL encoding only requires '"', '#' to be encoded as well as non-
243 * printable characters above.
244 */
245 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
246 memset(url_encode_map, 0, sizeof(url_encode_map));
247 for (i = 0; i < 32; i++) {
248 FD_SET(i, hdr_encode_map);
249 FD_SET(i, url_encode_map);
250 }
251 for (i = 127; i < 256; i++) {
252 FD_SET(i, hdr_encode_map);
253 FD_SET(i, url_encode_map);
254 }
255
256 tmp = "\"#{|}";
257 while (*tmp) {
258 FD_SET(*tmp, hdr_encode_map);
259 tmp++;
260 }
261
262 tmp = "\"#";
263 while (*tmp) {
264 FD_SET(*tmp, url_encode_map);
265 tmp++;
266 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200267
268 /* memory allocations */
269 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200270 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100271 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100272}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274/*
275 * We have 26 list of methods (1 per first letter), each of which can have
276 * up to 3 entries (2 valid, 1 null).
277 */
278struct http_method_desc {
279 http_meth_t meth;
280 int len;
281 const char text[8];
282};
283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100284const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100285 ['C' - 'A'] = {
286 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
287 },
288 ['D' - 'A'] = {
289 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
290 },
291 ['G' - 'A'] = {
292 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
293 },
294 ['H' - 'A'] = {
295 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
296 },
297 ['P' - 'A'] = {
298 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
299 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
300 },
301 ['T' - 'A'] = {
302 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
303 },
304 /* rest is empty like this :
305 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
306 */
307};
308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100309/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311 * RFC2616 for those chars.
312 */
313
314const char http_is_spht[256] = {
315 [' '] = 1, ['\t'] = 1,
316};
317
318const char http_is_crlf[256] = {
319 ['\r'] = 1, ['\n'] = 1,
320};
321
322const char http_is_lws[256] = {
323 [' '] = 1, ['\t'] = 1,
324 ['\r'] = 1, ['\n'] = 1,
325};
326
327const char http_is_sep[256] = {
328 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
329 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
330 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
331 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
332 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
333};
334
335const char http_is_ctl[256] = {
336 [0 ... 31] = 1,
337 [127] = 1,
338};
339
340/*
341 * A token is any ASCII char that is neither a separator nor a CTL char.
342 * Do not overwrite values in assignment since gcc-2.95 will not handle
343 * them correctly. Instead, define every non-CTL char's status.
344 */
345const char http_is_token[256] = {
346 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
347 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
348 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
349 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
350 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
351 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
352 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
353 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
354 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
355 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
356 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
357 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
358 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
359 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
360 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
361 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
362 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
363 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
364 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
365 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
366 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
367 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
368 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
369 ['|'] = 1, ['}'] = 0, ['~'] = 1,
370};
371
372
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100373/*
374 * An http ver_token is any ASCII which can be found in an HTTP version,
375 * which includes 'H', 'T', 'P', '/', '.' and any digit.
376 */
377const char http_is_ver_token[256] = {
378 ['.'] = 1, ['/'] = 1,
379 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
380 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
381 ['H'] = 1, ['P'] = 1, ['T'] = 1,
382};
383
384
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100386 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
387 */
388#if defined(DEBUG_FSM)
389static void http_silent_debug(int line, struct session *s)
390{
391 int size = 0;
392 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100404
405 write(-1, trash, size);
406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100412 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
413 * CRLF. Text length is measured first, so it cannot be NULL.
414 * The header is also automatically added to the index <hdr_idx>, and the end
415 * of headers is automatically adjusted. The number of bytes added is returned
416 * on success, otherwise <0 is returned indicating an error.
417 */
418int http_header_add_tail(struct buffer *b, struct http_msg *msg,
419 struct hdr_idx *hdr_idx, const char *text)
420{
421 int bytes, len;
422
423 len = strlen(text);
Willy Tarreauea1175a2012-03-05 15:52:30 +0100424 bytes = buffer_insert_line2(b, b->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100425 if (!bytes)
426 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100427 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100428 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
429}
430
431/*
432 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
433 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
434 * the buffer is only opened and the space reserved, but nothing is copied.
435 * The header is also automatically added to the index <hdr_idx>, and the end
436 * of headers is automatically adjusted. The number of bytes added is returned
437 * on success, otherwise <0 is returned indicating an error.
438 */
439int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
440 struct hdr_idx *hdr_idx, const char *text, int len)
441{
442 int bytes;
443
Willy Tarreauea1175a2012-03-05 15:52:30 +0100444 bytes = buffer_insert_line2(b, b->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100445 if (!bytes)
446 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100447 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
449}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
451/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100452 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
453 * If so, returns the position of the first non-space character relative to
454 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
455 * to return a pointer to the place after the first space. Returns 0 if the
456 * header name does not match. Checks are case-insensitive.
457 */
458int http_header_match2(const char *hdr, const char *end,
459 const char *name, int len)
460{
461 const char *val;
462
463 if (hdr + len >= end)
464 return 0;
465 if (hdr[len] != ':')
466 return 0;
467 if (strncasecmp(hdr, name, len) != 0)
468 return 0;
469 val = hdr + len + 1;
470 while (val < end && HTTP_IS_SPHT(*val))
471 val++;
472 if ((val >= end) && (len + 2 <= end - hdr))
473 return len + 2; /* we may replace starting from second space */
474 return val - hdr;
475}
476
Willy Tarreau68085d82010-01-18 14:54:04 +0100477/* Find the end of the header value contained between <s> and <e>. See RFC2616,
478 * par 2.2 for more information. Note that it requires a valid header to return
479 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100481char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482{
483 int quoted, qdpair;
484
485 quoted = qdpair = 0;
486 for (; s < e; s++) {
487 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200488 else if (quoted) {
489 if (*s == '\\') qdpair = 1;
490 else if (*s == '"') quoted = 0;
491 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 else if (*s == '"') quoted = 1;
493 else if (*s == ',') return s;
494 }
495 return s;
496}
497
498/* Find the first or next occurrence of header <name> in message buffer <sol>
499 * using headers index <idx>, and return it in the <ctx> structure. This
500 * structure holds everything necessary to use the header and find next
501 * occurrence. If its <idx> member is 0, the header is searched from the
502 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 * 1 when it finds a value, and 0 when there is no more. It is designed to work
504 * with headers defined as comma-separated lists. As a special case, if ctx->val
505 * is NULL when searching for a new values of a header, the current header is
506 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 */
508int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
Willy Tarreau68085d82010-01-18 14:54:04 +0100512 char *eol, *sov;
513 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515 cur_idx = ctx->idx;
516 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 /* We have previously returned a value, let's search
518 * another one on the same line.
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200521 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100522 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200523 eol = sol + idx->v[cur_idx].len;
524
525 if (sov >= eol)
526 /* no more values in this header */
527 goto next_hdr;
528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 /* values remaining for this header, skip the comma but save it
530 * for later use (eg: for header deletion).
531 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 sov++;
533 while (sov < eol && http_is_lws[(unsigned char)*sov])
534 sov++;
535
536 goto return_hdr;
537 }
538
539 /* first request for this header */
540 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100541 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 while (cur_idx) {
544 eol = sol + idx->v[cur_idx].len;
545
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200546 if (len == 0) {
547 /* No argument was passed, we want any header.
548 * To achieve this, we simply build a fake request. */
549 while (sol + len < eol && sol[len] != ':')
550 len++;
551 name = sol;
552 }
553
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 if ((len < eol - sol) &&
555 (sol[len] == ':') &&
556 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 sov = sol + len + 1;
559 while (sov < eol && http_is_lws[(unsigned char)*sov])
560 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 ctx->prev = old_idx;
564 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 ctx->idx = cur_idx;
566 ctx->val = sov - sol;
567
568 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200570 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200571 eol--;
572 ctx->tws++;
573 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 ctx->vlen = eol - sov;
575 return 1;
576 }
577 next_hdr:
578 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100579 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 cur_idx = idx->v[cur_idx].next;
581 }
582 return 0;
583}
584
585int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200587 struct hdr_ctx *ctx)
588{
589 return http_find_header2(name, strlen(name), sol, idx, ctx);
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Remove one value of a header. This only works on a <ctx> returned by one of
593 * the http_find_header functions. The value is removed, as well as surrounding
594 * commas if any. If the removed value was alone, the whole header is removed.
595 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
596 * message <msg>. The new index is returned. If it is zero, it means there is
597 * no more header, so any processing may stop. The ctx is always left in a form
598 * that can be handled by http_find_header2() to find next occurrence.
599 */
600int http_remove_header2(struct http_msg *msg, struct buffer *buf,
601 struct hdr_idx *idx, struct hdr_ctx *ctx)
602{
603 int cur_idx = ctx->idx;
604 char *sol = ctx->line;
605 struct hdr_idx_elem *hdr;
606 int delta, skip_comma;
607
608 if (!cur_idx)
609 return 0;
610
611 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200612 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* This was the only value of the header, we must now remove it entirely. */
614 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
615 http_msg_move_end(msg, delta);
616 idx->used--;
617 hdr->len = 0; /* unused entry */
618 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100619 if (idx->tail == ctx->idx)
620 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 ctx->idx = ctx->prev; /* walk back to the end of previous header */
622 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
623 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200624 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 return ctx->idx;
626 }
627
628 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200629 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
630 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100631 */
632
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200635 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 NULL, 0);
637 hdr->len += delta;
638 http_msg_move_end(msg, delta);
639 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200640 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 return ctx->idx;
642}
643
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644/* This function handles a server error at the stream interface level. The
645 * stream interface is assumed to be already in a closed state. An optional
646 * message is copied into the input buffer, and an HTTP status code stored.
647 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100648 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100650static void http_server_error(struct session *t, struct stream_interface *si,
651 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100653 buffer_auto_read(si->ob);
654 buffer_abort(si->ob);
655 buffer_auto_close(si->ob);
656 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200657 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100658 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100659 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100660 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100661 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 }
663 if (!(t->flags & SN_ERR_MASK))
664 t->flags |= err;
665 if (!(t->flags & SN_FINST_MASK))
666 t->flags |= finst;
667}
668
Willy Tarreau80587432006-12-24 17:47:20 +0100669/* This function returns the appropriate error location for the given session
670 * and message.
671 */
672
673struct chunk *error_message(struct session *s, int msgnum)
674{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200675 if (s->be->errmsg[msgnum].str)
676 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100677 else if (s->fe->errmsg[msgnum].str)
678 return &s->fe->errmsg[msgnum];
679 else
680 return &http_err_chunks[msgnum];
681}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683/*
684 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
685 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
686 */
687static http_meth_t find_http_meth(const char *str, const int len)
688{
689 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100690 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100691
692 m = ((unsigned)*str - 'A');
693
694 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 for (h = http_methods[m]; h->len > 0; h++) {
696 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100698 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100700 };
701 return HTTP_METH_OTHER;
702 }
703 return HTTP_METH_NONE;
704
705}
706
Willy Tarreau21d2af32008-02-14 20:25:24 +0100707/* Parse the URI from the given transaction (which is assumed to be in request
708 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
709 * It is returned otherwise.
710 */
711static char *
712http_get_path(struct http_txn *txn)
713{
714 char *ptr, *end;
715
Willy Tarreau962c3f42010-01-10 00:15:35 +0100716 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100717 end = ptr + txn->req.sl.rq.u_l;
718
719 if (ptr >= end)
720 return NULL;
721
722 /* RFC2616, par. 5.1.2 :
723 * Request-URI = "*" | absuri | abspath | authority
724 */
725
726 if (*ptr == '*')
727 return NULL;
728
729 if (isalpha((unsigned char)*ptr)) {
730 /* this is a scheme as described by RFC3986, par. 3.1 */
731 ptr++;
732 while (ptr < end &&
733 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
734 ptr++;
735 /* skip '://' */
736 if (ptr == end || *ptr++ != ':')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 if (ptr == end || *ptr++ != '/')
741 return NULL;
742 }
743 /* skip [user[:passwd]@]host[:[port]] */
744
745 while (ptr < end && *ptr != '/')
746 ptr++;
747
748 if (ptr == end)
749 return NULL;
750
751 /* OK, we got the '/' ! */
752 return ptr;
753}
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755/* Returns a 302 for a redirectable request. This may only be called just after
756 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
757 * left unchanged and will follow normal proxy processing.
758 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760{
761 struct http_txn *txn;
762 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764 char *path;
765 int len;
766
767 /* 1: create the response header */
768 rdr.len = strlen(HTTP_302);
769 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100770 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 memcpy(rdr.str, HTTP_302, rdr.len);
772
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 srv = target_srv(&s->target);
774
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100777 return;
778
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
781 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
782 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100783 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
785 /* 3: add the request URI */
786 txn = &s->txn;
787 path = http_get_path(txn);
788 if (!path)
789 return;
790
Willy Tarreau962c3f42010-01-10 00:15:35 +0100791 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200792 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793 return;
794
795 memcpy(rdr.str + rdr.len, path, len);
796 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100797
798 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
799 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
800 rdr.len += 29;
801 } else {
802 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
803 rdr.len += 23;
804 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100805
806 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100807 si->shutr(si);
808 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809 si->err_type = SI_ET_NONE;
810 si->err_loc = NULL;
811 si->state = SI_ST_CLO;
812
813 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100814 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100815
816 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 if (srv)
818 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819}
820
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100821/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 * that the server side is closed. Note that err_type is actually a
823 * bitmask, where almost only aborts may be cumulated with other
824 * values. We consider that aborted operations are more important
825 * than timeouts or errors due to the fact that nobody else in the
826 * logs might explain incomplete retries. All others should avoid
827 * being cumulated. It should normally not be possible to have multiple
828 * aborts at once, but just in case, the first one in sequence is reported.
829 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100832 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833
834 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
836 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
839 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100841 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
842 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
845 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
848 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
851 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
854 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855}
856
Willy Tarreau42250582007-04-01 01:30:43 +0200857extern const char sess_term_cond[8];
858extern const char sess_fin_state[8];
859extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200860struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200861struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100862struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100863
Willy Tarreau117f59e2007-03-04 18:17:17 +0100864/*
865 * Capture headers from message starting at <som> according to header list
866 * <cap_hdr>, and fill the <idx> structure appropriately.
867 */
868void capture_headers(char *som, struct hdr_idx *idx,
869 char **cap, struct cap_hdr *cap_hdr)
870{
871 char *eol, *sol, *col, *sov;
872 int cur_idx;
873 struct cap_hdr *h;
874 int len;
875
876 sol = som + hdr_idx_first_pos(idx);
877 cur_idx = hdr_idx_first_idx(idx);
878
879 while (cur_idx) {
880 eol = sol + idx->v[cur_idx].len;
881
882 col = sol;
883 while (col < eol && *col != ':')
884 col++;
885
886 sov = col + 1;
887 while (sov < eol && http_is_lws[(unsigned char)*sov])
888 sov++;
889
890 for (h = cap_hdr; h; h = h->next) {
891 if ((h->namelen == col - sol) &&
892 (strncasecmp(sol, h->name, h->namelen) == 0)) {
893 if (cap[h->index] == NULL)
894 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200895 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100896
897 if (cap[h->index] == NULL) {
898 Alert("HTTP capture : out of memory.\n");
899 continue;
900 }
901
902 len = eol - sov;
903 if (len > h->len)
904 len = h->len;
905
906 memcpy(cap[h->index], sov, len);
907 cap[h->index][len]=0;
908 }
909 }
910 sol = eol + idx->v[cur_idx].cr + 1;
911 cur_idx = idx->v[cur_idx].next;
912 }
913}
914
915
Willy Tarreau42250582007-04-01 01:30:43 +0200916/* either we find an LF at <ptr> or we jump to <bad>.
917 */
918#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
919
920/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
921 * otherwise to <http_msg_ood> with <state> set to <st>.
922 */
923#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
924 ptr++; \
925 if (likely(ptr < end)) \
926 goto good; \
927 else { \
928 state = (st); \
929 goto http_msg_ood; \
930 } \
931 } while (0)
932
933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100935 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100936 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
937 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
938 * will give undefined results.
939 * Note that it is upon the caller's responsibility to ensure that ptr < end,
940 * and that msg->sol points to the beginning of the response.
941 * If a complete line is found (which implies that at least one CR or LF is
942 * found before <end>, the updated <ptr> is returned, otherwise NULL is
943 * returned indicating an incomplete line (which does not mean that parts have
944 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
945 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
946 * upon next call.
947 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200948 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100949 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
950 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100953const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
Willy Tarreaua458b672012-03-05 11:17:50 +0100954 const char *msg_start,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100955 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100956 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100957{
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100959 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200960 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100961 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
963
964 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100965 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
967 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100968 state = HTTP_MSG_ERROR;
969 break;
970
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200972 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100973 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 goto http_msg_rpcode;
976 }
977 if (likely(HTTP_IS_SPHT(*ptr)))
978 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
979 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100980 state = HTTP_MSG_ERROR;
981 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100982
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200984 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 if (likely(!HTTP_IS_LWS(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
987
988 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100989 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
991 }
992
993 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100994 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 http_msg_rsp_reason:
996 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100997 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 msg->sl.st.r_l = 0;
999 goto http_msg_rpline_eol;
1000
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001002 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001004 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 goto http_msg_rpreason;
1006 }
1007 if (likely(HTTP_IS_SPHT(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1009 /* so it's a CR/LF, so there is no reason phrase */
1010 goto http_msg_rsp_reason;
1011
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001013 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 if (likely(!HTTP_IS_CRLF(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 http_msg_rpline_eol:
1018 /* We have seen the end of line. Note that we do not
1019 * necessarily have the \n yet, but at least we know that we
1020 * have EITHER \r OR \n, otherwise the response would not be
1021 * complete. We can then record the response length and return
1022 * to the caller which will be able to register it.
1023 */
1024 msg->sl.st.l = ptr - msg->sol;
1025 return ptr;
1026
1027#ifdef DEBUG_FULL
1028 default:
1029 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1030 exit(1);
1031#endif
1032 }
1033
1034 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001035 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 if (ret_state)
1037 *ret_state = state;
1038 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001039 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001041}
1042
Willy Tarreau8973c702007-01-21 23:58:29 +01001043/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 * This function parses a request line between <ptr> and <end>, starting with
1045 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1046 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1047 * will give undefined results.
1048 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1049 * and that msg->sol points to the beginning of the request.
1050 * If a complete line is found (which implies that at least one CR or LF is
1051 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1052 * returned indicating an incomplete line (which does not mean that parts have
1053 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1054 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1055 * upon next call.
1056 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001058 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1059 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001060 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001062const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
Willy Tarreaua458b672012-03-05 11:17:50 +01001063 const char *msg_start,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001064 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001065 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001069 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 if (likely(HTTP_IS_TOKEN(*ptr)))
1071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001074 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_CRLF(*ptr))) {
1079 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 msg->sl.rq.v_l = 0;
1088 goto http_msg_rqline_eol;
1089 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001090 state = HTTP_MSG_ERROR;
1091 break;
1092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001094 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 goto http_msg_rquri;
1098 }
1099 if (likely(HTTP_IS_SPHT(*ptr)))
1100 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1101 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1102 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001105 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001106 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001110 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001114 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001115 /* non-ASCII chars are forbidden unless option
1116 * accept-invalid-http-request is enabled in the frontend.
1117 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001119 if (msg->err_pos < -1)
1120 goto invalid_char;
1121 if (msg->err_pos == -1)
1122 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1124 }
1125
1126 if (likely(HTTP_IS_CRLF(*ptr))) {
1127 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1128 goto http_msg_req09_uri_e;
1129 }
1130
1131 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001132 invalid_char:
1133 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 goto http_msg_rqver;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1145 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1146 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001152
1153 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 http_msg_rqline_eol:
1156 /* We have seen the end of line. Note that we do not
1157 * necessarily have the \n yet, but at least we know that we
1158 * have EITHER \r OR \n, otherwise the request would not be
1159 * complete. We can then record the request length and return
1160 * to the caller which will be able to register it.
1161 */
1162 msg->sl.rq.l = ptr - msg->sol;
1163 return ptr;
1164 }
1165
1166 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001167 state = HTTP_MSG_ERROR;
1168 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170#ifdef DEBUG_FULL
1171 default:
1172 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1173 exit(1);
1174#endif
1175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001178 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 if (ret_state)
1180 *ret_state = state;
1181 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001182 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001186/*
1187 * Returns the data from Authorization header. Function may be called more
1188 * than once so data is stored in txn->auth_data. When no header is found
1189 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1190 * searching again for something we are unable to find anyway.
1191 */
1192
1193char get_http_auth_buff[BUFSIZE];
1194
1195int
1196get_http_auth(struct session *s)
1197{
1198
1199 struct http_txn *txn = &s->txn;
1200 struct chunk auth_method;
1201 struct hdr_ctx ctx;
1202 char *h, *p;
1203 int len;
1204
1205#ifdef DEBUG_AUTH
1206 printf("Auth for session %p: %d\n", s, txn->auth.method);
1207#endif
1208
1209 if (txn->auth.method == HTTP_AUTH_WRONG)
1210 return 0;
1211
1212 if (txn->auth.method)
1213 return 1;
1214
1215 txn->auth.method = HTTP_AUTH_WRONG;
1216
1217 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001218
1219 if (txn->flags & TX_USE_PX_CONN) {
1220 h = "Proxy-Authorization";
1221 len = strlen(h);
1222 } else {
1223 h = "Authorization";
1224 len = strlen(h);
1225 }
1226
1227 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001228 return 0;
1229
1230 h = ctx.line + ctx.val;
1231
1232 p = memchr(h, ' ', ctx.vlen);
1233 if (!p || p == h)
1234 return 0;
1235
1236 chunk_initlen(&auth_method, h, 0, p-h);
1237 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1238
1239 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1240
1241 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1242 get_http_auth_buff, BUFSIZE - 1);
1243
1244 if (len < 0)
1245 return 0;
1246
1247
1248 get_http_auth_buff[len] = '\0';
1249
1250 p = strchr(get_http_auth_buff, ':');
1251
1252 if (!p)
1253 return 0;
1254
1255 txn->auth.user = get_http_auth_buff;
1256 *p = '\0';
1257 txn->auth.pass = p+1;
1258
1259 txn->auth.method = HTTP_AUTH_BASIC;
1260 return 1;
1261 }
1262
1263 return 0;
1264}
1265
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8973c702007-01-21 23:58:29 +01001267/*
1268 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001269 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001270 * when data are missing and recalled at the exact same location with no
1271 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001273 * fields. Note that msg->som and msg->sol will be initialized after completing
1274 * the first state, so that none of the msg pointers has to be initialized
1275 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1278{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001279 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 state = msg->msg_state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001283 ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001284 end = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 if (unlikely(ptr >= end))
1287 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 /*
1291 * First, states that are specific to the response only.
1292 * We check them first so that request and headers are
1293 * closer to each other (accessed more often).
1294 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001296 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001298 /* we have a start of message, but we have to check
1299 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001300 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001301 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001302 char *beg = buf->p;
1303
Willy Tarreau15de77e2010-01-02 21:59:16 +01001304 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001305 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001306 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001307 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 }
Willy Tarreauea1175a2012-03-05 15:52:30 +01001310 msg->som = ptr - buf->p;
Willy Tarreau816b9792009-09-15 21:25:21 +02001311 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 hdr_idx_init(idx);
1313 state = HTTP_MSG_RPVER;
1314 goto http_msg_rpver;
1315 }
1316
1317 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1318 goto http_msg_invalid;
1319
1320 if (unlikely(*ptr == '\n'))
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1323 /* stop here */
1324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EXPECT_LF_HERE(ptr, http_msg_invalid);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 /* stop here */
1330
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001332 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPVER_SP:
1334 case HTTP_MSG_RPCODE:
1335 case HTTP_MSG_RPCODE_SP:
1336 case HTTP_MSG_RPREASON:
Willy Tarreaua458b672012-03-05 11:17:50 +01001337 ptr = (char *)http_parse_stsline(msg, buf->data, buf->p,
1338 state, ptr, end,
1339 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (unlikely(!ptr))
1341 return;
1342
1343 /* we have a full response and we know that we have either a CR
1344 * or an LF at <ptr>.
1345 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1347
1348 msg->sol = ptr;
1349 if (likely(*ptr == '\r'))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1351 goto http_msg_rpline_end;
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 /* msg->sol must point to the first of CR or LF. */
1356 EXPECT_LF_HERE(ptr, http_msg_invalid);
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1358 /* stop here */
1359
1360 /*
1361 * Second, states that are specific to the request only
1362 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001366 /* we have a start of message, but we have to check
1367 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001368 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001370 char *beg = buf->p;
1371
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001373 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001375 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 }
Willy Tarreauea1175a2012-03-05 15:52:30 +01001378 msg->som = ptr - buf->p;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001379 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001380 /* we will need this when keep-alive will be supported
1381 hdr_idx_init(idx);
1382 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 state = HTTP_MSG_RQMETH;
1384 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1388 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(*ptr == '\n'))
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001396 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 EXPECT_LF_HERE(ptr, http_msg_invalid);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH_SP:
1404 case HTTP_MSG_RQURI:
1405 case HTTP_MSG_RQURI_SP:
1406 case HTTP_MSG_RQVER:
Willy Tarreaua458b672012-03-05 11:17:50 +01001407 ptr = (char *)http_parse_reqline(msg, buf->data, buf->p,
1408 state, ptr, end,
1409 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 if (unlikely(!ptr))
1411 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 /* we have a full request and we know that we have either a CR
1414 * or an LF at <ptr>.
1415 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 msg->sol = ptr;
1419 if (likely(*ptr == '\r'))
1420 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001424 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 /* check for HTTP/0.9 request : no version information available.
1426 * msg->sol must point to the first of CR or LF.
1427 */
1428 if (unlikely(msg->sl.rq.v_l == 0))
1429 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 EXPECT_LF_HERE(ptr, http_msg_invalid);
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /*
1436 * Common states below
1437 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001439 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 msg->sol = ptr;
1441 if (likely(!HTTP_IS_CRLF(*ptr))) {
1442 goto http_msg_hdr_name;
1443 }
1444
1445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1447 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001450 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 /* assumes msg->sol points to the first char */
1452 if (likely(HTTP_IS_TOKEN(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 if (likely(*ptr == ':')) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001456 msg->col = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1458 }
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 */
1464 msg->err_pos = ptr - buf->data; /* >= 0 now */
1465
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 Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* assumes msg->sol points to the first char and msg->col to the colon */
1472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001500 msg->eol = buf->p + 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 Tarreau8d5d7f22007-01-21 19:16:41 +01001505 /* assumes msg->sol points to the first char, msg->col to the
1506 * colon, and msg->sov points to the first character of the
1507 * value.
1508 */
1509 if (likely(!HTTP_IS_CRLF(*ptr)))
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 msg->eol = ptr;
1513 /* Note: we could also copy eol into ->eoh so that we have the
1514 * real header end in case it ends with lots of LWS, but is this
1515 * really needed ?
1516 */
1517 if (likely(*ptr == '\r'))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1519 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 EXPECT_LF_HERE(ptr, http_msg_invalid);
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1529 /* LWS: replace HT,CR,LF with spaces */
1530 for (; msg->eol < ptr; msg->eol++)
1531 *msg->eol = ' ';
1532 goto http_msg_hdr_val;
1533 }
1534 http_msg_complete_header:
1535 /*
1536 * It was a new header, so the last one is finished.
1537 * Assumes msg->sol points to the first char, msg->col to the
1538 * colon, msg->sov points to the first character of the value
1539 * and msg->eol to the first CR or LF so we know how the line
1540 * ends. We insert last header into the index.
1541 */
1542 /*
1543 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1544 write(2, msg->sol, msg->eol-msg->sol);
1545 fprintf(stderr,"\n");
1546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1549 idx, idx->tail) < 0))
1550 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 msg->sol = ptr;
1553 if (likely(!HTTP_IS_CRLF(*ptr))) {
1554 goto http_msg_hdr_name;
1555 }
1556
1557 if (likely(*ptr == '\r'))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1559 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001562 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 /* Assumes msg->sol points to the first of either CR or LF */
1564 EXPECT_LF_HERE(ptr, http_msg_invalid);
1565 ptr++;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001566 msg->col = msg->sov = msg->next = ptr - buf->p;
1567 msg->eoh = msg->sol - buf->p;
1568 msg->sol = buf->p;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001569 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001571
1572 case HTTP_MSG_ERROR:
1573 /* this may only happen if we call http_msg_analyser() twice with an error */
1574 break;
1575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576#ifdef DEBUG_FULL
1577 default:
1578 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1579 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001580#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 }
1582 http_msg_ood:
1583 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001584 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001585 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 http_msg_invalid:
1589 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001590 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001591 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 return;
1593}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001594
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001595/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1596 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1597 * nothing is done and 1 is returned.
1598 */
1599static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1600{
1601 int delta;
1602 char *cur_end;
1603
1604 if (msg->sl.rq.v_l != 0)
1605 return 1;
1606
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end = msg->sol + msg->sl.rq.l;
1608 delta = 0;
1609
1610 if (msg->sl.rq.u_l == 0) {
1611 /* if no URI was set, add "/" */
1612 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1613 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001614 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 }
1616 /* add HTTP version */
1617 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001618 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001619 cur_end += delta;
Willy Tarreaua458b672012-03-05 11:17:50 +01001620 cur_end = (char *)http_parse_reqline(msg, req->data, req->p,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001621 HTTP_MSG_RQMETH,
1622 msg->sol, cur_end + 1,
1623 NULL, NULL);
1624 if (unlikely(!cur_end))
1625 return 0;
1626
1627 /* we have a full HTTP/1.0 request now and we know that
1628 * we have either a CR or an LF at <ptr>.
1629 */
1630 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1631 return 1;
1632}
1633
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634/* Parse the Connection: header of an HTTP request, looking for both "close"
1635 * and "keep-alive" values. If a buffer is provided and we already know that
1636 * some headers may safely be removed, we remove them now. The <to_del> flags
1637 * are used for that :
1638 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1639 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1640 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1641 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1642 * harmless combinations may be removed. Do not call that after changes have
1643 * been processed. If unused, the buffer can be NULL, and no data will be
1644 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001647{
Willy Tarreau5b154472009-12-21 20:11:07 +01001648 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001649 const char *hdr_val = "Connection";
1650 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001651
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001652 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 return;
1654
Willy Tarreau88d349d2010-01-25 12:15:43 +01001655 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1656 hdr_val = "Proxy-Connection";
1657 hdr_len = 16;
1658 }
1659
Willy Tarreau5b154472009-12-21 20:11:07 +01001660 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001662 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1664 txn->flags |= TX_HDR_CONN_KAL;
1665 if ((to_del & 2) && buf)
1666 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1667 else
1668 txn->flags |= TX_CON_KAL_SET;
1669 }
1670 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1671 txn->flags |= TX_HDR_CONN_CLO;
1672 if ((to_del & 1) && buf)
1673 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1674 else
1675 txn->flags |= TX_CON_CLO_SET;
1676 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001677 }
1678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679 txn->flags |= TX_HDR_CONN_PRS;
1680 return;
1681}
Willy Tarreau5b154472009-12-21 20:11:07 +01001682
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001683/* Apply desired changes on the Connection: header. Values may be removed and/or
1684 * added depending on the <wanted> flags, which are exclusively composed of
1685 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1686 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1687 */
1688void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1689{
1690 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001691 const char *hdr_val = "Connection";
1692 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001693
1694 ctx.idx = 0;
1695
Willy Tarreau88d349d2010-01-25 12:15:43 +01001696
1697 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1698 hdr_val = "Proxy-Connection";
1699 hdr_len = 16;
1700 }
1701
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001702 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001703 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1705 if (wanted & TX_CON_KAL_SET)
1706 txn->flags |= TX_CON_KAL_SET;
1707 else
1708 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001709 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001710 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1711 if (wanted & TX_CON_CLO_SET)
1712 txn->flags |= TX_CON_CLO_SET;
1713 else
1714 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001715 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001716 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001717
1718 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1719 return;
1720
1721 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1722 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001723 hdr_val = "Connection: close";
1724 hdr_len = 17;
1725 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1726 hdr_val = "Proxy-Connection: close";
1727 hdr_len = 23;
1728 }
1729 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 }
1731
1732 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1733 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001734 hdr_val = "Connection: keep-alive";
1735 hdr_len = 22;
1736 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1737 hdr_val = "Proxy-Connection: keep-alive";
1738 hdr_len = 28;
1739 }
1740 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 }
1742 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001743}
1744
Willy Tarreaua458b672012-03-05 11:17:50 +01001745/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 * first byte of body, and increments msg->sov by the number of bytes parsed,
1747 * so that we know we can forward between ->som and ->sov. Note that due to
1748 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1749 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001750 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001751 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001752 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001753int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001754{
Willy Tarreaua458b672012-03-05 11:17:50 +01001755 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1756 char *ptr_old = ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 char *end = buf->data + buf->size;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001758 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 unsigned int chunk = 0;
1760
1761 /* The chunk size is in the following form, though we are only
1762 * interested in the size and CRLF :
1763 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1764 */
1765 while (1) {
1766 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001767 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001770 if (c < 0) /* not a hex digit anymore */
1771 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001772 if (++ptr >= end)
1773 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001774 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001775 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001776 chunk = (chunk << 4) + c;
1777 }
1778
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001780 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001781 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001782
1783 while (http_is_spht[(unsigned char)*ptr]) {
1784 if (++ptr >= end)
1785 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001786 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001787 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001788 }
1789
Willy Tarreaud98cf932009-12-27 22:54:55 +01001790 /* Up to there, we know that at least one byte is present at *ptr. Check
1791 * for the end of chunk size.
1792 */
1793 while (1) {
1794 if (likely(HTTP_IS_CRLF(*ptr))) {
1795 /* we now have a CR or an LF at ptr */
1796 if (likely(*ptr == '\r')) {
1797 if (++ptr >= end)
1798 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001799 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 return 0;
1801 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001802
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001804 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001805 if (++ptr >= end)
1806 ptr = buf->data;
1807 /* done */
1808 break;
1809 }
1810 else if (*ptr == ';') {
1811 /* chunk extension, ends at next CRLF */
1812 if (++ptr >= end)
1813 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001814 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001815 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816
1817 while (!HTTP_IS_CRLF(*ptr)) {
1818 if (++ptr >= end)
1819 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001820 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 return 0;
1822 }
1823 /* we have a CRLF now, loop above */
1824 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001827 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 }
1829
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001831 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001833 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001834 msg->sov += ptr - ptr_old;
1835 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001836 msg->chunk_len = chunk;
1837 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001838 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001840 error:
1841 msg->err_pos = ptr - buf->data;
1842 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001843}
1844
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845/* This function skips trailers in the buffer <buf> associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001847 * the trailers is found, it is automatically scheduled to be forwarded,
1848 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1849 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001850 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001851 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001852 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001853 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1854 * which implies that all non-trailers data have already been scheduled for
1855 * forwarding, and that the difference between msg->som and msg->sov exactly
1856 * matches the length of trailers already parsed and not forwarded. It is also
1857 * important to note that this function is designed to be able to parse wrapped
1858 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 */
1860int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1861{
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 while (1) {
1864 char *p1 = NULL, *p2 = NULL;
Willy Tarreaua458b672012-03-05 11:17:50 +01001865 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001866 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001867 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868
1869 /* scan current line and stop at LF or CRLF */
1870 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001871 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872 return 0;
1873
1874 if (*ptr == '\n') {
1875 if (!p1)
1876 p1 = ptr;
1877 p2 = ptr;
1878 break;
1879 }
1880
1881 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001882 if (p1) {
1883 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001885 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 p1 = ptr;
1887 }
1888
1889 ptr++;
1890 if (ptr >= buf->data + buf->size)
1891 ptr = buf->data;
1892 }
1893
1894 /* after LF; point to beginning of next line */
1895 p2++;
1896 if (p2 >= buf->data + buf->size)
1897 p2 = buf->data;
1898
Willy Tarreaua458b672012-03-05 11:17:50 +01001899 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001900 if (bytes < 0)
1901 bytes += buf->size;
1902
1903 /* schedule this line for forwarding */
1904 msg->sov += bytes;
1905 if (msg->sov >= buf->size)
1906 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907
Willy Tarreaua458b672012-03-05 11:17:50 +01001908 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001909 /* LF/CRLF at beginning of line => end of trailers at p2.
1910 * Everything was scheduled for forwarding, there's nothing
1911 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001912 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001913 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 msg->msg_state = HTTP_MSG_DONE;
1915 return 1;
1916 }
1917 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001918 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 }
1920}
1921
1922/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1923 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001924 * ->som, ->next in order to include this part into the next forwarding phase.
1925 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1927 * not enough data are available, the function does not change anything and
1928 * returns zero. If a parse error is encountered, the function returns < 0 and
1929 * does not change anything. Note: this function is designed to parse wrapped
1930 * CRLF at the end of the buffer.
1931 */
1932int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1933{
1934 char *ptr;
1935 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 Tarreaua458b672012-03-05 11:17:50 +01001942 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 if (*ptr == '\r') {
1944 bytes++;
1945 ptr++;
1946 if (ptr >= buf->data + buf->size)
1947 ptr = buf->data;
1948 }
1949
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001950 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') {
1954 msg->err_pos = ptr - buf->data;
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++;
1959 if (ptr >= buf->data + buf->size)
1960 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001961 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1962 msg->som = 0;
1963 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
Willy Tarreau89fa7062012-03-02 16:13:16 +01001968/* This function may only be used when the buffer's o is empty */
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1970{
1971 char *end = buf->data + buf->size;
Willy Tarreau89fa7062012-03-02 16:13:16 +01001972 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001973
1974 /* two possible cases :
1975 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001976 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001977 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001978 if (buf->i) {
1979 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001980 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001981 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001982 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001983 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001984 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001985 }
1986 if (block2)
1987 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001988 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001989 if (block2)
1990 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001991 }
1992
1993 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001994 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001995 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1996 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1997
Willy Tarreau83e3af02009-12-28 17:39:57 +01001998 if (msg->err_pos >= 0) {
1999 msg->err_pos += off;
2000 if (msg->err_pos >= buf->size)
2001 msg->err_pos -= buf->size;
2002 }
2003
2004 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002005 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01002006 buf->flags |= BF_FULL;
2007}
2008
Willy Tarreaud787e662009-07-07 10:14:51 +02002009/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2010 * processing can continue on next analysers, or zero if it either needs more
2011 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2012 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2013 * when it has nothing left to do, and may remove any analyser when it wants to
2014 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002015 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002016int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002017{
Willy Tarreau59234e92008-11-30 23:51:27 +01002018 /*
2019 * We will parse the partial (or complete) lines.
2020 * We will check the request syntax, and also join multi-line
2021 * headers. An index of all the lines will be elaborated while
2022 * parsing.
2023 *
2024 * For the parsing, we use a 28 states FSM.
2025 *
2026 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002027 * req->p + msg->som = beginning of request
2028 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002029 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002030 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002032 *
2033 * At end of parsing, we may perform a capture of the error (if any), and
2034 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2035 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2036 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002037 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002038
Willy Tarreau59234e92008-11-30 23:51:27 +01002039 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002040 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002041 struct http_txn *txn = &s->txn;
2042 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002043 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002044
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002045 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 +01002046 now_ms, __FUNCTION__,
2047 s,
2048 req,
2049 req->rex, req->wex,
2050 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002051 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002052 req->analysers);
2053
Willy Tarreau52a0c602009-08-16 22:45:38 +02002054 /* we're speaking HTTP here, so let's speak HTTP to the client */
2055 s->srv_error = http_return_srv_error;
2056
Willy Tarreau83e3af02009-12-28 17:39:57 +01002057 /* There's a protected area at the end of the buffer for rewriting
2058 * purposes. We don't want to start to parse the request if the
2059 * protected area is affected, because we may have to move processed
2060 * data later, which is much more complicated.
2061 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002062 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002063 if ((txn->flags & TX_NOT_FIRST) &&
2064 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002065 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002066 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002067 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002068 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2069 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002070 /* some data has still not left the buffer, wake us once that's done */
2071 buffer_dont_connect(req);
2072 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2073 return 0;
2074 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002075 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002076 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002077 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002078 }
2079
Willy Tarreau065e8332010-01-08 00:30:20 +01002080 /* Note that we have the same problem with the response ; we
2081 * may want to send a redirect, error or anything which requires
2082 * some spare space. So we'll ensure that we have at least
2083 * maxrewrite bytes available in the response buffer before
2084 * processing that one. This will only affect pipelined
2085 * keep-alive requests.
2086 */
2087 if ((txn->flags & TX_NOT_FIRST) &&
2088 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002089 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) < buffer_wrap_add(s->rep, s->rep->p + txn->rsp.next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002090 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002091 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002092 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2093 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002094 /* don't let a connection request be initiated */
2095 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002096 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002097 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002098 return 0;
2099 }
2100 }
2101
Willy Tarreaua458b672012-03-05 11:17:50 +01002102 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002103 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002104 }
2105
Willy Tarreau59234e92008-11-30 23:51:27 +01002106 /* 1: we might have to print this header in debug mode */
2107 if (unlikely((global.mode & MODE_DEBUG) &&
2108 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002109 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002110 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreauea1175a2012-03-05 15:52:30 +01002113 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 eol = sol + msg->sl.rq.l;
2115 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 sol += hdr_idx_first_pos(&txn->hdr_idx);
2118 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002119
Willy Tarreau59234e92008-11-30 23:51:27 +01002120 while (cur_idx) {
2121 eol = sol + txn->hdr_idx.v[cur_idx].len;
2122 debug_hdr("clihdr", s, sol, eol);
2123 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2124 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002125 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 }
2127
Willy Tarreau58f10d72006-12-04 02:26:12 +01002128
Willy Tarreau59234e92008-11-30 23:51:27 +01002129 /*
2130 * Now we quickly check if we have found a full valid request.
2131 * If not so, we check the FD and buffer states before leaving.
2132 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002133 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002134 * requests are checked first. When waiting for a second request
2135 * on a keep-alive session, if we encounter and error, close, t/o,
2136 * we note the error in the session flags but don't set any state.
2137 * Since the error will be noted there, it will not be counted by
2138 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002139 * Last, we may increase some tracked counters' http request errors on
2140 * the cases that are deliberately the client's fault. For instance,
2141 * a timeout or connection reset is not counted as an error. However
2142 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002144
Willy Tarreau655dce92009-11-08 13:10:58 +01002145 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002146 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002148 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002149 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002150 session_inc_http_req_ctr(s);
2151 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002152 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002154 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002155
Willy Tarreau59234e92008-11-30 23:51:27 +01002156 /* 1: Since we are in header mode, if there's no space
2157 * left for headers, we won't be able to free more
2158 * later, so the session will never terminate. We
2159 * must terminate it now.
2160 */
2161 if (unlikely(req->flags & BF_FULL)) {
2162 /* FIXME: check if URI is set and return Status
2163 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002164 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002165 session_inc_http_req_ctr(s);
2166 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002167 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002168 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002169 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 goto return_bad_req;
2171 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 /* 2: have we encountered a read error ? */
2174 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002175 if (!(s->flags & SN_ERR_MASK))
2176 s->flags |= SN_ERR_CLICL;
2177
Willy Tarreaufcffa692010-01-10 14:21:19 +01002178 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002179 goto failed_keep_alive;
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002182 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002183 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002184 session_inc_http_err_ctr(s);
2185 }
2186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 msg->msg_state = HTTP_MSG_ERROR;
2188 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002189
Willy Tarreauda7ff642010-06-23 11:44:09 +02002190 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002191 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002192 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002193 if (s->listener->counters)
2194 s->listener->counters->failed_req++;
2195
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 if (!(s->flags & SN_FINST_MASK))
2197 s->flags |= SN_FINST_R;
2198 return 0;
2199 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002200
Willy Tarreau59234e92008-11-30 23:51:27 +01002201 /* 3: has the read timeout expired ? */
2202 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002203 if (!(s->flags & SN_ERR_MASK))
2204 s->flags |= SN_ERR_CLITO;
2205
Willy Tarreaufcffa692010-01-10 14:21:19 +01002206 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002207 goto failed_keep_alive;
2208
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002210 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002211 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002212 session_inc_http_err_ctr(s);
2213 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 txn->status = 408;
2215 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2216 msg->msg_state = HTTP_MSG_ERROR;
2217 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002218
Willy Tarreauda7ff642010-06-23 11:44:09 +02002219 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002220 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002221 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002222 if (s->listener->counters)
2223 s->listener->counters->failed_req++;
2224
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 if (!(s->flags & SN_FINST_MASK))
2226 s->flags |= SN_FINST_R;
2227 return 0;
2228 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002229
Willy Tarreau59234e92008-11-30 23:51:27 +01002230 /* 4: have we encountered a close ? */
2231 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002232 if (!(s->flags & SN_ERR_MASK))
2233 s->flags |= SN_ERR_CLICL;
2234
Willy Tarreaufcffa692010-01-10 14:21:19 +01002235 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002236 goto failed_keep_alive;
2237
Willy Tarreau4076a152009-04-02 15:18:36 +02002238 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002239 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002240 txn->status = 400;
2241 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2242 msg->msg_state = HTTP_MSG_ERROR;
2243 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002244
Willy Tarreauda7ff642010-06-23 11:44:09 +02002245 session_inc_http_err_ctr(s);
2246 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002247 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002248 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002249 if (s->listener->counters)
2250 s->listener->counters->failed_req++;
2251
Willy Tarreau59234e92008-11-30 23:51:27 +01002252 if (!(s->flags & SN_FINST_MASK))
2253 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002254 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002255 }
2256
Willy Tarreau520d95e2009-09-19 21:04:57 +02002257 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002258 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002259 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002260#ifdef TCP_QUICKACK
2261 if (s->listener->options & LI_O_NOQUICKACK) {
2262 /* We need more data, we have to re-enable quick-ack in case we
2263 * previously disabled it, otherwise we might cause the client
2264 * to delay next data.
2265 */
2266 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2267 }
2268#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002269
Willy Tarreaufcffa692010-01-10 14:21:19 +01002270 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2271 /* If the client starts to talk, let's fall back to
2272 * request timeout processing.
2273 */
2274 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002275 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002276 }
2277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002279 if (!tick_isset(req->analyse_exp)) {
2280 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2281 (txn->flags & TX_WAIT_NEXT_RQ) &&
2282 tick_isset(s->be->timeout.httpka))
2283 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2284 else
2285 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002287
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 /* we're not ready yet */
2289 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002290
2291 failed_keep_alive:
2292 /* Here we process low-level errors for keep-alive requests. In
2293 * short, if the request is not the first one and it experiences
2294 * a timeout, read error or shutdown, we just silently close so
2295 * that the client can try again.
2296 */
2297 txn->status = 0;
2298 msg->msg_state = HTTP_MSG_RQBEFORE;
2299 req->analysers = 0;
2300 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002301 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002302 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002303 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305
Willy Tarreaud787e662009-07-07 10:14:51 +02002306 /* OK now we have a complete HTTP request with indexed headers. Let's
2307 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002308 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2309 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002310 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa355d42009-11-29 18:12:29 +01002311 * byte after the last LF. msg->col and msg->sov point to the first
2312 * byte of data. msg->eol cannot be trusted because it may have been
2313 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002314 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002315
Willy Tarreauda7ff642010-06-23 11:44:09 +02002316 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002317 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2318
Willy Tarreaub16a5742010-01-10 14:46:16 +01002319 if (txn->flags & TX_WAIT_NEXT_RQ) {
2320 /* kill the pending keep-alive timeout */
2321 txn->flags &= ~TX_WAIT_NEXT_RQ;
2322 req->analyse_exp = TICK_ETERNITY;
2323 }
2324
2325
Willy Tarreaud787e662009-07-07 10:14:51 +02002326 /* Maybe we found in invalid header name while we were configured not
2327 * to block on that, so we have to capture it now.
2328 */
2329 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002330 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /*
2333 * 1: identify the method
2334 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002335 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002336
2337 /* we can make use of server redirect on GET and HEAD */
2338 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2339 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 /*
2342 * 2: check if the URI matches the monitor_uri.
2343 * We have to do this for every request which gets in, because
2344 * the monitor-uri is defined by the frontend.
2345 */
2346 if (unlikely((s->fe->monitor_uri_len != 0) &&
2347 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002348 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002349 s->fe->monitor_uri,
2350 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002351 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002353 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002357 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002360 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2361 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 ret = acl_pass(ret);
2364 if (cond->pol == ACL_COND_UNLESS)
2365 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 if (ret) {
2368 /* we fail this request, let's return 503 service unavail */
2369 txn->status = 503;
2370 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2371 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002372 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002373 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002374
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 /* nothing to fail, let's reply normaly */
2376 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002377 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 goto return_prx_cond;
2379 }
2380
2381 /*
2382 * 3: Maybe we have to copy the original REQURI for the logs ?
2383 * Note: we cannot log anymore if the request has been
2384 * classified as invalid.
2385 */
2386 if (unlikely(s->logs.logwait & LW_REQ)) {
2387 /* we have a complete HTTP request that we must log */
2388 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2389 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 if (urilen >= REQURI_LEN)
2392 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002393 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002395
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 if (!(s->logs.logwait &= ~LW_REQ))
2397 s->do_log(s);
2398 } else {
2399 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002400 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 }
Willy Tarreau06619262006-12-17 08:37:22 +01002402
William Lallemanda73203e2012-03-12 12:48:57 +01002403 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2404 s->unique_id = pool_alloc2(pool2_uniqueid);
2405 }
2406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002408 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2409 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002410
Willy Tarreau5b154472009-12-21 20:11:07 +01002411 /* ... and check if the request is HTTP/1.1 or above */
2412 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002413 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2414 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2415 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002416 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002417
2418 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002419 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002420
Willy Tarreau88d349d2010-01-25 12:15:43 +01002421 /* if the frontend has "option http-use-proxy-header", we'll check if
2422 * we have what looks like a proxied connection instead of a connection,
2423 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2424 * Note that this is *not* RFC-compliant, however browsers and proxies
2425 * happen to do that despite being non-standard :-(
2426 * We consider that a request not beginning with either '/' or '*' is
2427 * a proxied connection, which covers both "scheme://location" and
2428 * CONNECT ip:port.
2429 */
2430 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2431 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2432 txn->flags |= TX_USE_PX_CONN;
2433
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002434 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002435 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002438 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002439 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002441
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 /* 6: determine the transfer-length.
2443 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2444 * the presence of a message-body in a REQUEST and its transfer length
2445 * must be determined that way (in order of precedence) :
2446 * 1. The presence of a message-body in a request is signaled by the
2447 * inclusion of a Content-Length or Transfer-Encoding header field
2448 * in the request's header fields. When a request message contains
2449 * both a message-body of non-zero length and a method that does
2450 * not define any semantics for that request message-body, then an
2451 * origin server SHOULD either ignore the message-body or respond
2452 * with an appropriate error message (e.g., 413). A proxy or
2453 * gateway, when presented the same request, SHOULD either forward
2454 * the request inbound with the message- body or ignore the
2455 * message-body when determining a response.
2456 *
2457 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2458 * and the "chunked" transfer-coding (Section 6.2) is used, the
2459 * transfer-length is defined by the use of this transfer-coding.
2460 * If a Transfer-Encoding header field is present and the "chunked"
2461 * transfer-coding is not present, the transfer-length is defined
2462 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002463 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002464 * 3. If a Content-Length header field is present, its decimal value in
2465 * OCTETs represents both the entity-length and the transfer-length.
2466 * If a message is received with both a Transfer-Encoding header
2467 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002468 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002469 * 4. By the server closing the connection. (Closing the connection
2470 * cannot be used to indicate the end of a request body, since that
2471 * would leave no possibility for the server to send back a response.)
2472 *
2473 * Whenever a transfer-coding is applied to a message-body, the set of
2474 * transfer-codings MUST include "chunked", unless the message indicates
2475 * it is terminated by closing the connection. When the "chunked"
2476 * transfer-coding is used, it MUST be the last transfer-coding applied
2477 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 */
2479
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002480 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002481 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002482 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002483 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002484 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002485 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002486 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2487 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002488 /* bad transfer-encoding (chunked followed by something else) */
2489 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002490 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002491 break;
2492 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 }
2494
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002496 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2498 signed long long cl;
2499
Willy Tarreauad14f752011-09-02 20:33:27 +02002500 if (!ctx.vlen) {
2501 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002502 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002503 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002504
Willy Tarreauad14f752011-09-02 20:33:27 +02002505 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2506 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002507 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002508 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002509
Willy Tarreauad14f752011-09-02 20:33:27 +02002510 if (cl < 0) {
2511 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002512 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002513 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002514
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002515 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002516 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002517 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002518 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002519
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002520 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002521 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002522 }
2523
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002524 /* bodyless requests have a known length */
2525 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002526 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002527
Willy Tarreaud787e662009-07-07 10:14:51 +02002528 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002529 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002530 req->analyse_exp = TICK_ETERNITY;
2531 return 1;
2532
2533 return_bad_req:
2534 /* We centralize bad requests processing here */
2535 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2536 /* we detected a parsing error. We want to archive this request
2537 * in the dedicated proxy area for later troubleshooting.
2538 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002539 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002540 }
2541
2542 txn->req.msg_state = HTTP_MSG_ERROR;
2543 txn->status = 400;
2544 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002545
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002546 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002547 if (s->listener->counters)
2548 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002549
2550 return_prx_cond:
2551 if (!(s->flags & SN_ERR_MASK))
2552 s->flags |= SN_ERR_PRXCOND;
2553 if (!(s->flags & SN_FINST_MASK))
2554 s->flags |= SN_FINST_R;
2555
2556 req->analysers = 0;
2557 req->analyse_exp = TICK_ETERNITY;
2558 return 0;
2559}
2560
Cyril Bonté70be45d2010-10-12 00:14:35 +02002561/* We reached the stats page through a POST request.
2562 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002563 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002564 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002565int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002566{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002567 struct proxy *px = NULL;
2568 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002570 char key[LINESIZE];
2571 int action = ST_ADM_ACTION_NONE;
2572 int reprocess = 0;
2573
2574 int total_servers = 0;
2575 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002576
2577 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002578 char *st_cur_param = NULL;
2579 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580
Willy Tarreauea1175a2012-03-05 15:52:30 +01002581 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002582 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002583
2584 cur_param = next_param = end_params;
2585
Cyril Bonté23b39d92011-02-10 22:54:44 +01002586 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002588 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002589 return 1;
2590 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002591 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002592 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002593 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002594 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002595 }
2596
2597 *end_params = '\0';
2598
Willy Tarreau295a8372011-03-10 11:25:07 +01002599 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002600
2601 /*
2602 * Parse the parameters in reverse order to only store the last value.
2603 * From the html form, the backend and the action are at the end.
2604 */
2605 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002606 char *value;
2607 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608
2609 cur_param--;
2610 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002611 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002612 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002613 poffset = (cur_param != first_param ? 1 : 0);
2614 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2615 if ((plen > 0) && (plen <= sizeof(key))) {
2616 strncpy(key, cur_param + poffset, plen);
2617 key[plen - 1] = '\0';
2618 } else {
2619 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2620 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002621 }
2622
2623 /* Parse the value */
2624 value = key;
2625 while (*value != '\0' && *value != '=') {
2626 value++;
2627 }
2628 if (*value == '=') {
2629 /* Ok, a value is found, we can mark the end of the key */
2630 *value++ = '\0';
2631 }
2632
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002633 if (!url_decode(key) || !url_decode(value))
2634 break;
2635
Cyril Bonté70be45d2010-10-12 00:14:35 +02002636 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002637 if (!px && (strcmp(key, "b") == 0)) {
2638 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2639 /* the backend name is unknown or ambiguous (duplicate names) */
2640 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2641 goto out;
2642 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002644 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002645 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002646 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002647 }
2648 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002649 action = ST_ADM_ACTION_ENABLE;
2650 }
2651 else {
2652 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2653 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002654 }
2655 }
2656 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002657 if (!(px && action)) {
2658 /*
2659 * Indicates that we'll need to reprocess the parameters
2660 * as soon as backend and action are known
2661 */
2662 if (!reprocess) {
2663 st_cur_param = cur_param;
2664 st_next_param = next_param;
2665 }
2666 reprocess = 1;
2667 }
2668 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002669 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002670 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002671 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002672 /* Not already in maintenance, we can change the server state */
2673 sv->state |= SRV_MAINTAIN;
2674 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002675 altered_servers++;
2676 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002677 }
2678 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002679 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002680 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 /* Already in maintenance, we can change the server state */
2682 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002683 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002684 altered_servers++;
2685 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002686 }
2687 break;
2688 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002689 } else {
2690 /* the server name is unknown or ambiguous (duplicate names) */
2691 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002692 }
2693 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002694 if (reprocess && px && action) {
2695 /* Now, we know the backend and the action chosen by the user.
2696 * We can safely restart from the first server parameter
2697 * to reprocess them
2698 */
2699 cur_param = st_cur_param;
2700 next_param = st_next_param;
2701 reprocess = 0;
2702 goto reprocess_servers;
2703 }
2704
Cyril Bonté70be45d2010-10-12 00:14:35 +02002705 next_param = cur_param;
2706 }
2707 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002708
2709 if (total_servers == 0) {
2710 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2711 }
2712 else if (altered_servers == 0) {
2713 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2714 }
2715 else if (altered_servers == total_servers) {
2716 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2717 }
2718 else {
2719 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2720 }
2721 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002722 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002723}
2724
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002725/* returns a pointer to the first rule which forbids access (deny or http_auth),
2726 * or NULL if everything's OK.
2727 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002728static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002729http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2730{
Willy Tarreauff011f22011-01-06 17:51:27 +01002731 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002732
Willy Tarreauff011f22011-01-06 17:51:27 +01002733 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002734 int ret = 1;
2735
Willy Tarreauff011f22011-01-06 17:51:27 +01002736 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002737 continue;
2738
2739 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002740 if (rule->cond) {
2741 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002742 ret = acl_pass(ret);
2743
Willy Tarreauff011f22011-01-06 17:51:27 +01002744 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002745 ret = !ret;
2746 }
2747
2748 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002749 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002750 return NULL; /* no problem */
2751 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002752 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002753 }
2754 }
2755 return NULL;
2756}
2757
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002758/* This stream analyser runs all HTTP request processing which is common to
2759 * frontends and backends, which means blocking ACLs, filters, connection-close,
2760 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002762 * either needs more data or wants to immediately abort the request (eg: deny,
2763 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002764 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002766{
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 struct http_txn *txn = &s->txn;
2768 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002770 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002772 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002773 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002774
Willy Tarreau655dce92009-11-08 13:10:58 +01002775 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002776 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002777 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002778 return 0;
2779 }
2780
Willy Tarreau3a816292009-07-07 10:55:49 +02002781 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002782 req->analyse_exp = TICK_ETERNITY;
2783
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002784 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 +02002785 now_ms, __FUNCTION__,
2786 s,
2787 req,
2788 req->rex, req->wex,
2789 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002790 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002791 req->analysers);
2792
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 /* first check whether we have some ACLs set to block this request */
2794 list_for_each_entry(cond, &px->block_cond, list) {
2795 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002796
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 ret = acl_pass(ret);
2798 if (cond->pol == ACL_COND_UNLESS)
2799 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002800
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002801 if (ret) {
2802 txn->status = 403;
2803 /* let's log the request time */
2804 s->logs.tv_request = now;
2805 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002806 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002809 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002810
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002811 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002812 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002813
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002814 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002815 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002816 do_stats = stats_check_uri(s->rep->prod, txn, px);
2817 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002818 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 +01002819 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002820 else
2821 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002822
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002823 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002824 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002825 txn->status = 403;
2826 s->logs.tv_request = now;
2827 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002828 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002829 s->fe->fe_counters.denied_req++;
2830 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2831 s->be->be_counters.denied_req++;
2832 if (s->listener->counters)
2833 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002834 goto return_prx_cond;
2835 }
2836
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002837 /* try headers filters */
2838 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002839 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002840 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002841
Willy Tarreau59234e92008-11-30 23:51:27 +01002842 /* has the request been denied ? */
2843 if (txn->flags & TX_CLDENY) {
2844 /* no need to go further */
2845 txn->status = 403;
2846 /* let's log the request time */
2847 s->logs.tv_request = now;
2848 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002849 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 goto return_prx_cond;
2851 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002852
2853 /* When a connection is tarpitted, we use the tarpit timeout,
2854 * which may be the same as the connect timeout if unspecified.
2855 * If unset, then set it to zero because we really want it to
2856 * eventually expire. We build the tarpit as an analyser.
2857 */
2858 if (txn->flags & TX_CLTARPIT) {
2859 buffer_erase(s->req);
2860 /* wipe the request out so that we can drop the connection early
2861 * if the client closes first.
2862 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002863 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002864 req->analysers = 0; /* remove switching rules etc... */
2865 req->analysers |= AN_REQ_HTTP_TARPIT;
2866 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2867 if (!req->analyse_exp)
2868 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002869 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002870 return 1;
2871 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002872 }
Willy Tarreau06619262006-12-17 08:37:22 +01002873
Willy Tarreau5b154472009-12-21 20:11:07 +01002874 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2875 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002876 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2877 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002878 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2879 * avoid to redo the same work if FE and BE have the same settings (common).
2880 * The method consists in checking if options changed between the two calls
2881 * (implying that either one is non-null, or one of them is non-null and we
2882 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002883 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002884
Willy Tarreaudc008c52010-02-01 16:20:08 +01002885 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2886 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2887 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2888 (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 +01002889 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002890
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002891 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2892 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002893 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002894 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2895 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002896 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002897 tmp = TX_CON_WANT_CLO;
2898
Willy Tarreau5b154472009-12-21 20:11:07 +01002899 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2900 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002901
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002902 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2903 /* parse the Connection header and possibly clean it */
2904 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002905 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002906 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2907 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002908 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002910 to_del |= 1; /* remove "close" */
2911 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002912 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002913
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002914 /* check if client or config asks for explicit close in KAL/SCL */
2915 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2916 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2917 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002918 (!(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 +01002919 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002920 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002921 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002922 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2923 }
Willy Tarreau78599912009-10-17 20:12:21 +02002924
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002925 /* we can be blocked here because the request needs to be authenticated,
2926 * either to pass or to access stats.
2927 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002928 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002929 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002930 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002931
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002932 if (!realm)
2933 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2934
Willy Tarreau844a7e72010-01-31 21:46:18 +01002935 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002936 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2937 txn->status = 401;
2938 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002939 /* on 401 we still count one error, because normal browsing
2940 * won't significantly increase the counter but brute force
2941 * attempts will.
2942 */
2943 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002944 goto return_prx_cond;
2945 }
2946
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002947 /* add request headers from the rule sets in the same order */
2948 list_for_each_entry(wl, &px->req_add, list) {
2949 if (wl->cond) {
2950 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2951 ret = acl_pass(ret);
2952 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2953 ret = !ret;
2954 if (!ret)
2955 continue;
2956 }
2957
2958 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2959 goto return_bad_req;
2960 }
2961
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002962 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002963 struct stats_admin_rule *stats_admin_rule;
2964
2965 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 * FIXME!!! that one is rather dangerous, we want to
2967 * make it follow standard rules (eg: clear req->analysers).
2968 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002969
Cyril Bonté474be412010-10-12 00:14:36 +02002970 /* now check whether we have some admin rules for this request */
2971 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2972 int ret = 1;
2973
2974 if (stats_admin_rule->cond) {
2975 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2976 ret = acl_pass(ret);
2977 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2978 ret = !ret;
2979 }
2980
2981 if (ret) {
2982 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002983 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002984 break;
2985 }
2986 }
2987
Cyril Bonté70be45d2010-10-12 00:14:35 +02002988 /* Was the status page requested with a POST ? */
2989 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002990 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002991 if (msg->msg_state < HTTP_MSG_100_SENT) {
2992 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2993 * send an HTTP/1.1 100 Continue intermediate response.
2994 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002995 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002996 struct hdr_ctx ctx;
2997 ctx.idx = 0;
2998 /* Expect is allowed in 1.1, look for it */
2999 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3000 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3001 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3002 }
3003 }
3004 msg->msg_state = HTTP_MSG_100_SENT;
3005 s->logs.tv_request = now; /* update the request timer to reflect full request */
3006 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003007 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003008 /* we need more data */
3009 req->analysers |= an_bit;
3010 buffer_dont_connect(req);
3011 return 0;
3012 }
Cyril Bonté474be412010-10-12 00:14:36 +02003013 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003014 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003015 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003016 }
3017
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003018 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003019 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003020 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003021 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003022 s->rep->prod->applet.private = s;
3023 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003024 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003025 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3026 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003027
3028 return 0;
3029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003032 /* check whether we have some ACLs set to redirect this request */
3033 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003034 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003035
Willy Tarreauf285f542010-01-03 20:03:03 +01003036 if (rule->cond) {
3037 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3038 ret = acl_pass(ret);
3039 if (rule->cond->pol == ACL_COND_UNLESS)
3040 ret = !ret;
3041 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003044 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003046
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 /* build redirect message */
3048 switch(rule->code) {
3049 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003050 msg_fmt = HTTP_303;
3051 break;
3052 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 msg_fmt = HTTP_301;
3054 break;
3055 case 302:
3056 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 msg_fmt = HTTP_302;
3058 break;
3059 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003060
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003061 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003063
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 switch(rule->type) {
3065 case REDIRECT_TYPE_PREFIX: {
3066 const char *path;
3067 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 path = http_get_path(txn);
3070 /* build message using path */
3071 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003072 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3074 int qs = 0;
3075 while (qs < pathlen) {
3076 if (path[qs] == '?') {
3077 pathlen = qs;
3078 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003079 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003081 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 } else {
3084 path = "/";
3085 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003086 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003087
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003088 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 goto return_bad_req;
3090
3091 /* add prefix. Note that if prefix == "/", we don't want to
3092 * add anything, otherwise it makes it hard for the user to
3093 * configure a self-redirection.
3094 */
3095 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003096 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3097 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003098 }
3099
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003100 /* add path */
3101 memcpy(rdr.str + rdr.len, path, pathlen);
3102 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003103
3104 /* append a slash at the end of the location is needed and missing */
3105 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3106 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3107 if (rdr.len > rdr.size - 5)
3108 goto return_bad_req;
3109 rdr.str[rdr.len] = '/';
3110 rdr.len++;
3111 }
3112
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003113 break;
3114 }
3115 case REDIRECT_TYPE_LOCATION:
3116 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003117 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003119
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120 /* add location */
3121 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3122 rdr.len += rule->rdr_len;
3123 break;
3124 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003125
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003126 if (rule->cookie_len) {
3127 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3128 rdr.len += 14;
3129 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3130 rdr.len += rule->cookie_len;
3131 memcpy(rdr.str + rdr.len, "\r\n", 2);
3132 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003133 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003134
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003135 /* add end of headers and the keep-alive/close status.
3136 * We may choose to set keep-alive if the Location begins
3137 * with a slash, because the client will come back to the
3138 * same server.
3139 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003140 txn->status = rule->code;
3141 /* let's log the request time */
3142 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003143
3144 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003145 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3146 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003147 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3148 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3149 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003150 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003151 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3152 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3153 rdr.len += 30;
3154 } else {
3155 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3156 rdr.len += 24;
3157 }
Willy Tarreau75661452010-01-10 10:35:01 +01003158 }
3159 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3160 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003161 buffer_write(req->prod->ob, rdr.str, rdr.len);
3162 /* "eat" the request */
3163 buffer_ignore(req, msg->sov - msg->som);
3164 msg->som = msg->sov;
3165 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003166 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3167 txn->req.msg_state = HTTP_MSG_CLOSED;
3168 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003169 break;
3170 } else {
3171 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003172 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3173 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3174 rdr.len += 29;
3175 } else {
3176 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3177 rdr.len += 23;
3178 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003179 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003180 goto return_prx_cond;
3181 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003182 }
3183 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003184
Willy Tarreau2be39392010-01-03 17:24:51 +01003185 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3186 * If this happens, then the data will not come immediately, so we must
3187 * send all what we have without waiting. Note that due to the small gain
3188 * in waiting for the body of the request, it's easier to simply put the
3189 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3190 * itself once used.
3191 */
3192 req->flags |= BF_SEND_DONTWAIT;
3193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003194 /* that's OK for us now, let's move on to next analysers */
3195 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003196
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003197 return_bad_req:
3198 /* We centralize bad requests processing here */
3199 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3200 /* we detected a parsing error. We want to archive this request
3201 * in the dedicated proxy area for later troubleshooting.
3202 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003203 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003206 txn->req.msg_state = HTTP_MSG_ERROR;
3207 txn->status = 400;
3208 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003209
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003210 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003211 if (s->listener->counters)
3212 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003213
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003214 return_prx_cond:
3215 if (!(s->flags & SN_ERR_MASK))
3216 s->flags |= SN_ERR_PRXCOND;
3217 if (!(s->flags & SN_FINST_MASK))
3218 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003219
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003220 req->analysers = 0;
3221 req->analyse_exp = TICK_ETERNITY;
3222 return 0;
3223}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003224
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003225/* This function performs all the processing enabled for the current request.
3226 * It returns 1 if the processing can continue on next analysers, or zero if it
3227 * needs more data, encounters an error, or wants to immediately abort the
3228 * request. It relies on buffers flags, and updates s->req->analysers.
3229 */
3230int http_process_request(struct session *s, struct buffer *req, int an_bit)
3231{
3232 struct http_txn *txn = &s->txn;
3233 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003234
Willy Tarreau655dce92009-11-08 13:10:58 +01003235 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003236 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003237 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003238 return 0;
3239 }
3240
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003241 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 +02003242 now_ms, __FUNCTION__,
3243 s,
3244 req,
3245 req->rex, req->wex,
3246 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003247 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003248 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003249
Willy Tarreau59234e92008-11-30 23:51:27 +01003250 /*
3251 * Right now, we know that we have processed the entire headers
3252 * and that unwanted requests have been filtered out. We can do
3253 * whatever we want with the remaining request. Also, now we
3254 * may have separate values for ->fe, ->be.
3255 */
Willy Tarreau06619262006-12-17 08:37:22 +01003256
Willy Tarreau59234e92008-11-30 23:51:27 +01003257 /*
3258 * If HTTP PROXY is set we simply get remote server address
3259 * parsing incoming request.
3260 */
3261 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003262 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003263 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003264
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003266 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 * Note that doing so might move headers in the request, but
3268 * the fields will stay coherent and the URI will not move.
3269 * This should only be performed in the backend.
3270 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003271 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3273 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003274
Willy Tarreau59234e92008-11-30 23:51:27 +01003275 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003276 * 8: the appsession cookie was looked up very early in 1.2,
3277 * so let's do the same now.
3278 */
3279
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003280 /* It needs to look into the URI unless persistence must be ignored */
3281 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003282 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003283 }
3284
William Lallemanda73203e2012-03-12 12:48:57 +01003285 /* add unique-id if "header-unique-id" is specified */
3286
3287 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3288 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3289
3290 if (s->fe->header_unique_id && s->unique_id) {
3291 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3292 if (ret < 0 || ret > global.tune.bufsize)
3293 goto return_bad_req;
3294 if(unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, trash) < 0))
3295 goto return_bad_req;
3296 }
3297
Cyril Bontéb21570a2009-11-29 20:04:48 +01003298 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 * 9: add X-Forwarded-For if either the frontend or the backend
3300 * asks for it.
3301 */
3302 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003303 struct hdr_ctx ctx = { .idx = 0 };
3304
3305 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003306 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003307 /* The header is set to be added only if none is present
3308 * and we found it, so don't do anything.
3309 */
3310 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003311 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 /* Add an X-Forwarded-For header unless the source IP is
3313 * in the 'except' network range.
3314 */
3315 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003316 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003317 != s->fe->except_net.s_addr) &&
3318 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003319 (((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003321 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003323 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003324
3325 /* Note: we rely on the backend to get the header name to be used for
3326 * x-forwarded-for, because the header is really meant for the backends.
3327 * However, if the backend did not specify any option, we have to rely
3328 * on the frontend's header name.
3329 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003330 if (s->be->fwdfor_hdr_len) {
3331 len = s->be->fwdfor_hdr_len;
3332 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003333 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003334 len = s->fe->fwdfor_hdr_len;
3335 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003336 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003338
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003339 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003340 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003341 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003342 }
3343 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003344 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 /* FIXME: for the sake of completeness, we should also support
3346 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003347 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003348 int len;
3349 char pn[INET6_ADDRSTRLEN];
3350 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003351 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003353
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 /* Note: we rely on the backend to get the header name to be used for
3355 * x-forwarded-for, because the header is really meant for the backends.
3356 * However, if the backend did not specify any option, we have to rely
3357 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003358 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003359 if (s->be->fwdfor_hdr_len) {
3360 len = s->be->fwdfor_hdr_len;
3361 memcpy(trash, s->be->fwdfor_hdr_name, len);
3362 } else {
3363 len = s->fe->fwdfor_hdr_len;
3364 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003365 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003366 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003367
Willy Tarreau59234e92008-11-30 23:51:27 +01003368 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003369 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003370 goto return_bad_req;
3371 }
3372 }
3373
3374 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003375 * 10: add X-Original-To if either the frontend or the backend
3376 * asks for it.
3377 */
3378 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3379
3380 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003381 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003382 /* Add an X-Original-To header unless the destination IP is
3383 * in the 'except' network range.
3384 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003385 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003386
Willy Tarreau6471afb2011-09-23 10:54:59 +02003387 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003388 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003389 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003390 != s->fe->except_to.s_addr) &&
3391 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003392 (((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003393 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003394 int len;
3395 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003396 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003397
3398 /* Note: we rely on the backend to get the header name to be used for
3399 * x-original-to, because the header is really meant for the backends.
3400 * However, if the backend did not specify any option, we have to rely
3401 * on the frontend's header name.
3402 */
3403 if (s->be->orgto_hdr_len) {
3404 len = s->be->orgto_hdr_len;
3405 memcpy(trash, s->be->orgto_hdr_name, len);
3406 } else {
3407 len = s->fe->orgto_hdr_len;
3408 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003409 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003410 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3411
3412 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003413 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003414 goto return_bad_req;
3415 }
3416 }
3417 }
3418
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003419 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3420 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003421 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003422 unsigned int want_flags = 0;
3423
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003424 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003425 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3426 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3427 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003428 want_flags |= TX_CON_CLO_SET;
3429 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003430 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3431 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3432 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003433 want_flags |= TX_CON_KAL_SET;
3434 }
3435
3436 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3437 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003439
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003440
Willy Tarreau522d6c02009-12-06 18:49:18 +01003441 /* If we have no server assigned yet and we're balancing on url_param
3442 * with a POST request, we may be interested in checking the body for
3443 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003444 */
3445 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3446 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003447 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003448 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003449 buffer_dont_connect(req);
3450 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003451 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003452
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003453 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003454 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003455#ifdef TCP_QUICKACK
3456 /* We expect some data from the client. Unless we know for sure
3457 * we already have a full request, we have to re-enable quick-ack
3458 * in case we previously disabled it, otherwise we might cause
3459 * the client to delay further data.
3460 */
3461 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003462 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003463 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003464 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3465#endif
3466 }
Willy Tarreau03945942009-12-22 16:50:27 +01003467
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 /*************************************************************
3469 * OK, that's finished for the headers. We have done what we *
3470 * could. Let's switch to the DATA state. *
3471 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003472 req->analyse_exp = TICK_ETERNITY;
3473 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003474
Willy Tarreau59234e92008-11-30 23:51:27 +01003475 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003476 /* OK let's go on with the BODY now */
3477 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003478
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003480 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003481 /* we detected a parsing error. We want to archive this request
3482 * in the dedicated proxy area for later troubleshooting.
3483 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003484 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003485 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003486
Willy Tarreau59234e92008-11-30 23:51:27 +01003487 txn->req.msg_state = HTTP_MSG_ERROR;
3488 txn->status = 400;
3489 req->analysers = 0;
3490 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003491
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003492 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003493 if (s->listener->counters)
3494 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003495
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 if (!(s->flags & SN_ERR_MASK))
3497 s->flags |= SN_ERR_PRXCOND;
3498 if (!(s->flags & SN_FINST_MASK))
3499 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003500 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003501}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003502
Willy Tarreau60b85b02008-11-30 23:28:40 +01003503/* This function is an analyser which processes the HTTP tarpit. It always
3504 * returns zero, at the beginning because it prevents any other processing
3505 * from occurring, and at the end because it terminates the request.
3506 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003507int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003508{
3509 struct http_txn *txn = &s->txn;
3510
3511 /* This connection is being tarpitted. The CLIENT side has
3512 * already set the connect expiration date to the right
3513 * timeout. We just have to check that the client is still
3514 * there and that the timeout has not expired.
3515 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003516 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003517 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3518 !tick_is_expired(req->analyse_exp, now_ms))
3519 return 0;
3520
3521 /* We will set the queue timer to the time spent, just for
3522 * logging purposes. We fake a 500 server error, so that the
3523 * attacker will not suspect his connection has been tarpitted.
3524 * It will not cause trouble to the logs because we can exclude
3525 * the tarpitted connections by filtering on the 'PT' status flags.
3526 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003527 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3528
3529 txn->status = 500;
3530 if (req->flags != BF_READ_ERROR)
3531 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3532
3533 req->analysers = 0;
3534 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003535
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003536 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003537 if (s->listener->counters)
3538 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003539
Willy Tarreau60b85b02008-11-30 23:28:40 +01003540 if (!(s->flags & SN_ERR_MASK))
3541 s->flags |= SN_ERR_PRXCOND;
3542 if (!(s->flags & SN_FINST_MASK))
3543 s->flags |= SN_FINST_T;
3544 return 0;
3545}
3546
Willy Tarreaud34af782008-11-30 23:36:37 +01003547/* This function is an analyser which processes the HTTP request body. It looks
3548 * for parameters to be used for the load balancing algorithm (url_param). It
3549 * must only be called after the standard HTTP request processing has occurred,
3550 * because it expects the request to be parsed. It returns zero if it needs to
3551 * read more data, or 1 once it has completed its analysis.
3552 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003553int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003554{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003555 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003556 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003557 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003558
3559 /* We have to parse the HTTP request body to find any required data.
3560 * "balance url_param check_post" should have been the only way to get
3561 * into this. We were brought here after HTTP header analysis, so all
3562 * related structures are ready.
3563 */
3564
Willy Tarreau522d6c02009-12-06 18:49:18 +01003565 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3566 goto missing_data;
3567
3568 if (msg->msg_state < HTTP_MSG_100_SENT) {
3569 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3570 * send an HTTP/1.1 100 Continue intermediate response.
3571 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003572 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003573 struct hdr_ctx ctx;
3574 ctx.idx = 0;
3575 /* Expect is allowed in 1.1, look for it */
3576 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3577 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3578 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3579 }
3580 }
3581 msg->msg_state = HTTP_MSG_100_SENT;
3582 }
3583
3584 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003585 /* we have msg->col and msg->sov which both point to the first
3586 * byte of message body. msg->som still points to the beginning
Willy Tarreaua458b672012-03-05 11:17:50 +01003587 * of the message. We must save the body in msg->next because it
Willy Tarreaud98cf932009-12-27 22:54:55 +01003588 * survives buffer re-alignments.
3589 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003590 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003591
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003592 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003593 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3594 else
3595 msg->msg_state = HTTP_MSG_DATA;
3596 }
3597
3598 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003599 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003600 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003601 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003602 */
3603 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003604
Willy Tarreau115acb92009-12-26 13:56:06 +01003605 if (!ret)
3606 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003607 else if (ret < 0) {
3608 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003609 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003610 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003611 }
3612
Willy Tarreaud98cf932009-12-27 22:54:55 +01003613 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003614 * We have the first non-header byte in msg->col, which is either the
3615 * beginning of the chunk size or of the data. The first data byte is in
3616 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3617 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003618 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003619
Willy Tarreau124d9912011-03-01 20:30:48 +01003620 if (msg->body_len < limit)
3621 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003623 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624 goto http_end;
3625
3626 missing_data:
3627 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003628 if (req->flags & BF_FULL) {
3629 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003630 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003631 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003632
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3634 txn->status = 408;
3635 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003636
3637 if (!(s->flags & SN_ERR_MASK))
3638 s->flags |= SN_ERR_CLITO;
3639 if (!(s->flags & SN_FINST_MASK))
3640 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003642 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003643
3644 /* we get here if we need to wait for more data */
3645 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003646 /* Not enough data. We'll re-use the http-request
3647 * timeout here. Ideally, we should set the timeout
3648 * relative to the accept() date. We just set the
3649 * request timeout once at the beginning of the
3650 * request.
3651 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003652 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003653 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003654 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003655 return 0;
3656 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657
3658 http_end:
3659 /* The situation will not evolve, so let's give up on the analysis. */
3660 s->logs.tv_request = now; /* update the request timer to reflect full request */
3661 req->analysers &= ~an_bit;
3662 req->analyse_exp = TICK_ETERNITY;
3663 return 1;
3664
3665 return_bad_req: /* let's centralize all bad requests */
3666 txn->req.msg_state = HTTP_MSG_ERROR;
3667 txn->status = 400;
3668 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3669
Willy Tarreau79ebac62010-06-07 13:47:49 +02003670 if (!(s->flags & SN_ERR_MASK))
3671 s->flags |= SN_ERR_PRXCOND;
3672 if (!(s->flags & SN_FINST_MASK))
3673 s->flags |= SN_FINST_R;
3674
Willy Tarreau522d6c02009-12-06 18:49:18 +01003675 return_err_msg:
3676 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003677 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 if (s->listener->counters)
3679 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003680 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003681}
3682
Mark Lamourinec2247f02012-01-04 13:02:01 -05003683int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3684
3685 struct hdr_ctx ctx;
3686
Mark Lamourinec2247f02012-01-04 13:02:01 -05003687 char *hdr_name = be->server_id_hdr_name;
3688 int hdr_name_len = be->server_id_hdr_len;
3689
3690 char *hdr_val;
3691
William Lallemandd9e90662012-01-30 17:27:17 +01003692 ctx.idx = 0;
3693
Mark Lamourinec2247f02012-01-04 13:02:01 -05003694 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3695 /* remove any existing values from the header */
3696 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3697 }
3698
3699 /* Add the new header requested with the server value */
3700 hdr_val = trash;
3701 memcpy(hdr_val, hdr_name, hdr_name_len);
3702 hdr_val += hdr_name_len;
3703 *hdr_val++ = ':';
3704 *hdr_val++ = ' ';
3705 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3706 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3707
3708 return 0;
3709}
3710
Willy Tarreau610ecce2010-01-04 21:15:02 +01003711/* Terminate current transaction and prepare a new one. This is very tricky
3712 * right now but it works.
3713 */
3714void http_end_txn_clean_session(struct session *s)
3715{
3716 /* FIXME: We need a more portable way of releasing a backend's and a
3717 * server's connections. We need a safer way to reinitialize buffer
3718 * flags. We also need a more accurate method for computing per-request
3719 * data.
3720 */
3721 http_silent_debug(__LINE__, s);
3722
3723 s->req->cons->flags |= SI_FL_NOLINGER;
3724 s->req->cons->shutr(s->req->cons);
3725 s->req->cons->shutw(s->req->cons);
3726
3727 http_silent_debug(__LINE__, s);
3728
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003729 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003731 if (unlikely(s->srv_conn))
3732 sess_change_server(s, NULL);
3733 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734
3735 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3736 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003737 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003738
3739 if (s->txn.status) {
3740 int n;
3741
3742 n = s->txn.status / 100;
3743 if (n < 1 || n > 5)
3744 n = 0;
3745
3746 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003747 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
Willy Tarreau24657792010-02-26 10:30:28 +01003749 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003750 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003751 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003752 }
3753
3754 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003755 s->logs.bytes_in -= s->req->i;
3756 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757
3758 /* let's do a final log if we need it */
3759 if (s->logs.logwait &&
3760 !(s->flags & SN_MONITOR) &&
3761 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3762 s->do_log(s);
3763 }
3764
3765 s->logs.accept_date = date; /* user-visible date for logging */
3766 s->logs.tv_accept = now; /* corrected date for internal use */
3767 tv_zero(&s->logs.tv_request);
3768 s->logs.t_queue = -1;
3769 s->logs.t_connect = -1;
3770 s->logs.t_data = -1;
3771 s->logs.t_close = 0;
3772 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3773 s->logs.srv_queue_size = 0; /* we will get this number soon */
3774
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003775 s->logs.bytes_in = s->req->total = s->req->i;
3776 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777
3778 if (s->pend_pos)
3779 pendconn_free(s->pend_pos);
3780
Willy Tarreau827aee92011-03-10 16:55:02 +01003781 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 if (s->flags & SN_CURR_SESS) {
3783 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003784 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003786 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3787 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 }
3789
Willy Tarreau9e000c62011-03-10 14:03:36 +01003790 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791
3792 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3793 s->req->cons->fd = -1; /* just to help with debugging */
3794 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003795 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 s->req->cons->err_loc = NULL;
3797 s->req->cons->exp = TICK_ETERNITY;
3798 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003799 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3800 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL|BF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003801 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 +01003802 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3803 s->txn.meth = 0;
3804 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003805 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003806 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807 s->req->cons->flags |= SI_FL_INDEP_STR;
3808
Willy Tarreau96e31212011-05-30 18:10:30 +02003809 if (s->fe->options2 & PR_O2_NODELAY) {
3810 s->req->flags |= BF_NEVER_WAIT;
3811 s->rep->flags |= BF_NEVER_WAIT;
3812 }
3813
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 /* if the request buffer is not empty, it means we're
3815 * about to process another request, so send pending
3816 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003817 * Just don't do this if the buffer is close to be full,
3818 * because the request will wait for it to flush a little
3819 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003821 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003822 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003823 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003824 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003825 s->rep->flags |= BF_EXPECT_MORE;
3826 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003827
3828 /* we're removing the analysers, we MUST re-enable events detection */
3829 buffer_auto_read(s->req);
3830 buffer_auto_close(s->req);
3831 buffer_auto_read(s->rep);
3832 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003833
Willy Tarreau342b11c2010-11-24 16:22:09 +01003834 s->req->analysers = s->listener->analysers;
3835 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003836 s->rep->analysers = 0;
3837
3838 http_silent_debug(__LINE__, s);
3839}
3840
3841
3842/* This function updates the request state machine according to the response
3843 * state machine and buffer flags. It returns 1 if it changes anything (flag
3844 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3845 * it is only used to find when a request/response couple is complete. Both
3846 * this function and its equivalent should loop until both return zero. It
3847 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3848 */
3849int http_sync_req_state(struct session *s)
3850{
3851 struct buffer *buf = s->req;
3852 struct http_txn *txn = &s->txn;
3853 unsigned int old_flags = buf->flags;
3854 unsigned int old_state = txn->req.msg_state;
3855
3856 http_silent_debug(__LINE__, s);
3857 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3858 return 0;
3859
3860 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003861 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003862 * We can shut the read side unless we want to abort_on_close,
3863 * or we have a POST request. The issue with POST requests is
3864 * that some browsers still send a CRLF after the request, and
3865 * this CRLF must be read so that it does not remain in the kernel
3866 * buffers, otherwise a close could cause an RST on some systems
3867 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003868 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003869 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003870 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003871
3872 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3873 goto wait_other_side;
3874
3875 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3876 /* The server has not finished to respond, so we
3877 * don't want to move in order not to upset it.
3878 */
3879 goto wait_other_side;
3880 }
3881
3882 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3883 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003884 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 txn->req.msg_state = HTTP_MSG_TUNNEL;
3886 goto wait_other_side;
3887 }
3888
3889 /* When we get here, it means that both the request and the
3890 * response have finished receiving. Depending on the connection
3891 * mode, we'll have to wait for the last bytes to leave in either
3892 * direction, and sometimes for a close to be effective.
3893 */
3894
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003895 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3896 /* Server-close mode : queue a connection close to the server */
3897 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003899 }
3900 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3901 /* Option forceclose is set, or either side wants to close,
3902 * let's enforce it now that we're not expecting any new
3903 * data to come. The caller knows the session is complete
3904 * once both states are CLOSED.
3905 */
3906 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907 buffer_shutr_now(buf);
3908 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003909 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003910 }
3911 else {
3912 /* The last possible modes are keep-alive and tunnel. Since tunnel
3913 * mode does not set the body analyser, we can't reach this place
3914 * in tunnel mode, so we're left with keep-alive only.
3915 * This mode is currently not implemented, we switch to tunnel mode.
3916 */
3917 buffer_auto_read(buf);
3918 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003919 }
3920
3921 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3922 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003923 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3924
Willy Tarreau610ecce2010-01-04 21:15:02 +01003925 if (!(buf->flags & BF_OUT_EMPTY)) {
3926 txn->req.msg_state = HTTP_MSG_CLOSING;
3927 goto http_msg_closing;
3928 }
3929 else {
3930 txn->req.msg_state = HTTP_MSG_CLOSED;
3931 goto http_msg_closed;
3932 }
3933 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003934 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935 }
3936
3937 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3938 http_msg_closing:
3939 /* nothing else to forward, just waiting for the output buffer
3940 * to be empty and for the shutw_now to take effect.
3941 */
3942 if (buf->flags & BF_OUT_EMPTY) {
3943 txn->req.msg_state = HTTP_MSG_CLOSED;
3944 goto http_msg_closed;
3945 }
3946 else if (buf->flags & BF_SHUTW) {
3947 txn->req.msg_state = HTTP_MSG_ERROR;
3948 goto wait_other_side;
3949 }
3950 }
3951
3952 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3953 http_msg_closed:
3954 goto wait_other_side;
3955 }
3956
3957 wait_other_side:
3958 http_silent_debug(__LINE__, s);
3959 return txn->req.msg_state != old_state || buf->flags != old_flags;
3960}
3961
3962
3963/* This function updates the response state machine according to the request
3964 * state machine and buffer flags. It returns 1 if it changes anything (flag
3965 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3966 * it is only used to find when a request/response couple is complete. Both
3967 * this function and its equivalent should loop until both return zero. It
3968 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3969 */
3970int http_sync_res_state(struct session *s)
3971{
3972 struct buffer *buf = s->rep;
3973 struct http_txn *txn = &s->txn;
3974 unsigned int old_flags = buf->flags;
3975 unsigned int old_state = txn->rsp.msg_state;
3976
3977 http_silent_debug(__LINE__, s);
3978 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3979 return 0;
3980
3981 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3982 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003983 * still monitor the server connection for a possible close
3984 * while the request is being uploaded, so we don't disable
3985 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003986 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003987 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988
3989 if (txn->req.msg_state == HTTP_MSG_ERROR)
3990 goto wait_other_side;
3991
3992 if (txn->req.msg_state < HTTP_MSG_DONE) {
3993 /* The client seems to still be sending data, probably
3994 * because we got an error response during an upload.
3995 * We have the choice of either breaking the connection
3996 * or letting it pass through. Let's do the later.
3997 */
3998 goto wait_other_side;
3999 }
4000
4001 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4002 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004003 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004004 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4005 goto wait_other_side;
4006 }
4007
4008 /* When we get here, it means that both the request and the
4009 * response have finished receiving. Depending on the connection
4010 * mode, we'll have to wait for the last bytes to leave in either
4011 * direction, and sometimes for a close to be effective.
4012 */
4013
4014 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4015 /* Server-close mode : shut read and wait for the request
4016 * side to close its output buffer. The caller will detect
4017 * when we're in DONE and the other is in CLOSED and will
4018 * catch that for the final cleanup.
4019 */
4020 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4021 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004023 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4024 /* Option forceclose is set, or either side wants to close,
4025 * let's enforce it now that we're not expecting any new
4026 * data to come. The caller knows the session is complete
4027 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004029 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4030 buffer_shutr_now(buf);
4031 buffer_shutw_now(buf);
4032 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 }
4034 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004035 /* The last possible modes are keep-alive and tunnel. Since tunnel
4036 * mode does not set the body analyser, we can't reach this place
4037 * in tunnel mode, so we're left with keep-alive only.
4038 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004040 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004041 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 }
4043
4044 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4045 /* if we've just closed an output, let's switch */
4046 if (!(buf->flags & BF_OUT_EMPTY)) {
4047 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4048 goto http_msg_closing;
4049 }
4050 else {
4051 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4052 goto http_msg_closed;
4053 }
4054 }
4055 goto wait_other_side;
4056 }
4057
4058 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4059 http_msg_closing:
4060 /* nothing else to forward, just waiting for the output buffer
4061 * to be empty and for the shutw_now to take effect.
4062 */
4063 if (buf->flags & BF_OUT_EMPTY) {
4064 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4065 goto http_msg_closed;
4066 }
4067 else if (buf->flags & BF_SHUTW) {
4068 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004069 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004070 if (target_srv(&s->target))
4071 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 goto wait_other_side;
4073 }
4074 }
4075
4076 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4077 http_msg_closed:
4078 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004079 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004081 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004082 goto wait_other_side;
4083 }
4084
4085 wait_other_side:
4086 http_silent_debug(__LINE__, s);
4087 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4088}
4089
4090
4091/* Resync the request and response state machines. Return 1 if either state
4092 * changes.
4093 */
4094int http_resync_states(struct session *s)
4095{
4096 struct http_txn *txn = &s->txn;
4097 int old_req_state = txn->req.msg_state;
4098 int old_res_state = txn->rsp.msg_state;
4099
4100 http_silent_debug(__LINE__, s);
4101 http_sync_req_state(s);
4102 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004103 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 if (!http_sync_res_state(s))
4105 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004106 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 if (!http_sync_req_state(s))
4108 break;
4109 }
4110 http_silent_debug(__LINE__, s);
4111 /* OK, both state machines agree on a compatible state.
4112 * There are a few cases we're interested in :
4113 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4114 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4115 * directions, so let's simply disable both analysers.
4116 * - HTTP_MSG_CLOSED on the response only means we must abort the
4117 * request.
4118 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4119 * with server-close mode means we've completed one request and we
4120 * must re-initialize the server connection.
4121 */
4122
4123 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4124 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4125 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4126 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4127 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004128 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004131 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004132 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004134 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4135 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004136 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004137 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004138 s->rep->analysers = 0;
4139 buffer_auto_close(s->rep);
4140 buffer_auto_read(s->rep);
4141 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 buffer_abort(s->req);
4143 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004144 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004145 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004146 }
4147 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4148 txn->rsp.msg_state == HTTP_MSG_DONE &&
4149 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4150 /* server-close: terminate this server connection and
4151 * reinitialize a fresh-new transaction.
4152 */
4153 http_end_txn_clean_session(s);
4154 }
4155
4156 http_silent_debug(__LINE__, s);
4157 return txn->req.msg_state != old_req_state ||
4158 txn->rsp.msg_state != old_res_state;
4159}
4160
Willy Tarreaud98cf932009-12-27 22:54:55 +01004161/* This function is an analyser which forwards request body (including chunk
4162 * sizes if any). It is called as soon as we must forward, even if we forward
4163 * zero byte. The only situation where it must not be called is when we're in
4164 * tunnel mode and we want to forward till the close. It's used both to forward
4165 * remaining data and to resync after end of body. It expects the msg_state to
4166 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4167 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004168 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004169 * bytes of pending data + the headers if not already done (between som and sov).
4170 * It eventually adjusts som to match sov after the data in between have been sent.
4171 */
4172int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4173{
4174 struct http_txn *txn = &s->txn;
4175 struct http_msg *msg = &s->txn.req;
4176
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004177 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4178 return 0;
4179
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004180 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004181 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004182 /* Output closed while we were sending data. We must abort and
4183 * wake the other side up.
4184 */
4185 msg->msg_state = HTTP_MSG_ERROR;
4186 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004187 return 1;
4188 }
4189
Willy Tarreau4fe41902010-06-07 22:27:41 +02004190 /* in most states, we should abort in case of early close */
4191 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004192
4193 /* Note that we don't have to send 100-continue back because we don't
4194 * need the data to complete our job, and it's up to the server to
4195 * decide whether to return 100, 417 or anything else in return of
4196 * an "Expect: 100-continue" header.
4197 */
4198
4199 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4200 /* we have msg->col and msg->sov which both point to the first
4201 * byte of message body. msg->som still points to the beginning
Willy Tarreaua458b672012-03-05 11:17:50 +01004202 * of the message. We must save the body in msg->next because it
Willy Tarreaud98cf932009-12-27 22:54:55 +01004203 * survives buffer re-alignments.
4204 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004205 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004206
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004207 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004208 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4209 else {
4210 msg->msg_state = HTTP_MSG_DATA;
4211 }
4212 }
4213
Willy Tarreaud98cf932009-12-27 22:54:55 +01004214 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004215 int bytes;
4216
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004218 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004219 bytes = msg->sov - msg->som;
4220 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004221 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004222 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4223 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004224 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004225 msg->chunk_len += (unsigned int)bytes;
4226 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004227 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004228
Willy Tarreaucaabe412010-01-03 23:08:28 +01004229 if (msg->msg_state == HTTP_MSG_DATA) {
4230 /* must still forward */
4231 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004232 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004233
4234 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004235 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004236 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004238 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004239 }
4240 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004241 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004242 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004243 * TRAILERS state.
4244 */
4245 int ret = http_parse_chunk_size(req, msg);
4246
4247 if (!ret)
4248 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004249 else if (ret < 0) {
4250 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004251 if (msg->err_pos >= 0)
4252 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004253 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004254 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004255 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004256 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004257 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4258 /* we want the CRLF after the data */
4259 int ret;
4260
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 ret = http_skip_chunk_crlf(req, msg);
4262
4263 if (ret == 0)
4264 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004265 else if (ret < 0) {
4266 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004267 if (msg->err_pos >= 0)
4268 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004269 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004270 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004271 /* we're in MSG_CHUNK_SIZE now */
4272 }
4273 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4274 int ret = http_forward_trailers(req, msg);
4275
4276 if (ret == 0)
4277 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004278 else if (ret < 0) {
4279 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004280 if (msg->err_pos >= 0)
4281 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004282 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004283 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004284 /* we're in HTTP_MSG_DONE now */
4285 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004287 int old_state = msg->msg_state;
4288
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004290 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004291 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4292 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4293 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 if (http_resync_states(s)) {
4295 /* some state changes occurred, maybe the analyser
4296 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004297 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004298 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4299 if (req->flags & BF_SHUTW) {
4300 /* request errors are most likely due to
4301 * the server aborting the transfer.
4302 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004303 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004304 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004305 if (msg->err_pos >= 0)
4306 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004308 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004310 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004311
4312 /* If "option abortonclose" is set on the backend, we
4313 * want to monitor the client's connection and forward
4314 * any shutdown notification to the server, which will
4315 * decide whether to close or to go on processing the
4316 * request.
4317 */
4318 if (s->be->options & PR_O_ABRT_CLOSE) {
4319 buffer_auto_read(req);
4320 buffer_auto_close(req);
4321 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004322 else if (s->txn.meth == HTTP_METH_POST) {
4323 /* POST requests may require to read extra CRLF
4324 * sent by broken browsers and which could cause
4325 * an RST to be sent upon close on some systems
4326 * (eg: Linux).
4327 */
4328 buffer_auto_read(req);
4329 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004330
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004332 }
4333 }
4334
Willy Tarreaud98cf932009-12-27 22:54:55 +01004335 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004336 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004337 if (req->flags & BF_SHUTR) {
4338 if (!(s->flags & SN_ERR_MASK))
4339 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004340 if (!(s->flags & SN_FINST_MASK)) {
4341 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4342 s->flags |= SN_FINST_H;
4343 else
4344 s->flags |= SN_FINST_D;
4345 }
4346
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004347 s->fe->fe_counters.cli_aborts++;
4348 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004349 if (target_srv(&s->target))
4350 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004351
4352 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004353 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004354
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004355 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004356 if (req->flags & BF_SHUTW)
4357 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004359 /* When TE: chunked is used, we need to get there again to parse remaining
4360 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4361 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004362 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004363 buffer_dont_close(req);
4364
Willy Tarreau5c620922011-05-11 19:56:11 +02004365 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004366 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4367 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004368 * modes are already handled by the stream sock layer. We must not do
4369 * this in content-length mode because it could present the MSG_MORE
4370 * flag with the last block of forwarded data, which would cause an
4371 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004372 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004373 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004374 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004375
Willy Tarreau610ecce2010-01-04 21:15:02 +01004376 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004377 return 0;
4378
Willy Tarreaud98cf932009-12-27 22:54:55 +01004379 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004380 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004381 if (s->listener->counters)
4382 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004383 return_bad_req_stats_ok:
4384 txn->req.msg_state = HTTP_MSG_ERROR;
4385 if (txn->status) {
4386 /* Note: we don't send any error if some data were already sent */
4387 stream_int_retnclose(req->prod, NULL);
4388 } else {
4389 txn->status = 400;
4390 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4391 }
4392 req->analysers = 0;
4393 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004394
4395 if (!(s->flags & SN_ERR_MASK))
4396 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004397 if (!(s->flags & SN_FINST_MASK)) {
4398 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4399 s->flags |= SN_FINST_H;
4400 else
4401 s->flags |= SN_FINST_D;
4402 }
4403 return 0;
4404
4405 aborted_xfer:
4406 txn->req.msg_state = HTTP_MSG_ERROR;
4407 if (txn->status) {
4408 /* Note: we don't send any error if some data were already sent */
4409 stream_int_retnclose(req->prod, NULL);
4410 } else {
4411 txn->status = 502;
4412 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4413 }
4414 req->analysers = 0;
4415 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4416
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004417 s->fe->fe_counters.srv_aborts++;
4418 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004419 if (target_srv(&s->target))
4420 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004421
4422 if (!(s->flags & SN_ERR_MASK))
4423 s->flags |= SN_ERR_SRVCL;
4424 if (!(s->flags & SN_FINST_MASK)) {
4425 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4426 s->flags |= SN_FINST_H;
4427 else
4428 s->flags |= SN_FINST_D;
4429 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004430 return 0;
4431}
4432
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004433/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4434 * processing can continue on next analysers, or zero if it either needs more
4435 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4436 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4437 * when it has nothing left to do, and may remove any analyser when it wants to
4438 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004439 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004441{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004442 struct http_txn *txn = &s->txn;
4443 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004444 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004445 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004447 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004448
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004449 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 +02004450 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004451 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004452 rep,
4453 rep->rex, rep->wex,
4454 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004455 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004456 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004457
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 /*
4459 * Now parse the partial (or complete) lines.
4460 * We will check the response syntax, and also join multi-line
4461 * headers. An index of all the lines will be elaborated while
4462 * parsing.
4463 *
4464 * For the parsing, we use a 28 states FSM.
4465 *
4466 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004467 * rep->data + msg->som = beginning of response
4468 * rep->data + msg->eoh = end of processed headers / start of current one
4469 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004470 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004472 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004473 */
4474
Willy Tarreau83e3af02009-12-28 17:39:57 +01004475 /* There's a protected area at the end of the buffer for rewriting
4476 * purposes. We don't want to start to parse the request if the
4477 * protected area is affected, because we may have to move processed
4478 * data later, which is much more complicated.
4479 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004480 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004481 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004482 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004483 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004484 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004485 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004486 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4487 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004488 buffer_dont_close(rep);
4489 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4490 return 0;
4491 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004492 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004493 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004494 }
4495
Willy Tarreaua458b672012-03-05 11:17:50 +01004496 if (likely(msg->next < rep->i))
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004497 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004498 }
4499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500 /* 1: we might have to print this header in debug mode */
4501 if (unlikely((global.mode & MODE_DEBUG) &&
4502 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004503 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004504 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004506
Willy Tarreauea1175a2012-03-05 15:52:30 +01004507 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004508 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004510
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 sol += hdr_idx_first_pos(&txn->hdr_idx);
4512 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004513
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004514 while (cur_idx) {
4515 eol = sol + txn->hdr_idx.v[cur_idx].len;
4516 debug_hdr("srvhdr", s, sol, eol);
4517 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4518 cur_idx = txn->hdr_idx.v[cur_idx].next;
4519 }
4520 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004521
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522 /*
4523 * Now we quickly check if we have found a full valid response.
4524 * If not so, we check the FD and buffer states before leaving.
4525 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004526 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527 * responses are checked first.
4528 *
4529 * Depending on whether the client is still there or not, we
4530 * may send an error response back or not. Note that normally
4531 * we should only check for HTTP status there, and check I/O
4532 * errors somewhere else.
4533 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004534
Willy Tarreau655dce92009-11-08 13:10:58 +01004535 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004536 /* Invalid response */
4537 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4538 /* we detected a parsing error. We want to archive this response
4539 * in the dedicated proxy area for later troubleshooting.
4540 */
4541 hdr_response_bad:
4542 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004543 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004545 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004546 if (target_srv(&s->target)) {
4547 target_srv(&s->target)->counters.failed_resp++;
4548 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004549 }
Willy Tarreau64648412010-03-05 10:41:54 +01004550 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004551 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004552 rep->analysers = 0;
4553 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004554 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004555 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4557
4558 if (!(s->flags & SN_ERR_MASK))
4559 s->flags |= SN_ERR_PRXCOND;
4560 if (!(s->flags & SN_FINST_MASK))
4561 s->flags |= SN_FINST_H;
4562
4563 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004564 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004565
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 /* too large response does not fit in buffer. */
4567 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004568 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004569 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 goto hdr_response_bad;
4571 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 /* read error */
4574 else if (rep->flags & BF_READ_ERROR) {
4575 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004576 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004577
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004578 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004579 if (target_srv(&s->target)) {
4580 target_srv(&s->target)->counters.failed_resp++;
4581 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004582 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004583
Willy Tarreau90deb182010-01-07 00:20:41 +01004584 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 rep->analysers = 0;
4586 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004587 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004588 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004589 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004590
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 if (!(s->flags & SN_ERR_MASK))
4592 s->flags |= SN_ERR_SRVCL;
4593 if (!(s->flags & SN_FINST_MASK))
4594 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004595 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 /* read timeout : return a 504 to the client. */
4599 else if (rep->flags & BF_READ_TIMEOUT) {
4600 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004601 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004602
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004603 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004604 if (target_srv(&s->target)) {
4605 target_srv(&s->target)->counters.failed_resp++;
4606 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004607 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004608
Willy Tarreau90deb182010-01-07 00:20:41 +01004609 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 rep->analysers = 0;
4611 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004612 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004613 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004615
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 if (!(s->flags & SN_ERR_MASK))
4617 s->flags |= SN_ERR_SRVTO;
4618 if (!(s->flags & SN_FINST_MASK))
4619 s->flags |= SN_FINST_H;
4620 return 0;
4621 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004622
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004623 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004625 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004626 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004627
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004628 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004629 if (target_srv(&s->target)) {
4630 target_srv(&s->target)->counters.failed_resp++;
4631 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004632 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004633
Willy Tarreau90deb182010-01-07 00:20:41 +01004634 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635 rep->analysers = 0;
4636 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004637 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004638 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641 if (!(s->flags & SN_ERR_MASK))
4642 s->flags |= SN_ERR_SRVCL;
4643 if (!(s->flags & SN_FINST_MASK))
4644 s->flags |= SN_FINST_H;
4645 return 0;
4646 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004647
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 /* write error to client (we don't send any message then) */
4649 else if (rep->flags & BF_WRITE_ERROR) {
4650 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004651 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004652
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004653 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004655 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656
4657 if (!(s->flags & SN_ERR_MASK))
4658 s->flags |= SN_ERR_CLICL;
4659 if (!(s->flags & SN_FINST_MASK))
4660 s->flags |= SN_FINST_H;
4661
4662 /* process_session() will take care of the error */
4663 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004664 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 buffer_dont_close(rep);
4667 return 0;
4668 }
4669
4670 /* More interesting part now : we know that we have a complete
4671 * response which at least looks like HTTP. We have an indicator
4672 * of each header's length, so we can parse them quickly.
4673 */
4674
4675 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004676 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678 /*
4679 * 1: get the status code
4680 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004681 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004682 if (n < 1 || n > 5)
4683 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004684 /* when the client triggers a 4xx from the server, it's most often due
4685 * to a missing object or permission. These events should be tracked
4686 * because if they happen often, it may indicate a brute force or a
4687 * vulnerability scan.
4688 */
4689 if (n == 4)
4690 session_inc_http_err_ctr(s);
4691
Willy Tarreau827aee92011-03-10 16:55:02 +01004692 if (target_srv(&s->target))
4693 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694
Willy Tarreau5b154472009-12-21 20:11:07 +01004695 /* check if the response is HTTP/1.1 or above */
4696 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004697 ((msg->sol[5] > '1') ||
4698 ((msg->sol[5] == '1') &&
4699 (msg->sol[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004700 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004701
4702 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004703 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004704
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004705 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004706 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004707
Willy Tarreau962c3f42010-01-10 00:15:35 +01004708 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709
Willy Tarreau39650402010-03-15 19:44:39 +01004710 /* Adjust server's health based on status code. Note: status codes 501
4711 * and 505 are triggered on demand by client request, so we must not
4712 * count them as server failures.
4713 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004714 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004715 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004716 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004717 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004718 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004719 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004720
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 /*
4722 * 2: check for cacheability.
4723 */
4724
4725 switch (txn->status) {
4726 case 200:
4727 case 203:
4728 case 206:
4729 case 300:
4730 case 301:
4731 case 410:
4732 /* RFC2616 @13.4:
4733 * "A response received with a status code of
4734 * 200, 203, 206, 300, 301 or 410 MAY be stored
4735 * by a cache (...) unless a cache-control
4736 * directive prohibits caching."
4737 *
4738 * RFC2616 @9.5: POST method :
4739 * "Responses to this method are not cacheable,
4740 * unless the response includes appropriate
4741 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004742 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 if (likely(txn->meth != HTTP_METH_POST) &&
4744 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4745 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4746 break;
4747 default:
4748 break;
4749 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004750
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 /*
4752 * 3: we may need to capture headers
4753 */
4754 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004755 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004756 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004757 txn->rsp.cap, s->fe->rsp_cap);
4758
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004759 /* 4: determine the transfer-length.
4760 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4761 * the presence of a message-body in a RESPONSE and its transfer length
4762 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004763 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004764 * All responses to the HEAD request method MUST NOT include a
4765 * message-body, even though the presence of entity-header fields
4766 * might lead one to believe they do. All 1xx (informational), 204
4767 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4768 * message-body. All other responses do include a message-body,
4769 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004770 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004771 * 1. Any response which "MUST NOT" include a message-body (such as the
4772 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4773 * always terminated by the first empty line after the header fields,
4774 * regardless of the entity-header fields present in the message.
4775 *
4776 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4777 * the "chunked" transfer-coding (Section 6.2) is used, the
4778 * transfer-length is defined by the use of this transfer-coding.
4779 * If a Transfer-Encoding header field is present and the "chunked"
4780 * transfer-coding is not present, the transfer-length is defined by
4781 * the sender closing the connection.
4782 *
4783 * 3. If a Content-Length header field is present, its decimal value in
4784 * OCTETs represents both the entity-length and the transfer-length.
4785 * If a message is received with both a Transfer-Encoding header
4786 * field and a Content-Length header field, the latter MUST be ignored.
4787 *
4788 * 4. If the message uses the media type "multipart/byteranges", and
4789 * the transfer-length is not otherwise specified, then this self-
4790 * delimiting media type defines the transfer-length. This media
4791 * type MUST NOT be used unless the sender knows that the recipient
4792 * can parse it; the presence in a request of a Range header with
4793 * multiple byte-range specifiers from a 1.1 client implies that the
4794 * client can parse multipart/byteranges responses.
4795 *
4796 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004797 */
4798
4799 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004800 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004801 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004802 * FIXME: should we parse anyway and return an error on chunked encoding ?
4803 */
4804 if (txn->meth == HTTP_METH_HEAD ||
4805 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004806 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004807 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 goto skip_content_length;
4809 }
4810
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004811 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004813 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004814 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004815 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004816 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4817 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004818 /* bad transfer-encoding (chunked followed by something else) */
4819 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004820 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004821 break;
4822 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823 }
4824
4825 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4826 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004827 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4829 signed long long cl;
4830
Willy Tarreauad14f752011-09-02 20:33:27 +02004831 if (!ctx.vlen) {
4832 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004834 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004835
Willy Tarreauad14f752011-09-02 20:33:27 +02004836 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4837 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004838 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004839 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004840
Willy Tarreauad14f752011-09-02 20:33:27 +02004841 if (cl < 0) {
4842 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004843 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004844 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004845
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004846 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004847 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004848 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004849 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004850
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004851 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004852 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004853 }
4854
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004855 /* FIXME: we should also implement the multipart/byterange method.
4856 * For now on, we resort to close mode in this case (unknown length).
4857 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004858skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004859
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004860 /* end of job, return OK */
4861 rep->analysers &= ~an_bit;
4862 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004863 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 return 1;
4865}
4866
4867/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004868 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4869 * and updates t->rep->analysers. It might make sense to explode it into several
4870 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 */
4872int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4873{
4874 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004875 struct http_msg *msg = &txn->rsp;
4876 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004877 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004878
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004879 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 +02004880 now_ms, __FUNCTION__,
4881 t,
4882 rep,
4883 rep->rex, rep->wex,
4884 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004885 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004886 rep->analysers);
4887
Willy Tarreau655dce92009-11-08 13:10:58 +01004888 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889 return 0;
4890
4891 rep->analysers &= ~an_bit;
4892 rep->analyse_exp = TICK_ETERNITY;
4893
Willy Tarreau5b154472009-12-21 20:11:07 +01004894 /* Now we have to check if we need to modify the Connection header.
4895 * This is more difficult on the response than it is on the request,
4896 * because we can have two different HTTP versions and we don't know
4897 * how the client will interprete a response. For instance, let's say
4898 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4899 * HTTP/1.1 response without any header. Maybe it will bound itself to
4900 * HTTP/1.0 because it only knows about it, and will consider the lack
4901 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4902 * the lack of header as a keep-alive. Thus we will use two flags
4903 * indicating how a request MAY be understood by the client. In case
4904 * of multiple possibilities, we'll fix the header to be explicit. If
4905 * ambiguous cases such as both close and keepalive are seen, then we
4906 * will fall back to explicit close. Note that we won't take risks with
4907 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004908 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004909 */
4910
Willy Tarreaudc008c52010-02-01 16:20:08 +01004911 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4912 txn->status == 101)) {
4913 /* Either we've established an explicit tunnel, or we're
4914 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004915 * to understand the next protocols. We have to switch to tunnel
4916 * mode, so that we transfer the request and responses then let
4917 * this protocol pass unmodified. When we later implement specific
4918 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004919 * header which contains information about that protocol for
4920 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004921 */
4922 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4923 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004924 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4925 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4926 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004927 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004928
Willy Tarreau60466522010-01-18 19:08:45 +01004929 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004930 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004931 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4932 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004933
Willy Tarreau60466522010-01-18 19:08:45 +01004934 /* now adjust header transformations depending on current state */
4935 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4936 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4937 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004938 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004939 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004940 }
Willy Tarreau60466522010-01-18 19:08:45 +01004941 else { /* SCL / KAL */
4942 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004943 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004944 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004945 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004946
Willy Tarreau60466522010-01-18 19:08:45 +01004947 /* Parse and remove some headers from the connection header */
4948 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004949
Willy Tarreau60466522010-01-18 19:08:45 +01004950 /* Some keep-alive responses are converted to Server-close if
4951 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004952 */
Willy Tarreau60466522010-01-18 19:08:45 +01004953 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4954 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004955 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004956 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004957 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004958 }
4959
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004960 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004961 /*
4962 * 3: we will have to evaluate the filters.
4963 * As opposed to version 1.2, now they will be evaluated in the
4964 * filters order and not in the header order. This means that
4965 * each filter has to be validated among all headers.
4966 *
4967 * Filters are tried with ->be first, then with ->fe if it is
4968 * different from ->be.
4969 */
4970
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004971 cur_proxy = t->be;
4972 while (1) {
4973 struct proxy *rule_set = cur_proxy;
4974
4975 /* try headers filters */
4976 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004977 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004979 if (target_srv(&t->target)) {
4980 target_srv(&t->target)->counters.failed_resp++;
4981 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004982 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004983 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004984 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004985 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004987 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004988 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004989 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004990 if (!(t->flags & SN_ERR_MASK))
4991 t->flags |= SN_ERR_PRXCOND;
4992 if (!(t->flags & SN_FINST_MASK))
4993 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004994 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004995 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004996 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004997
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 /* has the response been denied ? */
4999 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005000 if (target_srv(&t->target))
5001 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005003 t->be->be_counters.denied_resp++;
5004 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005005 if (t->listener->counters)
5006 t->listener->counters->denied_resp++;
5007
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005008 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005009 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005011 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005012 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005013 if (txn->status < 200)
5014 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005015 if (wl->cond) {
5016 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5017 ret = acl_pass(ret);
5018 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5019 ret = !ret;
5020 if (!ret)
5021 continue;
5022 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005023 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005024 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005025 }
5026
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005027 /* check whether we're already working on the frontend */
5028 if (cur_proxy == t->fe)
5029 break;
5030 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005031 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005032
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005033 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005034 * We may be facing a 100-continue response, in which case this
5035 * is not the right response, and we're waiting for the next one.
5036 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005037 * next one.
5038 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005039 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005040 hdr_idx_init(&txn->hdr_idx);
Willy Tarreaua458b672012-03-05 11:17:50 +01005041 msg->next -= buffer_forward(rep, rep->p + msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005042 msg->msg_state = HTTP_MSG_RPBEFORE;
5043 txn->status = 0;
5044 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5045 return 1;
5046 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005047 else if (unlikely(txn->status < 200))
5048 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005049
5050 /* we don't have any 1xx status code now */
5051
5052 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 * 4: check for server cookie.
5054 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005055 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5056 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005057 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005058
Willy Tarreaubaaee002006-06-26 02:48:02 +02005059
Willy Tarreaua15645d2007-03-18 16:22:39 +01005060 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005062 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005063 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005065
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066 /*
5067 * 6: add server cookie in the response if needed
5068 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005069 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005070 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005071 (!(t->flags & SN_DIRECT) ||
5072 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5073 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5074 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5075 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005076 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5077 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005078 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005079 /* the server is known, it's not the one the client requested, or the
5080 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081 * insert a set-cookie here, except if we want to insert only on POST
5082 * requests and this one isn't. Note that servers which don't have cookies
5083 * (eg: some backup servers) will return a full cookie removal request.
5084 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005085 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005086 len = sprintf(trash,
5087 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5088 t->be->cookie_name);
5089 }
5090 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005091 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005092
5093 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5094 /* emit last_date, which is mandatory */
5095 trash[len++] = COOKIE_DELIM_DATE;
5096 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5097 if (t->be->cookie_maxlife) {
5098 /* emit first_date, which is either the original one or
5099 * the current date.
5100 */
5101 trash[len++] = COOKIE_DELIM_DATE;
5102 s30tob64(txn->cookie_first_date ?
5103 txn->cookie_first_date >> 2 :
5104 (date.tv_sec+3) >> 2, trash + len);
5105 len += 5;
5106 }
5107 }
5108 len += sprintf(trash + len, "; path=/");
5109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005110
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005111 if (t->be->cookie_domain)
5112 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005113
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005115 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005116 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005117
Willy Tarreauf1348312010-10-07 15:54:11 +02005118 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005119 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005120 /* the server did not change, only the date was updated */
5121 txn->flags |= TX_SCK_UPDATED;
5122 else
5123 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005124
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125 /* Here, we will tell an eventual cache on the client side that we don't
5126 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5127 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5128 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5129 */
5130 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005131
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005132 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5133
5134 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005135 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005136 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005137 }
5138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 /*
5141 * 7: check if result will be cacheable with a cookie.
5142 * We'll block the response if security checks have caught
5143 * nasty things such as a cacheable cookie.
5144 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005145 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5146 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005147 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148
5149 /* we're in presence of a cacheable response containing
5150 * a set-cookie header. We'll block it as requested by
5151 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005152 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005153 if (target_srv(&t->target))
5154 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005155
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005156 t->be->be_counters.denied_resp++;
5157 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005158 if (t->listener->counters)
5159 t->listener->counters->denied_resp++;
5160
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005162 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005163 send_log(t->be, LOG_ALERT,
5164 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005165 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005166 goto return_srv_prx_502;
5167 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005168
5169 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005170 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005171 */
Willy Tarreau60466522010-01-18 19:08:45 +01005172 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5173 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5174 unsigned int want_flags = 0;
5175
5176 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5177 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5178 /* we want a keep-alive response here. Keep-alive header
5179 * required if either side is not 1.1.
5180 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005181 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005182 want_flags |= TX_CON_KAL_SET;
5183 }
5184 else {
5185 /* we want a close response here. Close header required if
5186 * the server is 1.1, regardless of the client.
5187 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005188 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005189 want_flags |= TX_CON_CLO_SET;
5190 }
5191
5192 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5193 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005194 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005195
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005196 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005197 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005198 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005199 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 /*************************************************************
5202 * OK, that's finished for the headers. We have done what we *
5203 * could. Let's switch to the DATA state. *
5204 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005205
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /* if the user wants to log as soon as possible, without counting
5209 * bytes from the server, then this is the right moment. We have
5210 * to temporarily assign bytes_out to log what we currently have.
5211 */
5212 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5213 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5214 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005215 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 t->logs.bytes_out = 0;
5217 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005218
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219 /* Note: we must not try to cheat by jumping directly to DATA,
5220 * otherwise we would not let the client side wake up.
5221 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005222
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005223 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005224 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005225 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005226}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005227
Willy Tarreaud98cf932009-12-27 22:54:55 +01005228/* This function is an analyser which forwards response body (including chunk
5229 * sizes if any). It is called as soon as we must forward, even if we forward
5230 * zero byte. The only situation where it must not be called is when we're in
5231 * tunnel mode and we want to forward till the close. It's used both to forward
5232 * remaining data and to resync after end of body. It expects the msg_state to
5233 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5234 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005235 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005236 * bytes of pending data + the headers if not already done (between som and sov).
5237 * It eventually adjusts som to match sov after the data in between have been sent.
5238 */
5239int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5240{
5241 struct http_txn *txn = &s->txn;
5242 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005243 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005244
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005245 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5246 return 0;
5247
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005248 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005249 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005250 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005251 /* Output closed while we were sending data. We must abort and
5252 * wake the other side up.
5253 */
5254 msg->msg_state = HTTP_MSG_ERROR;
5255 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005256 return 1;
5257 }
5258
Willy Tarreau4fe41902010-06-07 22:27:41 +02005259 /* in most states, we should abort in case of early close */
5260 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005261
Willy Tarreaud98cf932009-12-27 22:54:55 +01005262 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5263 /* we have msg->col and msg->sov which both point to the first
5264 * byte of message body. msg->som still points to the beginning
Willy Tarreaua458b672012-03-05 11:17:50 +01005265 * of the message. We must save the body in msg->next because it
Willy Tarreaud98cf932009-12-27 22:54:55 +01005266 * survives buffer re-alignments.
5267 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005268 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005269
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005270 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005271 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5272 else {
5273 msg->msg_state = HTTP_MSG_DATA;
5274 }
5275 }
5276
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005278 int bytes;
5279
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005281 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005282 bytes = msg->sov - msg->som;
5283 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005284 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005285 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5286 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005287 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005288 msg->chunk_len += (unsigned int)bytes;
5289 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005290 }
5291
Willy Tarreaucaabe412010-01-03 23:08:28 +01005292 if (msg->msg_state == HTTP_MSG_DATA) {
5293 /* must still forward */
5294 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005295 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005296
5297 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005298 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005299 msg->msg_state = HTTP_MSG_DATA_CRLF;
5300 else
5301 msg->msg_state = HTTP_MSG_DONE;
5302 }
5303 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005304 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005305 * set ->sov and ->next to point to the body and switch to DATA or
5306 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005307 */
5308 int ret = http_parse_chunk_size(res, msg);
5309
5310 if (!ret)
5311 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005312 else if (ret < 0) {
5313 if (msg->err_pos >= 0)
5314 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005315 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005316 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005317 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005318 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005319 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5320 /* we want the CRLF after the data */
5321 int ret;
5322
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 ret = http_skip_chunk_crlf(res, msg);
5324
5325 if (!ret)
5326 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005327 else if (ret < 0) {
5328 if (msg->err_pos >= 0)
5329 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005331 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332 /* we're in MSG_CHUNK_SIZE now */
5333 }
5334 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5335 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005336
Willy Tarreaud98cf932009-12-27 22:54:55 +01005337 if (ret == 0)
5338 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005339 else if (ret < 0) {
5340 if (msg->err_pos >= 0)
5341 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005343 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005344 /* we're in HTTP_MSG_DONE now */
5345 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005346 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005347 int old_state = msg->msg_state;
5348
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005350 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005351 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5352 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5353 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 if (http_resync_states(s)) {
5355 http_silent_debug(__LINE__, s);
5356 /* some state changes occurred, maybe the analyser
5357 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005358 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005359 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5360 if (res->flags & BF_SHUTW) {
5361 /* response errors are most likely due to
5362 * the client aborting the transfer.
5363 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005364 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005365 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005366 if (msg->err_pos >= 0)
5367 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005369 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005371 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005373 }
5374 }
5375
Willy Tarreaud98cf932009-12-27 22:54:55 +01005376 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005377 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005378 if (res->flags & BF_SHUTR) {
5379 if (!(s->flags & SN_ERR_MASK))
5380 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005381 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005382 if (target_srv(&s->target))
5383 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005384 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005385 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005386
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005387 if (res->flags & BF_SHUTW)
5388 goto aborted_xfer;
5389
Willy Tarreau40dba092010-03-04 18:14:51 +01005390 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391 if (!s->req->analysers)
5392 goto return_bad_res;
5393
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005394 /* forward any pending data */
5395 bytes = msg->sov - msg->som;
5396 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005397 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005398 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5399 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005400 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005401 msg->chunk_len += (unsigned int)bytes;
5402 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 }
5404
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005405 /* When TE: chunked is used, we need to get there again to parse remaining
5406 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5407 * Similarly, with keep-alive on the client side, we don't want to forward a
5408 * close.
5409 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005410 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005411 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5412 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5413 buffer_dont_close(res);
5414
Willy Tarreau5c620922011-05-11 19:56:11 +02005415 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005416 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5417 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005418 * modes are already handled by the stream sock layer. We must not do
5419 * this in content-length mode because it could present the MSG_MORE
5420 * flag with the last block of forwarded data, which would cause an
5421 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005422 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005423 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005424 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005425
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 return 0;
5429
Willy Tarreau40dba092010-03-04 18:14:51 +01005430 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005431 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005432 if (target_srv(&s->target))
5433 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005434
5435 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005437 /* don't send any error message as we're in the body */
5438 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005440 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005441 if (target_srv(&s->target))
5442 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443
5444 if (!(s->flags & SN_ERR_MASK))
5445 s->flags |= SN_ERR_PRXCOND;
5446 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005447 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005448 return 0;
5449
5450 aborted_xfer:
5451 txn->rsp.msg_state = HTTP_MSG_ERROR;
5452 /* don't send any error message as we're in the body */
5453 stream_int_retnclose(res->cons, NULL);
5454 res->analysers = 0;
5455 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5456
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005457 s->fe->fe_counters.cli_aborts++;
5458 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005459 if (target_srv(&s->target))
5460 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005461
5462 if (!(s->flags & SN_ERR_MASK))
5463 s->flags |= SN_ERR_CLICL;
5464 if (!(s->flags & SN_FINST_MASK))
5465 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005466 return 0;
5467}
5468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005469/* Iterate the same filter through all request headers.
5470 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005471 * Since it can manage the switch to another backend, it updates the per-proxy
5472 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005473 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005475{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 char term;
5477 char *cur_ptr, *cur_end, *cur_next;
5478 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 struct hdr_idx_elem *cur_hdr;
5481 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005483 last_hdr = 0;
5484
Willy Tarreau962c3f42010-01-10 00:15:35 +01005485 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005486 old_idx = 0;
5487
5488 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005489 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005490 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005491 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005492 (exp->action == ACT_ALLOW ||
5493 exp->action == ACT_DENY ||
5494 exp->action == ACT_TARPIT))
5495 return 0;
5496
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005497 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005498 if (!cur_idx)
5499 break;
5500
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005501 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502 cur_ptr = cur_next;
5503 cur_end = cur_ptr + cur_hdr->len;
5504 cur_next = cur_end + cur_hdr->cr + 1;
5505
5506 /* Now we have one header between cur_ptr and cur_end,
5507 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005508 */
5509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005510 /* The annoying part is that pattern matching needs
5511 * that we modify the contents to null-terminate all
5512 * strings before testing them.
5513 */
5514
5515 term = *cur_end;
5516 *cur_end = '\0';
5517
5518 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5519 switch (exp->action) {
5520 case ACT_SETBE:
5521 /* It is not possible to jump a second time.
5522 * FIXME: should we return an HTTP/500 here so that
5523 * the admin knows there's a problem ?
5524 */
5525 if (t->be != t->fe)
5526 break;
5527
5528 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005529 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 last_hdr = 1;
5531 break;
5532
5533 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005534 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005535 last_hdr = 1;
5536 break;
5537
5538 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005539 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005540 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005541
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005542 t->fe->fe_counters.denied_req++;
5543 if (t->fe != t->be)
5544 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005545 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005546 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005548 break;
5549
5550 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005551 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005552 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005553
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005554 t->fe->fe_counters.denied_req++;
5555 if (t->fe != t->be)
5556 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005557 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005558 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005560 break;
5561
5562 case ACT_REPLACE:
5563 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5564 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5565 /* FIXME: if the user adds a newline in the replacement, the
5566 * index will not be recalculated for now, and the new line
5567 * will not be counted as a new header.
5568 */
5569
5570 cur_end += delta;
5571 cur_next += delta;
5572 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005573 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 break;
5575
5576 case ACT_REMOVE:
5577 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5578 cur_next += delta;
5579
Willy Tarreaufa355d42009-11-29 18:12:29 +01005580 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005581 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5582 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 cur_hdr->len = 0;
5584 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005585 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005586 break;
5587
5588 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005589 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005590 if (cur_end)
5591 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005593 /* keep the link from this header to next one in case of later
5594 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 old_idx = cur_idx;
5597 }
5598 return 0;
5599}
5600
5601
5602/* Apply the filter to the request line.
5603 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5604 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005605 * Since it can manage the switch to another backend, it updates the per-proxy
5606 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005607 */
5608int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5609{
5610 char term;
5611 char *cur_ptr, *cur_end;
5612 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005613 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005614 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005615
Willy Tarreau58f10d72006-12-04 02:26:12 +01005616
Willy Tarreau3d300592007-03-18 18:34:41 +01005617 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005618 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005619 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005620 (exp->action == ACT_ALLOW ||
5621 exp->action == ACT_DENY ||
5622 exp->action == ACT_TARPIT))
5623 return 0;
5624 else if (exp->action == ACT_REMOVE)
5625 return 0;
5626
5627 done = 0;
5628
Willy Tarreau962c3f42010-01-10 00:15:35 +01005629 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005630 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005631
5632 /* Now we have the request line between cur_ptr and cur_end */
5633
5634 /* The annoying part is that pattern matching needs
5635 * that we modify the contents to null-terminate all
5636 * strings before testing them.
5637 */
5638
5639 term = *cur_end;
5640 *cur_end = '\0';
5641
5642 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5643 switch (exp->action) {
5644 case ACT_SETBE:
5645 /* It is not possible to jump a second time.
5646 * FIXME: should we return an HTTP/500 here so that
5647 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 if (t->be != t->fe)
5650 break;
5651
5652 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005653 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 done = 1;
5655 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005658 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 done = 1;
5660 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005663 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005664
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005665 t->fe->fe_counters.denied_req++;
5666 if (t->fe != t->be)
5667 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005668 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005669 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 done = 1;
5672 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005676
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005677 t->fe->fe_counters.denied_req++;
5678 if (t->fe != t->be)
5679 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005680 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005681 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 done = 1;
5684 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 case ACT_REPLACE:
5687 *cur_end = term; /* restore the string terminator */
5688 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5689 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5690 /* FIXME: if the user adds a newline in the replacement, the
5691 * index will not be recalculated for now, and the new line
5692 * will not be counted as a new header.
5693 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005694
Willy Tarreaufa355d42009-11-29 18:12:29 +01005695 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696 cur_end += delta;
Willy Tarreaua458b672012-03-05 11:17:50 +01005697 cur_end = (char *)http_parse_reqline(&txn->req, req->data, req->p,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 HTTP_MSG_RQMETH,
5699 cur_ptr, cur_end + 1,
5700 NULL, NULL);
5701 if (unlikely(!cur_end))
5702 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005703
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005704 /* we have a full request and we know that we have either a CR
5705 * or an LF at <ptr>.
5706 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005707 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5708 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 /* there is no point trying this regex on headers */
5710 return 1;
5711 }
5712 }
5713 *cur_end = term; /* restore the string terminator */
5714 return done;
5715}
Willy Tarreau97de6242006-12-27 17:18:38 +01005716
Willy Tarreau58f10d72006-12-04 02:26:12 +01005717
Willy Tarreau58f10d72006-12-04 02:26:12 +01005718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005720 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005722 * unparsable request. Since it can manage the switch to another backend, it
5723 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005725int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005726{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005727 struct http_txn *txn = &s->txn;
5728 struct hdr_exp *exp;
5729
5730 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 /*
5734 * The interleaving of transformations and verdicts
5735 * makes it difficult to decide to continue or stop
5736 * the evaluation.
5737 */
5738
Willy Tarreau6c123b12010-01-28 20:22:06 +01005739 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5740 break;
5741
Willy Tarreau3d300592007-03-18 18:34:41 +01005742 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005744 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005746
5747 /* if this filter had a condition, evaluate it now and skip to
5748 * next filter if the condition does not match.
5749 */
5750 if (exp->cond) {
5751 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5752 ret = acl_pass(ret);
5753 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5754 ret = !ret;
5755
5756 if (!ret)
5757 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 }
5759
5760 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005761 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005762 if (unlikely(ret < 0))
5763 return -1;
5764
5765 if (likely(ret == 0)) {
5766 /* The filter did not match the request, it can be
5767 * iterated through all headers.
5768 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005769 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005771 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005773}
5774
5775
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776
Willy Tarreau58f10d72006-12-04 02:26:12 +01005777/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005778 * Try to retrieve the server associated to the appsession.
5779 * If the server is found, it's assigned to the session.
5780 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005781void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005782 struct http_txn *txn = &t->txn;
5783 appsess *asession = NULL;
5784 char *sessid_temp = NULL;
5785
Cyril Bontéb21570a2009-11-29 20:04:48 +01005786 if (len > t->be->appsession_len) {
5787 len = t->be->appsession_len;
5788 }
5789
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005790 if (t->be->options2 & PR_O2_AS_REQL) {
5791 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005792 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005793 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005794 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005795 }
5796
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005797 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005798 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5799 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5800 return;
5801 }
5802
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005803 memcpy(txn->sessid, buf, len);
5804 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005805 }
5806
5807 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5808 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5809 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5810 return;
5811 }
5812
Cyril Bontéb21570a2009-11-29 20:04:48 +01005813 memcpy(sessid_temp, buf, len);
5814 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005815
5816 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5817 /* free previously allocated memory */
5818 pool_free2(apools.sessid, sessid_temp);
5819
5820 if (asession != NULL) {
5821 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5822 if (!(t->be->options2 & PR_O2_AS_REQL))
5823 asession->request_count++;
5824
5825 if (asession->serverid != NULL) {
5826 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005827
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005828 while (srv) {
5829 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005830 if ((srv->state & SRV_RUNNING) ||
5831 (t->be->options & PR_O_PERSIST) ||
5832 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005833 /* we found the server and it's usable */
5834 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005835 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005836 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005837 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005838
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005839 break;
5840 } else {
5841 txn->flags &= ~TX_CK_MASK;
5842 txn->flags |= TX_CK_DOWN;
5843 }
5844 }
5845 srv = srv->next;
5846 }
5847 }
5848 }
5849}
5850
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005851/* Find the end of a cookie value contained between <s> and <e>. It works the
5852 * same way as with headers above except that the semi-colon also ends a token.
5853 * See RFC2965 for more information. Note that it requires a valid header to
5854 * return a valid result.
5855 */
5856char *find_cookie_value_end(char *s, const char *e)
5857{
5858 int quoted, qdpair;
5859
5860 quoted = qdpair = 0;
5861 for (; s < e; s++) {
5862 if (qdpair) qdpair = 0;
5863 else if (quoted) {
5864 if (*s == '\\') qdpair = 1;
5865 else if (*s == '"') quoted = 0;
5866 }
5867 else if (*s == '"') quoted = 1;
5868 else if (*s == ',' || *s == ';') return s;
5869 }
5870 return s;
5871}
5872
5873/* Delete a value in a header between delimiters <from> and <next> in buffer
5874 * <buf>. The number of characters displaced is returned, and the pointer to
5875 * the first delimiter is updated if required. The function tries as much as
5876 * possible to respect the following principles :
5877 * - replace <from> delimiter by the <next> one unless <from> points to a
5878 * colon, in which case <next> is simply removed
5879 * - set exactly one space character after the new first delimiter, unless
5880 * there are not enough characters in the block being moved to do so.
5881 * - remove unneeded spaces before the previous delimiter and after the new
5882 * one.
5883 *
5884 * It is the caller's responsibility to ensure that :
5885 * - <from> points to a valid delimiter or the colon ;
5886 * - <next> points to a valid delimiter or the final CR/LF ;
5887 * - there are non-space chars before <from> ;
5888 * - there is a CR/LF at or after <next>.
5889 */
5890int del_hdr_value(struct buffer *buf, char **from, char *next)
5891{
5892 char *prev = *from;
5893
5894 if (*prev == ':') {
5895 /* We're removing the first value, preserve the colon and add a
5896 * space if possible.
5897 */
5898 if (!http_is_crlf[(unsigned char)*next])
5899 next++;
5900 prev++;
5901 if (prev < next)
5902 *prev++ = ' ';
5903
5904 while (http_is_spht[(unsigned char)*next])
5905 next++;
5906 } else {
5907 /* Remove useless spaces before the old delimiter. */
5908 while (http_is_spht[(unsigned char)*(prev-1)])
5909 prev--;
5910 *from = prev;
5911
5912 /* copy the delimiter and if possible a space if we're
5913 * not at the end of the line.
5914 */
5915 if (!http_is_crlf[(unsigned char)*next]) {
5916 *prev++ = *next++;
5917 if (prev + 1 < next)
5918 *prev++ = ' ';
5919 while (http_is_spht[(unsigned char)*next])
5920 next++;
5921 }
5922 }
5923 return buffer_replace2(buf, prev, next, NULL, 0);
5924}
5925
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005926/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005927 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005928 * desirable to call it only when needed. This code is quite complex because
5929 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5930 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 */
5932void manage_client_side_cookies(struct session *t, struct buffer *req)
5933{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005934 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005936 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005937 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5938 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005940 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005941 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005944 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005946 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005948 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005949 hdr_beg = hdr_next;
5950 hdr_end = hdr_beg + cur_hdr->len;
5951 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005952
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005953 /* We have one full header between hdr_beg and hdr_end, and the
5954 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955 * "Cookie:" headers.
5956 */
5957
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005958 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005959 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005960 old_idx = cur_idx;
5961 continue;
5962 }
5963
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005964 del_from = NULL; /* nothing to be deleted */
5965 preserve_hdr = 0; /* assume we may kill the whole header */
5966
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967 /* Now look for cookies. Conforming to RFC2109, we have to support
5968 * attributes whose name begin with a '$', and associate them with
5969 * the right cookie, if we want to delete this cookie.
5970 * So there are 3 cases for each cookie read :
5971 * 1) it's a special attribute, beginning with a '$' : ignore it.
5972 * 2) it's a server id cookie that we *MAY* want to delete : save
5973 * some pointers on it (last semi-colon, beginning of cookie...)
5974 * 3) it's an application cookie : we *MAY* have to delete a previous
5975 * "special" cookie.
5976 * At the end of loop, if a "special" cookie remains, we may have to
5977 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005978 * *MUST* delete it.
5979 *
5980 * Note: RFC2965 is unclear about the processing of spaces around
5981 * the equal sign in the ATTR=VALUE form. A careful inspection of
5982 * the RFC explicitly allows spaces before it, and not within the
5983 * tokens (attrs or values). An inspection of RFC2109 allows that
5984 * too but section 10.1.3 lets one think that spaces may be allowed
5985 * after the equal sign too, resulting in some (rare) buggy
5986 * implementations trying to do that. So let's do what servers do.
5987 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5988 * allowed quoted strings in values, with any possible character
5989 * after a backslash, including control chars and delimitors, which
5990 * causes parsing to become ambiguous. Browsers also allow spaces
5991 * within values even without quotes.
5992 *
5993 * We have to keep multiple pointers in order to support cookie
5994 * removal at the beginning, middle or end of header without
5995 * corrupting the header. All of these headers are valid :
5996 *
5997 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5998 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5999 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6000 * | | | | | | | | |
6001 * | | | | | | | | hdr_end <--+
6002 * | | | | | | | +--> next
6003 * | | | | | | +----> val_end
6004 * | | | | | +-----------> val_beg
6005 * | | | | +--------------> equal
6006 * | | | +----------------> att_end
6007 * | | +---------------------> att_beg
6008 * | +--------------------------> prev
6009 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010 */
6011
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006012 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6013 /* Iterate through all cookies on this line */
6014
6015 /* find att_beg */
6016 att_beg = prev + 1;
6017 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6018 att_beg++;
6019
6020 /* find att_end : this is the first character after the last non
6021 * space before the equal. It may be equal to hdr_end.
6022 */
6023 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006025 while (equal < hdr_end) {
6026 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006028 if (http_is_spht[(unsigned char)*equal++])
6029 continue;
6030 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006031 }
6032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6034 * is between <att_beg> and <equal>, both may be identical.
6035 */
6036
6037 /* look for end of cookie if there is an equal sign */
6038 if (equal < hdr_end && *equal == '=') {
6039 /* look for the beginning of the value */
6040 val_beg = equal + 1;
6041 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6042 val_beg++;
6043
6044 /* find the end of the value, respecting quotes */
6045 next = find_cookie_value_end(val_beg, hdr_end);
6046
6047 /* make val_end point to the first white space or delimitor after the value */
6048 val_end = next;
6049 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6050 val_end--;
6051 } else {
6052 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006053 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 /* We have nothing to do with attributes beginning with '$'. However,
6056 * they will automatically be removed if a header before them is removed,
6057 * since they're supposed to be linked together.
6058 */
6059 if (*att_beg == '$')
6060 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 /* Ignore cookies with no equal sign */
6063 if (equal == next) {
6064 /* This is not our cookie, so we must preserve it. But if we already
6065 * scheduled another cookie for removal, we cannot remove the
6066 * complete header, but we can remove the previous block itself.
6067 */
6068 preserve_hdr = 1;
6069 if (del_from != NULL) {
6070 int delta = del_hdr_value(req, &del_from, prev);
6071 val_end += delta;
6072 next += delta;
6073 hdr_end += delta;
6074 hdr_next += delta;
6075 cur_hdr->len += delta;
6076 http_msg_move_end(&txn->req, delta);
6077 prev = del_from;
6078 del_from = NULL;
6079 }
6080 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006083 /* if there are spaces around the equal sign, we need to
6084 * strip them otherwise we'll get trouble for cookie captures,
6085 * or even for rewrites. Since this happens extremely rarely,
6086 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006088 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6089 int stripped_before = 0;
6090 int stripped_after = 0;
6091
6092 if (att_end != equal) {
6093 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6094 equal += stripped_before;
6095 val_beg += stripped_before;
6096 }
6097
6098 if (val_beg > equal + 1) {
6099 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6100 val_beg += stripped_after;
6101 stripped_before += stripped_after;
6102 }
6103
6104 val_end += stripped_before;
6105 next += stripped_before;
6106 hdr_end += stripped_before;
6107 hdr_next += stripped_before;
6108 cur_hdr->len += stripped_before;
6109 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006111 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 /* First, let's see if we want to capture this cookie. We check
6114 * that we don't already have a client side cookie, because we
6115 * can only capture one. Also as an optimisation, we ignore
6116 * cookies shorter than the declared name.
6117 */
6118 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6119 (val_end - att_beg >= t->fe->capture_namelen) &&
6120 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6121 int log_len = val_end - att_beg;
6122
6123 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6124 Alert("HTTP logging : out of memory.\n");
6125 } else {
6126 if (log_len > t->fe->capture_len)
6127 log_len = t->fe->capture_len;
6128 memcpy(txn->cli_cookie, att_beg, log_len);
6129 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006131 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132
Willy Tarreaubca99692010-10-06 19:25:55 +02006133 /* Persistence cookies in passive, rewrite or insert mode have the
6134 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006135 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006136 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006138 * For cookies in prefix mode, the form is :
6139 *
6140 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006141 */
6142 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6143 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6144 struct server *srv = t->be->srv;
6145 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6148 * have the server ID between val_beg and delim, and the original cookie between
6149 * delim+1 and val_end. Otherwise, delim==val_end :
6150 *
6151 * Cookie: NAME=SRV; # in all but prefix modes
6152 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6153 * | || || | |+-> next
6154 * | || || | +--> val_end
6155 * | || || +---------> delim
6156 * | || |+------------> val_beg
6157 * | || +-------------> att_end = equal
6158 * | |+-----------------> att_beg
6159 * | +------------------> prev
6160 * +-------------------------> hdr_beg
6161 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006162
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006163 if (t->be->options & PR_O_COOK_PFX) {
6164 for (delim = val_beg; delim < val_end; delim++)
6165 if (*delim == COOKIE_DELIM)
6166 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006167 } else {
6168 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006170 /* Now check if the cookie contains a date field, which would
6171 * appear after a vertical bar ('|') just after the server name
6172 * and before the delimiter.
6173 */
6174 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6175 if (vbar1) {
6176 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006177 * right is the last seen date. It is a base64 encoded
6178 * 30-bit value representing the UNIX date since the
6179 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006180 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006181 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006182 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006183 if (val_end - vbar1 >= 5) {
6184 val = b64tos30(vbar1);
6185 if (val > 0)
6186 txn->cookie_last_date = val << 2;
6187 }
6188 /* look for a second vertical bar */
6189 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6190 if (vbar1 && (val_end - vbar1 > 5)) {
6191 val = b64tos30(vbar1 + 1);
6192 if (val > 0)
6193 txn->cookie_first_date = val << 2;
6194 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006195 }
6196 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197
Willy Tarreauf64d1412010-10-07 20:06:11 +02006198 /* if the cookie has an expiration date and the proxy wants to check
6199 * it, then we do that now. We first check if the cookie is too old,
6200 * then only if it has expired. We detect strict overflow because the
6201 * time resolution here is not great (4 seconds). Cookies with dates
6202 * in the future are ignored if their offset is beyond one day. This
6203 * allows an admin to fix timezone issues without expiring everyone
6204 * and at the same time avoids keeping unwanted side effects for too
6205 * long.
6206 */
6207 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006208 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6209 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006210 txn->flags &= ~TX_CK_MASK;
6211 txn->flags |= TX_CK_OLD;
6212 delim = val_beg; // let's pretend we have not found the cookie
6213 txn->cookie_first_date = 0;
6214 txn->cookie_last_date = 0;
6215 }
6216 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006217 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6218 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006219 txn->flags &= ~TX_CK_MASK;
6220 txn->flags |= TX_CK_EXPIRED;
6221 delim = val_beg; // let's pretend we have not found the cookie
6222 txn->cookie_first_date = 0;
6223 txn->cookie_last_date = 0;
6224 }
6225
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 /* Here, we'll look for the first running server which supports the cookie.
6227 * This allows to share a same cookie between several servers, for example
6228 * to dedicate backup servers to specific servers only.
6229 * However, to prevent clients from sticking to cookie-less backup server
6230 * when they have incidentely learned an empty cookie, we simply ignore
6231 * empty cookies and mark them as invalid.
6232 * The same behaviour is applied when persistence must be ignored.
6233 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006234 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 while (srv) {
6238 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6239 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6240 if ((srv->state & SRV_RUNNING) ||
6241 (t->be->options & PR_O_PERSIST) ||
6242 (t->flags & SN_FORCE_PRST)) {
6243 /* we found the server and we can use it */
6244 txn->flags &= ~TX_CK_MASK;
6245 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6246 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006247 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 break;
6249 } else {
6250 /* we found a server, but it's down,
6251 * mark it as such and go on in case
6252 * another one is available.
6253 */
6254 txn->flags &= ~TX_CK_MASK;
6255 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 srv = srv->next;
6259 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006260
Willy Tarreauf64d1412010-10-07 20:06:11 +02006261 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006262 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006264 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6265 txn->flags |= TX_CK_UNUSED;
6266 else
6267 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 /* depending on the cookie mode, we may have to either :
6271 * - delete the complete cookie if we're in insert+indirect mode, so that
6272 * the server never sees it ;
6273 * - remove the server id from the cookie value, and tag the cookie as an
6274 * application cookie so that it does not get accidentely removed later,
6275 * if we're in cookie prefix mode
6276 */
6277 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6278 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006280 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6281 val_end += delta;
6282 next += delta;
6283 hdr_end += delta;
6284 hdr_next += delta;
6285 cur_hdr->len += delta;
6286 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 del_from = NULL;
6289 preserve_hdr = 1; /* we want to keep this cookie */
6290 }
6291 else if (del_from == NULL &&
6292 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6293 del_from = prev;
6294 }
6295 } else {
6296 /* This is not our cookie, so we must preserve it. But if we already
6297 * scheduled another cookie for removal, we cannot remove the
6298 * complete header, but we can remove the previous block itself.
6299 */
6300 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 if (del_from != NULL) {
6303 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006304 if (att_beg >= del_from)
6305 att_beg += delta;
6306 if (att_end >= del_from)
6307 att_end += delta;
6308 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 val_end += delta;
6310 next += delta;
6311 hdr_end += delta;
6312 hdr_next += delta;
6313 cur_hdr->len += delta;
6314 http_msg_move_end(&txn->req, delta);
6315 prev = del_from;
6316 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 /* Look for the appsession cookie unless persistence must be ignored */
6321 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6322 int cmp_len, value_len;
6323 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006324
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 if (t->be->options2 & PR_O2_AS_PFX) {
6326 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6327 value_begin = att_beg + t->be->appsession_name_len;
6328 value_len = val_end - att_beg - t->be->appsession_name_len;
6329 } else {
6330 cmp_len = att_end - att_beg;
6331 value_begin = val_beg;
6332 value_len = val_end - val_beg;
6333 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006334
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 /* let's see if the cookie is our appcookie */
6336 if (cmp_len == t->be->appsession_name_len &&
6337 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6338 manage_client_side_appsession(t, value_begin, value_len);
6339 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 }
6341
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 /* continue with next cookie on this header line */
6343 att_beg = next;
6344 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 /* There are no more cookies on this line.
6347 * We may still have one (or several) marked for deletion at the
6348 * end of the line. We must do this now in two ways :
6349 * - if some cookies must be preserved, we only delete from the
6350 * mark to the end of line ;
6351 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006355 if (preserve_hdr) {
6356 delta = del_hdr_value(req, &del_from, hdr_end);
6357 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358 cur_hdr->len += delta;
6359 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361
6362 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006363 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6364 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006366 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006367 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006368 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006369 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006370 }
6371
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375}
6376
6377
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378/* Iterate the same filter through all response headers contained in <rtr>.
6379 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6380 */
6381int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6382{
6383 char term;
6384 char *cur_ptr, *cur_end, *cur_next;
6385 int cur_idx, old_idx, last_hdr;
6386 struct http_txn *txn = &t->txn;
6387 struct hdr_idx_elem *cur_hdr;
6388 int len, delta;
6389
6390 last_hdr = 0;
6391
Willy Tarreau962c3f42010-01-10 00:15:35 +01006392 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006393 old_idx = 0;
6394
6395 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006396 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006397 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006398 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399 (exp->action == ACT_ALLOW ||
6400 exp->action == ACT_DENY))
6401 return 0;
6402
6403 cur_idx = txn->hdr_idx.v[old_idx].next;
6404 if (!cur_idx)
6405 break;
6406
6407 cur_hdr = &txn->hdr_idx.v[cur_idx];
6408 cur_ptr = cur_next;
6409 cur_end = cur_ptr + cur_hdr->len;
6410 cur_next = cur_end + cur_hdr->cr + 1;
6411
6412 /* Now we have one header between cur_ptr and cur_end,
6413 * and the next header starts at cur_next.
6414 */
6415
6416 /* The annoying part is that pattern matching needs
6417 * that we modify the contents to null-terminate all
6418 * strings before testing them.
6419 */
6420
6421 term = *cur_end;
6422 *cur_end = '\0';
6423
6424 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6425 switch (exp->action) {
6426 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006427 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006428 last_hdr = 1;
6429 break;
6430
6431 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006432 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006433 last_hdr = 1;
6434 break;
6435
6436 case ACT_REPLACE:
6437 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6438 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6439 /* FIXME: if the user adds a newline in the replacement, the
6440 * index will not be recalculated for now, and the new line
6441 * will not be counted as a new header.
6442 */
6443
6444 cur_end += delta;
6445 cur_next += delta;
6446 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006447 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006448 break;
6449
6450 case ACT_REMOVE:
6451 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6452 cur_next += delta;
6453
Willy Tarreaufa355d42009-11-29 18:12:29 +01006454 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6456 txn->hdr_idx.used--;
6457 cur_hdr->len = 0;
6458 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006459 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 break;
6461
6462 }
6463 }
6464 if (cur_end)
6465 *cur_end = term; /* restore the string terminator */
6466
6467 /* keep the link from this header to next one in case of later
6468 * removal of next header.
6469 */
6470 old_idx = cur_idx;
6471 }
6472 return 0;
6473}
6474
6475
6476/* Apply the filter to the status line in the response buffer <rtr>.
6477 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6478 * or -1 if a replacement resulted in an invalid status line.
6479 */
6480int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6481{
6482 char term;
6483 char *cur_ptr, *cur_end;
6484 int done;
6485 struct http_txn *txn = &t->txn;
6486 int len, delta;
6487
6488
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006491 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492 (exp->action == ACT_ALLOW ||
6493 exp->action == ACT_DENY))
6494 return 0;
6495 else if (exp->action == ACT_REMOVE)
6496 return 0;
6497
6498 done = 0;
6499
Willy Tarreau962c3f42010-01-10 00:15:35 +01006500 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006501 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006502
6503 /* Now we have the status line between cur_ptr and cur_end */
6504
6505 /* The annoying part is that pattern matching needs
6506 * that we modify the contents to null-terminate all
6507 * strings before testing them.
6508 */
6509
6510 term = *cur_end;
6511 *cur_end = '\0';
6512
6513 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6514 switch (exp->action) {
6515 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006516 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 done = 1;
6518 break;
6519
6520 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006521 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522 done = 1;
6523 break;
6524
6525 case ACT_REPLACE:
6526 *cur_end = term; /* restore the string terminator */
6527 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6528 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6529 /* FIXME: if the user adds a newline in the replacement, the
6530 * index will not be recalculated for now, and the new line
6531 * will not be counted as a new header.
6532 */
6533
Willy Tarreaufa355d42009-11-29 18:12:29 +01006534 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535 cur_end += delta;
Willy Tarreaua458b672012-03-05 11:17:50 +01006536 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, rtr->p,
Willy Tarreau02785762007-04-03 14:45:44 +02006537 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 cur_ptr, cur_end + 1,
6539 NULL, NULL);
6540 if (unlikely(!cur_end))
6541 return -1;
6542
6543 /* we have a full respnse and we know that we have either a CR
6544 * or an LF at <ptr>.
6545 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006546 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006547 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548 /* there is no point trying this regex on headers */
6549 return 1;
6550 }
6551 }
6552 *cur_end = term; /* restore the string terminator */
6553 return done;
6554}
6555
6556
6557
6558/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006559 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006560 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6561 * unparsable response.
6562 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006563int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006565 struct http_txn *txn = &s->txn;
6566 struct hdr_exp *exp;
6567
6568 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 int ret;
6570
6571 /*
6572 * The interleaving of transformations and verdicts
6573 * makes it difficult to decide to continue or stop
6574 * the evaluation.
6575 */
6576
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006577 if (txn->flags & TX_SVDENY)
6578 break;
6579
Willy Tarreau3d300592007-03-18 18:34:41 +01006580 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6582 exp->action == ACT_PASS)) {
6583 exp = exp->next;
6584 continue;
6585 }
6586
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006587 /* if this filter had a condition, evaluate it now and skip to
6588 * next filter if the condition does not match.
6589 */
6590 if (exp->cond) {
6591 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6592 ret = acl_pass(ret);
6593 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6594 ret = !ret;
6595 if (!ret)
6596 continue;
6597 }
6598
Willy Tarreaua15645d2007-03-18 16:22:39 +01006599 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006600 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 if (unlikely(ret < 0))
6602 return -1;
6603
6604 if (likely(ret == 0)) {
6605 /* The filter did not match the response, it can be
6606 * iterated through all headers.
6607 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006608 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 }
6611 return 0;
6612}
6613
6614
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006616 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006617 * desirable to call it only when needed. This function is also used when we
6618 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006620void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006621{
6622 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006623 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006624 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006626 char *hdr_beg, *hdr_end, *hdr_next;
6627 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 /* Iterate through the headers.
6630 * we start with the start line.
6631 */
6632 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006633 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634
6635 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6636 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006637 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006638
6639 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006640 hdr_beg = hdr_next;
6641 hdr_end = hdr_beg + cur_hdr->len;
6642 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 /* We have one full header between hdr_beg and hdr_end, and the
6645 * next header starts at hdr_next. We're only interested in
6646 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647 */
6648
Willy Tarreau24581ba2010-08-31 22:39:35 +02006649 is_cookie2 = 0;
6650 prev = hdr_beg + 10;
6651 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006652 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6654 if (!val) {
6655 old_idx = cur_idx;
6656 continue;
6657 }
6658 is_cookie2 = 1;
6659 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 }
6661
Willy Tarreau24581ba2010-08-31 22:39:35 +02006662 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6663 * <prev> points to the colon.
6664 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006665 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666
Willy Tarreau24581ba2010-08-31 22:39:35 +02006667 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6668 * check-cache is enabled) and we are not interested in checking
6669 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006670 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006671 if (t->be->cookie_name == NULL &&
6672 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006673 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 return;
6675
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676 /* OK so now we know we have to process this response cookie.
6677 * The format of the Set-Cookie header is slightly different
6678 * from the format of the Cookie header in that it does not
6679 * support the comma as a cookie delimiter (thus the header
6680 * cannot be folded) because the Expires attribute described in
6681 * the original Netscape's spec may contain an unquoted date
6682 * with a comma inside. We have to live with this because
6683 * many browsers don't support Max-Age and some browsers don't
6684 * support quoted strings. However the Set-Cookie2 header is
6685 * clean.
6686 *
6687 * We have to keep multiple pointers in order to support cookie
6688 * removal at the beginning, middle or end of header without
6689 * corrupting the header (in case of set-cookie2). A special
6690 * pointer, <scav> points to the beginning of the set-cookie-av
6691 * fields after the first semi-colon. The <next> pointer points
6692 * either to the end of line (set-cookie) or next unquoted comma
6693 * (set-cookie2). All of these headers are valid :
6694 *
6695 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6696 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6697 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6698 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6699 * | | | | | | | | | |
6700 * | | | | | | | | +-> next hdr_end <--+
6701 * | | | | | | | +------------> scav
6702 * | | | | | | +--------------> val_end
6703 * | | | | | +--------------------> val_beg
6704 * | | | | +----------------------> equal
6705 * | | | +------------------------> att_end
6706 * | | +----------------------------> att_beg
6707 * | +------------------------------> prev
6708 * +-----------------------------------------> hdr_beg
6709 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710
Willy Tarreau24581ba2010-08-31 22:39:35 +02006711 for (; prev < hdr_end; prev = next) {
6712 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau24581ba2010-08-31 22:39:35 +02006714 /* find att_beg */
6715 att_beg = prev + 1;
6716 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6717 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718
Willy Tarreau24581ba2010-08-31 22:39:35 +02006719 /* find att_end : this is the first character after the last non
6720 * space before the equal. It may be equal to hdr_end.
6721 */
6722 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723
Willy Tarreau24581ba2010-08-31 22:39:35 +02006724 while (equal < hdr_end) {
6725 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6726 break;
6727 if (http_is_spht[(unsigned char)*equal++])
6728 continue;
6729 att_end = equal;
6730 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731
Willy Tarreau24581ba2010-08-31 22:39:35 +02006732 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6733 * is between <att_beg> and <equal>, both may be identical.
6734 */
6735
6736 /* look for end of cookie if there is an equal sign */
6737 if (equal < hdr_end && *equal == '=') {
6738 /* look for the beginning of the value */
6739 val_beg = equal + 1;
6740 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6741 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742
Willy Tarreau24581ba2010-08-31 22:39:35 +02006743 /* find the end of the value, respecting quotes */
6744 next = find_cookie_value_end(val_beg, hdr_end);
6745
6746 /* make val_end point to the first white space or delimitor after the value */
6747 val_end = next;
6748 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6749 val_end--;
6750 } else {
6751 /* <equal> points to next comma, semi-colon or EOL */
6752 val_beg = val_end = next = equal;
6753 }
6754
6755 if (next < hdr_end) {
6756 /* Set-Cookie2 supports multiple cookies, and <next> points to
6757 * a colon or semi-colon before the end. So skip all attr-value
6758 * pairs and look for the next comma. For Set-Cookie, since
6759 * commas are permitted in values, skip to the end.
6760 */
6761 if (is_cookie2)
6762 next = find_hdr_value_end(next, hdr_end);
6763 else
6764 next = hdr_end;
6765 }
6766
6767 /* Now everything is as on the diagram above */
6768
6769 /* Ignore cookies with no equal sign */
6770 if (equal == val_end)
6771 continue;
6772
6773 /* If there are spaces around the equal sign, we need to
6774 * strip them otherwise we'll get trouble for cookie captures,
6775 * or even for rewrites. Since this happens extremely rarely,
6776 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006778 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6779 int stripped_before = 0;
6780 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781
Willy Tarreau24581ba2010-08-31 22:39:35 +02006782 if (att_end != equal) {
6783 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6784 equal += stripped_before;
6785 val_beg += stripped_before;
6786 }
6787
6788 if (val_beg > equal + 1) {
6789 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6790 val_beg += stripped_after;
6791 stripped_before += stripped_after;
6792 }
6793
6794 val_end += stripped_before;
6795 next += stripped_before;
6796 hdr_end += stripped_before;
6797 hdr_next += stripped_before;
6798 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006799 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006800 }
6801
6802 /* First, let's see if we want to capture this cookie. We check
6803 * that we don't already have a server side cookie, because we
6804 * can only capture one. Also as an optimisation, we ignore
6805 * cookies shorter than the declared name.
6806 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006807 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006808 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006809 (val_end - att_beg >= t->fe->capture_namelen) &&
6810 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6811 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006812 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813 Alert("HTTP logging : out of memory.\n");
6814 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006815 else {
6816 if (log_len > t->fe->capture_len)
6817 log_len = t->fe->capture_len;
6818 memcpy(txn->srv_cookie, att_beg, log_len);
6819 txn->srv_cookie[log_len] = 0;
6820 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821 }
6822
Willy Tarreau827aee92011-03-10 16:55:02 +01006823 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 if (!(t->flags & SN_IGNORE_PRST) &&
6826 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6827 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006828 /* assume passive cookie by default */
6829 txn->flags &= ~TX_SCK_MASK;
6830 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831
6832 /* If the cookie is in insert mode on a known server, we'll delete
6833 * this occurrence because we'll insert another one later.
6834 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 * a direct access.
6836 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006837 if (t->be->options2 & PR_O2_COOK_PSV) {
6838 /* The "preserve" flag was set, we don't want to touch the
6839 * server's cookie.
6840 */
6841 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006842 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006843 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006844 /* this cookie must be deleted */
6845 if (*prev == ':' && next == hdr_end) {
6846 /* whole header */
6847 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6848 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6849 txn->hdr_idx.used--;
6850 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006851 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 hdr_next += delta;
6853 http_msg_move_end(&txn->rsp, delta);
6854 /* note: while both invalid now, <next> and <hdr_end>
6855 * are still equal, so the for() will stop as expected.
6856 */
6857 } else {
6858 /* just remove the value */
6859 int delta = del_hdr_value(res, &prev, next);
6860 next = prev;
6861 hdr_end += delta;
6862 hdr_next += delta;
6863 cur_hdr->len += delta;
6864 http_msg_move_end(&txn->rsp, delta);
6865 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006866 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006867 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006868 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006870 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872 * with this server since we know it.
6873 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006874 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 next += delta;
6876 hdr_end += delta;
6877 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006879 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Willy Tarreauf1348312010-10-07 15:54:11 +02006881 txn->flags &= ~TX_SCK_MASK;
6882 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006884 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006888 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 next += delta;
6890 hdr_end += delta;
6891 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006893 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Willy Tarreau827aee92011-03-10 16:55:02 +01006895 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006896 txn->flags &= ~TX_SCK_MASK;
6897 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 }
6899 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006900 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6901 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006902 int cmp_len, value_len;
6903 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Cyril Bontéb21570a2009-11-29 20:04:48 +01006905 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6907 value_begin = att_beg + t->be->appsession_name_len;
6908 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006909 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 cmp_len = att_end - att_beg;
6911 value_begin = val_beg;
6912 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006913 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006914
Cyril Bonté17530c32010-04-06 21:11:10 +02006915 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6917 /* free a possibly previously allocated memory */
6918 pool_free2(apools.sessid, txn->sessid);
6919
Cyril Bontéb21570a2009-11-29 20:04:48 +01006920 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006921 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006922 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6923 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6924 return;
6925 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006926 memcpy(txn->sessid, value_begin, value_len);
6927 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 }
6930 /* that's done for this cookie, check the next one on the same
6931 * line when next != hdr_end (only if is_cookie2).
6932 */
6933 }
6934 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006935 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006936 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006938 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 appsess *asession = NULL;
6940 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006941 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006942 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006943 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006944 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6945 Alert("Not enough Memory process_srv():asession:calloc().\n");
6946 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6947 return;
6948 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006949 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6950
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6952 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006954 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006955 return;
6956 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006957 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006958 asession->sessid[t->be->appsession_len] = 0;
6959
Willy Tarreau827aee92011-03-10 16:55:02 +01006960 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006961 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006962 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006963 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006964 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965 return;
6966 }
6967 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006968 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969
6970 asession->request_count = 0;
6971 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6972 }
6973
6974 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6975 asession->request_count++;
6976 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977}
6978
6979
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980/*
6981 * Check if response is cacheable or not. Updates t->flags.
6982 */
6983void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6984{
6985 struct http_txn *txn = &t->txn;
6986 char *p1, *p2;
6987
6988 char *cur_ptr, *cur_end, *cur_next;
6989 int cur_idx;
6990
Willy Tarreau5df51872007-11-25 16:20:08 +01006991 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 return;
6993
6994 /* Iterate through the headers.
6995 * we start with the start line.
6996 */
6997 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006998 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999
7000 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7001 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007002 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003
7004 cur_hdr = &txn->hdr_idx.v[cur_idx];
7005 cur_ptr = cur_next;
7006 cur_end = cur_ptr + cur_hdr->len;
7007 cur_next = cur_end + cur_hdr->cr + 1;
7008
7009 /* We have one full header between cur_ptr and cur_end, and the
7010 * next header starts at cur_next. We're only interested in
7011 * "Cookie:" headers.
7012 */
7013
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007014 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7015 if (val) {
7016 if ((cur_end - (cur_ptr + val) >= 8) &&
7017 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7018 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7019 return;
7020 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021 }
7022
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007023 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7024 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 continue;
7026
7027 /* OK, right now we know we have a cache-control header at cur_ptr */
7028
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007029 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030
7031 if (p1 >= cur_end) /* no more info */
7032 continue;
7033
7034 /* p1 is at the beginning of the value */
7035 p2 = p1;
7036
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007037 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007038 p2++;
7039
7040 /* we have a complete value between p1 and p2 */
7041 if (p2 < cur_end && *p2 == '=') {
7042 /* we have something of the form no-cache="set-cookie" */
7043 if ((cur_end - p1 >= 21) &&
7044 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7045 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007046 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 continue;
7048 }
7049
7050 /* OK, so we know that either p2 points to the end of string or to a comma */
7051 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7052 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7053 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7054 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007055 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 return;
7057 }
7058
7059 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007060 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061 continue;
7062 }
7063 }
7064}
7065
7066
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067/*
7068 * Try to retrieve a known appsession in the URI, then the associated server.
7069 * If the server is found, it's assigned to the session.
7070 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007073 char *end_params, *first_param, *cur_param, *next_param;
7074 char separator;
7075 int value_len;
7076
7077 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007079 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007080 (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 +01007081 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083
Cyril Bontéb21570a2009-11-29 20:04:48 +01007084 first_param = NULL;
7085 switch (mode) {
7086 case PR_O2_AS_M_PP:
7087 first_param = memchr(begin, ';', len);
7088 break;
7089 case PR_O2_AS_M_QS:
7090 first_param = memchr(begin, '?', len);
7091 break;
7092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Cyril Bontéb21570a2009-11-29 20:04:48 +01007094 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097
Cyril Bontéb21570a2009-11-29 20:04:48 +01007098 switch (mode) {
7099 case PR_O2_AS_M_PP:
7100 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7101 end_params = (char *) begin + len;
7102 }
7103 separator = ';';
7104 break;
7105 case PR_O2_AS_M_QS:
7106 end_params = (char *) begin + len;
7107 separator = '&';
7108 break;
7109 default:
7110 /* unknown mode, shouldn't happen */
7111 return;
7112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113
Cyril Bontéb21570a2009-11-29 20:04:48 +01007114 cur_param = next_param = end_params;
7115 while (cur_param > first_param) {
7116 cur_param--;
7117 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7118 /* let's see if this is the appsession parameter */
7119 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7120 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7121 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7122 /* Cool... it's the right one */
7123 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7124 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7125 if (value_len > 0) {
7126 manage_client_side_appsession(t, cur_param, value_len);
7127 }
7128 break;
7129 }
7130 next_param = cur_param;
7131 }
7132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007133#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007134 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007135 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137}
7138
Willy Tarreaub2513902006-12-17 14:52:38 +01007139/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007140 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007141 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007142 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007143 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007144 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007145 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007146 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007147 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007148int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007149{
7150 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007152
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007153 if (!uri_auth)
7154 return 0;
7155
Cyril Bonté70be45d2010-10-12 00:14:35 +02007156 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007157 return 0;
7158
Willy Tarreau295a8372011-03-10 11:25:07 +01007159 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007160 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007161
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007162 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007163 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007164 return 0;
7165
Willy Tarreau962c3f42010-01-10 00:15:35 +01007166 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007167
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007168 /* the URI is in h */
7169 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007170 return 0;
7171
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007172 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007173 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007174 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007175 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007176 break;
7177 }
7178 h++;
7179 }
7180
7181 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007182 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7183 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007184 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007185 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007186 break;
7187 }
7188 h++;
7189 }
7190 }
7191
Willy Tarreau962c3f42010-01-10 00:15:35 +01007192 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7193 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007194 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007195 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007196 break;
7197 }
7198 h++;
7199 }
7200
Cyril Bonté70be45d2010-10-12 00:14:35 +02007201 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7202 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7203 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007204 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007205 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007206 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7207 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7208 si->applet.ctx.stats.st_code = i;
7209 break;
7210 }
7211 }
7212 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007213 break;
7214 }
7215 h++;
7216 }
7217
Willy Tarreau295a8372011-03-10 11:25:07 +01007218 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007219
Willy Tarreaub2513902006-12-17 14:52:38 +01007220 return 1;
7221}
7222
Willy Tarreau4076a152009-04-02 15:18:36 +02007223/*
7224 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007225 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreauea1175a2012-03-05 15:52:30 +01007226 * assume that msg->sol = buf->p + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007227 */
7228void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7229 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007230 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007231{
Willy Tarreauea1175a2012-03-05 15:52:30 +01007232 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7233 int len1 = buf->size - msg->som - (buf->p - buf->data);
7234 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7235 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007236 if (es->len > len1 && len1 < sizeof(es->buf))
7237 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7238 }
7239 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007240 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7241 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007242 }
7243
Willy Tarreau4076a152009-04-02 15:18:36 +02007244 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007245 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7246 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7247 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007248 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007249 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007250
Willy Tarreau4076a152009-04-02 15:18:36 +02007251 es->when = date; // user-visible date
7252 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007253 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007254 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007255 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007256 es->state = state;
7257 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007258 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007259}
Willy Tarreaub2513902006-12-17 14:52:38 +01007260
Willy Tarreau294c4732011-12-16 21:35:50 +01007261/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7262 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7263 * performed over the whole headers. Otherwise it must contain a valid header
7264 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7265 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7266 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7267 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7268 * -1.
7269 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007270 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007271unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7272 struct hdr_idx *idx, int occ,
7273 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007274{
Willy Tarreau294c4732011-12-16 21:35:50 +01007275 struct hdr_ctx local_ctx;
7276 char *ptr_hist[MAX_HDR_HISTORY];
7277 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007279 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007280
Willy Tarreau294c4732011-12-16 21:35:50 +01007281 if (!ctx) {
7282 local_ctx.idx = 0;
7283 ctx = &local_ctx;
7284 }
7285
Willy Tarreaubce70882009-09-07 11:51:47 +02007286 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007287 /* search from the beginning */
7288 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007289 occ--;
7290 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007291 *vptr = ctx->line + ctx->val;
7292 *vlen = ctx->vlen;
7293 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007294 }
7295 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007296 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007297 }
7298
7299 /* negative occurrence, we scan all the list then walk back */
7300 if (-occ > MAX_HDR_HISTORY)
7301 return 0;
7302
Willy Tarreau294c4732011-12-16 21:35:50 +01007303 found = hist_ptr = 0;
7304 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7305 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7306 len_hist[hist_ptr] = ctx->vlen;
7307 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007308 hist_ptr = 0;
7309 found++;
7310 }
7311 if (-occ > found)
7312 return 0;
7313 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7314 * find occurrence -occ, so we have to check [hist_ptr+occ].
7315 */
7316 hist_ptr += occ;
7317 if (hist_ptr >= MAX_HDR_HISTORY)
7318 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007319 *vptr = ptr_hist[hist_ptr];
7320 *vlen = len_hist[hist_ptr];
7321 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007322}
7323
Willy Tarreaubaaee002006-06-26 02:48:02 +02007324/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325 * Print a debug line with a header
7326 */
7327void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7328{
7329 int len, max;
7330 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007331 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332 max = end - start;
7333 UBOUND(max, sizeof(trash) - len - 1);
7334 len += strlcpy2(trash + len, start, max + 1);
7335 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007336 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337}
7338
Willy Tarreau0937bc42009-12-22 15:03:09 +01007339/*
7340 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7341 * the required fields are properly allocated and that we only need to (re)init
7342 * them. This should be used before processing any new request.
7343 */
7344void http_init_txn(struct session *s)
7345{
7346 struct http_txn *txn = &s->txn;
7347 struct proxy *fe = s->fe;
7348
7349 txn->flags = 0;
7350 txn->status = -1;
7351
William Lallemand5f232402012-04-05 18:02:55 +02007352 global.req_count++;
7353
Willy Tarreauf64d1412010-10-07 20:06:11 +02007354 txn->cookie_first_date = 0;
7355 txn->cookie_last_date = 0;
7356
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007357 txn->req.flags = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007358 txn->req.sol = txn->req.eol = NULL;
7359 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007360 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007361 txn->rsp.flags = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007362 txn->rsp.sol = txn->rsp.eol = NULL;
7363 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007364 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007365 txn->req.chunk_len = 0LL;
7366 txn->req.body_len = 0LL;
7367 txn->rsp.chunk_len = 0LL;
7368 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007369 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7370 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007371
7372 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007373
7374 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7375 if (fe->options2 & PR_O2_REQBUG_OK)
7376 txn->req.err_pos = -1; /* let buggy requests pass */
7377
Willy Tarreau46023632010-01-07 22:51:47 +01007378 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007379 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7380
Willy Tarreau46023632010-01-07 22:51:47 +01007381 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007382 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7383
7384 if (txn->hdr_idx.v)
7385 hdr_idx_init(&txn->hdr_idx);
7386}
7387
7388/* to be used at the end of a transaction */
7389void http_end_txn(struct session *s)
7390{
7391 struct http_txn *txn = &s->txn;
7392
7393 /* these ones will have been dynamically allocated */
7394 pool_free2(pool2_requri, txn->uri);
7395 pool_free2(pool2_capture, txn->cli_cookie);
7396 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007397 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007398 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007399
William Lallemanda73203e2012-03-12 12:48:57 +01007400 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007401 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007402 txn->uri = NULL;
7403 txn->srv_cookie = NULL;
7404 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007405
7406 if (txn->req.cap) {
7407 struct cap_hdr *h;
7408 for (h = s->fe->req_cap; h; h = h->next)
7409 pool_free2(h->pool, txn->req.cap[h->index]);
7410 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7411 }
7412
7413 if (txn->rsp.cap) {
7414 struct cap_hdr *h;
7415 for (h = s->fe->rsp_cap; h; h = h->next)
7416 pool_free2(h->pool, txn->rsp.cap[h->index]);
7417 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7418 }
7419
Willy Tarreau0937bc42009-12-22 15:03:09 +01007420}
7421
7422/* to be used at the end of a transaction to prepare a new one */
7423void http_reset_txn(struct session *s)
7424{
7425 http_end_txn(s);
7426 http_init_txn(s);
7427
7428 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007429 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007430 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007431 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007432 /* re-init store persistence */
7433 s->store_count = 0;
7434
Willy Tarreau0937bc42009-12-22 15:03:09 +01007435 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436
7437 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7438
Willy Tarreau739cfba2010-01-25 23:11:14 +01007439 /* We must trim any excess data from the response buffer, because we
7440 * may have blocked an invalid response from a server that we don't
7441 * want to accidentely forward once we disable the analysers, nor do
7442 * we want those data to come along with next response. A typical
7443 * example of such data would be from a buggy server responding to
7444 * a HEAD with some data, or sending more than the advertised
7445 * content-length.
7446 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007447 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007448 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007449
Willy Tarreau0937bc42009-12-22 15:03:09 +01007450 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007451 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007452
Willy Tarreaud04e8582010-05-31 12:31:35 +02007453 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007454 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007455
7456 s->req->rex = TICK_ETERNITY;
7457 s->req->wex = TICK_ETERNITY;
7458 s->req->analyse_exp = TICK_ETERNITY;
7459 s->rep->rex = TICK_ETERNITY;
7460 s->rep->wex = TICK_ETERNITY;
7461 s->rep->analyse_exp = TICK_ETERNITY;
7462}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463
Willy Tarreauff011f22011-01-06 17:51:27 +01007464void free_http_req_rules(struct list *r) {
7465 struct http_req_rule *tr, *pr;
7466
7467 list_for_each_entry_safe(pr, tr, r, list) {
7468 LIST_DEL(&pr->list);
7469 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7470 free(pr->http_auth.realm);
7471
7472 free(pr);
7473 }
7474}
7475
7476struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7477{
7478 struct http_req_rule *rule;
7479 int cur_arg;
7480
7481 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7482 if (!rule) {
7483 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7484 return NULL;
7485 }
7486
7487 if (!*args[0]) {
7488 goto req_error_parsing;
7489 } else if (!strcmp(args[0], "allow")) {
7490 rule->action = HTTP_REQ_ACT_ALLOW;
7491 cur_arg = 1;
7492 } else if (!strcmp(args[0], "deny")) {
7493 rule->action = HTTP_REQ_ACT_DENY;
7494 cur_arg = 1;
7495 } else if (!strcmp(args[0], "auth")) {
7496 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7497 cur_arg = 1;
7498
7499 while(*args[cur_arg]) {
7500 if (!strcmp(args[cur_arg], "realm")) {
7501 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7502 cur_arg+=2;
7503 continue;
7504 } else
7505 break;
7506 }
7507 } else {
7508req_error_parsing:
7509 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7510 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7511 return NULL;
7512 }
7513
7514 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7515 struct acl_cond *cond;
7516
7517 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7518 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7519 file, linenum, args[0]);
7520 return NULL;
7521 }
7522 rule->cond = cond;
7523 }
7524 else if (*args[cur_arg]) {
7525 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7526 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7527 file, linenum, args[0], args[cur_arg]);
7528 return NULL;
7529 }
7530
7531 return rule;
7532}
7533
Willy Tarreau8797c062007-05-07 00:55:35 +02007534/************************************************************************/
7535/* The code below is dedicated to ACL parsing and matching */
7536/************************************************************************/
7537
7538
7539
7540
7541/* 1. Check on METHOD
7542 * We use the pre-parsed method if it is known, and store its number as an
7543 * integer. If it is unknown, we use the pointer and the length.
7544 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007545static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007546{
7547 int len, meth;
7548
Willy Tarreauae8b7962007-06-09 23:10:04 +02007549 len = strlen(*text);
7550 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007551
7552 pattern->val.i = meth;
7553 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007554 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007555 if (!pattern->ptr.str)
7556 return 0;
7557 pattern->len = len;
7558 }
7559 return 1;
7560}
7561
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007562/* This function fetches the method of current HTTP request and stores
7563 * it in the global pattern struct as a chunk. There are two possibilities :
7564 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7565 * in <len> and <ptr> is NULL ;
7566 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7567 * <len> to its length.
7568 * This is intended to be used with acl_match_meth() only.
7569 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007570static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007571acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7572 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007573{
7574 int meth;
7575 struct http_txn *txn = l7;
7576
Willy Tarreaub6866442008-07-14 23:54:42 +02007577 if (!txn)
7578 return 0;
7579
Willy Tarreau655dce92009-11-08 13:10:58 +01007580 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007581 return 0;
7582
Willy Tarreau8797c062007-05-07 00:55:35 +02007583 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007584 temp_pattern.data.str.len = meth;
7585 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007586 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007587 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7588 /* ensure the indexes are not affected */
7589 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007590 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7591 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007592 }
7593 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7594 return 1;
7595}
7596
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007597/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007598static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7599{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007600 int icase;
7601
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007602
7603 if (temp_pattern.data.str.str == NULL) {
7604 /* well-known method */
7605 if (temp_pattern.data.str.len == pattern->val.i)
7606 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007607 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007608 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007609
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007610 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7611 if (pattern->val.i != HTTP_METH_OTHER)
7612 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007613
7614 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007615 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007616 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007617
7618 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007619 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7620 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007621 return ACL_PAT_FAIL;
7622 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007623}
7624
7625/* 2. Check on Request/Status Version
7626 * We simply compare strings here.
7627 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007628static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007629{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007630 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007631 if (!pattern->ptr.str)
7632 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007633 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007634 return 1;
7635}
7636
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007637static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007638acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7639 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007640{
7641 struct http_txn *txn = l7;
7642 char *ptr;
7643 int len;
7644
Willy Tarreaub6866442008-07-14 23:54:42 +02007645 if (!txn)
7646 return 0;
7647
Willy Tarreau655dce92009-11-08 13:10:58 +01007648 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007649 return 0;
7650
Willy Tarreau8797c062007-05-07 00:55:35 +02007651 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007652 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007653
7654 while ((len-- > 0) && (*ptr++ != '/'));
7655 if (len <= 0)
7656 return 0;
7657
Willy Tarreau664092c2011-12-16 19:11:42 +01007658 temp_pattern.data.str.str = ptr;
7659 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007660
7661 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7662 return 1;
7663}
7664
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007665static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007666acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7667 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007668{
7669 struct http_txn *txn = l7;
7670 char *ptr;
7671 int len;
7672
Willy Tarreaub6866442008-07-14 23:54:42 +02007673 if (!txn)
7674 return 0;
7675
Willy Tarreau655dce92009-11-08 13:10:58 +01007676 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007677 return 0;
7678
Willy Tarreau8797c062007-05-07 00:55:35 +02007679 len = txn->rsp.sl.st.v_l;
7680 ptr = txn->rsp.sol;
7681
7682 while ((len-- > 0) && (*ptr++ != '/'));
7683 if (len <= 0)
7684 return 0;
7685
Willy Tarreau664092c2011-12-16 19:11:42 +01007686 temp_pattern.data.str.str = ptr;
7687 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007688
7689 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7690 return 1;
7691}
7692
7693/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007694static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007695acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7696 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007697{
7698 struct http_txn *txn = l7;
7699 char *ptr;
7700 int len;
7701
Willy Tarreaub6866442008-07-14 23:54:42 +02007702 if (!txn)
7703 return 0;
7704
Willy Tarreau655dce92009-11-08 13:10:58 +01007705 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007706 return 0;
7707
Willy Tarreau8797c062007-05-07 00:55:35 +02007708 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007709 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007710
Willy Tarreaua5e37562011-12-16 17:06:15 +01007711 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007712 test->flags = ACL_TEST_F_VOL_1ST;
7713 return 1;
7714}
7715
7716/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007717static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007718acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7719 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007720{
7721 struct http_txn *txn = l7;
7722
Willy Tarreaub6866442008-07-14 23:54:42 +02007723 if (!txn)
7724 return 0;
7725
Willy Tarreau655dce92009-11-08 13:10:58 +01007726 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007727 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007728
Willy Tarreauc11416f2007-06-17 16:58:38 +02007729 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7730 /* ensure the indexes are not affected */
7731 return 0;
7732
Willy Tarreau664092c2011-12-16 19:11:42 +01007733 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7734 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007735
Willy Tarreauf3d25982007-05-08 22:45:09 +02007736 /* we do not need to set READ_ONLY because the data is in a buffer */
7737 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007738 return 1;
7739}
7740
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007741static int
7742acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7743 struct acl_expr *expr, struct acl_test *test)
7744{
7745 struct http_txn *txn = l7;
7746
Willy Tarreaub6866442008-07-14 23:54:42 +02007747 if (!txn)
7748 return 0;
7749
Willy Tarreau655dce92009-11-08 13:10:58 +01007750 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007751 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007752
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007753 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7754 /* ensure the indexes are not affected */
7755 return 0;
7756
7757 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007758 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007759 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7760 return 0;
7761 temp_pattern.type = PATTERN_TYPE_IP;
7762 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007763
7764 /*
7765 * If we are parsing url in frontend space, we prepare backend stage
7766 * to not parse again the same url ! optimization lazyness...
7767 */
7768 if (px->options & PR_O_HTTP_PROXY)
7769 l4->flags |= SN_ADDR_SET;
7770
Willy Tarreauf4362b32011-12-16 17:49:52 +01007771 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007772 return 1;
7773}
7774
7775static int
7776acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7777 struct acl_expr *expr, struct acl_test *test)
7778{
7779 struct http_txn *txn = l7;
7780
Willy Tarreaub6866442008-07-14 23:54:42 +02007781 if (!txn)
7782 return 0;
7783
Willy Tarreau655dce92009-11-08 13:10:58 +01007784 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007785 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007786
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007787 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7788 /* ensure the indexes are not affected */
7789 return 0;
7790
7791 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007792 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreaua5e37562011-12-16 17:06:15 +01007793 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007794
7795 if (px->options & PR_O_HTTP_PROXY)
7796 l4->flags |= SN_ADDR_SET;
7797
7798 test->flags = ACL_TEST_F_READ_ONLY;
7799 return 1;
7800}
7801
Willy Tarreauc11416f2007-06-17 16:58:38 +02007802/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7803 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7804 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007805static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007806acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007807 struct acl_expr *expr, struct acl_test *test)
7808{
7809 struct http_txn *txn = l7;
7810 struct hdr_idx *idx = &txn->hdr_idx;
7811 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007812
Willy Tarreaub6866442008-07-14 23:54:42 +02007813 if (!txn)
7814 return 0;
7815
Willy Tarreau33a7e692007-06-10 19:45:56 +02007816 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7817 /* search for header from the beginning */
7818 ctx->idx = 0;
7819
Willy Tarreau33a7e692007-06-10 19:45:56 +02007820 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7821 test->flags |= ACL_TEST_F_FETCH_MORE;
7822 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007823 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7824 temp_pattern.data.str.len = ctx->vlen;
7825
Willy Tarreau33a7e692007-06-10 19:45:56 +02007826 return 1;
7827 }
7828
7829 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7830 test->flags |= ACL_TEST_F_VOL_HDR;
7831 return 0;
7832}
7833
Willy Tarreau33a7e692007-06-10 19:45:56 +02007834static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007835acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7836 struct acl_expr *expr, struct acl_test *test)
7837{
7838 struct http_txn *txn = l7;
7839
Willy Tarreaub6866442008-07-14 23:54:42 +02007840 if (!txn)
7841 return 0;
7842
Willy Tarreau655dce92009-11-08 13:10:58 +01007843 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007844 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007845
Willy Tarreauc11416f2007-06-17 16:58:38 +02007846 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7847 /* ensure the indexes are not affected */
7848 return 0;
7849
7850 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7851}
7852
7853static int
7854acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7855 struct acl_expr *expr, struct acl_test *test)
7856{
7857 struct http_txn *txn = l7;
7858
Willy Tarreaub6866442008-07-14 23:54:42 +02007859 if (!txn)
7860 return 0;
7861
Willy Tarreau655dce92009-11-08 13:10:58 +01007862 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007863 return 0;
7864
7865 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7866}
7867
7868/* 6. Check on HTTP header count. The number of occurrences is returned.
7869 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7870 */
7871static int
7872acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007873 struct acl_expr *expr, struct acl_test *test)
7874{
7875 struct http_txn *txn = l7;
7876 struct hdr_idx *idx = &txn->hdr_idx;
7877 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007878 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007879
Willy Tarreaub6866442008-07-14 23:54:42 +02007880 if (!txn)
7881 return 0;
7882
Willy Tarreau33a7e692007-06-10 19:45:56 +02007883 ctx.idx = 0;
7884 cnt = 0;
7885 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7886 cnt++;
7887
Willy Tarreaua5e37562011-12-16 17:06:15 +01007888 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007889 test->flags = ACL_TEST_F_VOL_HDR;
7890 return 1;
7891}
7892
Willy Tarreauc11416f2007-06-17 16:58:38 +02007893static int
7894acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7895 struct acl_expr *expr, struct acl_test *test)
7896{
7897 struct http_txn *txn = l7;
7898
Willy Tarreaub6866442008-07-14 23:54:42 +02007899 if (!txn)
7900 return 0;
7901
Willy Tarreau655dce92009-11-08 13:10:58 +01007902 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007903 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007904
Willy Tarreauc11416f2007-06-17 16:58:38 +02007905 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7906 /* ensure the indexes are not affected */
7907 return 0;
7908
7909 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7910}
7911
7912static int
7913acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7914 struct acl_expr *expr, struct acl_test *test)
7915{
7916 struct http_txn *txn = l7;
7917
Willy Tarreaub6866442008-07-14 23:54:42 +02007918 if (!txn)
7919 return 0;
7920
Willy Tarreau655dce92009-11-08 13:10:58 +01007921 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007922 return 0;
7923
7924 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7925}
7926
Willy Tarreau33a7e692007-06-10 19:45:56 +02007927/* 7. Check on HTTP header's integer value. The integer value is returned.
7928 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007929 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007930 */
7931static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007932acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007933 struct acl_expr *expr, struct acl_test *test)
7934{
7935 struct http_txn *txn = l7;
7936 struct hdr_idx *idx = &txn->hdr_idx;
7937 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007938
Willy Tarreaub6866442008-07-14 23:54:42 +02007939 if (!txn)
7940 return 0;
7941
Willy Tarreau33a7e692007-06-10 19:45:56 +02007942 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7943 /* search for header from the beginning */
7944 ctx->idx = 0;
7945
Willy Tarreau33a7e692007-06-10 19:45:56 +02007946 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7947 test->flags |= ACL_TEST_F_FETCH_MORE;
7948 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007949 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007950 return 1;
7951 }
7952
7953 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7954 test->flags |= ACL_TEST_F_VOL_HDR;
7955 return 0;
7956}
7957
Willy Tarreauc11416f2007-06-17 16:58:38 +02007958static int
7959acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7960 struct acl_expr *expr, struct acl_test *test)
7961{
7962 struct http_txn *txn = l7;
7963
Willy Tarreaub6866442008-07-14 23:54:42 +02007964 if (!txn)
7965 return 0;
7966
Willy Tarreau655dce92009-11-08 13:10:58 +01007967 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007968 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007969
Willy Tarreauc11416f2007-06-17 16:58:38 +02007970 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7971 /* ensure the indexes are not affected */
7972 return 0;
7973
7974 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7975}
7976
7977static int
7978acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7979 struct acl_expr *expr, struct acl_test *test)
7980{
7981 struct http_txn *txn = l7;
7982
Willy Tarreaub6866442008-07-14 23:54:42 +02007983 if (!txn)
7984 return 0;
7985
Willy Tarreau655dce92009-11-08 13:10:58 +01007986 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007987 return 0;
7988
7989 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7990}
7991
Willy Tarreau106f9792009-09-19 07:54:16 +02007992/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7993 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7994 */
7995static int
7996acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7997 struct acl_expr *expr, struct acl_test *test)
7998{
7999 struct http_txn *txn = l7;
8000 struct hdr_idx *idx = &txn->hdr_idx;
8001 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8002
8003 if (!txn)
8004 return 0;
8005
8006 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8007 /* search for header from the beginning */
8008 ctx->idx = 0;
8009
Willy Tarreauf4362b32011-12-16 17:49:52 +01008010 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008011 test->flags |= ACL_TEST_F_FETCH_MORE;
8012 test->flags |= ACL_TEST_F_VOL_HDR;
8013 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008014 temp_pattern.type = PATTERN_TYPE_IP;
8015 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8016 return 1;
8017 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008018 }
8019
8020 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8021 test->flags |= ACL_TEST_F_VOL_HDR;
8022 return 0;
8023}
8024
8025static int
8026acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8027 struct acl_expr *expr, struct acl_test *test)
8028{
8029 struct http_txn *txn = l7;
8030
8031 if (!txn)
8032 return 0;
8033
Willy Tarreau655dce92009-11-08 13:10:58 +01008034 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008035 return 0;
8036
8037 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8038 /* ensure the indexes are not affected */
8039 return 0;
8040
8041 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8042}
8043
8044static int
8045acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8046 struct acl_expr *expr, struct acl_test *test)
8047{
8048 struct http_txn *txn = l7;
8049
8050 if (!txn)
8051 return 0;
8052
Willy Tarreau655dce92009-11-08 13:10:58 +01008053 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008054 return 0;
8055
8056 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8057}
8058
Willy Tarreau737b0c12007-06-10 21:28:46 +02008059/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8060 * the first '/' after the possible hostname, and ends before the possible '?'.
8061 */
8062static int
8063acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8064 struct acl_expr *expr, struct acl_test *test)
8065{
8066 struct http_txn *txn = l7;
8067 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008068
Willy Tarreaub6866442008-07-14 23:54:42 +02008069 if (!txn)
8070 return 0;
8071
Willy Tarreau655dce92009-11-08 13:10:58 +01008072 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008073 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008074
Willy Tarreauc11416f2007-06-17 16:58:38 +02008075 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8076 /* ensure the indexes are not affected */
8077 return 0;
8078
Willy Tarreau962c3f42010-01-10 00:15:35 +01008079 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008080 ptr = http_get_path(txn);
8081 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008082 return 0;
8083
8084 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008085 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008086
8087 while (ptr < end && *ptr != '?')
8088 ptr++;
8089
Willy Tarreau664092c2011-12-16 19:11:42 +01008090 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008091
8092 /* we do not need to set READ_ONLY because the data is in a buffer */
8093 test->flags = ACL_TEST_F_VOL_1ST;
8094 return 1;
8095}
8096
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008097static int
8098acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8099 struct acl_expr *expr, struct acl_test *test)
8100{
8101 struct buffer *req = s->req;
8102 struct http_txn *txn = &s->txn;
8103 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008104
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008105 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8106 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8107 */
8108
8109 if (!s || !req)
8110 return 0;
8111
Willy Tarreau655dce92009-11-08 13:10:58 +01008112 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008113 /* Already decoded as OK */
8114 test->flags |= ACL_TEST_F_SET_RES_PASS;
8115 return 1;
8116 }
8117
8118 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008119 if (likely(msg->next < req->i))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008120 http_msg_analyzer(req, msg, &txn->hdr_idx);
8121
Willy Tarreau655dce92009-11-08 13:10:58 +01008122 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008123 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8124 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8125 return 1;
8126 }
8127 /* wait for final state */
8128 test->flags |= ACL_TEST_F_MAY_CHANGE;
8129 return 0;
8130 }
8131
8132 /* OK we got a valid HTTP request. We have some minor preparation to
8133 * perform so that further checks can rely on HTTP tests.
8134 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008135 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008136 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8137 s->flags |= SN_REDIRECTABLE;
8138
8139 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8140 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8141 return 1;
8142 }
8143
8144 test->flags |= ACL_TEST_F_SET_RES_PASS;
8145 return 1;
8146}
8147
Willy Tarreau7f18e522010-10-22 20:04:13 +02008148/* return a valid test if the current request is the first one on the connection */
8149static int
8150acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8151 struct acl_expr *expr, struct acl_test *test)
8152{
8153 if (!s)
8154 return 0;
8155
8156 if (s->txn.flags & TX_NOT_FIRST)
8157 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8158 else
8159 test->flags |= ACL_TEST_F_SET_RES_PASS;
8160
8161 return 1;
8162}
8163
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008164static int
8165acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8166 struct acl_expr *expr, struct acl_test *test)
8167{
8168
8169 if (!s)
8170 return 0;
8171
8172 if (!get_http_auth(s))
8173 return 0;
8174
8175 test->ctx.a[0] = expr->arg.ul;
8176 test->ctx.a[1] = s->txn.auth.user;
8177 test->ctx.a[2] = s->txn.auth.pass;
8178
8179 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8180
8181 return 1;
8182}
Willy Tarreau8797c062007-05-07 00:55:35 +02008183
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008184/* Try to find the next occurrence of a cookie name in a cookie header value.
8185 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8186 * the cookie value is returned into *value and *value_l, and the function
8187 * returns a pointer to the next pointer to search from if the value was found.
8188 * Otherwise if the cookie was not found, NULL is returned and neither value
8189 * nor value_l are touched. The input <hdr> string should first point to the
8190 * header's value, and the <hdr_end> pointer must point to the first character
8191 * not part of the value. <list> must be non-zero if value may represent a list
8192 * of values (cookie headers). This makes it faster to abort parsing when no
8193 * list is expected.
8194 */
8195static char *
8196extract_cookie_value(char *hdr, const char *hdr_end,
8197 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008198 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008199{
8200 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8201 char *next;
8202
8203 /* we search at least a cookie name followed by an equal, and more
8204 * generally something like this :
8205 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8206 */
8207 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8208 /* Iterate through all cookies on this line */
8209
8210 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8211 att_beg++;
8212
8213 /* find att_end : this is the first character after the last non
8214 * space before the equal. It may be equal to hdr_end.
8215 */
8216 equal = att_end = att_beg;
8217
8218 while (equal < hdr_end) {
8219 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8220 break;
8221 if (http_is_spht[(unsigned char)*equal++])
8222 continue;
8223 att_end = equal;
8224 }
8225
8226 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8227 * is between <att_beg> and <equal>, both may be identical.
8228 */
8229
8230 /* look for end of cookie if there is an equal sign */
8231 if (equal < hdr_end && *equal == '=') {
8232 /* look for the beginning of the value */
8233 val_beg = equal + 1;
8234 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8235 val_beg++;
8236
8237 /* find the end of the value, respecting quotes */
8238 next = find_cookie_value_end(val_beg, hdr_end);
8239
8240 /* make val_end point to the first white space or delimitor after the value */
8241 val_end = next;
8242 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8243 val_end--;
8244 } else {
8245 val_beg = val_end = next = equal;
8246 }
8247
8248 /* We have nothing to do with attributes beginning with '$'. However,
8249 * they will automatically be removed if a header before them is removed,
8250 * since they're supposed to be linked together.
8251 */
8252 if (*att_beg == '$')
8253 continue;
8254
8255 /* Ignore cookies with no equal sign */
8256 if (equal == next)
8257 continue;
8258
8259 /* Now we have the cookie name between att_beg and att_end, and
8260 * its value between val_beg and val_end.
8261 */
8262
8263 if (att_end - att_beg == cookie_name_l &&
8264 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8265 /* let's return this value and indicate where to go on from */
8266 *value = val_beg;
8267 *value_l = val_end - val_beg;
8268 return next + 1;
8269 }
8270
8271 /* Set-Cookie headers only have the name in the first attr=value part */
8272 if (!list)
8273 break;
8274 }
8275
8276 return NULL;
8277}
8278
8279/* Iterate over all cookies present in a request. The context is stored in
8280 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8281 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8282 * non-null, then multiple cookies may be parsed on the same line.
8283 * The cookie name is in expr->arg and the name length in expr->arg_len.
8284 */
8285static int
8286acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8287 const char *hdr_name, int hdr_name_len, int multi,
8288 struct acl_expr *expr, struct acl_test *test)
8289{
8290 struct http_txn *txn = l7;
8291 struct hdr_idx *idx = &txn->hdr_idx;
8292 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8293
8294 if (!txn)
8295 return 0;
8296
8297 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8298 /* search for the header from the beginning, we must first initialize
8299 * the search parameters.
8300 */
8301 test->ctx.a[0] = NULL;
8302 ctx->idx = 0;
8303 }
8304
8305 while (1) {
8306 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8307 if (!test->ctx.a[0]) {
8308 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8309 goto out;
8310
8311 if (ctx->vlen < expr->arg_len + 1)
8312 continue;
8313
8314 test->ctx.a[0] = ctx->line + ctx->val;
8315 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8316 }
8317
8318 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8319 expr->arg.str, expr->arg_len, multi,
8320 &temp_pattern.data.str.str,
8321 &temp_pattern.data.str.len);
8322 if (test->ctx.a[0]) {
8323 /* one value was returned into temp_pattern.data.str.{str,len} */
8324 test->flags |= ACL_TEST_F_FETCH_MORE;
8325 test->flags |= ACL_TEST_F_VOL_HDR;
8326 return 1;
8327 }
8328 }
8329
8330 out:
8331 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8332 test->flags |= ACL_TEST_F_VOL_HDR;
8333 return 0;
8334}
8335
8336static int
8337acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8338 struct acl_expr *expr, struct acl_test *test)
8339{
8340 struct http_txn *txn = l7;
8341
8342 if (!txn)
8343 return 0;
8344
8345 if (txn->req.msg_state < HTTP_MSG_BODY)
8346 return 0;
8347
8348 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8349 /* ensure the indexes are not affected */
8350 return 0;
8351
8352 /* The Cookie header allows multiple cookies on the same line */
8353 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8354}
8355
8356static int
8357acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8358 struct acl_expr *expr, struct acl_test *test)
8359{
8360 struct http_txn *txn = l7;
8361
8362 if (!txn)
8363 return 0;
8364
8365 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8366 return 0;
8367
8368 /* The Set-Cookie header allows only one cookie on the same line */
8369 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8370}
8371
8372/* Iterate over all cookies present in a request to count how many occurrences
8373 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8374 * multiple cookies may be parsed on the same line.
8375 */
8376static int
8377acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8378 const char *hdr_name, int hdr_name_len, int multi,
8379 struct acl_expr *expr, struct acl_test *test)
8380{
8381 struct http_txn *txn = l7;
8382 struct hdr_idx *idx = &txn->hdr_idx;
8383 struct hdr_ctx ctx;
8384 int cnt;
8385 char *val_beg, *val_end;
8386
8387 if (!txn)
8388 return 0;
8389
Willy Tarreau46787ed2012-04-11 17:28:40 +02008390 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008391 ctx.idx = 0;
8392 cnt = 0;
8393
8394 while (1) {
8395 /* Note: val_beg == NULL every time we need to fetch a new header */
8396 if (!val_beg) {
8397 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8398 break;
8399
8400 if (ctx.vlen < expr->arg_len + 1)
8401 continue;
8402
8403 val_beg = ctx.line + ctx.val;
8404 val_end = val_beg + ctx.vlen;
8405 }
8406
8407 while ((val_beg = extract_cookie_value(val_beg, val_end,
8408 expr->arg.str, expr->arg_len, multi,
8409 &temp_pattern.data.str.str,
8410 &temp_pattern.data.str.len))) {
8411 cnt++;
8412 }
8413 }
8414
8415 temp_pattern.data.integer = cnt;
8416 test->flags |= ACL_TEST_F_VOL_HDR;
8417 return 1;
8418}
8419
8420static int
8421acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8422 struct acl_expr *expr, struct acl_test *test)
8423{
8424 struct http_txn *txn = l7;
8425
8426 if (!txn)
8427 return 0;
8428
8429 if (txn->req.msg_state < HTTP_MSG_BODY)
8430 return 0;
8431
8432 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8433 /* ensure the indexes are not affected */
8434 return 0;
8435
8436 /* The Cookie header allows multiple cookies on the same line */
8437 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8438}
8439
8440static int
8441acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8442 struct acl_expr *expr, struct acl_test *test)
8443{
8444 struct http_txn *txn = l7;
8445
8446 if (!txn)
8447 return 0;
8448
8449 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8450 return 0;
8451
8452 /* The Set-Cookie header allows only one cookie on the same line */
8453 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8454}
8455
Willy Tarreau8797c062007-05-07 00:55:35 +02008456/************************************************************************/
8457/* All supported keywords must be declared here. */
8458/************************************************************************/
8459
8460/* Note: must not be declared <const> as its list will be overwritten */
8461static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008462 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8463
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008464 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008465 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8466 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008467 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008468
Willy Tarreauc4262962010-05-10 23:42:40 +02008469 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008470 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8471 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8472 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8473 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8474 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8475 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008476 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008477 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008478 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008479
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008480 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008481 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008482 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8483 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8484 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8485 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8486 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8487 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8488 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008489 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008490 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008491 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008492
Willy Tarreauc4262962010-05-10 23:42:40 +02008493 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008494 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8495 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8496 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8497 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8498 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8499 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8500 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008501 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008502 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008503 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008504
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008505 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8506 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8507 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8508 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8509 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8510 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8511 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8512 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8513 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8514
8515 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8516 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8517 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8518 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8519 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8520 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8521 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8522 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8523 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8524
Willy Tarreauc4262962010-05-10 23:42:40 +02008525 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008526 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8527 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8528 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8529 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8530 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8531 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008532 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008533
Willy Tarreau7f18e522010-10-22 20:04:13 +02008534 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8535 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8536 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008537 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008538}};
8539
Willy Tarreau4a568972010-05-12 08:08:50 +02008540/************************************************************************/
8541/* The code below is dedicated to pattern fetching and matching */
8542/************************************************************************/
8543
Willy Tarreaue428fb72011-12-16 21:50:30 +01008544/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008545static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008546pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8547 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008548{
8549 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008550
Willy Tarreaue428fb72011-12-16 21:50:30 +01008551 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8552 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008553}
8554
David Cournapeau16023ee2010-12-23 20:55:41 +09008555/*
8556 * Given a path string and its length, find the position of beginning of the
8557 * query string. Returns NULL if no query string is found in the path.
8558 *
8559 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8560 *
8561 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8562 */
8563static inline char *find_query_string(char *path, size_t path_l)
8564{
8565 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008566
David Cournapeau16023ee2010-12-23 20:55:41 +09008567 p = memchr(path, '?', path_l);
8568 return p ? p + 1 : NULL;
8569}
8570
8571static inline int is_param_delimiter(char c)
8572{
8573 return c == '&' || c == ';';
8574}
8575
8576/*
8577 * Given a url parameter, find the starting position of the first occurence,
8578 * or NULL if the parameter is not found.
8579 *
8580 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8581 * the function will return query_string+8.
8582 */
8583static char*
8584find_url_param_pos(char* query_string, size_t query_string_l,
8585 char* url_param_name, size_t url_param_name_l)
8586{
8587 char *pos, *last;
8588
8589 pos = query_string;
8590 last = query_string + query_string_l - url_param_name_l - 1;
8591
8592 while (pos <= last) {
8593 if (pos[url_param_name_l] == '=') {
8594 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8595 return pos;
8596 pos += url_param_name_l + 1;
8597 }
8598 while (pos <= last && !is_param_delimiter(*pos))
8599 pos++;
8600 pos++;
8601 }
8602 return NULL;
8603}
8604
8605/*
8606 * Given a url parameter name, returns its value and size into *value and
8607 * *value_l respectively, and returns non-zero. If the parameter is not found,
8608 * zero is returned and value/value_l are not touched.
8609 */
8610static int
8611find_url_param_value(char* path, size_t path_l,
8612 char* url_param_name, size_t url_param_name_l,
8613 char** value, size_t* value_l)
8614{
8615 char *query_string, *qs_end;
8616 char *arg_start;
8617 char *value_start, *value_end;
8618
8619 query_string = find_query_string(path, path_l);
8620 if (!query_string)
8621 return 0;
8622
8623 qs_end = path + path_l;
8624 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8625 url_param_name, url_param_name_l);
8626 if (!arg_start)
8627 return 0;
8628
8629 value_start = arg_start + url_param_name_l + 1;
8630 value_end = value_start;
8631
8632 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8633 value_end++;
8634
8635 *value = value_start;
8636 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008637 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008638}
8639
8640static int
8641pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8642 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8643{
8644 struct http_txn *txn = l7;
8645 struct http_msg *msg = &txn->req;
8646 char *url_param_value;
8647 size_t url_param_value_l;
8648
8649 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8650 arg_p->data.str.str, arg_p->data.str.len,
8651 &url_param_value, &url_param_value_l))
8652 return 0;
8653
8654 data->str.str = url_param_value;
8655 data->str.len = url_param_value_l;
8656 return 1;
8657}
8658
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008659/* Try to find in request or response message is in <msg> and whose transaction
8660 * is in <txn> the last occurrence of a cookie name in all cookie header values
8661 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8662 * pointer and size of the last occurrence of the cookie value is returned into
8663 * <value> and <value_l>, and the function returns non-zero if the value was
8664 * found. Otherwise if the cookie was not found, zero is returned and neither
8665 * value nor value_l are touched. The input hdr string should begin at the
8666 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8667 * value may represent a list of values (cookie headers).
8668 */
8669
8670static int
8671find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8672 const char *hdr_name, int hdr_name_len,
8673 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008674 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008675{
8676 struct hdr_ctx ctx;
8677 int found = 0;
8678
8679 ctx.idx = 0;
8680 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008681 char *hdr, *end;
8682
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008683 if (ctx.vlen < cookie_name_l + 1)
8684 continue;
8685
Willy Tarreau4573af92012-04-06 18:20:06 +02008686 hdr = ctx.line + ctx.val;
8687 end = hdr + ctx.vlen;
8688 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8689 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008690 }
8691 return found;
8692}
8693
8694static int
8695pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8696 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8697{
8698 struct http_txn *txn = l7;
8699 struct http_msg *msg = &txn->req;
8700 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008701 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008702 int found = 0;
8703
8704 found = find_cookie_value(msg, txn, "Cookie", 6,
8705 arg_p->data.str.str, arg_p->data.str.len, 1,
8706 &cookie_value, &cookie_value_l);
8707 if (found) {
8708 data->str.str = cookie_value;
8709 data->str.len = cookie_value_l;
8710 }
8711
8712 return found;
8713}
8714
8715
8716static int
8717pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8718 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8719{
8720 struct http_txn *txn = l7;
8721 struct http_msg *msg = &txn->rsp;
8722 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008723 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008724 int found = 0;
8725
8726 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8727 arg_p->data.str.str, arg_p->data.str.len, 1,
8728 &cookie_value, &cookie_value_l);
8729 if (found) {
8730 data->str.str = cookie_value;
8731 data->str.len = cookie_value_l;
8732 }
8733
8734 return found;
8735}
8736
Emeric Brun485479d2010-09-23 18:02:19 +02008737
Willy Tarreau4a568972010-05-12 08:08:50 +02008738/************************************************************************/
8739/* All supported keywords must be declared here. */
8740/************************************************************************/
8741/* Note: must not be declared <const> as its list will be overwritten */
8742static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008743 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008744 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008745 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8746 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008747 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008748}};
8749
Willy Tarreau8797c062007-05-07 00:55:35 +02008750
8751__attribute__((constructor))
8752static void __http_protocol_init(void)
8753{
8754 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008755 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008756}
8757
8758
Willy Tarreau58f10d72006-12-04 02:26:12 +01008759/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008760 * Local variables:
8761 * c-indent-level: 8
8762 * c-basic-offset: 8
8763 * End:
8764 */