blob: 0006f2fe4318e3f24d782980c4b5459d4f508b52 [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,
393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - size,
400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
404
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);
424 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
425 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
444 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
445 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,
954 unsigned int state, const char *ptr, const char *end,
955 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100956{
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200959 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100960 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
962
963 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100964 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
966 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100967 state = HTTP_MSG_ERROR;
968 break;
969
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200971 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100973 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 goto http_msg_rpcode;
975 }
976 if (likely(HTTP_IS_SPHT(*ptr)))
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
978 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100979 state = HTTP_MSG_ERROR;
980 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100981
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200983 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 if (likely(!HTTP_IS_LWS(*ptr)))
985 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
986
987 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100988 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
990 }
991
992 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100993 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 http_msg_rsp_reason:
995 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100996 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 msg->sl.st.r_l = 0;
998 goto http_msg_rpline_eol;
999
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001001 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001003 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 goto http_msg_rpreason;
1005 }
1006 if (likely(HTTP_IS_SPHT(*ptr)))
1007 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1008 /* so it's a CR/LF, so there is no reason phrase */
1009 goto http_msg_rsp_reason;
1010
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001012 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 if (likely(!HTTP_IS_CRLF(*ptr)))
1014 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001015 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 http_msg_rpline_eol:
1017 /* We have seen the end of line. Note that we do not
1018 * necessarily have the \n yet, but at least we know that we
1019 * have EITHER \r OR \n, otherwise the response would not be
1020 * complete. We can then record the response length and return
1021 * to the caller which will be able to register it.
1022 */
1023 msg->sl.st.l = ptr - msg->sol;
1024 return ptr;
1025
1026#ifdef DEBUG_FULL
1027 default:
1028 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1029 exit(1);
1030#endif
1031 }
1032
1033 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001034 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 if (ret_state)
1036 *ret_state = state;
1037 if (ret_ptr)
1038 *ret_ptr = (char *)ptr;
1039 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040}
1041
Willy Tarreau8973c702007-01-21 23:58:29 +01001042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
1063 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001067 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 if (likely(HTTP_IS_TOKEN(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001072 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_CRLF(*ptr))) {
1077 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001078 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001080 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001082 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001084 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 msg->sl.rq.v_l = 0;
1086 goto http_msg_rqline_eol;
1087 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001088 state = HTTP_MSG_ERROR;
1089 break;
1090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001092 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001094 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 goto http_msg_rquri;
1096 }
1097 if (likely(HTTP_IS_SPHT(*ptr)))
1098 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1099 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1100 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001103 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001104 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001108 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001112 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001113 /* non-ASCII chars are forbidden unless option
1114 * accept-invalid-http-request is enabled in the frontend.
1115 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001116 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001117 if (msg->err_pos < -1)
1118 goto invalid_char;
1119 if (msg->err_pos == -1)
1120 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001121 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1122 }
1123
1124 if (likely(HTTP_IS_CRLF(*ptr))) {
1125 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1126 goto http_msg_req09_uri_e;
1127 }
1128
1129 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001130 invalid_char:
1131 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001132 state = HTTP_MSG_ERROR;
1133 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001135 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001136 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001138 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 goto http_msg_rqver;
1140 }
1141 if (likely(HTTP_IS_SPHT(*ptr)))
1142 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1143 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1144 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001147 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001148 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150
1151 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001152 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001153 http_msg_rqline_eol:
1154 /* We have seen the end of line. Note that we do not
1155 * necessarily have the \n yet, but at least we know that we
1156 * have EITHER \r OR \n, otherwise the request would not be
1157 * complete. We can then record the request length and return
1158 * to the caller which will be able to register it.
1159 */
1160 msg->sl.rq.l = ptr - msg->sol;
1161 return ptr;
1162 }
1163
1164 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001165 state = HTTP_MSG_ERROR;
1166 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168#ifdef DEBUG_FULL
1169 default:
1170 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1171 exit(1);
1172#endif
1173 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001176 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 if (ret_state)
1178 *ret_state = state;
1179 if (ret_ptr)
1180 *ret_ptr = (char *)ptr;
1181 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001183
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001184/*
1185 * Returns the data from Authorization header. Function may be called more
1186 * than once so data is stored in txn->auth_data. When no header is found
1187 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1188 * searching again for something we are unable to find anyway.
1189 */
1190
1191char get_http_auth_buff[BUFSIZE];
1192
1193int
1194get_http_auth(struct session *s)
1195{
1196
1197 struct http_txn *txn = &s->txn;
1198 struct chunk auth_method;
1199 struct hdr_ctx ctx;
1200 char *h, *p;
1201 int len;
1202
1203#ifdef DEBUG_AUTH
1204 printf("Auth for session %p: %d\n", s, txn->auth.method);
1205#endif
1206
1207 if (txn->auth.method == HTTP_AUTH_WRONG)
1208 return 0;
1209
1210 if (txn->auth.method)
1211 return 1;
1212
1213 txn->auth.method = HTTP_AUTH_WRONG;
1214
1215 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001216
1217 if (txn->flags & TX_USE_PX_CONN) {
1218 h = "Proxy-Authorization";
1219 len = strlen(h);
1220 } else {
1221 h = "Authorization";
1222 len = strlen(h);
1223 }
1224
1225 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001226 return 0;
1227
1228 h = ctx.line + ctx.val;
1229
1230 p = memchr(h, ' ', ctx.vlen);
1231 if (!p || p == h)
1232 return 0;
1233
1234 chunk_initlen(&auth_method, h, 0, p-h);
1235 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1236
1237 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1238
1239 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1240 get_http_auth_buff, BUFSIZE - 1);
1241
1242 if (len < 0)
1243 return 0;
1244
1245
1246 get_http_auth_buff[len] = '\0';
1247
1248 p = strchr(get_http_auth_buff, ':');
1249
1250 if (!p)
1251 return 0;
1252
1253 txn->auth.user = get_http_auth_buff;
1254 *p = '\0';
1255 txn->auth.pass = p+1;
1256
1257 txn->auth.method = HTTP_AUTH_BASIC;
1258 return 1;
1259 }
1260
1261 return 0;
1262}
1263
Willy Tarreau58f10d72006-12-04 02:26:12 +01001264
Willy Tarreau8973c702007-01-21 23:58:29 +01001265/*
1266 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001267 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * when data are missing and recalled at the exact same location with no
1269 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001270 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001271 * fields. Note that msg->som and msg->sol will be initialized after completing
1272 * the first state, so that none of the msg pointers has to be initialized
1273 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1276{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001277 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001280 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 ptr = buf->lr;
1282 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 if (unlikely(ptr >= end))
1285 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 /*
1289 * First, states that are specific to the response only.
1290 * We check them first so that request and headers are
1291 * closer to each other (accessed more often).
1292 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001296 /* we have a start of message, but we have to check
1297 * first if we need to remove some CRLF. We can only
1298 * do this when send_max=0.
1299 */
1300 char *beg = buf->w + buf->send_max;
1301 if (beg >= buf->data + buf->size)
1302 beg -= buf->size;
1303 if (unlikely(ptr != beg)) {
1304 if (buf->send_max)
1305 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001306 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001310 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 hdr_idx_init(idx);
1312 state = HTTP_MSG_RPVER;
1313 goto http_msg_rpver;
1314 }
1315
1316 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1317 goto http_msg_invalid;
1318
1319 if (unlikely(*ptr == '\n'))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1322 /* stop here */
1323
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001325 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 EXPECT_LF_HERE(ptr, http_msg_invalid);
1327 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1328 /* stop here */
1329
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001331 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPVER_SP:
1333 case HTTP_MSG_RPCODE:
1334 case HTTP_MSG_RPCODE_SP:
1335 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001336 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 if (unlikely(!ptr))
1339 return;
1340
1341 /* we have a full response and we know that we have either a CR
1342 * or an LF at <ptr>.
1343 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001344 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1346
1347 msg->sol = ptr;
1348 if (likely(*ptr == '\r'))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1350 goto http_msg_rpline_end;
1351
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001353 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 /* msg->sol must point to the first of CR or LF. */
1355 EXPECT_LF_HERE(ptr, http_msg_invalid);
1356 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1357 /* stop here */
1358
1359 /*
1360 * Second, states that are specific to the request only
1361 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001363 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001365 /* we have a start of message, but we have to check
1366 * first if we need to remove some CRLF. We can only
1367 * do this when send_max=0.
1368 */
1369 char *beg = buf->w + buf->send_max;
1370 if (beg >= buf->data + buf->size)
1371 beg -= buf->size;
1372 if (likely(ptr != beg)) {
1373 if (buf->send_max)
1374 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 Tarreau15de77e2010-01-02 21:59:16 +01001378 msg->som = ptr - buf->data;
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:
1407 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001408 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (unlikely(!ptr))
1410 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 /* we have a full request and we know that we have either a CR
1413 * or an LF at <ptr>.
1414 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001415 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
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 == ':')) {
1456 msg->col = ptr - buf->data;
1457 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 */
1476 msg->sov = ptr - buf->data;
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 */
1495 for (; buf->data+msg->sov < ptr; msg->sov++)
1496 buf->data[msg->sov] = ' ';
1497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
1500 msg->eol = buf->data + 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++;
1566 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001567 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001569 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001570 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001572
1573 case HTTP_MSG_ERROR:
1574 /* this may only happen if we call http_msg_analyser() twice with an error */
1575 break;
1576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577#ifdef DEBUG_FULL
1578 default:
1579 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1580 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001581#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 }
1583 http_msg_ood:
1584 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001585 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 buf->lr = ptr;
1587 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 http_msg_invalid:
1590 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001591 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001592 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 return;
1594}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001595
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001596/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1597 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1598 * nothing is done and 1 is returned.
1599 */
1600static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1601{
1602 int delta;
1603 char *cur_end;
1604
1605 if (msg->sl.rq.v_l != 0)
1606 return 1;
1607
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 cur_end = msg->sol + msg->sl.rq.l;
1609 delta = 0;
1610
1611 if (msg->sl.rq.u_l == 0) {
1612 /* if no URI was set, add "/" */
1613 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1614 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001615 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001616 }
1617 /* add HTTP version */
1618 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001619 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001620 cur_end += delta;
1621 cur_end = (char *)http_parse_reqline(msg, req->data,
1622 HTTP_MSG_RQMETH,
1623 msg->sol, cur_end + 1,
1624 NULL, NULL);
1625 if (unlikely(!cur_end))
1626 return 0;
1627
1628 /* we have a full HTTP/1.0 request now and we know that
1629 * we have either a CR or an LF at <ptr>.
1630 */
1631 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1632 return 1;
1633}
1634
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001635/* Parse the Connection: header of an HTTP request, looking for both "close"
1636 * and "keep-alive" values. If a buffer is provided and we already know that
1637 * some headers may safely be removed, we remove them now. The <to_del> flags
1638 * are used for that :
1639 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1640 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1641 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1642 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1643 * harmless combinations may be removed. Do not call that after changes have
1644 * been processed. If unused, the buffer can be NULL, and no data will be
1645 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001646 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001647void 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 +01001648{
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001650 const char *hdr_val = "Connection";
1651 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001652
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001653 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001654 return;
1655
Willy Tarreau88d349d2010-01-25 12:15:43 +01001656 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1657 hdr_val = "Proxy-Connection";
1658 hdr_len = 16;
1659 }
1660
Willy Tarreau5b154472009-12-21 20:11:07 +01001661 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001662 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001663 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001664 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1665 txn->flags |= TX_HDR_CONN_KAL;
1666 if ((to_del & 2) && buf)
1667 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1668 else
1669 txn->flags |= TX_CON_KAL_SET;
1670 }
1671 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1672 txn->flags |= TX_HDR_CONN_CLO;
1673 if ((to_del & 1) && buf)
1674 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1675 else
1676 txn->flags |= TX_CON_CLO_SET;
1677 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001678 }
1679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680 txn->flags |= TX_HDR_CONN_PRS;
1681 return;
1682}
Willy Tarreau5b154472009-12-21 20:11:07 +01001683
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001684/* Apply desired changes on the Connection: header. Values may be removed and/or
1685 * added depending on the <wanted> flags, which are exclusively composed of
1686 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1687 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1688 */
1689void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1690{
1691 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692 const char *hdr_val = "Connection";
1693 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694
1695 ctx.idx = 0;
1696
Willy Tarreau88d349d2010-01-25 12:15:43 +01001697
1698 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1699 hdr_val = "Proxy-Connection";
1700 hdr_len = 16;
1701 }
1702
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001703 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001704 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001705 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1706 if (wanted & TX_CON_KAL_SET)
1707 txn->flags |= TX_CON_KAL_SET;
1708 else
1709 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001710 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001711 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1712 if (wanted & TX_CON_CLO_SET)
1713 txn->flags |= TX_CON_CLO_SET;
1714 else
1715 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001716 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001717 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718
1719 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1720 return;
1721
1722 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1723 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001724 hdr_val = "Connection: close";
1725 hdr_len = 17;
1726 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1727 hdr_val = "Proxy-Connection: close";
1728 hdr_len = 23;
1729 }
1730 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001731 }
1732
1733 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1734 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001735 hdr_val = "Connection: keep-alive";
1736 hdr_len = 22;
1737 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1738 hdr_val = "Proxy-Connection: keep-alive";
1739 hdr_len = 28;
1740 }
1741 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001742 }
1743 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001744}
1745
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1747 * first byte of body, and increments msg->sov by the number of bytes parsed,
1748 * so that we know we can forward between ->som and ->sov. Note that due to
1749 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1750 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001751 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001752 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001753 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001754int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001755{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001756 char *ptr = buf->lr;
1757 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001758 unsigned int chunk = 0;
1759
1760 /* The chunk size is in the following form, though we are only
1761 * interested in the size and CRLF :
1762 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1763 */
1764 while (1) {
1765 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001767 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 if (c < 0) /* not a hex digit anymore */
1770 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771 if (++ptr >= end)
1772 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001773 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001774 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001775 chunk = (chunk << 4) + c;
1776 }
1777
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778 /* empty size not allowed */
1779 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001780 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001781
1782 while (http_is_spht[(unsigned char)*ptr]) {
1783 if (++ptr >= end)
1784 ptr = buf->data;
1785 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001786 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001787 }
1788
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 /* Up to there, we know that at least one byte is present at *ptr. Check
1790 * for the end of chunk size.
1791 */
1792 while (1) {
1793 if (likely(HTTP_IS_CRLF(*ptr))) {
1794 /* we now have a CR or an LF at ptr */
1795 if (likely(*ptr == '\r')) {
1796 if (++ptr >= end)
1797 ptr = buf->data;
1798 if (ptr == buf->r)
1799 return 0;
1800 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001801
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001803 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001804 if (++ptr >= end)
1805 ptr = buf->data;
1806 /* done */
1807 break;
1808 }
1809 else if (*ptr == ';') {
1810 /* chunk extension, ends at next CRLF */
1811 if (++ptr >= end)
1812 ptr = buf->data;
1813 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001814 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815
1816 while (!HTTP_IS_CRLF(*ptr)) {
1817 if (++ptr >= end)
1818 ptr = buf->data;
1819 if (ptr == buf->r)
1820 return 0;
1821 }
1822 /* we have a CRLF now, loop above */
1823 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001824 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001826 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001827 }
1828
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 /* OK we found our CRLF and now <ptr> points to the next byte,
1830 * which may or may not be present. We save that into ->lr and
1831 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001832 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833 msg->sov += ptr - buf->lr;
1834 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01001835 msg->chunk_len = chunk;
1836 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001839 error:
1840 msg->err_pos = ptr - buf->data;
1841 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001842}
1843
Willy Tarreaud98cf932009-12-27 22:54:55 +01001844/* This function skips trailers in the buffer <buf> associated with HTTP
1845 * message <msg>. The first visited position is buf->lr. If the end of
1846 * the trailers is found, it is automatically scheduled to be forwarded,
1847 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1848 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001849 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1850 * zero. If a parse error is encountered, the function returns < 0 and does not
1851 * change anything except maybe buf->lr and msg->sov. Note that the message
1852 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1853 * which implies that all non-trailers data have already been scheduled for
1854 * forwarding, and that the difference between msg->som and msg->sov exactly
1855 * matches the length of trailers already parsed and not forwarded. It is also
1856 * important to note that this function is designed to be able to parse wrapped
1857 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 */
1859int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1860{
1861 /* we have buf->lr which points to next line. Look for CRLF. */
1862 while (1) {
1863 char *p1 = NULL, *p2 = NULL;
1864 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866
1867 /* scan current line and stop at LF or CRLF */
1868 while (1) {
1869 if (ptr == buf->r)
1870 return 0;
1871
1872 if (*ptr == '\n') {
1873 if (!p1)
1874 p1 = ptr;
1875 p2 = ptr;
1876 break;
1877 }
1878
1879 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001880 if (p1) {
1881 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 p1 = ptr;
1885 }
1886
1887 ptr++;
1888 if (ptr >= buf->data + buf->size)
1889 ptr = buf->data;
1890 }
1891
1892 /* after LF; point to beginning of next line */
1893 p2++;
1894 if (p2 >= buf->data + buf->size)
1895 p2 = buf->data;
1896
Willy Tarreau638cd022010-01-03 07:42:04 +01001897 bytes = p2 - buf->lr;
1898 if (bytes < 0)
1899 bytes += buf->size;
1900
1901 /* schedule this line for forwarding */
1902 msg->sov += bytes;
1903 if (msg->sov >= buf->size)
1904 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905
Willy Tarreau638cd022010-01-03 07:42:04 +01001906 if (p1 == buf->lr) {
1907 /* LF/CRLF at beginning of line => end of trailers at p2.
1908 * Everything was scheduled for forwarding, there's nothing
1909 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001910 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001911 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 msg->msg_state = HTTP_MSG_DONE;
1913 return 1;
1914 }
1915 /* OK, next line then */
1916 buf->lr = p2;
1917 }
1918}
1919
1920/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1921 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1922 * ->som, buf->lr in order to include this part into the next forwarding phase.
1923 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1924 * not enough data are available, the function does not change anything and
1925 * returns zero. If a parse error is encountered, the function returns < 0 and
1926 * does not change anything. Note: this function is designed to parse wrapped
1927 * CRLF at the end of the buffer.
1928 */
1929int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1930{
1931 char *ptr;
1932 int bytes;
1933
1934 /* NB: we'll check data availabilty at the end. It's not a
1935 * problem because whatever we match first will be checked
1936 * against the correct length.
1937 */
1938 bytes = 1;
1939 ptr = buf->lr;
1940 if (*ptr == '\r') {
1941 bytes++;
1942 ptr++;
1943 if (ptr >= buf->data + buf->size)
1944 ptr = buf->data;
1945 }
1946
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01001947 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 return 0;
1949
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001950 if (*ptr != '\n') {
1951 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954
1955 ptr++;
1956 if (ptr >= buf->data + buf->size)
1957 ptr = buf->data;
1958 buf->lr = ptr;
1959 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1960 msg->sov = ptr - buf->data;
1961 msg->som = msg->sov - bytes;
1962 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1963 return 1;
1964}
Willy Tarreau5b154472009-12-21 20:11:07 +01001965
Willy Tarreau83e3af02009-12-28 17:39:57 +01001966void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1967{
1968 char *end = buf->data + buf->size;
1969 int off = buf->data + buf->size - buf->w;
1970
1971 /* two possible cases :
1972 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001973 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001974 */
1975 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01001976 int block1 = buf->l;
1977 int block2 = 0;
1978 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001979 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01001980 block1 = buf->data + buf->size - buf->w;
1981 block2 = buf->r - buf->data;
1982 }
1983 if (block2)
1984 memcpy(swap_buffer, buf->data, block2);
1985 memmove(buf->data, buf->w, block1);
1986 if (block2)
1987 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001988 }
1989
1990 /* adjust all known pointers */
1991 buf->w = buf->data;
1992 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
1993 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
1994 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1995 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1996
1997 /* adjust relative pointers */
1998 msg->som = 0;
1999 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2000 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2001 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2002
Willy Tarreau83e3af02009-12-28 17:39:57 +01002003 if (msg->err_pos >= 0) {
2004 msg->err_pos += off;
2005 if (msg->err_pos >= buf->size)
2006 msg->err_pos -= buf->size;
2007 }
2008
2009 buf->flags &= ~BF_FULL;
2010 if (buf->l >= buffer_max_len(buf))
2011 buf->flags |= BF_FULL;
2012}
2013
Willy Tarreaud787e662009-07-07 10:14:51 +02002014/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2015 * processing can continue on next analysers, or zero if it either needs more
2016 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2017 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2018 * when it has nothing left to do, and may remove any analyser when it wants to
2019 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002020 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002021int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002022{
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 /*
2024 * We will parse the partial (or complete) lines.
2025 * We will check the request syntax, and also join multi-line
2026 * headers. An index of all the lines will be elaborated while
2027 * parsing.
2028 *
2029 * For the parsing, we use a 28 states FSM.
2030 *
2031 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002032 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002033 * req->data + msg->eoh = end of processed headers / start of current one
2034 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 * req->lr = first non-visited byte
2036 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002037 *
2038 * At end of parsing, we may perform a capture of the error (if any), and
2039 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2040 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2041 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002042 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002043
Willy Tarreau59234e92008-11-30 23:51:27 +01002044 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002045 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002046 struct http_txn *txn = &s->txn;
2047 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002048 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002049
Willy Tarreau6bf17362009-02-24 10:48:35 +01002050 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2051 now_ms, __FUNCTION__,
2052 s,
2053 req,
2054 req->rex, req->wex,
2055 req->flags,
2056 req->l,
2057 req->analysers);
2058
Willy Tarreau52a0c602009-08-16 22:45:38 +02002059 /* we're speaking HTTP here, so let's speak HTTP to the client */
2060 s->srv_error = http_return_srv_error;
2061
Willy Tarreau83e3af02009-12-28 17:39:57 +01002062 /* There's a protected area at the end of the buffer for rewriting
2063 * purposes. We don't want to start to parse the request if the
2064 * protected area is affected, because we may have to move processed
2065 * data later, which is much more complicated.
2066 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002067 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002068 if ((txn->flags & TX_NOT_FIRST) &&
2069 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002070 req->r < req->lr ||
2071 req->r > req->data + req->size - global.tune.maxrewrite)) {
2072 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002073 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2074 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002075 /* some data has still not left the buffer, wake us once that's done */
2076 buffer_dont_connect(req);
2077 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2078 return 0;
2079 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002080 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002081 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002082 }
2083
Willy Tarreau065e8332010-01-08 00:30:20 +01002084 /* Note that we have the same problem with the response ; we
2085 * may want to send a redirect, error or anything which requires
2086 * some spare space. So we'll ensure that we have at least
2087 * maxrewrite bytes available in the response buffer before
2088 * processing that one. This will only affect pipelined
2089 * keep-alive requests.
2090 */
2091 if ((txn->flags & TX_NOT_FIRST) &&
2092 unlikely((s->rep->flags & BF_FULL) ||
2093 s->rep->r < s->rep->lr ||
2094 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2095 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002096 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2097 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002098 /* don't let a connection request be initiated */
2099 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002100 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002101 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002102 return 0;
2103 }
2104 }
2105
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002106 if (likely(req->lr < req->r))
2107 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002108 }
2109
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 /* 1: we might have to print this header in debug mode */
2111 if (unlikely((global.mode & MODE_DEBUG) &&
2112 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002113 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002114 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002115 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002116
Willy Tarreau663308b2010-06-07 14:06:08 +02002117 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002118 eol = sol + msg->sl.rq.l;
2119 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002120
Willy Tarreau59234e92008-11-30 23:51:27 +01002121 sol += hdr_idx_first_pos(&txn->hdr_idx);
2122 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002123
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 while (cur_idx) {
2125 eol = sol + txn->hdr_idx.v[cur_idx].len;
2126 debug_hdr("clihdr", s, sol, eol);
2127 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2128 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002129 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 }
2131
Willy Tarreau58f10d72006-12-04 02:26:12 +01002132
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 /*
2134 * Now we quickly check if we have found a full valid request.
2135 * If not so, we check the FD and buffer states before leaving.
2136 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002137 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002138 * requests are checked first. When waiting for a second request
2139 * on a keep-alive session, if we encounter and error, close, t/o,
2140 * we note the error in the session flags but don't set any state.
2141 * Since the error will be noted there, it will not be counted by
2142 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002143 * Last, we may increase some tracked counters' http request errors on
2144 * the cases that are deliberately the client's fault. For instance,
2145 * a timeout or connection reset is not counted as an error. However
2146 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002148
Willy Tarreau655dce92009-11-08 13:10:58 +01002149 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002150 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002152 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002153 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002154 session_inc_http_req_ctr(s);
2155 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002156 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002158 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002159
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 /* 1: Since we are in header mode, if there's no space
2161 * left for headers, we won't be able to free more
2162 * later, so the session will never terminate. We
2163 * must terminate it now.
2164 */
2165 if (unlikely(req->flags & BF_FULL)) {
2166 /* FIXME: check if URI is set and return Status
2167 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002168 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_req_ctr(s);
2170 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002171 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002172 if (msg->err_pos < 0)
2173 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 goto return_bad_req;
2175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002176
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 /* 2: have we encountered a read error ? */
2178 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002179 if (!(s->flags & SN_ERR_MASK))
2180 s->flags |= SN_ERR_CLICL;
2181
Willy Tarreaufcffa692010-01-10 14:21:19 +01002182 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002183 goto failed_keep_alive;
2184
Willy Tarreau59234e92008-11-30 23:51:27 +01002185 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002186 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002187 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002188 session_inc_http_err_ctr(s);
2189 }
2190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 msg->msg_state = HTTP_MSG_ERROR;
2192 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002193
Willy Tarreauda7ff642010-06-23 11:44:09 +02002194 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002195 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002196 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002197 if (s->listener->counters)
2198 s->listener->counters->failed_req++;
2199
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 if (!(s->flags & SN_FINST_MASK))
2201 s->flags |= SN_FINST_R;
2202 return 0;
2203 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002204
Willy Tarreau59234e92008-11-30 23:51:27 +01002205 /* 3: has the read timeout expired ? */
2206 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002207 if (!(s->flags & SN_ERR_MASK))
2208 s->flags |= SN_ERR_CLITO;
2209
Willy Tarreaufcffa692010-01-10 14:21:19 +01002210 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002211 goto failed_keep_alive;
2212
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002214 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002215 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002216 session_inc_http_err_ctr(s);
2217 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 txn->status = 408;
2219 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2220 msg->msg_state = HTTP_MSG_ERROR;
2221 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002222
Willy Tarreauda7ff642010-06-23 11:44:09 +02002223 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002224 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002225 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002226 if (s->listener->counters)
2227 s->listener->counters->failed_req++;
2228
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 if (!(s->flags & SN_FINST_MASK))
2230 s->flags |= SN_FINST_R;
2231 return 0;
2232 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002233
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 /* 4: have we encountered a close ? */
2235 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002236 if (!(s->flags & SN_ERR_MASK))
2237 s->flags |= SN_ERR_CLICL;
2238
Willy Tarreaufcffa692010-01-10 14:21:19 +01002239 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002240 goto failed_keep_alive;
2241
Willy Tarreau4076a152009-04-02 15:18:36 +02002242 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002243 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 txn->status = 400;
2245 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2246 msg->msg_state = HTTP_MSG_ERROR;
2247 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002248
Willy Tarreauda7ff642010-06-23 11:44:09 +02002249 session_inc_http_err_ctr(s);
2250 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002251 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002252 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002253 if (s->listener->counters)
2254 s->listener->counters->failed_req++;
2255
Willy Tarreau59234e92008-11-30 23:51:27 +01002256 if (!(s->flags & SN_FINST_MASK))
2257 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002258 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002259 }
2260
Willy Tarreau520d95e2009-09-19 21:04:57 +02002261 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002262 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002263 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002264#ifdef TCP_QUICKACK
2265 if (s->listener->options & LI_O_NOQUICKACK) {
2266 /* We need more data, we have to re-enable quick-ack in case we
2267 * previously disabled it, otherwise we might cause the client
2268 * to delay next data.
2269 */
2270 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2271 }
2272#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002273
Willy Tarreaufcffa692010-01-10 14:21:19 +01002274 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2275 /* If the client starts to talk, let's fall back to
2276 * request timeout processing.
2277 */
2278 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002279 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002280 }
2281
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002283 if (!tick_isset(req->analyse_exp)) {
2284 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2285 (txn->flags & TX_WAIT_NEXT_RQ) &&
2286 tick_isset(s->be->timeout.httpka))
2287 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2288 else
2289 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002291
Willy Tarreau59234e92008-11-30 23:51:27 +01002292 /* we're not ready yet */
2293 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002294
2295 failed_keep_alive:
2296 /* Here we process low-level errors for keep-alive requests. In
2297 * short, if the request is not the first one and it experiences
2298 * a timeout, read error or shutdown, we just silently close so
2299 * that the client can try again.
2300 */
2301 txn->status = 0;
2302 msg->msg_state = HTTP_MSG_RQBEFORE;
2303 req->analysers = 0;
2304 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002305 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002306 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002307 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002309
Willy Tarreaud787e662009-07-07 10:14:51 +02002310 /* OK now we have a complete HTTP request with indexed headers. Let's
2311 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002312 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2313 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2314 * points to the CRLF of the request line. req->lr points to the first
2315 * byte after the last LF. msg->col and msg->sov point to the first
2316 * byte of data. msg->eol cannot be trusted because it may have been
2317 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002318 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002319
Willy Tarreauda7ff642010-06-23 11:44:09 +02002320 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002321 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2322
Willy Tarreaub16a5742010-01-10 14:46:16 +01002323 if (txn->flags & TX_WAIT_NEXT_RQ) {
2324 /* kill the pending keep-alive timeout */
2325 txn->flags &= ~TX_WAIT_NEXT_RQ;
2326 req->analyse_exp = TICK_ETERNITY;
2327 }
2328
2329
Willy Tarreaud787e662009-07-07 10:14:51 +02002330 /* Maybe we found in invalid header name while we were configured not
2331 * to block on that, so we have to capture it now.
2332 */
2333 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002334 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002335
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*
2337 * 1: identify the method
2338 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002339 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002340
2341 /* we can make use of server redirect on GET and HEAD */
2342 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2343 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 /*
2346 * 2: check if the URI matches the monitor_uri.
2347 * We have to do this for every request which gets in, because
2348 * the monitor-uri is defined by the frontend.
2349 */
2350 if (unlikely((s->fe->monitor_uri_len != 0) &&
2351 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002352 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 s->fe->monitor_uri,
2354 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002355 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002361 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002364 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2365 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 ret = acl_pass(ret);
2368 if (cond->pol == ACL_COND_UNLESS)
2369 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 if (ret) {
2372 /* we fail this request, let's return 503 service unavail */
2373 txn->status = 503;
2374 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2375 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002376 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002377 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002378
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 /* nothing to fail, let's reply normaly */
2380 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002381 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 goto return_prx_cond;
2383 }
2384
2385 /*
2386 * 3: Maybe we have to copy the original REQURI for the logs ?
2387 * Note: we cannot log anymore if the request has been
2388 * classified as invalid.
2389 */
2390 if (unlikely(s->logs.logwait & LW_REQ)) {
2391 /* we have a complete HTTP request that we must log */
2392 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2393 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002394
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 if (urilen >= REQURI_LEN)
2396 urilen = REQURI_LEN - 1;
2397 memcpy(txn->uri, &req->data[msg->som], urilen);
2398 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002399
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 if (!(s->logs.logwait &= ~LW_REQ))
2401 s->do_log(s);
2402 } else {
2403 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002404 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002405 }
Willy Tarreau06619262006-12-17 08:37:22 +01002406
William Lallemanda73203e2012-03-12 12:48:57 +01002407 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2408 s->unique_id = pool_alloc2(pool2_uniqueid);
2409 }
2410
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002412 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2413 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002414
Willy Tarreau5b154472009-12-21 20:11:07 +01002415 /* ... and check if the request is HTTP/1.1 or above */
2416 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002417 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2418 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2419 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002420 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002421
2422 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002423 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002424
Willy Tarreau88d349d2010-01-25 12:15:43 +01002425 /* if the frontend has "option http-use-proxy-header", we'll check if
2426 * we have what looks like a proxied connection instead of a connection,
2427 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2428 * Note that this is *not* RFC-compliant, however browsers and proxies
2429 * happen to do that despite being non-standard :-(
2430 * We consider that a request not beginning with either '/' or '*' is
2431 * a proxied connection, which covers both "scheme://location" and
2432 * CONNECT ip:port.
2433 */
2434 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2435 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2436 txn->flags |= TX_USE_PX_CONN;
2437
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002438 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002439 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002440
Willy Tarreau59234e92008-11-30 23:51:27 +01002441 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002442 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002443 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002445
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002446 /* 6: determine the transfer-length.
2447 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2448 * the presence of a message-body in a REQUEST and its transfer length
2449 * must be determined that way (in order of precedence) :
2450 * 1. The presence of a message-body in a request is signaled by the
2451 * inclusion of a Content-Length or Transfer-Encoding header field
2452 * in the request's header fields. When a request message contains
2453 * both a message-body of non-zero length and a method that does
2454 * not define any semantics for that request message-body, then an
2455 * origin server SHOULD either ignore the message-body or respond
2456 * with an appropriate error message (e.g., 413). A proxy or
2457 * gateway, when presented the same request, SHOULD either forward
2458 * the request inbound with the message- body or ignore the
2459 * message-body when determining a response.
2460 *
2461 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2462 * and the "chunked" transfer-coding (Section 6.2) is used, the
2463 * transfer-length is defined by the use of this transfer-coding.
2464 * If a Transfer-Encoding header field is present and the "chunked"
2465 * transfer-coding is not present, the transfer-length is defined
2466 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002467 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002468 * 3. If a Content-Length header field is present, its decimal value in
2469 * OCTETs represents both the entity-length and the transfer-length.
2470 * If a message is received with both a Transfer-Encoding header
2471 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002473 * 4. By the server closing the connection. (Closing the connection
2474 * cannot be used to indicate the end of a request body, since that
2475 * would leave no possibility for the server to send back a response.)
2476 *
2477 * Whenever a transfer-coding is applied to a message-body, the set of
2478 * transfer-codings MUST include "chunked", unless the message indicates
2479 * it is terminated by closing the connection. When the "chunked"
2480 * transfer-coding is used, it MUST be the last transfer-coding applied
2481 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002482 */
2483
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002485 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002486 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002487 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002488 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002489 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002490 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2491 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002492 /* bad transfer-encoding (chunked followed by something else) */
2493 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002494 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002495 break;
2496 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497 }
2498
Willy Tarreau32b47f42009-10-18 20:55:02 +02002499 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002500 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2502 signed long long cl;
2503
Willy Tarreauad14f752011-09-02 20:33:27 +02002504 if (!ctx.vlen) {
2505 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002506 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002507 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508
Willy Tarreauad14f752011-09-02 20:33:27 +02002509 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2510 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002512 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002513
Willy Tarreauad14f752011-09-02 20:33:27 +02002514 if (cl < 0) {
2515 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002516 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002517 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002518
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002519 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002520 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002521 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002522 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002523
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002524 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002525 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002526 }
2527
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002528 /* bodyless requests have a known length */
2529 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002530 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002531
Willy Tarreaud787e662009-07-07 10:14:51 +02002532 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002533 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002534 req->analyse_exp = TICK_ETERNITY;
2535 return 1;
2536
2537 return_bad_req:
2538 /* We centralize bad requests processing here */
2539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2540 /* we detected a parsing error. We want to archive this request
2541 * in the dedicated proxy area for later troubleshooting.
2542 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002543 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002544 }
2545
2546 txn->req.msg_state = HTTP_MSG_ERROR;
2547 txn->status = 400;
2548 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002549
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002550 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002551 if (s->listener->counters)
2552 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002553
2554 return_prx_cond:
2555 if (!(s->flags & SN_ERR_MASK))
2556 s->flags |= SN_ERR_PRXCOND;
2557 if (!(s->flags & SN_FINST_MASK))
2558 s->flags |= SN_FINST_R;
2559
2560 req->analysers = 0;
2561 req->analyse_exp = TICK_ETERNITY;
2562 return 0;
2563}
2564
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565/* We reached the stats page through a POST request.
2566 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002567 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002569int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002570{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002571 struct proxy *px = NULL;
2572 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002573
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002574 char key[LINESIZE];
2575 int action = ST_ADM_ACTION_NONE;
2576 int reprocess = 0;
2577
2578 int total_servers = 0;
2579 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580
2581 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002582 char *st_cur_param = NULL;
2583 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584
2585 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002586 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587
2588 cur_param = next_param = end_params;
2589
Cyril Bonté23b39d92011-02-10 22:54:44 +01002590 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002591 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002592 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593 return 1;
2594 }
2595 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002596 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002597 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002598 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599 }
2600
2601 *end_params = '\0';
2602
Willy Tarreau295a8372011-03-10 11:25:07 +01002603 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604
2605 /*
2606 * Parse the parameters in reverse order to only store the last value.
2607 * From the html form, the backend and the action are at the end.
2608 */
2609 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002610 char *value;
2611 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002612
2613 cur_param--;
2614 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002615 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002616 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002617 poffset = (cur_param != first_param ? 1 : 0);
2618 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2619 if ((plen > 0) && (plen <= sizeof(key))) {
2620 strncpy(key, cur_param + poffset, plen);
2621 key[plen - 1] = '\0';
2622 } else {
2623 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2624 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 }
2626
2627 /* Parse the value */
2628 value = key;
2629 while (*value != '\0' && *value != '=') {
2630 value++;
2631 }
2632 if (*value == '=') {
2633 /* Ok, a value is found, we can mark the end of the key */
2634 *value++ = '\0';
2635 }
2636
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002637 if (!url_decode(key) || !url_decode(value))
2638 break;
2639
Cyril Bonté70be45d2010-10-12 00:14:35 +02002640 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002641 if (!px && (strcmp(key, "b") == 0)) {
2642 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2643 /* the backend name is unknown or ambiguous (duplicate names) */
2644 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2645 goto out;
2646 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002647 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002648 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002650 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002651 }
2652 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002653 action = ST_ADM_ACTION_ENABLE;
2654 }
2655 else {
2656 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2657 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002658 }
2659 }
2660 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002661 if (!(px && action)) {
2662 /*
2663 * Indicates that we'll need to reprocess the parameters
2664 * as soon as backend and action are known
2665 */
2666 if (!reprocess) {
2667 st_cur_param = cur_param;
2668 st_next_param = next_param;
2669 }
2670 reprocess = 1;
2671 }
2672 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002674 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002675 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002676 /* Not already in maintenance, we can change the server state */
2677 sv->state |= SRV_MAINTAIN;
2678 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002679 altered_servers++;
2680 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 }
2682 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002683 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002684 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002685 /* Already in maintenance, we can change the server state */
2686 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002687 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002688 altered_servers++;
2689 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002690 }
2691 break;
2692 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002693 } else {
2694 /* the server name is unknown or ambiguous (duplicate names) */
2695 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002696 }
2697 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002698 if (reprocess && px && action) {
2699 /* Now, we know the backend and the action chosen by the user.
2700 * We can safely restart from the first server parameter
2701 * to reprocess them
2702 */
2703 cur_param = st_cur_param;
2704 next_param = st_next_param;
2705 reprocess = 0;
2706 goto reprocess_servers;
2707 }
2708
Cyril Bonté70be45d2010-10-12 00:14:35 +02002709 next_param = cur_param;
2710 }
2711 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002712
2713 if (total_servers == 0) {
2714 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2715 }
2716 else if (altered_servers == 0) {
2717 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2718 }
2719 else if (altered_servers == total_servers) {
2720 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2721 }
2722 else {
2723 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2724 }
2725 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002726 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002727}
2728
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002729/* returns a pointer to the first rule which forbids access (deny or http_auth),
2730 * or NULL if everything's OK.
2731 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002732static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002733http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2734{
Willy Tarreauff011f22011-01-06 17:51:27 +01002735 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002736
Willy Tarreauff011f22011-01-06 17:51:27 +01002737 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738 int ret = 1;
2739
Willy Tarreauff011f22011-01-06 17:51:27 +01002740 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002741 continue;
2742
2743 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002744 if (rule->cond) {
2745 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002746 ret = acl_pass(ret);
2747
Willy Tarreauff011f22011-01-06 17:51:27 +01002748 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002749 ret = !ret;
2750 }
2751
2752 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002753 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002754 return NULL; /* no problem */
2755 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002756 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002757 }
2758 }
2759 return NULL;
2760}
2761
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002762/* This stream analyser runs all HTTP request processing which is common to
2763 * frontends and backends, which means blocking ACLs, filters, connection-close,
2764 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002766 * either needs more data or wants to immediately abort the request (eg: deny,
2767 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002768 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002770{
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 struct http_txn *txn = &s->txn;
2772 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002774 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002776 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002777 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002778
Willy Tarreau655dce92009-11-08 13:10:58 +01002779 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002780 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002781 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002782 return 0;
2783 }
2784
Willy Tarreau3a816292009-07-07 10:55:49 +02002785 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002786 req->analyse_exp = TICK_ETERNITY;
2787
2788 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2789 now_ms, __FUNCTION__,
2790 s,
2791 req,
2792 req->rex, req->wex,
2793 req->flags,
2794 req->l,
2795 req->analysers);
2796
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 /* first check whether we have some ACLs set to block this request */
2798 list_for_each_entry(cond, &px->block_cond, list) {
2799 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002800
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002801 ret = acl_pass(ret);
2802 if (cond->pol == ACL_COND_UNLESS)
2803 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002804
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 if (ret) {
2806 txn->status = 403;
2807 /* let's log the request time */
2808 s->logs.tv_request = now;
2809 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002810 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002814
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002815 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002816 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002817
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002818 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002819 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002820 do_stats = stats_check_uri(s->rep->prod, txn, px);
2821 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002822 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 +01002823 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002824 else
2825 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002826
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002827 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002828 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002829 txn->status = 403;
2830 s->logs.tv_request = now;
2831 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002832 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002833 s->fe->fe_counters.denied_req++;
2834 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2835 s->be->be_counters.denied_req++;
2836 if (s->listener->counters)
2837 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002838 goto return_prx_cond;
2839 }
2840
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 /* try headers filters */
2842 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002843 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002844 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002845
Willy Tarreau59234e92008-11-30 23:51:27 +01002846 /* has the request been denied ? */
2847 if (txn->flags & TX_CLDENY) {
2848 /* no need to go further */
2849 txn->status = 403;
2850 /* let's log the request time */
2851 s->logs.tv_request = now;
2852 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002853 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 goto return_prx_cond;
2855 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002856
2857 /* When a connection is tarpitted, we use the tarpit timeout,
2858 * which may be the same as the connect timeout if unspecified.
2859 * If unset, then set it to zero because we really want it to
2860 * eventually expire. We build the tarpit as an analyser.
2861 */
2862 if (txn->flags & TX_CLTARPIT) {
2863 buffer_erase(s->req);
2864 /* wipe the request out so that we can drop the connection early
2865 * if the client closes first.
2866 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002867 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002868 req->analysers = 0; /* remove switching rules etc... */
2869 req->analysers |= AN_REQ_HTTP_TARPIT;
2870 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2871 if (!req->analyse_exp)
2872 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002873 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002874 return 1;
2875 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002876 }
Willy Tarreau06619262006-12-17 08:37:22 +01002877
Willy Tarreau5b154472009-12-21 20:11:07 +01002878 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2879 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002880 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2881 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002882 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2883 * avoid to redo the same work if FE and BE have the same settings (common).
2884 * The method consists in checking if options changed between the two calls
2885 * (implying that either one is non-null, or one of them is non-null and we
2886 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002887 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002888
Willy Tarreaudc008c52010-02-01 16:20:08 +01002889 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2890 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2891 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2892 (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 +01002893 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002895 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2896 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002897 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002898 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2899 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002900 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002901 tmp = TX_CON_WANT_CLO;
2902
Willy Tarreau5b154472009-12-21 20:11:07 +01002903 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2904 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002905
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2907 /* parse the Connection header and possibly clean it */
2908 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002910 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2911 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002912 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002913 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002914 to_del |= 1; /* remove "close" */
2915 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002916 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002917
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002918 /* check if client or config asks for explicit close in KAL/SCL */
2919 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2920 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2921 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002922 (!(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 +01002923 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002924 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002925 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002926 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2927 }
Willy Tarreau78599912009-10-17 20:12:21 +02002928
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002929 /* we can be blocked here because the request needs to be authenticated,
2930 * either to pass or to access stats.
2931 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002932 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002933 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002934 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002935
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002936 if (!realm)
2937 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2938
Willy Tarreau844a7e72010-01-31 21:46:18 +01002939 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002940 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2941 txn->status = 401;
2942 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002943 /* on 401 we still count one error, because normal browsing
2944 * won't significantly increase the counter but brute force
2945 * attempts will.
2946 */
2947 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002948 goto return_prx_cond;
2949 }
2950
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002951 /* add request headers from the rule sets in the same order */
2952 list_for_each_entry(wl, &px->req_add, list) {
2953 if (wl->cond) {
2954 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2955 ret = acl_pass(ret);
2956 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2957 ret = !ret;
2958 if (!ret)
2959 continue;
2960 }
2961
2962 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2963 goto return_bad_req;
2964 }
2965
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002966 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002967 struct stats_admin_rule *stats_admin_rule;
2968
2969 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002970 * FIXME!!! that one is rather dangerous, we want to
2971 * make it follow standard rules (eg: clear req->analysers).
2972 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002973
Cyril Bonté474be412010-10-12 00:14:36 +02002974 /* now check whether we have some admin rules for this request */
2975 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2976 int ret = 1;
2977
2978 if (stats_admin_rule->cond) {
2979 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2980 ret = acl_pass(ret);
2981 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2982 ret = !ret;
2983 }
2984
2985 if (ret) {
2986 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002987 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002988 break;
2989 }
2990 }
2991
Cyril Bonté70be45d2010-10-12 00:14:35 +02002992 /* Was the status page requested with a POST ? */
2993 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002994 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002995 if (msg->msg_state < HTTP_MSG_100_SENT) {
2996 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2997 * send an HTTP/1.1 100 Continue intermediate response.
2998 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003000 struct hdr_ctx ctx;
3001 ctx.idx = 0;
3002 /* Expect is allowed in 1.1, look for it */
3003 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3004 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3005 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3006 }
3007 }
3008 msg->msg_state = HTTP_MSG_100_SENT;
3009 s->logs.tv_request = now; /* update the request timer to reflect full request */
3010 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003011 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003012 /* we need more data */
3013 req->analysers |= an_bit;
3014 buffer_dont_connect(req);
3015 return 0;
3016 }
Cyril Bonté474be412010-10-12 00:14:36 +02003017 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003018 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003019 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003020 }
3021
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003022 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003023 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003024 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003025 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003026 s->rep->prod->applet.private = s;
3027 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003028 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003029 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3030 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003031
3032 return 0;
3033
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 /* check whether we have some ACLs set to redirect this request */
3037 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003038 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003039
Willy Tarreauf285f542010-01-03 20:03:03 +01003040 if (rule->cond) {
3041 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3042 ret = acl_pass(ret);
3043 if (rule->cond->pol == ACL_COND_UNLESS)
3044 ret = !ret;
3045 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003046
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003048 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 /* build redirect message */
3052 switch(rule->code) {
3053 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 msg_fmt = HTTP_303;
3055 break;
3056 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 msg_fmt = HTTP_301;
3058 break;
3059 case 302:
3060 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 msg_fmt = HTTP_302;
3062 break;
3063 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003064
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003065 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 switch(rule->type) {
3069 case REDIRECT_TYPE_PREFIX: {
3070 const char *path;
3071 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 path = http_get_path(txn);
3074 /* build message using path */
3075 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003076 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3078 int qs = 0;
3079 while (qs < pathlen) {
3080 if (path[qs] == '?') {
3081 pathlen = qs;
3082 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003083 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003085 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003086 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003087 } else {
3088 path = "/";
3089 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003092 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 goto return_bad_req;
3094
3095 /* add prefix. Note that if prefix == "/", we don't want to
3096 * add anything, otherwise it makes it hard for the user to
3097 * configure a self-redirection.
3098 */
3099 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003100 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3101 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003102 }
3103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 /* add path */
3105 memcpy(rdr.str + rdr.len, path, pathlen);
3106 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003107
3108 /* append a slash at the end of the location is needed and missing */
3109 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3110 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3111 if (rdr.len > rdr.size - 5)
3112 goto return_bad_req;
3113 rdr.str[rdr.len] = '/';
3114 rdr.len++;
3115 }
3116
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 break;
3118 }
3119 case REDIRECT_TYPE_LOCATION:
3120 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003121 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 /* add location */
3125 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3126 rdr.len += rule->rdr_len;
3127 break;
3128 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003129
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 if (rule->cookie_len) {
3131 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3132 rdr.len += 14;
3133 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3134 rdr.len += rule->cookie_len;
3135 memcpy(rdr.str + rdr.len, "\r\n", 2);
3136 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003137 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003138
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003139 /* add end of headers and the keep-alive/close status.
3140 * We may choose to set keep-alive if the Location begins
3141 * with a slash, because the client will come back to the
3142 * same server.
3143 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003144 txn->status = rule->code;
3145 /* let's log the request time */
3146 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003147
3148 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003149 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3150 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003151 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3152 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3153 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003154 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003155 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3156 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3157 rdr.len += 30;
3158 } else {
3159 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3160 rdr.len += 24;
3161 }
Willy Tarreau75661452010-01-10 10:35:01 +01003162 }
3163 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3164 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003165 buffer_write(req->prod->ob, rdr.str, rdr.len);
3166 /* "eat" the request */
3167 buffer_ignore(req, msg->sov - msg->som);
3168 msg->som = msg->sov;
3169 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003170 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3171 txn->req.msg_state = HTTP_MSG_CLOSED;
3172 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003173 break;
3174 } else {
3175 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003176 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3177 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3178 rdr.len += 29;
3179 } else {
3180 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3181 rdr.len += 23;
3182 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003183 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003184 goto return_prx_cond;
3185 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003186 }
3187 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003188
Willy Tarreau2be39392010-01-03 17:24:51 +01003189 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3190 * If this happens, then the data will not come immediately, so we must
3191 * send all what we have without waiting. Note that due to the small gain
3192 * in waiting for the body of the request, it's easier to simply put the
3193 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3194 * itself once used.
3195 */
3196 req->flags |= BF_SEND_DONTWAIT;
3197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 /* that's OK for us now, let's move on to next analysers */
3199 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003200
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003201 return_bad_req:
3202 /* We centralize bad requests processing here */
3203 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3204 /* we detected a parsing error. We want to archive this request
3205 * in the dedicated proxy area for later troubleshooting.
3206 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003207 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003208 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 txn->req.msg_state = HTTP_MSG_ERROR;
3211 txn->status = 400;
3212 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003213
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003214 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003215 if (s->listener->counters)
3216 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003217
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 return_prx_cond:
3219 if (!(s->flags & SN_ERR_MASK))
3220 s->flags |= SN_ERR_PRXCOND;
3221 if (!(s->flags & SN_FINST_MASK))
3222 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003223
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 req->analysers = 0;
3225 req->analyse_exp = TICK_ETERNITY;
3226 return 0;
3227}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003228
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229/* This function performs all the processing enabled for the current request.
3230 * It returns 1 if the processing can continue on next analysers, or zero if it
3231 * needs more data, encounters an error, or wants to immediately abort the
3232 * request. It relies on buffers flags, and updates s->req->analysers.
3233 */
3234int http_process_request(struct session *s, struct buffer *req, int an_bit)
3235{
3236 struct http_txn *txn = &s->txn;
3237 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003238
Willy Tarreau655dce92009-11-08 13:10:58 +01003239 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003240 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003241 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003242 return 0;
3243 }
3244
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003245 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3246 now_ms, __FUNCTION__,
3247 s,
3248 req,
3249 req->rex, req->wex,
3250 req->flags,
3251 req->l,
3252 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003253
Willy Tarreau59234e92008-11-30 23:51:27 +01003254 /*
3255 * Right now, we know that we have processed the entire headers
3256 * and that unwanted requests have been filtered out. We can do
3257 * whatever we want with the remaining request. Also, now we
3258 * may have separate values for ->fe, ->be.
3259 */
Willy Tarreau06619262006-12-17 08:37:22 +01003260
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 /*
3262 * If HTTP PROXY is set we simply get remote server address
3263 * parsing incoming request.
3264 */
3265 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003266 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003268
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003270 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 * Note that doing so might move headers in the request, but
3272 * the fields will stay coherent and the URI will not move.
3273 * This should only be performed in the backend.
3274 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003275 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3277 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003278
Willy Tarreau59234e92008-11-30 23:51:27 +01003279 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003280 * 8: the appsession cookie was looked up very early in 1.2,
3281 * so let's do the same now.
3282 */
3283
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003284 /* It needs to look into the URI unless persistence must be ignored */
3285 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003286 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003287 }
3288
William Lallemanda73203e2012-03-12 12:48:57 +01003289 /* add unique-id if "header-unique-id" is specified */
3290
3291 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3292 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3293
3294 if (s->fe->header_unique_id && s->unique_id) {
3295 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3296 if (ret < 0 || ret > global.tune.bufsize)
3297 goto return_bad_req;
3298 if(unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, trash) < 0))
3299 goto return_bad_req;
3300 }
3301
Cyril Bontéb21570a2009-11-29 20:04:48 +01003302 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003303 * 9: add X-Forwarded-For if either the frontend or the backend
3304 * asks for it.
3305 */
3306 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003307 struct hdr_ctx ctx = { .idx = 0 };
3308
3309 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003310 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 +02003311 /* The header is set to be added only if none is present
3312 * and we found it, so don't do anything.
3313 */
3314 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003315 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 /* Add an X-Forwarded-For header unless the source IP is
3317 * in the 'except' network range.
3318 */
3319 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003320 (((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 +01003321 != s->fe->except_net.s_addr) &&
3322 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003323 (((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 +01003324 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003325 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003326 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003327 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003328
3329 /* Note: we rely on the backend to get the header name to be used for
3330 * x-forwarded-for, because the header is really meant for the backends.
3331 * However, if the backend did not specify any option, we have to rely
3332 * on the frontend's header name.
3333 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003334 if (s->be->fwdfor_hdr_len) {
3335 len = s->be->fwdfor_hdr_len;
3336 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003337 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 len = s->fe->fwdfor_hdr_len;
3339 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003340 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003341 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003342
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003343 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003344 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003345 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003346 }
3347 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003348 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 /* FIXME: for the sake of completeness, we should also support
3350 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003351 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 int len;
3353 char pn[INET6_ADDRSTRLEN];
3354 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003355 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003356 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003357
Willy Tarreau59234e92008-11-30 23:51:27 +01003358 /* Note: we rely on the backend to get the header name to be used for
3359 * x-forwarded-for, because the header is really meant for the backends.
3360 * However, if the backend did not specify any option, we have to rely
3361 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003362 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003363 if (s->be->fwdfor_hdr_len) {
3364 len = s->be->fwdfor_hdr_len;
3365 memcpy(trash, s->be->fwdfor_hdr_name, len);
3366 } else {
3367 len = s->fe->fwdfor_hdr_len;
3368 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003369 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003370 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003371
Willy Tarreau59234e92008-11-30 23:51:27 +01003372 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003373 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003374 goto return_bad_req;
3375 }
3376 }
3377
3378 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003379 * 10: add X-Original-To if either the frontend or the backend
3380 * asks for it.
3381 */
3382 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3383
3384 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003385 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003386 /* Add an X-Original-To header unless the destination IP is
3387 * in the 'except' network range.
3388 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003389 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003390
Willy Tarreau6471afb2011-09-23 10:54:59 +02003391 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003392 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003393 (((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 +02003394 != s->fe->except_to.s_addr) &&
3395 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003396 (((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 +02003397 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003398 int len;
3399 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003400 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003401
3402 /* Note: we rely on the backend to get the header name to be used for
3403 * x-original-to, because the header is really meant for the backends.
3404 * However, if the backend did not specify any option, we have to rely
3405 * on the frontend's header name.
3406 */
3407 if (s->be->orgto_hdr_len) {
3408 len = s->be->orgto_hdr_len;
3409 memcpy(trash, s->be->orgto_hdr_name, len);
3410 } else {
3411 len = s->fe->orgto_hdr_len;
3412 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003413 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003414 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3415
3416 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003417 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003418 goto return_bad_req;
3419 }
3420 }
3421 }
3422
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003423 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3424 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003425 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003426 unsigned int want_flags = 0;
3427
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003428 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003429 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3430 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3431 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003432 want_flags |= TX_CON_CLO_SET;
3433 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003434 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3435 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3436 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003437 want_flags |= TX_CON_KAL_SET;
3438 }
3439
3440 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3441 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003443
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003444
Willy Tarreau522d6c02009-12-06 18:49:18 +01003445 /* If we have no server assigned yet and we're balancing on url_param
3446 * with a POST request, we may be interested in checking the body for
3447 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 */
3449 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3450 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003451 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003452 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003453 buffer_dont_connect(req);
3454 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003456
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003457 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003458 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003459#ifdef TCP_QUICKACK
3460 /* We expect some data from the client. Unless we know for sure
3461 * we already have a full request, we have to re-enable quick-ack
3462 * in case we previously disabled it, otherwise we might cause
3463 * the client to delay further data.
3464 */
3465 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003466 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau5e205522011-12-17 16:34:27 +01003467 (msg->body_len > req->l - txn->req.eoh - 2)))
3468 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3469#endif
3470 }
Willy Tarreau03945942009-12-22 16:50:27 +01003471
Willy Tarreau59234e92008-11-30 23:51:27 +01003472 /*************************************************************
3473 * OK, that's finished for the headers. We have done what we *
3474 * could. Let's switch to the DATA state. *
3475 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003476 req->analyse_exp = TICK_ETERNITY;
3477 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003478
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003480 /* OK let's go on with the BODY now */
3481 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003482
Willy Tarreau59234e92008-11-30 23:51:27 +01003483 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003484 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003485 /* we detected a parsing error. We want to archive this request
3486 * in the dedicated proxy area for later troubleshooting.
3487 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003488 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003489 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003490
Willy Tarreau59234e92008-11-30 23:51:27 +01003491 txn->req.msg_state = HTTP_MSG_ERROR;
3492 txn->status = 400;
3493 req->analysers = 0;
3494 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003495
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003496 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003497 if (s->listener->counters)
3498 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 if (!(s->flags & SN_ERR_MASK))
3501 s->flags |= SN_ERR_PRXCOND;
3502 if (!(s->flags & SN_FINST_MASK))
3503 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003504 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003505}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003506
Willy Tarreau60b85b02008-11-30 23:28:40 +01003507/* This function is an analyser which processes the HTTP tarpit. It always
3508 * returns zero, at the beginning because it prevents any other processing
3509 * from occurring, and at the end because it terminates the request.
3510 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003511int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003512{
3513 struct http_txn *txn = &s->txn;
3514
3515 /* This connection is being tarpitted. The CLIENT side has
3516 * already set the connect expiration date to the right
3517 * timeout. We just have to check that the client is still
3518 * there and that the timeout has not expired.
3519 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003520 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003521 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3522 !tick_is_expired(req->analyse_exp, now_ms))
3523 return 0;
3524
3525 /* We will set the queue timer to the time spent, just for
3526 * logging purposes. We fake a 500 server error, so that the
3527 * attacker will not suspect his connection has been tarpitted.
3528 * It will not cause trouble to the logs because we can exclude
3529 * the tarpitted connections by filtering on the 'PT' status flags.
3530 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003531 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3532
3533 txn->status = 500;
3534 if (req->flags != BF_READ_ERROR)
3535 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3536
3537 req->analysers = 0;
3538 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003539
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003540 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003541 if (s->listener->counters)
3542 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003543
Willy Tarreau60b85b02008-11-30 23:28:40 +01003544 if (!(s->flags & SN_ERR_MASK))
3545 s->flags |= SN_ERR_PRXCOND;
3546 if (!(s->flags & SN_FINST_MASK))
3547 s->flags |= SN_FINST_T;
3548 return 0;
3549}
3550
Willy Tarreaud34af782008-11-30 23:36:37 +01003551/* This function is an analyser which processes the HTTP request body. It looks
3552 * for parameters to be used for the load balancing algorithm (url_param). It
3553 * must only be called after the standard HTTP request processing has occurred,
3554 * because it expects the request to be parsed. It returns zero if it needs to
3555 * read more data, or 1 once it has completed its analysis.
3556 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003557int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003558{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003560 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003561 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003562
3563 /* We have to parse the HTTP request body to find any required data.
3564 * "balance url_param check_post" should have been the only way to get
3565 * into this. We were brought here after HTTP header analysis, so all
3566 * related structures are ready.
3567 */
3568
Willy Tarreau522d6c02009-12-06 18:49:18 +01003569 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3570 goto missing_data;
3571
3572 if (msg->msg_state < HTTP_MSG_100_SENT) {
3573 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3574 * send an HTTP/1.1 100 Continue intermediate response.
3575 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003576 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 struct hdr_ctx ctx;
3578 ctx.idx = 0;
3579 /* Expect is allowed in 1.1, look for it */
3580 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3581 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3582 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3583 }
3584 }
3585 msg->msg_state = HTTP_MSG_100_SENT;
3586 }
3587
3588 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003589 /* we have msg->col and msg->sov which both point to the first
3590 * byte of message body. msg->som still points to the beginning
3591 * of the message. We must save the body in req->lr because it
3592 * survives buffer re-alignments.
3593 */
3594 req->lr = req->data + msg->sov;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003595 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003596 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3597 else
3598 msg->msg_state = HTTP_MSG_DATA;
3599 }
3600
3601 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003602 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003603 * set ->sov and ->lr to point to the body and switch to DATA or
3604 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003605 */
3606 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003607
Willy Tarreau115acb92009-12-26 13:56:06 +01003608 if (!ret)
3609 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003610 else if (ret < 0) {
3611 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003613 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003614 }
3615
Willy Tarreaud98cf932009-12-27 22:54:55 +01003616 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617 * We have the first non-header byte in msg->col, which is either the
3618 * beginning of the chunk size or of the data. The first data byte is in
3619 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3620 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003621 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622
Willy Tarreau124d9912011-03-01 20:30:48 +01003623 if (msg->body_len < limit)
3624 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003625
Willy Tarreau7c96f672009-12-27 22:47:25 +01003626 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627 goto http_end;
3628
3629 missing_data:
3630 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003631 if (req->flags & BF_FULL) {
3632 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003633 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003634 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003635
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3637 txn->status = 408;
3638 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003639
3640 if (!(s->flags & SN_ERR_MASK))
3641 s->flags |= SN_ERR_CLITO;
3642 if (!(s->flags & SN_FINST_MASK))
3643 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003644 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003645 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003646
3647 /* we get here if we need to wait for more data */
3648 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003649 /* Not enough data. We'll re-use the http-request
3650 * timeout here. Ideally, we should set the timeout
3651 * relative to the accept() date. We just set the
3652 * request timeout once at the beginning of the
3653 * request.
3654 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003655 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003656 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003657 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003658 return 0;
3659 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003660
3661 http_end:
3662 /* The situation will not evolve, so let's give up on the analysis. */
3663 s->logs.tv_request = now; /* update the request timer to reflect full request */
3664 req->analysers &= ~an_bit;
3665 req->analyse_exp = TICK_ETERNITY;
3666 return 1;
3667
3668 return_bad_req: /* let's centralize all bad requests */
3669 txn->req.msg_state = HTTP_MSG_ERROR;
3670 txn->status = 400;
3671 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3672
Willy Tarreau79ebac62010-06-07 13:47:49 +02003673 if (!(s->flags & SN_ERR_MASK))
3674 s->flags |= SN_ERR_PRXCOND;
3675 if (!(s->flags & SN_FINST_MASK))
3676 s->flags |= SN_FINST_R;
3677
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 return_err_msg:
3679 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003680 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003681 if (s->listener->counters)
3682 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003683 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003684}
3685
Mark Lamourinec2247f02012-01-04 13:02:01 -05003686int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3687
3688 struct hdr_ctx ctx;
3689
Mark Lamourinec2247f02012-01-04 13:02:01 -05003690 char *hdr_name = be->server_id_hdr_name;
3691 int hdr_name_len = be->server_id_hdr_len;
3692
3693 char *hdr_val;
3694
William Lallemandd9e90662012-01-30 17:27:17 +01003695 ctx.idx = 0;
3696
Mark Lamourinec2247f02012-01-04 13:02:01 -05003697 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3698 /* remove any existing values from the header */
3699 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3700 }
3701
3702 /* Add the new header requested with the server value */
3703 hdr_val = trash;
3704 memcpy(hdr_val, hdr_name, hdr_name_len);
3705 hdr_val += hdr_name_len;
3706 *hdr_val++ = ':';
3707 *hdr_val++ = ' ';
3708 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3709 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3710
3711 return 0;
3712}
3713
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714/* Terminate current transaction and prepare a new one. This is very tricky
3715 * right now but it works.
3716 */
3717void http_end_txn_clean_session(struct session *s)
3718{
3719 /* FIXME: We need a more portable way of releasing a backend's and a
3720 * server's connections. We need a safer way to reinitialize buffer
3721 * flags. We also need a more accurate method for computing per-request
3722 * data.
3723 */
3724 http_silent_debug(__LINE__, s);
3725
3726 s->req->cons->flags |= SI_FL_NOLINGER;
3727 s->req->cons->shutr(s->req->cons);
3728 s->req->cons->shutw(s->req->cons);
3729
3730 http_silent_debug(__LINE__, s);
3731
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003732 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003734 if (unlikely(s->srv_conn))
3735 sess_change_server(s, NULL);
3736 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737
3738 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3739 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003740 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741
3742 if (s->txn.status) {
3743 int n;
3744
3745 n = s->txn.status / 100;
3746 if (n < 1 || n > 5)
3747 n = 0;
3748
3749 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003750 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751
Willy Tarreau24657792010-02-26 10:30:28 +01003752 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003753 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003754 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 }
3756
3757 /* don't count other requests' data */
3758 s->logs.bytes_in -= s->req->l - s->req->send_max;
3759 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3760
3761 /* let's do a final log if we need it */
3762 if (s->logs.logwait &&
3763 !(s->flags & SN_MONITOR) &&
3764 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3765 s->do_log(s);
3766 }
3767
3768 s->logs.accept_date = date; /* user-visible date for logging */
3769 s->logs.tv_accept = now; /* corrected date for internal use */
3770 tv_zero(&s->logs.tv_request);
3771 s->logs.t_queue = -1;
3772 s->logs.t_connect = -1;
3773 s->logs.t_data = -1;
3774 s->logs.t_close = 0;
3775 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3776 s->logs.srv_queue_size = 0; /* we will get this number soon */
3777
3778 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3779 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3780
3781 if (s->pend_pos)
3782 pendconn_free(s->pend_pos);
3783
Willy Tarreau827aee92011-03-10 16:55:02 +01003784 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 if (s->flags & SN_CURR_SESS) {
3786 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003787 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003789 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3790 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791 }
3792
Willy Tarreau9e000c62011-03-10 14:03:36 +01003793 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794
3795 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3796 s->req->cons->fd = -1; /* just to help with debugging */
3797 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003798 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 s->req->cons->err_loc = NULL;
3800 s->req->cons->exp = TICK_ETERNITY;
3801 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003802 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3803 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 +02003804 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 +01003805 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3806 s->txn.meth = 0;
3807 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003808 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003809 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 s->req->cons->flags |= SI_FL_INDEP_STR;
3811
Willy Tarreau96e31212011-05-30 18:10:30 +02003812 if (s->fe->options2 & PR_O2_NODELAY) {
3813 s->req->flags |= BF_NEVER_WAIT;
3814 s->rep->flags |= BF_NEVER_WAIT;
3815 }
3816
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 /* if the request buffer is not empty, it means we're
3818 * about to process another request, so send pending
3819 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003820 * Just don't do this if the buffer is close to be full,
3821 * because the request will wait for it to flush a little
3822 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003824 if (s->req->l > s->req->send_max) {
3825 if (s->rep->send_max &&
3826 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003827 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3828 s->rep->flags |= BF_EXPECT_MORE;
3829 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003830
3831 /* we're removing the analysers, we MUST re-enable events detection */
3832 buffer_auto_read(s->req);
3833 buffer_auto_close(s->req);
3834 buffer_auto_read(s->rep);
3835 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003836
3837 /* make ->lr point to the first non-forwarded byte */
3838 s->req->lr = s->req->w + s->req->send_max;
3839 if (s->req->lr >= s->req->data + s->req->size)
3840 s->req->lr -= s->req->size;
3841 s->rep->lr = s->rep->w + s->rep->send_max;
3842 if (s->rep->lr >= s->rep->data + s->rep->size)
3843 s->rep->lr -= s->req->size;
3844
Willy Tarreau342b11c2010-11-24 16:22:09 +01003845 s->req->analysers = s->listener->analysers;
3846 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847 s->rep->analysers = 0;
3848
3849 http_silent_debug(__LINE__, s);
3850}
3851
3852
3853/* This function updates the request state machine according to the response
3854 * state machine and buffer flags. It returns 1 if it changes anything (flag
3855 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3856 * it is only used to find when a request/response couple is complete. Both
3857 * this function and its equivalent should loop until both return zero. It
3858 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3859 */
3860int http_sync_req_state(struct session *s)
3861{
3862 struct buffer *buf = s->req;
3863 struct http_txn *txn = &s->txn;
3864 unsigned int old_flags = buf->flags;
3865 unsigned int old_state = txn->req.msg_state;
3866
3867 http_silent_debug(__LINE__, s);
3868 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3869 return 0;
3870
3871 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003872 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003873 * We can shut the read side unless we want to abort_on_close,
3874 * or we have a POST request. The issue with POST requests is
3875 * that some browsers still send a CRLF after the request, and
3876 * this CRLF must be read so that it does not remain in the kernel
3877 * buffers, otherwise a close could cause an RST on some systems
3878 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003879 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003880 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003881 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882
3883 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3884 goto wait_other_side;
3885
3886 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3887 /* The server has not finished to respond, so we
3888 * don't want to move in order not to upset it.
3889 */
3890 goto wait_other_side;
3891 }
3892
3893 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3894 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003895 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 txn->req.msg_state = HTTP_MSG_TUNNEL;
3897 goto wait_other_side;
3898 }
3899
3900 /* When we get here, it means that both the request and the
3901 * response have finished receiving. Depending on the connection
3902 * mode, we'll have to wait for the last bytes to leave in either
3903 * direction, and sometimes for a close to be effective.
3904 */
3905
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003906 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3907 /* Server-close mode : queue a connection close to the server */
3908 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003909 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003910 }
3911 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3912 /* Option forceclose is set, or either side wants to close,
3913 * let's enforce it now that we're not expecting any new
3914 * data to come. The caller knows the session is complete
3915 * once both states are CLOSED.
3916 */
3917 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 buffer_shutr_now(buf);
3919 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003920 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003921 }
3922 else {
3923 /* The last possible modes are keep-alive and tunnel. Since tunnel
3924 * mode does not set the body analyser, we can't reach this place
3925 * in tunnel mode, so we're left with keep-alive only.
3926 * This mode is currently not implemented, we switch to tunnel mode.
3927 */
3928 buffer_auto_read(buf);
3929 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 }
3931
3932 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3933 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003934 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3935
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 if (!(buf->flags & BF_OUT_EMPTY)) {
3937 txn->req.msg_state = HTTP_MSG_CLOSING;
3938 goto http_msg_closing;
3939 }
3940 else {
3941 txn->req.msg_state = HTTP_MSG_CLOSED;
3942 goto http_msg_closed;
3943 }
3944 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003945 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 }
3947
3948 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3949 http_msg_closing:
3950 /* nothing else to forward, just waiting for the output buffer
3951 * to be empty and for the shutw_now to take effect.
3952 */
3953 if (buf->flags & BF_OUT_EMPTY) {
3954 txn->req.msg_state = HTTP_MSG_CLOSED;
3955 goto http_msg_closed;
3956 }
3957 else if (buf->flags & BF_SHUTW) {
3958 txn->req.msg_state = HTTP_MSG_ERROR;
3959 goto wait_other_side;
3960 }
3961 }
3962
3963 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3964 http_msg_closed:
3965 goto wait_other_side;
3966 }
3967
3968 wait_other_side:
3969 http_silent_debug(__LINE__, s);
3970 return txn->req.msg_state != old_state || buf->flags != old_flags;
3971}
3972
3973
3974/* This function updates the response state machine according to the request
3975 * state machine and buffer flags. It returns 1 if it changes anything (flag
3976 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3977 * it is only used to find when a request/response couple is complete. Both
3978 * this function and its equivalent should loop until both return zero. It
3979 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3980 */
3981int http_sync_res_state(struct session *s)
3982{
3983 struct buffer *buf = s->rep;
3984 struct http_txn *txn = &s->txn;
3985 unsigned int old_flags = buf->flags;
3986 unsigned int old_state = txn->rsp.msg_state;
3987
3988 http_silent_debug(__LINE__, s);
3989 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3990 return 0;
3991
3992 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3993 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003994 * still monitor the server connection for a possible close
3995 * while the request is being uploaded, so we don't disable
3996 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003998 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003999
4000 if (txn->req.msg_state == HTTP_MSG_ERROR)
4001 goto wait_other_side;
4002
4003 if (txn->req.msg_state < HTTP_MSG_DONE) {
4004 /* The client seems to still be sending data, probably
4005 * because we got an error response during an upload.
4006 * We have the choice of either breaking the connection
4007 * or letting it pass through. Let's do the later.
4008 */
4009 goto wait_other_side;
4010 }
4011
4012 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4013 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004014 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4016 goto wait_other_side;
4017 }
4018
4019 /* When we get here, it means that both the request and the
4020 * response have finished receiving. Depending on the connection
4021 * mode, we'll have to wait for the last bytes to leave in either
4022 * direction, and sometimes for a close to be effective.
4023 */
4024
4025 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4026 /* Server-close mode : shut read and wait for the request
4027 * side to close its output buffer. The caller will detect
4028 * when we're in DONE and the other is in CLOSED and will
4029 * catch that for the final cleanup.
4030 */
4031 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4032 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004034 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4035 /* Option forceclose is set, or either side wants to close,
4036 * let's enforce it now that we're not expecting any new
4037 * data to come. The caller knows the session is complete
4038 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004040 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4041 buffer_shutr_now(buf);
4042 buffer_shutw_now(buf);
4043 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 }
4045 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004046 /* The last possible modes are keep-alive and tunnel. Since tunnel
4047 * mode does not set the body analyser, we can't reach this place
4048 * in tunnel mode, so we're left with keep-alive only.
4049 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004051 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004053 }
4054
4055 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4056 /* if we've just closed an output, let's switch */
4057 if (!(buf->flags & BF_OUT_EMPTY)) {
4058 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4059 goto http_msg_closing;
4060 }
4061 else {
4062 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4063 goto http_msg_closed;
4064 }
4065 }
4066 goto wait_other_side;
4067 }
4068
4069 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4070 http_msg_closing:
4071 /* nothing else to forward, just waiting for the output buffer
4072 * to be empty and for the shutw_now to take effect.
4073 */
4074 if (buf->flags & BF_OUT_EMPTY) {
4075 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4076 goto http_msg_closed;
4077 }
4078 else if (buf->flags & BF_SHUTW) {
4079 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004080 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004081 if (target_srv(&s->target))
4082 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 goto wait_other_side;
4084 }
4085 }
4086
4087 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4088 http_msg_closed:
4089 /* drop any pending data */
4090 buffer_ignore(buf, buf->l - buf->send_max);
4091 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004092 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 goto wait_other_side;
4094 }
4095
4096 wait_other_side:
4097 http_silent_debug(__LINE__, s);
4098 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4099}
4100
4101
4102/* Resync the request and response state machines. Return 1 if either state
4103 * changes.
4104 */
4105int http_resync_states(struct session *s)
4106{
4107 struct http_txn *txn = &s->txn;
4108 int old_req_state = txn->req.msg_state;
4109 int old_res_state = txn->rsp.msg_state;
4110
4111 http_silent_debug(__LINE__, s);
4112 http_sync_req_state(s);
4113 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004114 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 if (!http_sync_res_state(s))
4116 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004117 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 if (!http_sync_req_state(s))
4119 break;
4120 }
4121 http_silent_debug(__LINE__, s);
4122 /* OK, both state machines agree on a compatible state.
4123 * There are a few cases we're interested in :
4124 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4125 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4126 * directions, so let's simply disable both analysers.
4127 * - HTTP_MSG_CLOSED on the response only means we must abort the
4128 * request.
4129 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4130 * with server-close mode means we've completed one request and we
4131 * must re-initialize the server connection.
4132 */
4133
4134 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4135 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4136 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4137 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4138 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004139 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004140 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004142 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004143 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004145 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4146 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004147 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004148 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004149 s->rep->analysers = 0;
4150 buffer_auto_close(s->rep);
4151 buffer_auto_read(s->rep);
4152 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004153 buffer_abort(s->req);
4154 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004155 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157 }
4158 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4159 txn->rsp.msg_state == HTTP_MSG_DONE &&
4160 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4161 /* server-close: terminate this server connection and
4162 * reinitialize a fresh-new transaction.
4163 */
4164 http_end_txn_clean_session(s);
4165 }
4166
4167 http_silent_debug(__LINE__, s);
4168 return txn->req.msg_state != old_req_state ||
4169 txn->rsp.msg_state != old_res_state;
4170}
4171
Willy Tarreaud98cf932009-12-27 22:54:55 +01004172/* This function is an analyser which forwards request body (including chunk
4173 * sizes if any). It is called as soon as we must forward, even if we forward
4174 * zero byte. The only situation where it must not be called is when we're in
4175 * tunnel mode and we want to forward till the close. It's used both to forward
4176 * remaining data and to resync after end of body. It expects the msg_state to
4177 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4178 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004179 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004180 * bytes of pending data + the headers if not already done (between som and sov).
4181 * It eventually adjusts som to match sov after the data in between have been sent.
4182 */
4183int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4184{
4185 struct http_txn *txn = &s->txn;
4186 struct http_msg *msg = &s->txn.req;
4187
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004188 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4189 return 0;
4190
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004191 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4192 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004193 /* Output closed while we were sending data. We must abort and
4194 * wake the other side up.
4195 */
4196 msg->msg_state = HTTP_MSG_ERROR;
4197 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004198 return 1;
4199 }
4200
Willy Tarreau4fe41902010-06-07 22:27:41 +02004201 /* in most states, we should abort in case of early close */
4202 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004203
4204 /* Note that we don't have to send 100-continue back because we don't
4205 * need the data to complete our job, and it's up to the server to
4206 * decide whether to return 100, 417 or anything else in return of
4207 * an "Expect: 100-continue" header.
4208 */
4209
4210 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4211 /* we have msg->col and msg->sov which both point to the first
4212 * byte of message body. msg->som still points to the beginning
4213 * of the message. We must save the body in req->lr because it
4214 * survives buffer re-alignments.
4215 */
4216 req->lr = req->data + msg->sov;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004217 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004218 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4219 else {
4220 msg->msg_state = HTTP_MSG_DATA;
4221 }
4222 }
4223
Willy Tarreaud98cf932009-12-27 22:54:55 +01004224 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004225 int bytes;
4226
Willy Tarreau610ecce2010-01-04 21:15:02 +01004227 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004228 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004229 bytes = msg->sov - msg->som;
4230 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004231 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004232 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4233 bytes += req->size;
4234 msg->chunk_len += (unsigned int)bytes;
4235 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004236 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004237
Willy Tarreaucaabe412010-01-03 23:08:28 +01004238 if (msg->msg_state == HTTP_MSG_DATA) {
4239 /* must still forward */
4240 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004241 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004242
4243 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004244 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004245 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004247 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004248 }
4249 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004250 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004251 * set ->sov and ->lr to point to the body and switch to DATA or
4252 * TRAILERS state.
4253 */
4254 int ret = http_parse_chunk_size(req, msg);
4255
4256 if (!ret)
4257 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004258 else if (ret < 0) {
4259 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004260 if (msg->err_pos >= 0)
4261 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004262 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004263 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004264 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004265 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004266 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4267 /* we want the CRLF after the data */
4268 int ret;
4269
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004270 req->lr = req->w + req->send_max;
4271 if (req->lr >= req->data + req->size)
4272 req->lr -= req->size;
4273
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274 ret = http_skip_chunk_crlf(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_DATA_CRLF, 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 MSG_CHUNK_SIZE now */
4285 }
4286 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4287 int ret = http_forward_trailers(req, msg);
4288
4289 if (ret == 0)
4290 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004291 else if (ret < 0) {
4292 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004293 if (msg->err_pos >= 0)
4294 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004295 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004296 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004297 /* we're in HTTP_MSG_DONE now */
4298 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004300 int old_state = msg->msg_state;
4301
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004303 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004304 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4305 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4306 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 if (http_resync_states(s)) {
4308 /* some state changes occurred, maybe the analyser
4309 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004310 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004311 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4312 if (req->flags & BF_SHUTW) {
4313 /* request errors are most likely due to
4314 * the server aborting the transfer.
4315 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004316 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004317 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004318 if (msg->err_pos >= 0)
4319 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004321 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004323 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004324
4325 /* If "option abortonclose" is set on the backend, we
4326 * want to monitor the client's connection and forward
4327 * any shutdown notification to the server, which will
4328 * decide whether to close or to go on processing the
4329 * request.
4330 */
4331 if (s->be->options & PR_O_ABRT_CLOSE) {
4332 buffer_auto_read(req);
4333 buffer_auto_close(req);
4334 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004335 else if (s->txn.meth == HTTP_METH_POST) {
4336 /* POST requests may require to read extra CRLF
4337 * sent by broken browsers and which could cause
4338 * an RST to be sent upon close on some systems
4339 * (eg: Linux).
4340 */
4341 buffer_auto_read(req);
4342 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004343
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004345 }
4346 }
4347
Willy Tarreaud98cf932009-12-27 22:54:55 +01004348 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004349 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004350 if (req->flags & BF_SHUTR) {
4351 if (!(s->flags & SN_ERR_MASK))
4352 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004353 if (!(s->flags & SN_FINST_MASK)) {
4354 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4355 s->flags |= SN_FINST_H;
4356 else
4357 s->flags |= SN_FINST_D;
4358 }
4359
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004360 s->fe->fe_counters.cli_aborts++;
4361 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004362 if (target_srv(&s->target))
4363 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364
4365 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004366 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004367
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004368 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004369 if (req->flags & BF_SHUTW)
4370 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004372 /* When TE: chunked is used, we need to get there again to parse remaining
4373 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4374 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004375 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004376 buffer_dont_close(req);
4377
Willy Tarreau5c620922011-05-11 19:56:11 +02004378 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004379 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4380 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004381 * modes are already handled by the stream sock layer. We must not do
4382 * this in content-length mode because it could present the MSG_MORE
4383 * flag with the last block of forwarded data, which would cause an
4384 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004385 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004386 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004387 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004388
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004390 return 0;
4391
Willy Tarreaud98cf932009-12-27 22:54:55 +01004392 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004393 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004394 if (s->listener->counters)
4395 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004396 return_bad_req_stats_ok:
4397 txn->req.msg_state = HTTP_MSG_ERROR;
4398 if (txn->status) {
4399 /* Note: we don't send any error if some data were already sent */
4400 stream_int_retnclose(req->prod, NULL);
4401 } else {
4402 txn->status = 400;
4403 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4404 }
4405 req->analysers = 0;
4406 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004407
4408 if (!(s->flags & SN_ERR_MASK))
4409 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004410 if (!(s->flags & SN_FINST_MASK)) {
4411 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4412 s->flags |= SN_FINST_H;
4413 else
4414 s->flags |= SN_FINST_D;
4415 }
4416 return 0;
4417
4418 aborted_xfer:
4419 txn->req.msg_state = HTTP_MSG_ERROR;
4420 if (txn->status) {
4421 /* Note: we don't send any error if some data were already sent */
4422 stream_int_retnclose(req->prod, NULL);
4423 } else {
4424 txn->status = 502;
4425 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4426 }
4427 req->analysers = 0;
4428 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4429
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004430 s->fe->fe_counters.srv_aborts++;
4431 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004432 if (target_srv(&s->target))
4433 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004434
4435 if (!(s->flags & SN_ERR_MASK))
4436 s->flags |= SN_ERR_SRVCL;
4437 if (!(s->flags & SN_FINST_MASK)) {
4438 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4439 s->flags |= SN_FINST_H;
4440 else
4441 s->flags |= SN_FINST_D;
4442 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004443 return 0;
4444}
4445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4447 * processing can continue on next analysers, or zero if it either needs more
4448 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4449 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4450 * when it has nothing left to do, and may remove any analyser when it wants to
4451 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004452 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004454{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004455 struct http_txn *txn = &s->txn;
4456 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004457 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004458 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004460 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004461
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004462 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004463 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004465 rep,
4466 rep->rex, rep->wex,
4467 rep->flags,
4468 rep->l,
4469 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004470
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 /*
4472 * Now parse the partial (or complete) lines.
4473 * We will check the response syntax, and also join multi-line
4474 * headers. An index of all the lines will be elaborated while
4475 * parsing.
4476 *
4477 * For the parsing, we use a 28 states FSM.
4478 *
4479 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004480 * rep->data + msg->som = beginning of response
4481 * rep->data + msg->eoh = end of processed headers / start of current one
4482 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004483 * rep->lr = first non-visited byte
4484 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004485 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 */
4487
Willy Tarreau83e3af02009-12-28 17:39:57 +01004488 /* There's a protected area at the end of the buffer for rewriting
4489 * purposes. We don't want to start to parse the request if the
4490 * protected area is affected, because we may have to move processed
4491 * data later, which is much more complicated.
4492 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004493 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4494 if (unlikely((rep->flags & BF_FULL) ||
4495 rep->r < rep->lr ||
4496 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4497 if (rep->send_max) {
4498 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004499 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4500 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004501 buffer_dont_close(rep);
4502 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4503 return 0;
4504 }
4505 if (rep->l <= rep->size - global.tune.maxrewrite)
4506 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004507 }
4508
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004509 if (likely(rep->lr < rep->r))
4510 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004511 }
4512
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 /* 1: we might have to print this header in debug mode */
4514 if (unlikely((global.mode & MODE_DEBUG) &&
4515 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004516 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004517 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004519
Willy Tarreau663308b2010-06-07 14:06:08 +02004520 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004521 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004523
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 sol += hdr_idx_first_pos(&txn->hdr_idx);
4525 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004526
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527 while (cur_idx) {
4528 eol = sol + txn->hdr_idx.v[cur_idx].len;
4529 debug_hdr("srvhdr", s, sol, eol);
4530 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4531 cur_idx = txn->hdr_idx.v[cur_idx].next;
4532 }
4533 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004534
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 /*
4536 * Now we quickly check if we have found a full valid response.
4537 * If not so, we check the FD and buffer states before leaving.
4538 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004539 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 * responses are checked first.
4541 *
4542 * Depending on whether the client is still there or not, we
4543 * may send an error response back or not. Note that normally
4544 * we should only check for HTTP status there, and check I/O
4545 * errors somewhere else.
4546 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004547
Willy Tarreau655dce92009-11-08 13:10:58 +01004548 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 /* Invalid response */
4550 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4551 /* we detected a parsing error. We want to archive this response
4552 * in the dedicated proxy area for later troubleshooting.
4553 */
4554 hdr_response_bad:
4555 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004556 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004558 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004559 if (target_srv(&s->target)) {
4560 target_srv(&s->target)->counters.failed_resp++;
4561 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004562 }
Willy Tarreau64648412010-03-05 10:41:54 +01004563 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004564 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 rep->analysers = 0;
4566 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004567 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004568 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4570
4571 if (!(s->flags & SN_ERR_MASK))
4572 s->flags |= SN_ERR_PRXCOND;
4573 if (!(s->flags & SN_FINST_MASK))
4574 s->flags |= SN_FINST_H;
4575
4576 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004577 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004578
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 /* too large response does not fit in buffer. */
4580 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004581 if (msg->err_pos < 0)
4582 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004583 goto hdr_response_bad;
4584 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 /* read error */
4587 else if (rep->flags & BF_READ_ERROR) {
4588 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004589 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004590
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004591 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004592 if (target_srv(&s->target)) {
4593 target_srv(&s->target)->counters.failed_resp++;
4594 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004595 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004596
Willy Tarreau90deb182010-01-07 00:20:41 +01004597 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 rep->analysers = 0;
4599 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004600 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004601 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004602 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004603
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 if (!(s->flags & SN_ERR_MASK))
4605 s->flags |= SN_ERR_SRVCL;
4606 if (!(s->flags & SN_FINST_MASK))
4607 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004608 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004609 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004610
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004611 /* read timeout : return a 504 to the client. */
4612 else if (rep->flags & BF_READ_TIMEOUT) {
4613 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004614 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004615
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004616 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004617 if (target_srv(&s->target)) {
4618 target_srv(&s->target)->counters.failed_resp++;
4619 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004620 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004621
Willy Tarreau90deb182010-01-07 00:20:41 +01004622 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 rep->analysers = 0;
4624 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004625 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004626 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 if (!(s->flags & SN_ERR_MASK))
4630 s->flags |= SN_ERR_SRVTO;
4631 if (!(s->flags & SN_FINST_MASK))
4632 s->flags |= SN_FINST_H;
4633 return 0;
4634 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004635
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004636 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004638 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004639 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004641 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004642 if (target_srv(&s->target)) {
4643 target_srv(&s->target)->counters.failed_resp++;
4644 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004645 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004646
Willy Tarreau90deb182010-01-07 00:20:41 +01004647 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 rep->analysers = 0;
4649 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004650 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004651 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004653
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 if (!(s->flags & SN_ERR_MASK))
4655 s->flags |= SN_ERR_SRVCL;
4656 if (!(s->flags & SN_FINST_MASK))
4657 s->flags |= SN_FINST_H;
4658 return 0;
4659 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004660
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 /* write error to client (we don't send any message then) */
4662 else if (rep->flags & BF_WRITE_ERROR) {
4663 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004664 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004665
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004666 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004668 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669
4670 if (!(s->flags & SN_ERR_MASK))
4671 s->flags |= SN_ERR_CLICL;
4672 if (!(s->flags & SN_FINST_MASK))
4673 s->flags |= SN_FINST_H;
4674
4675 /* process_session() will take care of the error */
4676 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004677 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004678
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679 buffer_dont_close(rep);
4680 return 0;
4681 }
4682
4683 /* More interesting part now : we know that we have a complete
4684 * response which at least looks like HTTP. We have an indicator
4685 * of each header's length, so we can parse them quickly.
4686 */
4687
4688 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004689 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004691 /*
4692 * 1: get the status code
4693 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004694 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004695 if (n < 1 || n > 5)
4696 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004697 /* when the client triggers a 4xx from the server, it's most often due
4698 * to a missing object or permission. These events should be tracked
4699 * because if they happen often, it may indicate a brute force or a
4700 * vulnerability scan.
4701 */
4702 if (n == 4)
4703 session_inc_http_err_ctr(s);
4704
Willy Tarreau827aee92011-03-10 16:55:02 +01004705 if (target_srv(&s->target))
4706 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707
Willy Tarreau5b154472009-12-21 20:11:07 +01004708 /* check if the response is HTTP/1.1 or above */
4709 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004710 ((msg->sol[5] > '1') ||
4711 ((msg->sol[5] == '1') &&
4712 (msg->sol[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004713 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004714
4715 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004716 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 +01004717
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004718 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004719 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004720
Willy Tarreau962c3f42010-01-10 00:15:35 +01004721 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722
Willy Tarreau39650402010-03-15 19:44:39 +01004723 /* Adjust server's health based on status code. Note: status codes 501
4724 * and 505 are triggered on demand by client request, so we must not
4725 * count them as server failures.
4726 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004727 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004728 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004729 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004730 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004731 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004732 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004733
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004734 /*
4735 * 2: check for cacheability.
4736 */
4737
4738 switch (txn->status) {
4739 case 200:
4740 case 203:
4741 case 206:
4742 case 300:
4743 case 301:
4744 case 410:
4745 /* RFC2616 @13.4:
4746 * "A response received with a status code of
4747 * 200, 203, 206, 300, 301 or 410 MAY be stored
4748 * by a cache (...) unless a cache-control
4749 * directive prohibits caching."
4750 *
4751 * RFC2616 @9.5: POST method :
4752 * "Responses to this method are not cacheable,
4753 * unless the response includes appropriate
4754 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004755 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 if (likely(txn->meth != HTTP_METH_POST) &&
4757 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4758 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4759 break;
4760 default:
4761 break;
4762 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 /*
4765 * 3: we may need to capture headers
4766 */
4767 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004768 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004769 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 txn->rsp.cap, s->fe->rsp_cap);
4771
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004772 /* 4: determine the transfer-length.
4773 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4774 * the presence of a message-body in a RESPONSE and its transfer length
4775 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004776 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004777 * All responses to the HEAD request method MUST NOT include a
4778 * message-body, even though the presence of entity-header fields
4779 * might lead one to believe they do. All 1xx (informational), 204
4780 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4781 * message-body. All other responses do include a message-body,
4782 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004783 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004784 * 1. Any response which "MUST NOT" include a message-body (such as the
4785 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4786 * always terminated by the first empty line after the header fields,
4787 * regardless of the entity-header fields present in the message.
4788 *
4789 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4790 * the "chunked" transfer-coding (Section 6.2) is used, the
4791 * transfer-length is defined by the use of this transfer-coding.
4792 * If a Transfer-Encoding header field is present and the "chunked"
4793 * transfer-coding is not present, the transfer-length is defined by
4794 * the sender closing the connection.
4795 *
4796 * 3. If a Content-Length header field is present, its decimal value in
4797 * OCTETs represents both the entity-length and the transfer-length.
4798 * If a message is received with both a Transfer-Encoding header
4799 * field and a Content-Length header field, the latter MUST be ignored.
4800 *
4801 * 4. If the message uses the media type "multipart/byteranges", and
4802 * the transfer-length is not otherwise specified, then this self-
4803 * delimiting media type defines the transfer-length. This media
4804 * type MUST NOT be used unless the sender knows that the recipient
4805 * can parse it; the presence in a request of a Range header with
4806 * multiple byte-range specifiers from a 1.1 client implies that the
4807 * client can parse multipart/byteranges responses.
4808 *
4809 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004810 */
4811
4812 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004813 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004814 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004815 * FIXME: should we parse anyway and return an error on chunked encoding ?
4816 */
4817 if (txn->meth == HTTP_METH_HEAD ||
4818 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004819 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004820 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004821 goto skip_content_length;
4822 }
4823
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004824 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004825 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004826 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004827 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004828 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4830 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004831 /* bad transfer-encoding (chunked followed by something else) */
4832 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004833 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004834 break;
4835 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836 }
4837
4838 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4839 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004840 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004841 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4842 signed long long cl;
4843
Willy Tarreauad14f752011-09-02 20:33:27 +02004844 if (!ctx.vlen) {
4845 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004846 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004847 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004848
Willy Tarreauad14f752011-09-02 20:33:27 +02004849 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4850 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004851 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004852 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004853
Willy Tarreauad14f752011-09-02 20:33:27 +02004854 if (cl < 0) {
4855 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004856 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004857 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004858
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004859 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004860 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004861 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004862 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004863
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004864 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004865 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004866 }
4867
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004868 /* FIXME: we should also implement the multipart/byterange method.
4869 * For now on, we resort to close mode in this case (unknown length).
4870 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004871skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004872
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 /* end of job, return OK */
4874 rep->analysers &= ~an_bit;
4875 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004876 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004877 return 1;
4878}
4879
4880/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004881 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4882 * and updates t->rep->analysers. It might make sense to explode it into several
4883 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004884 */
4885int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4886{
4887 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004888 struct http_msg *msg = &txn->rsp;
4889 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004890 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004891
4892 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4893 now_ms, __FUNCTION__,
4894 t,
4895 rep,
4896 rep->rex, rep->wex,
4897 rep->flags,
4898 rep->l,
4899 rep->analysers);
4900
Willy Tarreau655dce92009-11-08 13:10:58 +01004901 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004902 return 0;
4903
4904 rep->analysers &= ~an_bit;
4905 rep->analyse_exp = TICK_ETERNITY;
4906
Willy Tarreau5b154472009-12-21 20:11:07 +01004907 /* Now we have to check if we need to modify the Connection header.
4908 * This is more difficult on the response than it is on the request,
4909 * because we can have two different HTTP versions and we don't know
4910 * how the client will interprete a response. For instance, let's say
4911 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4912 * HTTP/1.1 response without any header. Maybe it will bound itself to
4913 * HTTP/1.0 because it only knows about it, and will consider the lack
4914 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4915 * the lack of header as a keep-alive. Thus we will use two flags
4916 * indicating how a request MAY be understood by the client. In case
4917 * of multiple possibilities, we'll fix the header to be explicit. If
4918 * ambiguous cases such as both close and keepalive are seen, then we
4919 * will fall back to explicit close. Note that we won't take risks with
4920 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004921 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004922 */
4923
Willy Tarreaudc008c52010-02-01 16:20:08 +01004924 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4925 txn->status == 101)) {
4926 /* Either we've established an explicit tunnel, or we're
4927 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004928 * to understand the next protocols. We have to switch to tunnel
4929 * mode, so that we transfer the request and responses then let
4930 * this protocol pass unmodified. When we later implement specific
4931 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004932 * header which contains information about that protocol for
4933 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004934 */
4935 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4936 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004937 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4938 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4939 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004940 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004941
Willy Tarreau60466522010-01-18 19:08:45 +01004942 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004943 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004944 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4945 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004946
Willy Tarreau60466522010-01-18 19:08:45 +01004947 /* now adjust header transformations depending on current state */
4948 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4949 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4950 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004951 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004952 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004953 }
Willy Tarreau60466522010-01-18 19:08:45 +01004954 else { /* SCL / KAL */
4955 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004956 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004957 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004958 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004959
Willy Tarreau60466522010-01-18 19:08:45 +01004960 /* Parse and remove some headers from the connection header */
4961 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004962
Willy Tarreau60466522010-01-18 19:08:45 +01004963 /* Some keep-alive responses are converted to Server-close if
4964 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004965 */
Willy Tarreau60466522010-01-18 19:08:45 +01004966 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4967 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004968 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004969 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004970 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004971 }
4972
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004973 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 /*
4975 * 3: we will have to evaluate the filters.
4976 * As opposed to version 1.2, now they will be evaluated in the
4977 * filters order and not in the header order. This means that
4978 * each filter has to be validated among all headers.
4979 *
4980 * Filters are tried with ->be first, then with ->fe if it is
4981 * different from ->be.
4982 */
4983
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004984 cur_proxy = t->be;
4985 while (1) {
4986 struct proxy *rule_set = cur_proxy;
4987
4988 /* try headers filters */
4989 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004990 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004992 if (target_srv(&t->target)) {
4993 target_srv(&t->target)->counters.failed_resp++;
4994 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004995 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004996 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004997 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004998 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004999 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005000 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005001 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005002 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005003 if (!(t->flags & SN_ERR_MASK))
5004 t->flags |= SN_ERR_PRXCOND;
5005 if (!(t->flags & SN_FINST_MASK))
5006 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005007 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005008 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005009 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005010
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005011 /* has the response been denied ? */
5012 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005013 if (target_srv(&t->target))
5014 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005016 t->be->be_counters.denied_resp++;
5017 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005018 if (t->listener->counters)
5019 t->listener->counters->denied_resp++;
5020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005021 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005022 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005024 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005025 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005026 if (txn->status < 200)
5027 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005028 if (wl->cond) {
5029 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5030 ret = acl_pass(ret);
5031 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5032 ret = !ret;
5033 if (!ret)
5034 continue;
5035 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005036 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005037 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005038 }
5039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 /* check whether we're already working on the frontend */
5041 if (cur_proxy == t->fe)
5042 break;
5043 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005044 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005045
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005046 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005047 * We may be facing a 100-continue response, in which case this
5048 * is not the right response, and we're waiting for the next one.
5049 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005050 * next one.
5051 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005052 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005053 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005054 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005055 msg->msg_state = HTTP_MSG_RPBEFORE;
5056 txn->status = 0;
5057 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5058 return 1;
5059 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005060 else if (unlikely(txn->status < 200))
5061 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005062
5063 /* we don't have any 1xx status code now */
5064
5065 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066 * 4: check for server cookie.
5067 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005068 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5069 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005070 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005071
Willy Tarreaubaaee002006-06-26 02:48:02 +02005072
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005074 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005075 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005076 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005078
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 /*
5080 * 6: add server cookie in the response if needed
5081 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005082 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005083 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005084 (!(t->flags & SN_DIRECT) ||
5085 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5086 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5087 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5088 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005089 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5090 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005092 /* the server is known, it's not the one the client requested, or the
5093 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 * insert a set-cookie here, except if we want to insert only on POST
5095 * requests and this one isn't. Note that servers which don't have cookies
5096 * (eg: some backup servers) will return a full cookie removal request.
5097 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005098 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005099 len = sprintf(trash,
5100 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5101 t->be->cookie_name);
5102 }
5103 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005104 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005105
5106 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5107 /* emit last_date, which is mandatory */
5108 trash[len++] = COOKIE_DELIM_DATE;
5109 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5110 if (t->be->cookie_maxlife) {
5111 /* emit first_date, which is either the original one or
5112 * the current date.
5113 */
5114 trash[len++] = COOKIE_DELIM_DATE;
5115 s30tob64(txn->cookie_first_date ?
5116 txn->cookie_first_date >> 2 :
5117 (date.tv_sec+3) >> 2, trash + len);
5118 len += 5;
5119 }
5120 }
5121 len += sprintf(trash + len, "; path=/");
5122 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005124 if (t->be->cookie_domain)
5125 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005127 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005128 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005130
Willy Tarreauf1348312010-10-07 15:54:11 +02005131 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005132 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005133 /* the server did not change, only the date was updated */
5134 txn->flags |= TX_SCK_UPDATED;
5135 else
5136 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005137
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005138 /* Here, we will tell an eventual cache on the client side that we don't
5139 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5140 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5141 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5142 */
5143 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005144
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5146
5147 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005148 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005150 }
5151 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005152
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005153 /*
5154 * 7: check if result will be cacheable with a cookie.
5155 * We'll block the response if security checks have caught
5156 * nasty things such as a cacheable cookie.
5157 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005158 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5159 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005160 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161
5162 /* we're in presence of a cacheable response containing
5163 * a set-cookie header. We'll block it as requested by
5164 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005166 if (target_srv(&t->target))
5167 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005168
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005169 t->be->be_counters.denied_resp++;
5170 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005171 if (t->listener->counters)
5172 t->listener->counters->denied_resp++;
5173
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005175 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 send_log(t->be, LOG_ALERT,
5177 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005178 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 goto return_srv_prx_502;
5180 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005181
5182 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005183 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184 */
Willy Tarreau60466522010-01-18 19:08:45 +01005185 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5186 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5187 unsigned int want_flags = 0;
5188
5189 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5190 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5191 /* we want a keep-alive response here. Keep-alive header
5192 * required if either side is not 1.1.
5193 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005194 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005195 want_flags |= TX_CON_KAL_SET;
5196 }
5197 else {
5198 /* we want a close response here. Close header required if
5199 * the server is 1.1, regardless of the client.
5200 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005201 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005202 want_flags |= TX_CON_CLO_SET;
5203 }
5204
5205 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5206 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005207 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005208
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005209 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005210 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005211 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005212 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 /*************************************************************
5215 * OK, that's finished for the headers. We have done what we *
5216 * could. Let's switch to the DATA state. *
5217 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005218
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005220
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005221 /* if the user wants to log as soon as possible, without counting
5222 * bytes from the server, then this is the right moment. We have
5223 * to temporarily assign bytes_out to log what we currently have.
5224 */
5225 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5226 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5227 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005228 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005229 t->logs.bytes_out = 0;
5230 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005231
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 /* Note: we must not try to cheat by jumping directly to DATA,
5233 * otherwise we would not let the client side wake up.
5234 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005235
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005236 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005238 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005240
Willy Tarreaud98cf932009-12-27 22:54:55 +01005241/* This function is an analyser which forwards response body (including chunk
5242 * sizes if any). It is called as soon as we must forward, even if we forward
5243 * zero byte. The only situation where it must not be called is when we're in
5244 * tunnel mode and we want to forward till the close. It's used both to forward
5245 * remaining data and to resync after end of body. It expects the msg_state to
5246 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5247 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005248 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249 * bytes of pending data + the headers if not already done (between som and sov).
5250 * It eventually adjusts som to match sov after the data in between have been sent.
5251 */
5252int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5253{
5254 struct http_txn *txn = &s->txn;
5255 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005256 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005257
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005258 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5259 return 0;
5260
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005261 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005262 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005263 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005264 /* Output closed while we were sending data. We must abort and
5265 * wake the other side up.
5266 */
5267 msg->msg_state = HTTP_MSG_ERROR;
5268 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005269 return 1;
5270 }
5271
Willy Tarreau4fe41902010-06-07 22:27:41 +02005272 /* in most states, we should abort in case of early close */
5273 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005274
Willy Tarreaud98cf932009-12-27 22:54:55 +01005275 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5276 /* we have msg->col and msg->sov which both point to the first
5277 * byte of message body. msg->som still points to the beginning
5278 * of the message. We must save the body in req->lr because it
5279 * survives buffer re-alignments.
5280 */
5281 res->lr = res->data + msg->sov;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005282 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5284 else {
5285 msg->msg_state = HTTP_MSG_DATA;
5286 }
5287 }
5288
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005290 int bytes;
5291
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005293 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005294 bytes = msg->sov - msg->som;
5295 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005296 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005297 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5298 bytes += res->size;
5299 msg->chunk_len += (unsigned int)bytes;
5300 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005301 }
5302
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005303
Willy Tarreaucaabe412010-01-03 23:08:28 +01005304 if (msg->msg_state == HTTP_MSG_DATA) {
5305 /* must still forward */
5306 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005307 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005308
5309 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005310 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005311 msg->msg_state = HTTP_MSG_DATA_CRLF;
5312 else
5313 msg->msg_state = HTTP_MSG_DONE;
5314 }
5315 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005316 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005317 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5318 */
5319 int ret = http_parse_chunk_size(res, msg);
5320
5321 if (!ret)
5322 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005323 else if (ret < 0) {
5324 if (msg->err_pos >= 0)
5325 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005326 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005327 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005329 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5331 /* we want the CRLF after the data */
5332 int ret;
5333
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005334 res->lr = res->w + res->send_max;
5335 if (res->lr >= res->data + res->size)
5336 res->lr -= res->size;
5337
Willy Tarreaud98cf932009-12-27 22:54:55 +01005338 ret = http_skip_chunk_crlf(res, msg);
5339
5340 if (!ret)
5341 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005342 else if (ret < 0) {
5343 if (msg->err_pos >= 0)
5344 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005345 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005346 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 /* we're in MSG_CHUNK_SIZE now */
5348 }
5349 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5350 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005351
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352 if (ret == 0)
5353 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005354 else if (ret < 0) {
5355 if (msg->err_pos >= 0)
5356 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005357 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005358 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005359 /* we're in HTTP_MSG_DONE now */
5360 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005361 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005362 int old_state = msg->msg_state;
5363
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005365 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005366 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5367 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5368 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 if (http_resync_states(s)) {
5370 http_silent_debug(__LINE__, s);
5371 /* some state changes occurred, maybe the analyser
5372 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005373 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005374 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5375 if (res->flags & BF_SHUTW) {
5376 /* response errors are most likely due to
5377 * the client aborting the transfer.
5378 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005379 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005380 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005381 if (msg->err_pos >= 0)
5382 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005384 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005386 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005388 }
5389 }
5390
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005392 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005393 if (res->flags & BF_SHUTR) {
5394 if (!(s->flags & SN_ERR_MASK))
5395 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005396 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005397 if (target_srv(&s->target))
5398 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005399 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005400 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005401
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005402 if (res->flags & BF_SHUTW)
5403 goto aborted_xfer;
5404
Willy Tarreau40dba092010-03-04 18:14:51 +01005405 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 if (!s->req->analysers)
5407 goto return_bad_res;
5408
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005409 /* forward any pending data */
5410 bytes = msg->sov - msg->som;
5411 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005413 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5414 bytes += res->size;
5415 msg->chunk_len += (unsigned int)bytes;
5416 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 }
5418
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005419 /* When TE: chunked is used, we need to get there again to parse remaining
5420 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5421 * Similarly, with keep-alive on the client side, we don't want to forward a
5422 * close.
5423 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005424 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005425 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5426 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5427 buffer_dont_close(res);
5428
Willy Tarreau5c620922011-05-11 19:56:11 +02005429 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005430 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5431 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005432 * modes are already handled by the stream sock layer. We must not do
5433 * this in content-length mode because it could present the MSG_MORE
5434 * flag with the last block of forwarded data, which would cause an
5435 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005436 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005437 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005438 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005439
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005442 return 0;
5443
Willy Tarreau40dba092010-03-04 18:14:51 +01005444 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005445 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005446 if (target_srv(&s->target))
5447 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005448
5449 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005451 /* don't send any error message as we're in the body */
5452 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005453 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005454 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005455 if (target_srv(&s->target))
5456 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005457
5458 if (!(s->flags & SN_ERR_MASK))
5459 s->flags |= SN_ERR_PRXCOND;
5460 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005461 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005462 return 0;
5463
5464 aborted_xfer:
5465 txn->rsp.msg_state = HTTP_MSG_ERROR;
5466 /* don't send any error message as we're in the body */
5467 stream_int_retnclose(res->cons, NULL);
5468 res->analysers = 0;
5469 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5470
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005471 s->fe->fe_counters.cli_aborts++;
5472 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005473 if (target_srv(&s->target))
5474 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005475
5476 if (!(s->flags & SN_ERR_MASK))
5477 s->flags |= SN_ERR_CLICL;
5478 if (!(s->flags & SN_FINST_MASK))
5479 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005480 return 0;
5481}
5482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005483/* Iterate the same filter through all request headers.
5484 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005485 * Since it can manage the switch to another backend, it updates the per-proxy
5486 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005487 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005489{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005490 char term;
5491 char *cur_ptr, *cur_end, *cur_next;
5492 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005493 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005494 struct hdr_idx_elem *cur_hdr;
5495 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005497 last_hdr = 0;
5498
Willy Tarreau962c3f42010-01-10 00:15:35 +01005499 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005500 old_idx = 0;
5501
5502 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005503 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005504 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005505 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005506 (exp->action == ACT_ALLOW ||
5507 exp->action == ACT_DENY ||
5508 exp->action == ACT_TARPIT))
5509 return 0;
5510
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005511 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005512 if (!cur_idx)
5513 break;
5514
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005515 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 cur_ptr = cur_next;
5517 cur_end = cur_ptr + cur_hdr->len;
5518 cur_next = cur_end + cur_hdr->cr + 1;
5519
5520 /* Now we have one header between cur_ptr and cur_end,
5521 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005522 */
5523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005524 /* The annoying part is that pattern matching needs
5525 * that we modify the contents to null-terminate all
5526 * strings before testing them.
5527 */
5528
5529 term = *cur_end;
5530 *cur_end = '\0';
5531
5532 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5533 switch (exp->action) {
5534 case ACT_SETBE:
5535 /* It is not possible to jump a second time.
5536 * FIXME: should we return an HTTP/500 here so that
5537 * the admin knows there's a problem ?
5538 */
5539 if (t->be != t->fe)
5540 break;
5541
5542 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005543 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005544 last_hdr = 1;
5545 break;
5546
5547 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005548 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005549 last_hdr = 1;
5550 break;
5551
5552 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005553 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005554 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005555
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005556 t->fe->fe_counters.denied_req++;
5557 if (t->fe != t->be)
5558 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005559 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005560 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005562 break;
5563
5564 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005565 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005566 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005567
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005568 t->fe->fe_counters.denied_req++;
5569 if (t->fe != t->be)
5570 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005571 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005572 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 break;
5575
5576 case ACT_REPLACE:
5577 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5578 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5579 /* FIXME: if the user adds a newline in the replacement, the
5580 * index will not be recalculated for now, and the new line
5581 * will not be counted as a new header.
5582 */
5583
5584 cur_end += delta;
5585 cur_next += delta;
5586 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005587 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005588 break;
5589
5590 case ACT_REMOVE:
5591 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5592 cur_next += delta;
5593
Willy Tarreaufa355d42009-11-29 18:12:29 +01005594 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005595 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5596 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005597 cur_hdr->len = 0;
5598 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005599 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 break;
5601
5602 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005603 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 if (cur_end)
5605 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005607 /* keep the link from this header to next one in case of later
5608 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005609 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005610 old_idx = cur_idx;
5611 }
5612 return 0;
5613}
5614
5615
5616/* Apply the filter to the request line.
5617 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5618 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005619 * Since it can manage the switch to another backend, it updates the per-proxy
5620 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 */
5622int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5623{
5624 char term;
5625 char *cur_ptr, *cur_end;
5626 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005627 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005629
Willy Tarreau58f10d72006-12-04 02:26:12 +01005630
Willy Tarreau3d300592007-03-18 18:34:41 +01005631 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005632 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005633 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 (exp->action == ACT_ALLOW ||
5635 exp->action == ACT_DENY ||
5636 exp->action == ACT_TARPIT))
5637 return 0;
5638 else if (exp->action == ACT_REMOVE)
5639 return 0;
5640
5641 done = 0;
5642
Willy Tarreau962c3f42010-01-10 00:15:35 +01005643 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005644 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645
5646 /* Now we have the request line between cur_ptr and cur_end */
5647
5648 /* The annoying part is that pattern matching needs
5649 * that we modify the contents to null-terminate all
5650 * strings before testing them.
5651 */
5652
5653 term = *cur_end;
5654 *cur_end = '\0';
5655
5656 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5657 switch (exp->action) {
5658 case ACT_SETBE:
5659 /* It is not possible to jump a second time.
5660 * FIXME: should we return an HTTP/500 here so that
5661 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005662 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 if (t->be != t->fe)
5664 break;
5665
5666 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005667 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 done = 1;
5669 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005672 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 done = 1;
5674 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005677 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005678
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005679 t->fe->fe_counters.denied_req++;
5680 if (t->fe != t->be)
5681 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005682 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005683 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 done = 1;
5686 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005689 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005690
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005691 t->fe->fe_counters.denied_req++;
5692 if (t->fe != t->be)
5693 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005694 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005695 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 done = 1;
5698 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 case ACT_REPLACE:
5701 *cur_end = term; /* restore the string terminator */
5702 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5703 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5704 /* FIXME: if the user adds a newline in the replacement, the
5705 * index will not be recalculated for now, and the new line
5706 * will not be counted as a new header.
5707 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005708
Willy Tarreaufa355d42009-11-29 18:12:29 +01005709 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005711 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 HTTP_MSG_RQMETH,
5713 cur_ptr, cur_end + 1,
5714 NULL, NULL);
5715 if (unlikely(!cur_end))
5716 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005717
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005718 /* we have a full request and we know that we have either a CR
5719 * or an LF at <ptr>.
5720 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005721 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5722 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 /* there is no point trying this regex on headers */
5724 return 1;
5725 }
5726 }
5727 *cur_end = term; /* restore the string terminator */
5728 return done;
5729}
Willy Tarreau97de6242006-12-27 17:18:38 +01005730
Willy Tarreau58f10d72006-12-04 02:26:12 +01005731
Willy Tarreau58f10d72006-12-04 02:26:12 +01005732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005734 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005736 * unparsable request. Since it can manage the switch to another backend, it
5737 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005738 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005739int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005741 struct http_txn *txn = &s->txn;
5742 struct hdr_exp *exp;
5743
5744 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005747 /*
5748 * The interleaving of transformations and verdicts
5749 * makes it difficult to decide to continue or stop
5750 * the evaluation.
5751 */
5752
Willy Tarreau6c123b12010-01-28 20:22:06 +01005753 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5754 break;
5755
Willy Tarreau3d300592007-03-18 18:34:41 +01005756 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005757 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005758 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005759 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005760
5761 /* if this filter had a condition, evaluate it now and skip to
5762 * next filter if the condition does not match.
5763 */
5764 if (exp->cond) {
5765 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5766 ret = acl_pass(ret);
5767 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5768 ret = !ret;
5769
5770 if (!ret)
5771 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 }
5773
5774 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005775 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005776 if (unlikely(ret < 0))
5777 return -1;
5778
5779 if (likely(ret == 0)) {
5780 /* The filter did not match the request, it can be
5781 * iterated through all headers.
5782 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005783 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005785 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005786 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005787}
5788
5789
Willy Tarreaua15645d2007-03-18 16:22:39 +01005790
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005792 * Try to retrieve the server associated to the appsession.
5793 * If the server is found, it's assigned to the session.
5794 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005795void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005796 struct http_txn *txn = &t->txn;
5797 appsess *asession = NULL;
5798 char *sessid_temp = NULL;
5799
Cyril Bontéb21570a2009-11-29 20:04:48 +01005800 if (len > t->be->appsession_len) {
5801 len = t->be->appsession_len;
5802 }
5803
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005804 if (t->be->options2 & PR_O2_AS_REQL) {
5805 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005806 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005807 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005808 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005809 }
5810
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005811 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005812 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5813 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5814 return;
5815 }
5816
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005817 memcpy(txn->sessid, buf, len);
5818 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005819 }
5820
5821 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5822 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5823 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5824 return;
5825 }
5826
Cyril Bontéb21570a2009-11-29 20:04:48 +01005827 memcpy(sessid_temp, buf, len);
5828 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005829
5830 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5831 /* free previously allocated memory */
5832 pool_free2(apools.sessid, sessid_temp);
5833
5834 if (asession != NULL) {
5835 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5836 if (!(t->be->options2 & PR_O2_AS_REQL))
5837 asession->request_count++;
5838
5839 if (asession->serverid != NULL) {
5840 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005841
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005842 while (srv) {
5843 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005844 if ((srv->state & SRV_RUNNING) ||
5845 (t->be->options & PR_O_PERSIST) ||
5846 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005847 /* we found the server and it's usable */
5848 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005849 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005850 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005851 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005852
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005853 break;
5854 } else {
5855 txn->flags &= ~TX_CK_MASK;
5856 txn->flags |= TX_CK_DOWN;
5857 }
5858 }
5859 srv = srv->next;
5860 }
5861 }
5862 }
5863}
5864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005865/* Find the end of a cookie value contained between <s> and <e>. It works the
5866 * same way as with headers above except that the semi-colon also ends a token.
5867 * See RFC2965 for more information. Note that it requires a valid header to
5868 * return a valid result.
5869 */
5870char *find_cookie_value_end(char *s, const char *e)
5871{
5872 int quoted, qdpair;
5873
5874 quoted = qdpair = 0;
5875 for (; s < e; s++) {
5876 if (qdpair) qdpair = 0;
5877 else if (quoted) {
5878 if (*s == '\\') qdpair = 1;
5879 else if (*s == '"') quoted = 0;
5880 }
5881 else if (*s == '"') quoted = 1;
5882 else if (*s == ',' || *s == ';') return s;
5883 }
5884 return s;
5885}
5886
5887/* Delete a value in a header between delimiters <from> and <next> in buffer
5888 * <buf>. The number of characters displaced is returned, and the pointer to
5889 * the first delimiter is updated if required. The function tries as much as
5890 * possible to respect the following principles :
5891 * - replace <from> delimiter by the <next> one unless <from> points to a
5892 * colon, in which case <next> is simply removed
5893 * - set exactly one space character after the new first delimiter, unless
5894 * there are not enough characters in the block being moved to do so.
5895 * - remove unneeded spaces before the previous delimiter and after the new
5896 * one.
5897 *
5898 * It is the caller's responsibility to ensure that :
5899 * - <from> points to a valid delimiter or the colon ;
5900 * - <next> points to a valid delimiter or the final CR/LF ;
5901 * - there are non-space chars before <from> ;
5902 * - there is a CR/LF at or after <next>.
5903 */
5904int del_hdr_value(struct buffer *buf, char **from, char *next)
5905{
5906 char *prev = *from;
5907
5908 if (*prev == ':') {
5909 /* We're removing the first value, preserve the colon and add a
5910 * space if possible.
5911 */
5912 if (!http_is_crlf[(unsigned char)*next])
5913 next++;
5914 prev++;
5915 if (prev < next)
5916 *prev++ = ' ';
5917
5918 while (http_is_spht[(unsigned char)*next])
5919 next++;
5920 } else {
5921 /* Remove useless spaces before the old delimiter. */
5922 while (http_is_spht[(unsigned char)*(prev-1)])
5923 prev--;
5924 *from = prev;
5925
5926 /* copy the delimiter and if possible a space if we're
5927 * not at the end of the line.
5928 */
5929 if (!http_is_crlf[(unsigned char)*next]) {
5930 *prev++ = *next++;
5931 if (prev + 1 < next)
5932 *prev++ = ' ';
5933 while (http_is_spht[(unsigned char)*next])
5934 next++;
5935 }
5936 }
5937 return buffer_replace2(buf, prev, next, NULL, 0);
5938}
5939
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005940/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005941 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 * desirable to call it only when needed. This code is quite complex because
5943 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5944 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 */
5946void manage_client_side_cookies(struct session *t, struct buffer *req)
5947{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005948 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005949 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005951 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5952 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005954 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005955 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005956 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005958 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005960 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005961
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005962 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005963 hdr_beg = hdr_next;
5964 hdr_end = hdr_beg + cur_hdr->len;
5965 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005967 /* We have one full header between hdr_beg and hdr_end, and the
5968 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969 * "Cookie:" headers.
5970 */
5971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005972 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005973 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005974 old_idx = cur_idx;
5975 continue;
5976 }
5977
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005978 del_from = NULL; /* nothing to be deleted */
5979 preserve_hdr = 0; /* assume we may kill the whole header */
5980
Willy Tarreau58f10d72006-12-04 02:26:12 +01005981 /* Now look for cookies. Conforming to RFC2109, we have to support
5982 * attributes whose name begin with a '$', and associate them with
5983 * the right cookie, if we want to delete this cookie.
5984 * So there are 3 cases for each cookie read :
5985 * 1) it's a special attribute, beginning with a '$' : ignore it.
5986 * 2) it's a server id cookie that we *MAY* want to delete : save
5987 * some pointers on it (last semi-colon, beginning of cookie...)
5988 * 3) it's an application cookie : we *MAY* have to delete a previous
5989 * "special" cookie.
5990 * At the end of loop, if a "special" cookie remains, we may have to
5991 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005992 * *MUST* delete it.
5993 *
5994 * Note: RFC2965 is unclear about the processing of spaces around
5995 * the equal sign in the ATTR=VALUE form. A careful inspection of
5996 * the RFC explicitly allows spaces before it, and not within the
5997 * tokens (attrs or values). An inspection of RFC2109 allows that
5998 * too but section 10.1.3 lets one think that spaces may be allowed
5999 * after the equal sign too, resulting in some (rare) buggy
6000 * implementations trying to do that. So let's do what servers do.
6001 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6002 * allowed quoted strings in values, with any possible character
6003 * after a backslash, including control chars and delimitors, which
6004 * causes parsing to become ambiguous. Browsers also allow spaces
6005 * within values even without quotes.
6006 *
6007 * We have to keep multiple pointers in order to support cookie
6008 * removal at the beginning, middle or end of header without
6009 * corrupting the header. All of these headers are valid :
6010 *
6011 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6012 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6013 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6014 * | | | | | | | | |
6015 * | | | | | | | | hdr_end <--+
6016 * | | | | | | | +--> next
6017 * | | | | | | +----> val_end
6018 * | | | | | +-----------> val_beg
6019 * | | | | +--------------> equal
6020 * | | | +----------------> att_end
6021 * | | +---------------------> att_beg
6022 * | +--------------------------> prev
6023 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024 */
6025
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006026 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6027 /* Iterate through all cookies on this line */
6028
6029 /* find att_beg */
6030 att_beg = prev + 1;
6031 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6032 att_beg++;
6033
6034 /* find att_end : this is the first character after the last non
6035 * space before the equal. It may be equal to hdr_end.
6036 */
6037 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006038
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006039 while (equal < hdr_end) {
6040 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006042 if (http_is_spht[(unsigned char)*equal++])
6043 continue;
6044 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006045 }
6046
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6048 * is between <att_beg> and <equal>, both may be identical.
6049 */
6050
6051 /* look for end of cookie if there is an equal sign */
6052 if (equal < hdr_end && *equal == '=') {
6053 /* look for the beginning of the value */
6054 val_beg = equal + 1;
6055 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6056 val_beg++;
6057
6058 /* find the end of the value, respecting quotes */
6059 next = find_cookie_value_end(val_beg, hdr_end);
6060
6061 /* make val_end point to the first white space or delimitor after the value */
6062 val_end = next;
6063 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6064 val_end--;
6065 } else {
6066 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006067 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006068
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006069 /* We have nothing to do with attributes beginning with '$'. However,
6070 * they will automatically be removed if a header before them is removed,
6071 * since they're supposed to be linked together.
6072 */
6073 if (*att_beg == '$')
6074 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006076 /* Ignore cookies with no equal sign */
6077 if (equal == next) {
6078 /* This is not our cookie, so we must preserve it. But if we already
6079 * scheduled another cookie for removal, we cannot remove the
6080 * complete header, but we can remove the previous block itself.
6081 */
6082 preserve_hdr = 1;
6083 if (del_from != NULL) {
6084 int delta = del_hdr_value(req, &del_from, prev);
6085 val_end += delta;
6086 next += delta;
6087 hdr_end += delta;
6088 hdr_next += delta;
6089 cur_hdr->len += delta;
6090 http_msg_move_end(&txn->req, delta);
6091 prev = del_from;
6092 del_from = NULL;
6093 }
6094 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006095 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006097 /* if there are spaces around the equal sign, we need to
6098 * strip them otherwise we'll get trouble for cookie captures,
6099 * or even for rewrites. Since this happens extremely rarely,
6100 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006102 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6103 int stripped_before = 0;
6104 int stripped_after = 0;
6105
6106 if (att_end != equal) {
6107 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6108 equal += stripped_before;
6109 val_beg += stripped_before;
6110 }
6111
6112 if (val_beg > equal + 1) {
6113 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6114 val_beg += stripped_after;
6115 stripped_before += stripped_after;
6116 }
6117
6118 val_end += stripped_before;
6119 next += stripped_before;
6120 hdr_end += stripped_before;
6121 hdr_next += stripped_before;
6122 cur_hdr->len += stripped_before;
6123 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 /* First, let's see if we want to capture this cookie. We check
6128 * that we don't already have a client side cookie, because we
6129 * can only capture one. Also as an optimisation, we ignore
6130 * cookies shorter than the declared name.
6131 */
6132 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6133 (val_end - att_beg >= t->fe->capture_namelen) &&
6134 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6135 int log_len = val_end - att_beg;
6136
6137 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6138 Alert("HTTP logging : out of memory.\n");
6139 } else {
6140 if (log_len > t->fe->capture_len)
6141 log_len = t->fe->capture_len;
6142 memcpy(txn->cli_cookie, att_beg, log_len);
6143 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006145 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006146
Willy Tarreaubca99692010-10-06 19:25:55 +02006147 /* Persistence cookies in passive, rewrite or insert mode have the
6148 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006149 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006150 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006151 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006152 * For cookies in prefix mode, the form is :
6153 *
6154 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 */
6156 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6157 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6158 struct server *srv = t->be->srv;
6159 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006160
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6162 * have the server ID between val_beg and delim, and the original cookie between
6163 * delim+1 and val_end. Otherwise, delim==val_end :
6164 *
6165 * Cookie: NAME=SRV; # in all but prefix modes
6166 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6167 * | || || | |+-> next
6168 * | || || | +--> val_end
6169 * | || || +---------> delim
6170 * | || |+------------> val_beg
6171 * | || +-------------> att_end = equal
6172 * | |+-----------------> att_beg
6173 * | +------------------> prev
6174 * +-------------------------> hdr_beg
6175 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006177 if (t->be->options & PR_O_COOK_PFX) {
6178 for (delim = val_beg; delim < val_end; delim++)
6179 if (*delim == COOKIE_DELIM)
6180 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006181 } else {
6182 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006183 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006184 /* Now check if the cookie contains a date field, which would
6185 * appear after a vertical bar ('|') just after the server name
6186 * and before the delimiter.
6187 */
6188 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6189 if (vbar1) {
6190 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006191 * right is the last seen date. It is a base64 encoded
6192 * 30-bit value representing the UNIX date since the
6193 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006194 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006195 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006196 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006197 if (val_end - vbar1 >= 5) {
6198 val = b64tos30(vbar1);
6199 if (val > 0)
6200 txn->cookie_last_date = val << 2;
6201 }
6202 /* look for a second vertical bar */
6203 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6204 if (vbar1 && (val_end - vbar1 > 5)) {
6205 val = b64tos30(vbar1 + 1);
6206 if (val > 0)
6207 txn->cookie_first_date = val << 2;
6208 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006209 }
6210 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211
Willy Tarreauf64d1412010-10-07 20:06:11 +02006212 /* if the cookie has an expiration date and the proxy wants to check
6213 * it, then we do that now. We first check if the cookie is too old,
6214 * then only if it has expired. We detect strict overflow because the
6215 * time resolution here is not great (4 seconds). Cookies with dates
6216 * in the future are ignored if their offset is beyond one day. This
6217 * allows an admin to fix timezone issues without expiring everyone
6218 * and at the same time avoids keeping unwanted side effects for too
6219 * long.
6220 */
6221 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006222 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6223 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006224 txn->flags &= ~TX_CK_MASK;
6225 txn->flags |= TX_CK_OLD;
6226 delim = val_beg; // let's pretend we have not found the cookie
6227 txn->cookie_first_date = 0;
6228 txn->cookie_last_date = 0;
6229 }
6230 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006231 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6232 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006233 txn->flags &= ~TX_CK_MASK;
6234 txn->flags |= TX_CK_EXPIRED;
6235 delim = val_beg; // let's pretend we have not found the cookie
6236 txn->cookie_first_date = 0;
6237 txn->cookie_last_date = 0;
6238 }
6239
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 /* Here, we'll look for the first running server which supports the cookie.
6241 * This allows to share a same cookie between several servers, for example
6242 * to dedicate backup servers to specific servers only.
6243 * However, to prevent clients from sticking to cookie-less backup server
6244 * when they have incidentely learned an empty cookie, we simply ignore
6245 * empty cookies and mark them as invalid.
6246 * The same behaviour is applied when persistence must be ignored.
6247 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006248 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 while (srv) {
6252 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6253 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6254 if ((srv->state & SRV_RUNNING) ||
6255 (t->be->options & PR_O_PERSIST) ||
6256 (t->flags & SN_FORCE_PRST)) {
6257 /* we found the server and we can use it */
6258 txn->flags &= ~TX_CK_MASK;
6259 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6260 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006261 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 break;
6263 } else {
6264 /* we found a server, but it's down,
6265 * mark it as such and go on in case
6266 * another one is available.
6267 */
6268 txn->flags &= ~TX_CK_MASK;
6269 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 srv = srv->next;
6273 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274
Willy Tarreauf64d1412010-10-07 20:06:11 +02006275 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006276 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006278 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6279 txn->flags |= TX_CK_UNUSED;
6280 else
6281 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 /* depending on the cookie mode, we may have to either :
6285 * - delete the complete cookie if we're in insert+indirect mode, so that
6286 * the server never sees it ;
6287 * - remove the server id from the cookie value, and tag the cookie as an
6288 * application cookie so that it does not get accidentely removed later,
6289 * if we're in cookie prefix mode
6290 */
6291 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6292 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006293
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006294 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6295 val_end += delta;
6296 next += delta;
6297 hdr_end += delta;
6298 hdr_next += delta;
6299 cur_hdr->len += delta;
6300 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 del_from = NULL;
6303 preserve_hdr = 1; /* we want to keep this cookie */
6304 }
6305 else if (del_from == NULL &&
6306 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6307 del_from = prev;
6308 }
6309 } else {
6310 /* This is not our cookie, so we must preserve it. But if we already
6311 * scheduled another cookie for removal, we cannot remove the
6312 * complete header, but we can remove the previous block itself.
6313 */
6314 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 if (del_from != NULL) {
6317 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006318 if (att_beg >= del_from)
6319 att_beg += delta;
6320 if (att_end >= del_from)
6321 att_end += delta;
6322 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 val_end += delta;
6324 next += delta;
6325 hdr_end += delta;
6326 hdr_next += delta;
6327 cur_hdr->len += delta;
6328 http_msg_move_end(&txn->req, delta);
6329 prev = del_from;
6330 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 /* Look for the appsession cookie unless persistence must be ignored */
6335 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6336 int cmp_len, value_len;
6337 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006338
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 if (t->be->options2 & PR_O2_AS_PFX) {
6340 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6341 value_begin = att_beg + t->be->appsession_name_len;
6342 value_len = val_end - att_beg - t->be->appsession_name_len;
6343 } else {
6344 cmp_len = att_end - att_beg;
6345 value_begin = val_beg;
6346 value_len = val_end - val_beg;
6347 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006348
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 /* let's see if the cookie is our appcookie */
6350 if (cmp_len == t->be->appsession_name_len &&
6351 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6352 manage_client_side_appsession(t, value_begin, value_len);
6353 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354 }
6355
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 /* continue with next cookie on this header line */
6357 att_beg = next;
6358 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 /* There are no more cookies on this line.
6361 * We may still have one (or several) marked for deletion at the
6362 * end of the line. We must do this now in two ways :
6363 * - if some cookies must be preserved, we only delete from the
6364 * mark to the end of line ;
6365 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006368 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 if (preserve_hdr) {
6370 delta = del_hdr_value(req, &del_from, hdr_end);
6371 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372 cur_hdr->len += delta;
6373 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375
6376 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006377 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6378 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006380 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006383 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384 }
6385
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006387 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006389}
6390
6391
Willy Tarreaua15645d2007-03-18 16:22:39 +01006392/* Iterate the same filter through all response headers contained in <rtr>.
6393 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6394 */
6395int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6396{
6397 char term;
6398 char *cur_ptr, *cur_end, *cur_next;
6399 int cur_idx, old_idx, last_hdr;
6400 struct http_txn *txn = &t->txn;
6401 struct hdr_idx_elem *cur_hdr;
6402 int len, delta;
6403
6404 last_hdr = 0;
6405
Willy Tarreau962c3f42010-01-10 00:15:35 +01006406 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006407 old_idx = 0;
6408
6409 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006410 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006411 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006412 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006413 (exp->action == ACT_ALLOW ||
6414 exp->action == ACT_DENY))
6415 return 0;
6416
6417 cur_idx = txn->hdr_idx.v[old_idx].next;
6418 if (!cur_idx)
6419 break;
6420
6421 cur_hdr = &txn->hdr_idx.v[cur_idx];
6422 cur_ptr = cur_next;
6423 cur_end = cur_ptr + cur_hdr->len;
6424 cur_next = cur_end + cur_hdr->cr + 1;
6425
6426 /* Now we have one header between cur_ptr and cur_end,
6427 * and the next header starts at cur_next.
6428 */
6429
6430 /* The annoying part is that pattern matching needs
6431 * that we modify the contents to null-terminate all
6432 * strings before testing them.
6433 */
6434
6435 term = *cur_end;
6436 *cur_end = '\0';
6437
6438 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6439 switch (exp->action) {
6440 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006441 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442 last_hdr = 1;
6443 break;
6444
6445 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006446 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006447 last_hdr = 1;
6448 break;
6449
6450 case ACT_REPLACE:
6451 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6452 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6453 /* FIXME: if the user adds a newline in the replacement, the
6454 * index will not be recalculated for now, and the new line
6455 * will not be counted as a new header.
6456 */
6457
6458 cur_end += delta;
6459 cur_next += delta;
6460 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006461 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006462 break;
6463
6464 case ACT_REMOVE:
6465 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6466 cur_next += delta;
6467
Willy Tarreaufa355d42009-11-29 18:12:29 +01006468 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6470 txn->hdr_idx.used--;
6471 cur_hdr->len = 0;
6472 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006473 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 break;
6475
6476 }
6477 }
6478 if (cur_end)
6479 *cur_end = term; /* restore the string terminator */
6480
6481 /* keep the link from this header to next one in case of later
6482 * removal of next header.
6483 */
6484 old_idx = cur_idx;
6485 }
6486 return 0;
6487}
6488
6489
6490/* Apply the filter to the status line in the response buffer <rtr>.
6491 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6492 * or -1 if a replacement resulted in an invalid status line.
6493 */
6494int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6495{
6496 char term;
6497 char *cur_ptr, *cur_end;
6498 int done;
6499 struct http_txn *txn = &t->txn;
6500 int len, delta;
6501
6502
Willy Tarreau3d300592007-03-18 18:34:41 +01006503 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006505 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006506 (exp->action == ACT_ALLOW ||
6507 exp->action == ACT_DENY))
6508 return 0;
6509 else if (exp->action == ACT_REMOVE)
6510 return 0;
6511
6512 done = 0;
6513
Willy Tarreau962c3f42010-01-10 00:15:35 +01006514 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006515 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516
6517 /* Now we have the status line between cur_ptr and cur_end */
6518
6519 /* The annoying part is that pattern matching needs
6520 * that we modify the contents to null-terminate all
6521 * strings before testing them.
6522 */
6523
6524 term = *cur_end;
6525 *cur_end = '\0';
6526
6527 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6528 switch (exp->action) {
6529 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006530 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531 done = 1;
6532 break;
6533
6534 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006535 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 done = 1;
6537 break;
6538
6539 case ACT_REPLACE:
6540 *cur_end = term; /* restore the string terminator */
6541 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6542 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6543 /* FIXME: if the user adds a newline in the replacement, the
6544 * index will not be recalculated for now, and the new line
6545 * will not be counted as a new header.
6546 */
6547
Willy Tarreaufa355d42009-11-29 18:12:29 +01006548 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006551 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006552 cur_ptr, cur_end + 1,
6553 NULL, NULL);
6554 if (unlikely(!cur_end))
6555 return -1;
6556
6557 /* we have a full respnse and we know that we have either a CR
6558 * or an LF at <ptr>.
6559 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006560 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006561 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 /* there is no point trying this regex on headers */
6563 return 1;
6564 }
6565 }
6566 *cur_end = term; /* restore the string terminator */
6567 return done;
6568}
6569
6570
6571
6572/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006573 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006574 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6575 * unparsable response.
6576 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006577int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006579 struct http_txn *txn = &s->txn;
6580 struct hdr_exp *exp;
6581
6582 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583 int ret;
6584
6585 /*
6586 * The interleaving of transformations and verdicts
6587 * makes it difficult to decide to continue or stop
6588 * the evaluation.
6589 */
6590
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006591 if (txn->flags & TX_SVDENY)
6592 break;
6593
Willy Tarreau3d300592007-03-18 18:34:41 +01006594 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6596 exp->action == ACT_PASS)) {
6597 exp = exp->next;
6598 continue;
6599 }
6600
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006601 /* if this filter had a condition, evaluate it now and skip to
6602 * next filter if the condition does not match.
6603 */
6604 if (exp->cond) {
6605 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6606 ret = acl_pass(ret);
6607 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6608 ret = !ret;
6609 if (!ret)
6610 continue;
6611 }
6612
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006614 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615 if (unlikely(ret < 0))
6616 return -1;
6617
6618 if (likely(ret == 0)) {
6619 /* The filter did not match the response, it can be
6620 * iterated through all headers.
6621 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006622 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624 }
6625 return 0;
6626}
6627
6628
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006630 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006631 * desirable to call it only when needed. This function is also used when we
6632 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635{
6636 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006637 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006638 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006640 char *hdr_beg, *hdr_end, *hdr_next;
6641 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643 /* Iterate through the headers.
6644 * we start with the start line.
6645 */
6646 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006647 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648
6649 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6650 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006651 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652
6653 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006654 hdr_beg = hdr_next;
6655 hdr_end = hdr_beg + cur_hdr->len;
6656 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657
Willy Tarreau24581ba2010-08-31 22:39:35 +02006658 /* We have one full header between hdr_beg and hdr_end, and the
6659 * next header starts at hdr_next. We're only interested in
6660 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006661 */
6662
Willy Tarreau24581ba2010-08-31 22:39:35 +02006663 is_cookie2 = 0;
6664 prev = hdr_beg + 10;
6665 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006666 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006667 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6668 if (!val) {
6669 old_idx = cur_idx;
6670 continue;
6671 }
6672 is_cookie2 = 1;
6673 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 }
6675
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6677 * <prev> points to the colon.
6678 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006679 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680
Willy Tarreau24581ba2010-08-31 22:39:35 +02006681 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6682 * check-cache is enabled) and we are not interested in checking
6683 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006684 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006685 if (t->be->cookie_name == NULL &&
6686 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006687 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 return;
6689
Willy Tarreau24581ba2010-08-31 22:39:35 +02006690 /* OK so now we know we have to process this response cookie.
6691 * The format of the Set-Cookie header is slightly different
6692 * from the format of the Cookie header in that it does not
6693 * support the comma as a cookie delimiter (thus the header
6694 * cannot be folded) because the Expires attribute described in
6695 * the original Netscape's spec may contain an unquoted date
6696 * with a comma inside. We have to live with this because
6697 * many browsers don't support Max-Age and some browsers don't
6698 * support quoted strings. However the Set-Cookie2 header is
6699 * clean.
6700 *
6701 * We have to keep multiple pointers in order to support cookie
6702 * removal at the beginning, middle or end of header without
6703 * corrupting the header (in case of set-cookie2). A special
6704 * pointer, <scav> points to the beginning of the set-cookie-av
6705 * fields after the first semi-colon. The <next> pointer points
6706 * either to the end of line (set-cookie) or next unquoted comma
6707 * (set-cookie2). All of these headers are valid :
6708 *
6709 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6710 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6711 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6712 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6713 * | | | | | | | | | |
6714 * | | | | | | | | +-> next hdr_end <--+
6715 * | | | | | | | +------------> scav
6716 * | | | | | | +--------------> val_end
6717 * | | | | | +--------------------> val_beg
6718 * | | | | +----------------------> equal
6719 * | | | +------------------------> att_end
6720 * | | +----------------------------> att_beg
6721 * | +------------------------------> prev
6722 * +-----------------------------------------> hdr_beg
6723 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006724
Willy Tarreau24581ba2010-08-31 22:39:35 +02006725 for (; prev < hdr_end; prev = next) {
6726 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727
Willy Tarreau24581ba2010-08-31 22:39:35 +02006728 /* find att_beg */
6729 att_beg = prev + 1;
6730 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6731 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 /* find att_end : this is the first character after the last non
6734 * space before the equal. It may be equal to hdr_end.
6735 */
6736 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreau24581ba2010-08-31 22:39:35 +02006738 while (equal < hdr_end) {
6739 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6740 break;
6741 if (http_is_spht[(unsigned char)*equal++])
6742 continue;
6743 att_end = equal;
6744 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6747 * is between <att_beg> and <equal>, both may be identical.
6748 */
6749
6750 /* look for end of cookie if there is an equal sign */
6751 if (equal < hdr_end && *equal == '=') {
6752 /* look for the beginning of the value */
6753 val_beg = equal + 1;
6754 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6755 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006756
Willy Tarreau24581ba2010-08-31 22:39:35 +02006757 /* find the end of the value, respecting quotes */
6758 next = find_cookie_value_end(val_beg, hdr_end);
6759
6760 /* make val_end point to the first white space or delimitor after the value */
6761 val_end = next;
6762 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6763 val_end--;
6764 } else {
6765 /* <equal> points to next comma, semi-colon or EOL */
6766 val_beg = val_end = next = equal;
6767 }
6768
6769 if (next < hdr_end) {
6770 /* Set-Cookie2 supports multiple cookies, and <next> points to
6771 * a colon or semi-colon before the end. So skip all attr-value
6772 * pairs and look for the next comma. For Set-Cookie, since
6773 * commas are permitted in values, skip to the end.
6774 */
6775 if (is_cookie2)
6776 next = find_hdr_value_end(next, hdr_end);
6777 else
6778 next = hdr_end;
6779 }
6780
6781 /* Now everything is as on the diagram above */
6782
6783 /* Ignore cookies with no equal sign */
6784 if (equal == val_end)
6785 continue;
6786
6787 /* If there are spaces around the equal sign, we need to
6788 * strip them otherwise we'll get trouble for cookie captures,
6789 * or even for rewrites. Since this happens extremely rarely,
6790 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006792 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6793 int stripped_before = 0;
6794 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795
Willy Tarreau24581ba2010-08-31 22:39:35 +02006796 if (att_end != equal) {
6797 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6798 equal += stripped_before;
6799 val_beg += stripped_before;
6800 }
6801
6802 if (val_beg > equal + 1) {
6803 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6804 val_beg += stripped_after;
6805 stripped_before += stripped_after;
6806 }
6807
6808 val_end += stripped_before;
6809 next += stripped_before;
6810 hdr_end += stripped_before;
6811 hdr_next += stripped_before;
6812 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006813 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006814 }
6815
6816 /* First, let's see if we want to capture this cookie. We check
6817 * that we don't already have a server side cookie, because we
6818 * can only capture one. Also as an optimisation, we ignore
6819 * cookies shorter than the declared name.
6820 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006821 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006822 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006823 (val_end - att_beg >= t->fe->capture_namelen) &&
6824 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6825 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006826 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006827 Alert("HTTP logging : out of memory.\n");
6828 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006829 else {
6830 if (log_len > t->fe->capture_len)
6831 log_len = t->fe->capture_len;
6832 memcpy(txn->srv_cookie, att_beg, log_len);
6833 txn->srv_cookie[log_len] = 0;
6834 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835 }
6836
Willy Tarreau827aee92011-03-10 16:55:02 +01006837 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 if (!(t->flags & SN_IGNORE_PRST) &&
6840 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6841 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006842 /* assume passive cookie by default */
6843 txn->flags &= ~TX_SCK_MASK;
6844 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845
6846 /* If the cookie is in insert mode on a known server, we'll delete
6847 * this occurrence because we'll insert another one later.
6848 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 * a direct access.
6850 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006851 if (t->be->options2 & PR_O2_COOK_PSV) {
6852 /* The "preserve" flag was set, we don't want to touch the
6853 * server's cookie.
6854 */
6855 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006856 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006857 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006858 /* this cookie must be deleted */
6859 if (*prev == ':' && next == hdr_end) {
6860 /* whole header */
6861 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6862 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6863 txn->hdr_idx.used--;
6864 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006865 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006866 hdr_next += delta;
6867 http_msg_move_end(&txn->rsp, delta);
6868 /* note: while both invalid now, <next> and <hdr_end>
6869 * are still equal, so the for() will stop as expected.
6870 */
6871 } else {
6872 /* just remove the value */
6873 int delta = del_hdr_value(res, &prev, next);
6874 next = prev;
6875 hdr_end += delta;
6876 hdr_next += delta;
6877 cur_hdr->len += delta;
6878 http_msg_move_end(&txn->rsp, delta);
6879 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006880 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006881 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006882 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006884 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886 * with this server since we know it.
6887 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006888 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
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 Tarreauf1348312010-10-07 15:54:11 +02006895 txn->flags &= ~TX_SCK_MASK;
6896 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006898 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006902 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 next += delta;
6904 hdr_end += delta;
6905 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006907 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908
Willy Tarreau827aee92011-03-10 16:55:02 +01006909 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006910 txn->flags &= ~TX_SCK_MASK;
6911 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 }
6913 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006914 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6915 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006916 int cmp_len, value_len;
6917 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918
Cyril Bontéb21570a2009-11-29 20:04:48 +01006919 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006920 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6921 value_begin = att_beg + t->be->appsession_name_len;
6922 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006923 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 cmp_len = att_end - att_beg;
6925 value_begin = val_beg;
6926 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006927 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006928
Cyril Bonté17530c32010-04-06 21:11:10 +02006929 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006930 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6931 /* free a possibly previously allocated memory */
6932 pool_free2(apools.sessid, txn->sessid);
6933
Cyril Bontéb21570a2009-11-29 20:04:48 +01006934 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006935 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006936 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6937 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6938 return;
6939 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006940 memcpy(txn->sessid, value_begin, value_len);
6941 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006942 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 }
6944 /* that's done for this cookie, check the next one on the same
6945 * line when next != hdr_end (only if is_cookie2).
6946 */
6947 }
6948 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006952 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006953 appsess *asession = NULL;
6954 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006955 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006956 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006957 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006958 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6959 Alert("Not enough Memory process_srv():asession:calloc().\n");
6960 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6961 return;
6962 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006963 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6964
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6966 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6967 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006968 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969 return;
6970 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006971 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006972 asession->sessid[t->be->appsession_len] = 0;
6973
Willy Tarreau827aee92011-03-10 16:55:02 +01006974 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006975 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006976 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006977 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006978 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006979 return;
6980 }
6981 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006982 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006983
6984 asession->request_count = 0;
6985 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6986 }
6987
6988 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6989 asession->request_count++;
6990 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991}
6992
6993
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994/*
6995 * Check if response is cacheable or not. Updates t->flags.
6996 */
6997void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6998{
6999 struct http_txn *txn = &t->txn;
7000 char *p1, *p2;
7001
7002 char *cur_ptr, *cur_end, *cur_next;
7003 int cur_idx;
7004
Willy Tarreau5df51872007-11-25 16:20:08 +01007005 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 return;
7007
7008 /* Iterate through the headers.
7009 * we start with the start line.
7010 */
7011 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007012 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
7014 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7015 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007016 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017
7018 cur_hdr = &txn->hdr_idx.v[cur_idx];
7019 cur_ptr = cur_next;
7020 cur_end = cur_ptr + cur_hdr->len;
7021 cur_next = cur_end + cur_hdr->cr + 1;
7022
7023 /* We have one full header between cur_ptr and cur_end, and the
7024 * next header starts at cur_next. We're only interested in
7025 * "Cookie:" headers.
7026 */
7027
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007028 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7029 if (val) {
7030 if ((cur_end - (cur_ptr + val) >= 8) &&
7031 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7032 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7033 return;
7034 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007035 }
7036
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007037 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7038 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007039 continue;
7040
7041 /* OK, right now we know we have a cache-control header at cur_ptr */
7042
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007043 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044
7045 if (p1 >= cur_end) /* no more info */
7046 continue;
7047
7048 /* p1 is at the beginning of the value */
7049 p2 = p1;
7050
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007051 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 p2++;
7053
7054 /* we have a complete value between p1 and p2 */
7055 if (p2 < cur_end && *p2 == '=') {
7056 /* we have something of the form no-cache="set-cookie" */
7057 if ((cur_end - p1 >= 21) &&
7058 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7059 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007060 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061 continue;
7062 }
7063
7064 /* OK, so we know that either p2 points to the end of string or to a comma */
7065 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7066 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7067 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7068 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007069 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007070 return;
7071 }
7072
7073 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007074 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007075 continue;
7076 }
7077 }
7078}
7079
7080
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081/*
7082 * Try to retrieve a known appsession in the URI, then the associated server.
7083 * If the server is found, it's assigned to the session.
7084 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007085void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007087 char *end_params, *first_param, *cur_param, *next_param;
7088 char separator;
7089 int value_len;
7090
7091 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007093 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007094 (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 +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 first_param = NULL;
7099 switch (mode) {
7100 case PR_O2_AS_M_PP:
7101 first_param = memchr(begin, ';', len);
7102 break;
7103 case PR_O2_AS_M_QS:
7104 first_param = memchr(begin, '?', len);
7105 break;
7106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
Cyril Bontéb21570a2009-11-29 20:04:48 +01007108 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111
Cyril Bontéb21570a2009-11-29 20:04:48 +01007112 switch (mode) {
7113 case PR_O2_AS_M_PP:
7114 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7115 end_params = (char *) begin + len;
7116 }
7117 separator = ';';
7118 break;
7119 case PR_O2_AS_M_QS:
7120 end_params = (char *) begin + len;
7121 separator = '&';
7122 break;
7123 default:
7124 /* unknown mode, shouldn't happen */
7125 return;
7126 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127
Cyril Bontéb21570a2009-11-29 20:04:48 +01007128 cur_param = next_param = end_params;
7129 while (cur_param > first_param) {
7130 cur_param--;
7131 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7132 /* let's see if this is the appsession parameter */
7133 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7134 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7135 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7136 /* Cool... it's the right one */
7137 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7138 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7139 if (value_len > 0) {
7140 manage_client_side_appsession(t, cur_param, value_len);
7141 }
7142 break;
7143 }
7144 next_param = cur_param;
7145 }
7146 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007148 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007149 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007150#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007151}
7152
Willy Tarreaub2513902006-12-17 14:52:38 +01007153/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007154 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007155 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007156 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007157 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007158 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007159 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007160 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007161 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007162int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007163{
7164 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007165 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007166
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007167 if (!uri_auth)
7168 return 0;
7169
Cyril Bonté70be45d2010-10-12 00:14:35 +02007170 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007171 return 0;
7172
Willy Tarreau295a8372011-03-10 11:25:07 +01007173 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007174 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007177 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007178 return 0;
7179
Willy Tarreau962c3f42010-01-10 00:15:35 +01007180 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007182 /* the URI is in h */
7183 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007184 return 0;
7185
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007186 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007187 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007188 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007189 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007190 break;
7191 }
7192 h++;
7193 }
7194
7195 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007196 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7197 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007198 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007199 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007200 break;
7201 }
7202 h++;
7203 }
7204 }
7205
Willy Tarreau962c3f42010-01-10 00:15:35 +01007206 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7207 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007208 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007209 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007210 break;
7211 }
7212 h++;
7213 }
7214
Cyril Bonté70be45d2010-10-12 00:14:35 +02007215 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7216 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7217 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007218 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007219 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007220 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7221 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7222 si->applet.ctx.stats.st_code = i;
7223 break;
7224 }
7225 }
7226 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007227 break;
7228 }
7229 h++;
7230 }
7231
Willy Tarreau295a8372011-03-10 11:25:07 +01007232 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007233
Willy Tarreaub2513902006-12-17 14:52:38 +01007234 return 1;
7235}
7236
Willy Tarreau4076a152009-04-02 15:18:36 +02007237/*
7238 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007239 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7240 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007241 */
7242void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7243 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007244 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007245{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007246 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7247 int len1 = buf->size - msg->som;
7248 es->len = buf->r - (buf->data + msg->som) + buf->size;
7249 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7250 if (es->len > len1 && len1 < sizeof(es->buf))
7251 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7252 }
7253 else {
7254 es->len = buf->r - (buf->data + msg->som);
7255 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7256 }
7257
Willy Tarreau4076a152009-04-02 15:18:36 +02007258 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007259 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007260 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007261 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007262 else
7263 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7264
Willy Tarreau4076a152009-04-02 15:18:36 +02007265 es->when = date; // user-visible date
7266 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007267 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007268 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007269 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007270 es->state = state;
7271 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007272 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007273}
Willy Tarreaub2513902006-12-17 14:52:38 +01007274
Willy Tarreau294c4732011-12-16 21:35:50 +01007275/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7276 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7277 * performed over the whole headers. Otherwise it must contain a valid header
7278 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7279 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7280 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7281 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7282 * -1.
7283 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007284 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007285unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7286 struct hdr_idx *idx, int occ,
7287 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007288{
Willy Tarreau294c4732011-12-16 21:35:50 +01007289 struct hdr_ctx local_ctx;
7290 char *ptr_hist[MAX_HDR_HISTORY];
7291 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007292 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007293 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007294
Willy Tarreau294c4732011-12-16 21:35:50 +01007295 if (!ctx) {
7296 local_ctx.idx = 0;
7297 ctx = &local_ctx;
7298 }
7299
Willy Tarreaubce70882009-09-07 11:51:47 +02007300 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007301 /* search from the beginning */
7302 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007303 occ--;
7304 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007305 *vptr = ctx->line + ctx->val;
7306 *vlen = ctx->vlen;
7307 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007308 }
7309 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007310 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007311 }
7312
7313 /* negative occurrence, we scan all the list then walk back */
7314 if (-occ > MAX_HDR_HISTORY)
7315 return 0;
7316
Willy Tarreau294c4732011-12-16 21:35:50 +01007317 found = hist_ptr = 0;
7318 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7319 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7320 len_hist[hist_ptr] = ctx->vlen;
7321 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007322 hist_ptr = 0;
7323 found++;
7324 }
7325 if (-occ > found)
7326 return 0;
7327 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7328 * find occurrence -occ, so we have to check [hist_ptr+occ].
7329 */
7330 hist_ptr += occ;
7331 if (hist_ptr >= MAX_HDR_HISTORY)
7332 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007333 *vptr = ptr_hist[hist_ptr];
7334 *vlen = len_hist[hist_ptr];
7335 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007336}
7337
Willy Tarreaubaaee002006-06-26 02:48:02 +02007338/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 * Print a debug line with a header
7340 */
7341void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7342{
7343 int len, max;
7344 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007345 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 max = end - start;
7347 UBOUND(max, sizeof(trash) - len - 1);
7348 len += strlcpy2(trash + len, start, max + 1);
7349 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007350 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007351}
7352
Willy Tarreau0937bc42009-12-22 15:03:09 +01007353/*
7354 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7355 * the required fields are properly allocated and that we only need to (re)init
7356 * them. This should be used before processing any new request.
7357 */
7358void http_init_txn(struct session *s)
7359{
7360 struct http_txn *txn = &s->txn;
7361 struct proxy *fe = s->fe;
7362
7363 txn->flags = 0;
7364 txn->status = -1;
7365
William Lallemand5f232402012-04-05 18:02:55 +02007366 global.req_count++;
7367
Willy Tarreauf64d1412010-10-07 20:06:11 +02007368 txn->cookie_first_date = 0;
7369 txn->cookie_last_date = 0;
7370
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007371 txn->req.flags = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007372 txn->req.sol = txn->req.eol = NULL;
7373 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007374 txn->rsp.flags = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007375 txn->rsp.sol = txn->rsp.eol = NULL;
7376 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007377 txn->req.chunk_len = 0LL;
7378 txn->req.body_len = 0LL;
7379 txn->rsp.chunk_len = 0LL;
7380 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007381 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7382 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007383
7384 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007385
7386 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7387 if (fe->options2 & PR_O2_REQBUG_OK)
7388 txn->req.err_pos = -1; /* let buggy requests pass */
7389
Willy Tarreau46023632010-01-07 22:51:47 +01007390 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007391 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7392
Willy Tarreau46023632010-01-07 22:51:47 +01007393 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007394 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7395
7396 if (txn->hdr_idx.v)
7397 hdr_idx_init(&txn->hdr_idx);
7398}
7399
7400/* to be used at the end of a transaction */
7401void http_end_txn(struct session *s)
7402{
7403 struct http_txn *txn = &s->txn;
7404
7405 /* these ones will have been dynamically allocated */
7406 pool_free2(pool2_requri, txn->uri);
7407 pool_free2(pool2_capture, txn->cli_cookie);
7408 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007409 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007410 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007411
William Lallemanda73203e2012-03-12 12:48:57 +01007412 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007413 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007414 txn->uri = NULL;
7415 txn->srv_cookie = NULL;
7416 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007417
7418 if (txn->req.cap) {
7419 struct cap_hdr *h;
7420 for (h = s->fe->req_cap; h; h = h->next)
7421 pool_free2(h->pool, txn->req.cap[h->index]);
7422 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7423 }
7424
7425 if (txn->rsp.cap) {
7426 struct cap_hdr *h;
7427 for (h = s->fe->rsp_cap; h; h = h->next)
7428 pool_free2(h->pool, txn->rsp.cap[h->index]);
7429 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7430 }
7431
Willy Tarreau0937bc42009-12-22 15:03:09 +01007432}
7433
7434/* to be used at the end of a transaction to prepare a new one */
7435void http_reset_txn(struct session *s)
7436{
7437 http_end_txn(s);
7438 http_init_txn(s);
7439
7440 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007441 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007442 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007443 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007444 /* re-init store persistence */
7445 s->store_count = 0;
7446
Willy Tarreau0937bc42009-12-22 15:03:09 +01007447 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007448
7449 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7450
Willy Tarreau739cfba2010-01-25 23:11:14 +01007451 /* We must trim any excess data from the response buffer, because we
7452 * may have blocked an invalid response from a server that we don't
7453 * want to accidentely forward once we disable the analysers, nor do
7454 * we want those data to come along with next response. A typical
7455 * example of such data would be from a buggy server responding to
7456 * a HEAD with some data, or sending more than the advertised
7457 * content-length.
7458 */
7459 if (unlikely(s->rep->l > s->rep->send_max)) {
7460 s->rep->l = s->rep->send_max;
7461 s->rep->r = s->rep->w + s->rep->l;
7462 if (s->rep->r >= s->rep->data + s->rep->size)
7463 s->rep->r -= s->rep->size;
7464 }
7465
Willy Tarreau0937bc42009-12-22 15:03:09 +01007466 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007467 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007468
Willy Tarreaud04e8582010-05-31 12:31:35 +02007469 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007470 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007471
7472 s->req->rex = TICK_ETERNITY;
7473 s->req->wex = TICK_ETERNITY;
7474 s->req->analyse_exp = TICK_ETERNITY;
7475 s->rep->rex = TICK_ETERNITY;
7476 s->rep->wex = TICK_ETERNITY;
7477 s->rep->analyse_exp = TICK_ETERNITY;
7478}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007479
Willy Tarreauff011f22011-01-06 17:51:27 +01007480void free_http_req_rules(struct list *r) {
7481 struct http_req_rule *tr, *pr;
7482
7483 list_for_each_entry_safe(pr, tr, r, list) {
7484 LIST_DEL(&pr->list);
7485 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7486 free(pr->http_auth.realm);
7487
7488 free(pr);
7489 }
7490}
7491
7492struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7493{
7494 struct http_req_rule *rule;
7495 int cur_arg;
7496
7497 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7498 if (!rule) {
7499 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7500 return NULL;
7501 }
7502
7503 if (!*args[0]) {
7504 goto req_error_parsing;
7505 } else if (!strcmp(args[0], "allow")) {
7506 rule->action = HTTP_REQ_ACT_ALLOW;
7507 cur_arg = 1;
7508 } else if (!strcmp(args[0], "deny")) {
7509 rule->action = HTTP_REQ_ACT_DENY;
7510 cur_arg = 1;
7511 } else if (!strcmp(args[0], "auth")) {
7512 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7513 cur_arg = 1;
7514
7515 while(*args[cur_arg]) {
7516 if (!strcmp(args[cur_arg], "realm")) {
7517 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7518 cur_arg+=2;
7519 continue;
7520 } else
7521 break;
7522 }
7523 } else {
7524req_error_parsing:
7525 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7526 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7527 return NULL;
7528 }
7529
7530 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7531 struct acl_cond *cond;
7532
7533 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7534 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7535 file, linenum, args[0]);
7536 return NULL;
7537 }
7538 rule->cond = cond;
7539 }
7540 else if (*args[cur_arg]) {
7541 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7542 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7543 file, linenum, args[0], args[cur_arg]);
7544 return NULL;
7545 }
7546
7547 return rule;
7548}
7549
Willy Tarreau8797c062007-05-07 00:55:35 +02007550/************************************************************************/
7551/* The code below is dedicated to ACL parsing and matching */
7552/************************************************************************/
7553
7554
7555
7556
7557/* 1. Check on METHOD
7558 * We use the pre-parsed method if it is known, and store its number as an
7559 * integer. If it is unknown, we use the pointer and the length.
7560 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007561static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007562{
7563 int len, meth;
7564
Willy Tarreauae8b7962007-06-09 23:10:04 +02007565 len = strlen(*text);
7566 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007567
7568 pattern->val.i = meth;
7569 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007570 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007571 if (!pattern->ptr.str)
7572 return 0;
7573 pattern->len = len;
7574 }
7575 return 1;
7576}
7577
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007578/* This function fetches the method of current HTTP request and stores
7579 * it in the global pattern struct as a chunk. There are two possibilities :
7580 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7581 * in <len> and <ptr> is NULL ;
7582 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7583 * <len> to its length.
7584 * This is intended to be used with acl_match_meth() only.
7585 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007586static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007587acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7588 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007589{
7590 int meth;
7591 struct http_txn *txn = l7;
7592
Willy Tarreaub6866442008-07-14 23:54:42 +02007593 if (!txn)
7594 return 0;
7595
Willy Tarreau655dce92009-11-08 13:10:58 +01007596 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007597 return 0;
7598
Willy Tarreau8797c062007-05-07 00:55:35 +02007599 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007600 temp_pattern.data.str.len = meth;
7601 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007602 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007603 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7604 /* ensure the indexes are not affected */
7605 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007606 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7607 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007608 }
7609 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7610 return 1;
7611}
7612
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007613/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007614static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7615{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007616 int icase;
7617
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007618
7619 if (temp_pattern.data.str.str == NULL) {
7620 /* well-known method */
7621 if (temp_pattern.data.str.len == pattern->val.i)
7622 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007623 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007624 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007625
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007626 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7627 if (pattern->val.i != HTTP_METH_OTHER)
7628 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007629
7630 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007631 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007632 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007633
7634 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007635 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7636 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007637 return ACL_PAT_FAIL;
7638 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007639}
7640
7641/* 2. Check on Request/Status Version
7642 * We simply compare strings here.
7643 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007644static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007645{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007646 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007647 if (!pattern->ptr.str)
7648 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007649 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007650 return 1;
7651}
7652
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007653static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007654acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7655 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007656{
7657 struct http_txn *txn = l7;
7658 char *ptr;
7659 int len;
7660
Willy Tarreaub6866442008-07-14 23:54:42 +02007661 if (!txn)
7662 return 0;
7663
Willy Tarreau655dce92009-11-08 13:10:58 +01007664 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007665 return 0;
7666
Willy Tarreau8797c062007-05-07 00:55:35 +02007667 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007668 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007669
7670 while ((len-- > 0) && (*ptr++ != '/'));
7671 if (len <= 0)
7672 return 0;
7673
Willy Tarreau664092c2011-12-16 19:11:42 +01007674 temp_pattern.data.str.str = ptr;
7675 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007676
7677 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7678 return 1;
7679}
7680
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007681static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007682acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7683 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007684{
7685 struct http_txn *txn = l7;
7686 char *ptr;
7687 int len;
7688
Willy Tarreaub6866442008-07-14 23:54:42 +02007689 if (!txn)
7690 return 0;
7691
Willy Tarreau655dce92009-11-08 13:10:58 +01007692 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007693 return 0;
7694
Willy Tarreau8797c062007-05-07 00:55:35 +02007695 len = txn->rsp.sl.st.v_l;
7696 ptr = txn->rsp.sol;
7697
7698 while ((len-- > 0) && (*ptr++ != '/'));
7699 if (len <= 0)
7700 return 0;
7701
Willy Tarreau664092c2011-12-16 19:11:42 +01007702 temp_pattern.data.str.str = ptr;
7703 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007704
7705 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7706 return 1;
7707}
7708
7709/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007710static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007711acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7712 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007713{
7714 struct http_txn *txn = l7;
7715 char *ptr;
7716 int len;
7717
Willy Tarreaub6866442008-07-14 23:54:42 +02007718 if (!txn)
7719 return 0;
7720
Willy Tarreau655dce92009-11-08 13:10:58 +01007721 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007722 return 0;
7723
Willy Tarreau8797c062007-05-07 00:55:35 +02007724 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007725 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007726
Willy Tarreaua5e37562011-12-16 17:06:15 +01007727 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007728 test->flags = ACL_TEST_F_VOL_1ST;
7729 return 1;
7730}
7731
7732/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007733static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007734acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7735 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007736{
7737 struct http_txn *txn = l7;
7738
Willy Tarreaub6866442008-07-14 23:54:42 +02007739 if (!txn)
7740 return 0;
7741
Willy Tarreau655dce92009-11-08 13:10:58 +01007742 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007743 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007744
Willy Tarreauc11416f2007-06-17 16:58:38 +02007745 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7746 /* ensure the indexes are not affected */
7747 return 0;
7748
Willy Tarreau664092c2011-12-16 19:11:42 +01007749 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7750 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007751
Willy Tarreauf3d25982007-05-08 22:45:09 +02007752 /* we do not need to set READ_ONLY because the data is in a buffer */
7753 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007754 return 1;
7755}
7756
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007757static int
7758acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7759 struct acl_expr *expr, struct acl_test *test)
7760{
7761 struct http_txn *txn = l7;
7762
Willy Tarreaub6866442008-07-14 23:54:42 +02007763 if (!txn)
7764 return 0;
7765
Willy Tarreau655dce92009-11-08 13:10:58 +01007766 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007767 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007768
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007769 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7770 /* ensure the indexes are not affected */
7771 return 0;
7772
7773 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007774 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 +01007775 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7776 return 0;
7777 temp_pattern.type = PATTERN_TYPE_IP;
7778 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007779
7780 /*
7781 * If we are parsing url in frontend space, we prepare backend stage
7782 * to not parse again the same url ! optimization lazyness...
7783 */
7784 if (px->options & PR_O_HTTP_PROXY)
7785 l4->flags |= SN_ADDR_SET;
7786
Willy Tarreauf4362b32011-12-16 17:49:52 +01007787 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007788 return 1;
7789}
7790
7791static int
7792acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7793 struct acl_expr *expr, struct acl_test *test)
7794{
7795 struct http_txn *txn = l7;
7796
Willy Tarreaub6866442008-07-14 23:54:42 +02007797 if (!txn)
7798 return 0;
7799
Willy Tarreau655dce92009-11-08 13:10:58 +01007800 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007801 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007802
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007803 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7804 /* ensure the indexes are not affected */
7805 return 0;
7806
7807 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007808 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 +01007809 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007810
7811 if (px->options & PR_O_HTTP_PROXY)
7812 l4->flags |= SN_ADDR_SET;
7813
7814 test->flags = ACL_TEST_F_READ_ONLY;
7815 return 1;
7816}
7817
Willy Tarreauc11416f2007-06-17 16:58:38 +02007818/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7819 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7820 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007821static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007822acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007823 struct acl_expr *expr, struct acl_test *test)
7824{
7825 struct http_txn *txn = l7;
7826 struct hdr_idx *idx = &txn->hdr_idx;
7827 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007828
Willy Tarreaub6866442008-07-14 23:54:42 +02007829 if (!txn)
7830 return 0;
7831
Willy Tarreau33a7e692007-06-10 19:45:56 +02007832 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7833 /* search for header from the beginning */
7834 ctx->idx = 0;
7835
Willy Tarreau33a7e692007-06-10 19:45:56 +02007836 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7837 test->flags |= ACL_TEST_F_FETCH_MORE;
7838 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007839 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7840 temp_pattern.data.str.len = ctx->vlen;
7841
Willy Tarreau33a7e692007-06-10 19:45:56 +02007842 return 1;
7843 }
7844
7845 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7846 test->flags |= ACL_TEST_F_VOL_HDR;
7847 return 0;
7848}
7849
Willy Tarreau33a7e692007-06-10 19:45:56 +02007850static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007851acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7852 struct acl_expr *expr, struct acl_test *test)
7853{
7854 struct http_txn *txn = l7;
7855
Willy Tarreaub6866442008-07-14 23:54:42 +02007856 if (!txn)
7857 return 0;
7858
Willy Tarreau655dce92009-11-08 13:10:58 +01007859 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007860 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007861
Willy Tarreauc11416f2007-06-17 16:58:38 +02007862 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7863 /* ensure the indexes are not affected */
7864 return 0;
7865
7866 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7867}
7868
7869static int
7870acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7871 struct acl_expr *expr, struct acl_test *test)
7872{
7873 struct http_txn *txn = l7;
7874
Willy Tarreaub6866442008-07-14 23:54:42 +02007875 if (!txn)
7876 return 0;
7877
Willy Tarreau655dce92009-11-08 13:10:58 +01007878 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007879 return 0;
7880
7881 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7882}
7883
7884/* 6. Check on HTTP header count. The number of occurrences is returned.
7885 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7886 */
7887static int
7888acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007889 struct acl_expr *expr, struct acl_test *test)
7890{
7891 struct http_txn *txn = l7;
7892 struct hdr_idx *idx = &txn->hdr_idx;
7893 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007894 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007895
Willy Tarreaub6866442008-07-14 23:54:42 +02007896 if (!txn)
7897 return 0;
7898
Willy Tarreau33a7e692007-06-10 19:45:56 +02007899 ctx.idx = 0;
7900 cnt = 0;
7901 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7902 cnt++;
7903
Willy Tarreaua5e37562011-12-16 17:06:15 +01007904 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007905 test->flags = ACL_TEST_F_VOL_HDR;
7906 return 1;
7907}
7908
Willy Tarreauc11416f2007-06-17 16:58:38 +02007909static int
7910acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7911 struct acl_expr *expr, struct acl_test *test)
7912{
7913 struct http_txn *txn = l7;
7914
Willy Tarreaub6866442008-07-14 23:54:42 +02007915 if (!txn)
7916 return 0;
7917
Willy Tarreau655dce92009-11-08 13:10:58 +01007918 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007919 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007920
Willy Tarreauc11416f2007-06-17 16:58:38 +02007921 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7922 /* ensure the indexes are not affected */
7923 return 0;
7924
7925 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7926}
7927
7928static int
7929acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7930 struct acl_expr *expr, struct acl_test *test)
7931{
7932 struct http_txn *txn = l7;
7933
Willy Tarreaub6866442008-07-14 23:54:42 +02007934 if (!txn)
7935 return 0;
7936
Willy Tarreau655dce92009-11-08 13:10:58 +01007937 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007938 return 0;
7939
7940 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7941}
7942
Willy Tarreau33a7e692007-06-10 19:45:56 +02007943/* 7. Check on HTTP header's integer value. The integer value is returned.
7944 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007945 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007946 */
7947static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007949 struct acl_expr *expr, struct acl_test *test)
7950{
7951 struct http_txn *txn = l7;
7952 struct hdr_idx *idx = &txn->hdr_idx;
7953 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007954
Willy Tarreaub6866442008-07-14 23:54:42 +02007955 if (!txn)
7956 return 0;
7957
Willy Tarreau33a7e692007-06-10 19:45:56 +02007958 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7959 /* search for header from the beginning */
7960 ctx->idx = 0;
7961
Willy Tarreau33a7e692007-06-10 19:45:56 +02007962 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7963 test->flags |= ACL_TEST_F_FETCH_MORE;
7964 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007965 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007966 return 1;
7967 }
7968
7969 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7970 test->flags |= ACL_TEST_F_VOL_HDR;
7971 return 0;
7972}
7973
Willy Tarreauc11416f2007-06-17 16:58:38 +02007974static int
7975acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7976 struct acl_expr *expr, struct acl_test *test)
7977{
7978 struct http_txn *txn = l7;
7979
Willy Tarreaub6866442008-07-14 23:54:42 +02007980 if (!txn)
7981 return 0;
7982
Willy Tarreau655dce92009-11-08 13:10:58 +01007983 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007984 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007985
Willy Tarreauc11416f2007-06-17 16:58:38 +02007986 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7987 /* ensure the indexes are not affected */
7988 return 0;
7989
7990 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7991}
7992
7993static int
7994acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7995 struct acl_expr *expr, struct acl_test *test)
7996{
7997 struct http_txn *txn = l7;
7998
Willy Tarreaub6866442008-07-14 23:54:42 +02007999 if (!txn)
8000 return 0;
8001
Willy Tarreau655dce92009-11-08 13:10:58 +01008002 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008003 return 0;
8004
8005 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8006}
8007
Willy Tarreau106f9792009-09-19 07:54:16 +02008008/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8009 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8010 */
8011static int
8012acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8013 struct acl_expr *expr, struct acl_test *test)
8014{
8015 struct http_txn *txn = l7;
8016 struct hdr_idx *idx = &txn->hdr_idx;
8017 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8018
8019 if (!txn)
8020 return 0;
8021
8022 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8023 /* search for header from the beginning */
8024 ctx->idx = 0;
8025
Willy Tarreauf4362b32011-12-16 17:49:52 +01008026 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008027 test->flags |= ACL_TEST_F_FETCH_MORE;
8028 test->flags |= ACL_TEST_F_VOL_HDR;
8029 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008030 temp_pattern.type = PATTERN_TYPE_IP;
8031 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8032 return 1;
8033 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008034 }
8035
8036 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8037 test->flags |= ACL_TEST_F_VOL_HDR;
8038 return 0;
8039}
8040
8041static int
8042acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8043 struct acl_expr *expr, struct acl_test *test)
8044{
8045 struct http_txn *txn = l7;
8046
8047 if (!txn)
8048 return 0;
8049
Willy Tarreau655dce92009-11-08 13:10:58 +01008050 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008051 return 0;
8052
8053 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8054 /* ensure the indexes are not affected */
8055 return 0;
8056
8057 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8058}
8059
8060static int
8061acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8062 struct acl_expr *expr, struct acl_test *test)
8063{
8064 struct http_txn *txn = l7;
8065
8066 if (!txn)
8067 return 0;
8068
Willy Tarreau655dce92009-11-08 13:10:58 +01008069 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008070 return 0;
8071
8072 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8073}
8074
Willy Tarreau737b0c12007-06-10 21:28:46 +02008075/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8076 * the first '/' after the possible hostname, and ends before the possible '?'.
8077 */
8078static int
8079acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8080 struct acl_expr *expr, struct acl_test *test)
8081{
8082 struct http_txn *txn = l7;
8083 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008084
Willy Tarreaub6866442008-07-14 23:54:42 +02008085 if (!txn)
8086 return 0;
8087
Willy Tarreau655dce92009-11-08 13:10:58 +01008088 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008089 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008090
Willy Tarreauc11416f2007-06-17 16:58:38 +02008091 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8092 /* ensure the indexes are not affected */
8093 return 0;
8094
Willy Tarreau962c3f42010-01-10 00:15:35 +01008095 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008096 ptr = http_get_path(txn);
8097 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008098 return 0;
8099
8100 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008101 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008102
8103 while (ptr < end && *ptr != '?')
8104 ptr++;
8105
Willy Tarreau664092c2011-12-16 19:11:42 +01008106 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008107
8108 /* we do not need to set READ_ONLY because the data is in a buffer */
8109 test->flags = ACL_TEST_F_VOL_1ST;
8110 return 1;
8111}
8112
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008113static int
8114acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8115 struct acl_expr *expr, struct acl_test *test)
8116{
8117 struct buffer *req = s->req;
8118 struct http_txn *txn = &s->txn;
8119 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008120
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008121 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8122 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8123 */
8124
8125 if (!s || !req)
8126 return 0;
8127
Willy Tarreau655dce92009-11-08 13:10:58 +01008128 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008129 /* Already decoded as OK */
8130 test->flags |= ACL_TEST_F_SET_RES_PASS;
8131 return 1;
8132 }
8133
8134 /* Try to decode HTTP request */
8135 if (likely(req->lr < req->r))
8136 http_msg_analyzer(req, msg, &txn->hdr_idx);
8137
Willy Tarreau655dce92009-11-08 13:10:58 +01008138 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008139 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8140 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8141 return 1;
8142 }
8143 /* wait for final state */
8144 test->flags |= ACL_TEST_F_MAY_CHANGE;
8145 return 0;
8146 }
8147
8148 /* OK we got a valid HTTP request. We have some minor preparation to
8149 * perform so that further checks can rely on HTTP tests.
8150 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008151 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008152 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8153 s->flags |= SN_REDIRECTABLE;
8154
8155 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8156 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8157 return 1;
8158 }
8159
8160 test->flags |= ACL_TEST_F_SET_RES_PASS;
8161 return 1;
8162}
8163
Willy Tarreau7f18e522010-10-22 20:04:13 +02008164/* return a valid test if the current request is the first one on the connection */
8165static int
8166acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8167 struct acl_expr *expr, struct acl_test *test)
8168{
8169 if (!s)
8170 return 0;
8171
8172 if (s->txn.flags & TX_NOT_FIRST)
8173 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8174 else
8175 test->flags |= ACL_TEST_F_SET_RES_PASS;
8176
8177 return 1;
8178}
8179
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008180static int
8181acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8182 struct acl_expr *expr, struct acl_test *test)
8183{
8184
8185 if (!s)
8186 return 0;
8187
8188 if (!get_http_auth(s))
8189 return 0;
8190
8191 test->ctx.a[0] = expr->arg.ul;
8192 test->ctx.a[1] = s->txn.auth.user;
8193 test->ctx.a[2] = s->txn.auth.pass;
8194
8195 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8196
8197 return 1;
8198}
Willy Tarreau8797c062007-05-07 00:55:35 +02008199
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008200/* Try to find the next occurrence of a cookie name in a cookie header value.
8201 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8202 * the cookie value is returned into *value and *value_l, and the function
8203 * returns a pointer to the next pointer to search from if the value was found.
8204 * Otherwise if the cookie was not found, NULL is returned and neither value
8205 * nor value_l are touched. The input <hdr> string should first point to the
8206 * header's value, and the <hdr_end> pointer must point to the first character
8207 * not part of the value. <list> must be non-zero if value may represent a list
8208 * of values (cookie headers). This makes it faster to abort parsing when no
8209 * list is expected.
8210 */
8211static char *
8212extract_cookie_value(char *hdr, const char *hdr_end,
8213 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008214 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008215{
8216 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8217 char *next;
8218
8219 /* we search at least a cookie name followed by an equal, and more
8220 * generally something like this :
8221 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8222 */
8223 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8224 /* Iterate through all cookies on this line */
8225
8226 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8227 att_beg++;
8228
8229 /* find att_end : this is the first character after the last non
8230 * space before the equal. It may be equal to hdr_end.
8231 */
8232 equal = att_end = att_beg;
8233
8234 while (equal < hdr_end) {
8235 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8236 break;
8237 if (http_is_spht[(unsigned char)*equal++])
8238 continue;
8239 att_end = equal;
8240 }
8241
8242 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8243 * is between <att_beg> and <equal>, both may be identical.
8244 */
8245
8246 /* look for end of cookie if there is an equal sign */
8247 if (equal < hdr_end && *equal == '=') {
8248 /* look for the beginning of the value */
8249 val_beg = equal + 1;
8250 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8251 val_beg++;
8252
8253 /* find the end of the value, respecting quotes */
8254 next = find_cookie_value_end(val_beg, hdr_end);
8255
8256 /* make val_end point to the first white space or delimitor after the value */
8257 val_end = next;
8258 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8259 val_end--;
8260 } else {
8261 val_beg = val_end = next = equal;
8262 }
8263
8264 /* We have nothing to do with attributes beginning with '$'. However,
8265 * they will automatically be removed if a header before them is removed,
8266 * since they're supposed to be linked together.
8267 */
8268 if (*att_beg == '$')
8269 continue;
8270
8271 /* Ignore cookies with no equal sign */
8272 if (equal == next)
8273 continue;
8274
8275 /* Now we have the cookie name between att_beg and att_end, and
8276 * its value between val_beg and val_end.
8277 */
8278
8279 if (att_end - att_beg == cookie_name_l &&
8280 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8281 /* let's return this value and indicate where to go on from */
8282 *value = val_beg;
8283 *value_l = val_end - val_beg;
8284 return next + 1;
8285 }
8286
8287 /* Set-Cookie headers only have the name in the first attr=value part */
8288 if (!list)
8289 break;
8290 }
8291
8292 return NULL;
8293}
8294
8295/* Iterate over all cookies present in a request. The context is stored in
8296 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8297 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8298 * non-null, then multiple cookies may be parsed on the same line.
8299 * The cookie name is in expr->arg and the name length in expr->arg_len.
8300 */
8301static int
8302acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8303 const char *hdr_name, int hdr_name_len, int multi,
8304 struct acl_expr *expr, struct acl_test *test)
8305{
8306 struct http_txn *txn = l7;
8307 struct hdr_idx *idx = &txn->hdr_idx;
8308 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8309
8310 if (!txn)
8311 return 0;
8312
8313 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8314 /* search for the header from the beginning, we must first initialize
8315 * the search parameters.
8316 */
8317 test->ctx.a[0] = NULL;
8318 ctx->idx = 0;
8319 }
8320
8321 while (1) {
8322 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8323 if (!test->ctx.a[0]) {
8324 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8325 goto out;
8326
8327 if (ctx->vlen < expr->arg_len + 1)
8328 continue;
8329
8330 test->ctx.a[0] = ctx->line + ctx->val;
8331 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8332 }
8333
8334 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8335 expr->arg.str, expr->arg_len, multi,
8336 &temp_pattern.data.str.str,
8337 &temp_pattern.data.str.len);
8338 if (test->ctx.a[0]) {
8339 /* one value was returned into temp_pattern.data.str.{str,len} */
8340 test->flags |= ACL_TEST_F_FETCH_MORE;
8341 test->flags |= ACL_TEST_F_VOL_HDR;
8342 return 1;
8343 }
8344 }
8345
8346 out:
8347 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8348 test->flags |= ACL_TEST_F_VOL_HDR;
8349 return 0;
8350}
8351
8352static int
8353acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8354 struct acl_expr *expr, struct acl_test *test)
8355{
8356 struct http_txn *txn = l7;
8357
8358 if (!txn)
8359 return 0;
8360
8361 if (txn->req.msg_state < HTTP_MSG_BODY)
8362 return 0;
8363
8364 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8365 /* ensure the indexes are not affected */
8366 return 0;
8367
8368 /* The Cookie header allows multiple cookies on the same line */
8369 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8370}
8371
8372static int
8373acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8374 struct acl_expr *expr, struct acl_test *test)
8375{
8376 struct http_txn *txn = l7;
8377
8378 if (!txn)
8379 return 0;
8380
8381 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8382 return 0;
8383
8384 /* The Set-Cookie header allows only one cookie on the same line */
8385 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8386}
8387
8388/* Iterate over all cookies present in a request to count how many occurrences
8389 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8390 * multiple cookies may be parsed on the same line.
8391 */
8392static int
8393acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8394 const char *hdr_name, int hdr_name_len, int multi,
8395 struct acl_expr *expr, struct acl_test *test)
8396{
8397 struct http_txn *txn = l7;
8398 struct hdr_idx *idx = &txn->hdr_idx;
8399 struct hdr_ctx ctx;
8400 int cnt;
8401 char *val_beg, *val_end;
8402
8403 if (!txn)
8404 return 0;
8405
Willy Tarreau46787ed2012-04-11 17:28:40 +02008406 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008407 ctx.idx = 0;
8408 cnt = 0;
8409
8410 while (1) {
8411 /* Note: val_beg == NULL every time we need to fetch a new header */
8412 if (!val_beg) {
8413 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8414 break;
8415
8416 if (ctx.vlen < expr->arg_len + 1)
8417 continue;
8418
8419 val_beg = ctx.line + ctx.val;
8420 val_end = val_beg + ctx.vlen;
8421 }
8422
8423 while ((val_beg = extract_cookie_value(val_beg, val_end,
8424 expr->arg.str, expr->arg_len, multi,
8425 &temp_pattern.data.str.str,
8426 &temp_pattern.data.str.len))) {
8427 cnt++;
8428 }
8429 }
8430
8431 temp_pattern.data.integer = cnt;
8432 test->flags |= ACL_TEST_F_VOL_HDR;
8433 return 1;
8434}
8435
8436static int
8437acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8438 struct acl_expr *expr, struct acl_test *test)
8439{
8440 struct http_txn *txn = l7;
8441
8442 if (!txn)
8443 return 0;
8444
8445 if (txn->req.msg_state < HTTP_MSG_BODY)
8446 return 0;
8447
8448 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8449 /* ensure the indexes are not affected */
8450 return 0;
8451
8452 /* The Cookie header allows multiple cookies on the same line */
8453 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.sol, "Cookie", 6, 1, expr, test);
8454}
8455
8456static int
8457acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8458 struct acl_expr *expr, struct acl_test *test)
8459{
8460 struct http_txn *txn = l7;
8461
8462 if (!txn)
8463 return 0;
8464
8465 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8466 return 0;
8467
8468 /* The Set-Cookie header allows only one cookie on the same line */
8469 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
8470}
8471
Willy Tarreau8797c062007-05-07 00:55:35 +02008472/************************************************************************/
8473/* All supported keywords must be declared here. */
8474/************************************************************************/
8475
8476/* Note: must not be declared <const> as its list will be overwritten */
8477static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008478 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8479
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008480 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008481 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8482 { "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 +02008483 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008484
Willy Tarreauc4262962010-05-10 23:42:40 +02008485 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008486 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8487 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8488 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8489 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8490 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8491 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008492 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008493 { "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 +02008494 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008495
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008496 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008497 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008498 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8499 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8500 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8501 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8502 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8503 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8504 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008505 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008506 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008507 { "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 +02008508
Willy Tarreauc4262962010-05-10 23:42:40 +02008509 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008510 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8511 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8512 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8513 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8514 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8515 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8516 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008517 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008518 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008519 { "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 +02008520
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008521 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8522 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8523 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8524 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8525 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8526 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8527 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8528 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8529 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8530
8531 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8532 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8533 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8534 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8535 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8536 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8537 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8538 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8539 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8540
Willy Tarreauc4262962010-05-10 23:42:40 +02008541 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008542 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8543 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8544 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8545 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8546 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8547 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008548 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008549
Willy Tarreau7f18e522010-10-22 20:04:13 +02008550 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8551 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8552 { "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 +02008553 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008554}};
8555
Willy Tarreau4a568972010-05-12 08:08:50 +02008556/************************************************************************/
8557/* The code below is dedicated to pattern fetching and matching */
8558/************************************************************************/
8559
Willy Tarreaue428fb72011-12-16 21:50:30 +01008560/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008561static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008562pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8563 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008564{
8565 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008566
Willy Tarreaue428fb72011-12-16 21:50:30 +01008567 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8568 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008569}
8570
David Cournapeau16023ee2010-12-23 20:55:41 +09008571/*
8572 * Given a path string and its length, find the position of beginning of the
8573 * query string. Returns NULL if no query string is found in the path.
8574 *
8575 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8576 *
8577 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8578 */
8579static inline char *find_query_string(char *path, size_t path_l)
8580{
8581 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008582
David Cournapeau16023ee2010-12-23 20:55:41 +09008583 p = memchr(path, '?', path_l);
8584 return p ? p + 1 : NULL;
8585}
8586
8587static inline int is_param_delimiter(char c)
8588{
8589 return c == '&' || c == ';';
8590}
8591
8592/*
8593 * Given a url parameter, find the starting position of the first occurence,
8594 * or NULL if the parameter is not found.
8595 *
8596 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8597 * the function will return query_string+8.
8598 */
8599static char*
8600find_url_param_pos(char* query_string, size_t query_string_l,
8601 char* url_param_name, size_t url_param_name_l)
8602{
8603 char *pos, *last;
8604
8605 pos = query_string;
8606 last = query_string + query_string_l - url_param_name_l - 1;
8607
8608 while (pos <= last) {
8609 if (pos[url_param_name_l] == '=') {
8610 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8611 return pos;
8612 pos += url_param_name_l + 1;
8613 }
8614 while (pos <= last && !is_param_delimiter(*pos))
8615 pos++;
8616 pos++;
8617 }
8618 return NULL;
8619}
8620
8621/*
8622 * Given a url parameter name, returns its value and size into *value and
8623 * *value_l respectively, and returns non-zero. If the parameter is not found,
8624 * zero is returned and value/value_l are not touched.
8625 */
8626static int
8627find_url_param_value(char* path, size_t path_l,
8628 char* url_param_name, size_t url_param_name_l,
8629 char** value, size_t* value_l)
8630{
8631 char *query_string, *qs_end;
8632 char *arg_start;
8633 char *value_start, *value_end;
8634
8635 query_string = find_query_string(path, path_l);
8636 if (!query_string)
8637 return 0;
8638
8639 qs_end = path + path_l;
8640 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8641 url_param_name, url_param_name_l);
8642 if (!arg_start)
8643 return 0;
8644
8645 value_start = arg_start + url_param_name_l + 1;
8646 value_end = value_start;
8647
8648 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8649 value_end++;
8650
8651 *value = value_start;
8652 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008653 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008654}
8655
8656static int
8657pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8658 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8659{
8660 struct http_txn *txn = l7;
8661 struct http_msg *msg = &txn->req;
8662 char *url_param_value;
8663 size_t url_param_value_l;
8664
8665 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8666 arg_p->data.str.str, arg_p->data.str.len,
8667 &url_param_value, &url_param_value_l))
8668 return 0;
8669
8670 data->str.str = url_param_value;
8671 data->str.len = url_param_value_l;
8672 return 1;
8673}
8674
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008675/* Try to find in request or response message is in <msg> and whose transaction
8676 * is in <txn> the last occurrence of a cookie name in all cookie header values
8677 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8678 * pointer and size of the last occurrence of the cookie value is returned into
8679 * <value> and <value_l>, and the function returns non-zero if the value was
8680 * found. Otherwise if the cookie was not found, zero is returned and neither
8681 * value nor value_l are touched. The input hdr string should begin at the
8682 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8683 * value may represent a list of values (cookie headers).
8684 */
8685
8686static int
8687find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8688 const char *hdr_name, int hdr_name_len,
8689 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008690 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008691{
8692 struct hdr_ctx ctx;
8693 int found = 0;
8694
8695 ctx.idx = 0;
8696 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008697 char *hdr, *end;
8698
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008699 if (ctx.vlen < cookie_name_l + 1)
8700 continue;
8701
Willy Tarreau4573af92012-04-06 18:20:06 +02008702 hdr = ctx.line + ctx.val;
8703 end = hdr + ctx.vlen;
8704 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8705 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008706 }
8707 return found;
8708}
8709
8710static int
8711pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8712 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8713{
8714 struct http_txn *txn = l7;
8715 struct http_msg *msg = &txn->req;
8716 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008717 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008718 int found = 0;
8719
8720 found = find_cookie_value(msg, txn, "Cookie", 6,
8721 arg_p->data.str.str, arg_p->data.str.len, 1,
8722 &cookie_value, &cookie_value_l);
8723 if (found) {
8724 data->str.str = cookie_value;
8725 data->str.len = cookie_value_l;
8726 }
8727
8728 return found;
8729}
8730
8731
8732static int
8733pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8734 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8735{
8736 struct http_txn *txn = l7;
8737 struct http_msg *msg = &txn->rsp;
8738 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008739 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008740 int found = 0;
8741
8742 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8743 arg_p->data.str.str, arg_p->data.str.len, 1,
8744 &cookie_value, &cookie_value_l);
8745 if (found) {
8746 data->str.str = cookie_value;
8747 data->str.len = cookie_value_l;
8748 }
8749
8750 return found;
8751}
8752
Emeric Brun485479d2010-09-23 18:02:19 +02008753
Willy Tarreau4a568972010-05-12 08:08:50 +02008754/************************************************************************/
8755/* All supported keywords must be declared here. */
8756/************************************************************************/
8757/* Note: must not be declared <const> as its list will be overwritten */
8758static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008759 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008760 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008761 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8762 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008763 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008764}};
8765
Willy Tarreau8797c062007-05-07 00:55:35 +02008766
8767__attribute__((constructor))
8768static void __http_protocol_init(void)
8769{
8770 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008771 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008772}
8773
8774
Willy Tarreau58f10d72006-12-04 02:26:12 +01008775/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008776 * Local variables:
8777 * c-indent-level: 8
8778 * c-basic-offset: 8
8779 * End:
8780 */