blob: ce462e101e74ef6740a9b9d40e9040ed5d1c5064 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020053#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010055#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020056#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010058#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020060#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010061#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010063#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/task.h>
65
Willy Tarreau522d6c02009-12-06 18:49:18 +010066const char HTTP_100[] =
67 "HTTP/1.1 100 Continue\r\n\r\n";
68
69const struct chunk http_100_chunk = {
70 .str = (char *)&HTTP_100,
71 .len = sizeof(HTTP_100)-1
72};
73
Willy Tarreaua9679ac2010-01-03 17:32:57 +010074/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020075const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010076 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
95const char *HTTP_401_fmt =
96 "HTTP/1.0 401 Unauthorized\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020099 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
101 "\r\n"
102 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
103
Willy Tarreau844a7e72010-01-31 21:46:18 +0100104const char *HTTP_407_fmt =
105 "HTTP/1.0 407 Unauthorized\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Connection: close\r\n"
108 "Content-Type: text/html\r\n"
109 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
110 "\r\n"
111 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
112
Willy Tarreau0f772532006-12-23 20:51:41 +0100113
114const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200115 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200126 [HTTP_ERR_200] =
127 "HTTP/1.0 200 OK\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
133
Willy Tarreau0f772532006-12-23 20:51:41 +0100134 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100135 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
141
142 [HTTP_ERR_403] =
143 "HTTP/1.0 403 Forbidden\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
149
150 [HTTP_ERR_408] =
151 "HTTP/1.0 408 Request Time-out\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
157
158 [HTTP_ERR_500] =
159 "HTTP/1.0 500 Server Error\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
165
166 [HTTP_ERR_502] =
167 "HTTP/1.0 502 Bad Gateway\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
173
174 [HTTP_ERR_503] =
175 "HTTP/1.0 503 Service Unavailable\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
181
182 [HTTP_ERR_504] =
183 "HTTP/1.0 504 Gateway Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
189
190};
191
Cyril Bonté19979e12012-04-04 12:57:21 +0200192/* status codes available for the stats admin page (strictly 4 chars length) */
193const char *stat_status_codes[STAT_STATUS_SIZE] = {
194 [STAT_STATUS_DENY] = "DENY",
195 [STAT_STATUS_DONE] = "DONE",
196 [STAT_STATUS_ERRP] = "ERRP",
197 [STAT_STATUS_EXCD] = "EXCD",
198 [STAT_STATUS_NONE] = "NONE",
199 [STAT_STATUS_PART] = "PART",
200 [STAT_STATUS_UNKN] = "UNKN",
201};
202
203
Willy Tarreau80587432006-12-24 17:47:20 +0100204/* We must put the messages here since GCC cannot initialize consts depending
205 * on strlen().
206 */
207struct chunk http_err_chunks[HTTP_ERR_SIZE];
208
Willy Tarreau42250582007-04-01 01:30:43 +0200209#define FD_SETS_ARE_BITFIELDS
210#ifdef FD_SETS_ARE_BITFIELDS
211/*
212 * This map is used with all the FD_* macros to check whether a particular bit
213 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
214 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
215 * byte should be encoded. Be careful to always pass bytes from 0 to 255
216 * exclusively to the macros.
217 */
218fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220
221#else
222#error "Check if your OS uses bitfields for fd_sets"
223#endif
224
Willy Tarreau80587432006-12-24 17:47:20 +0100225void init_proto_http()
226{
Willy Tarreau42250582007-04-01 01:30:43 +0200227 int i;
228 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100229 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200230
Willy Tarreau80587432006-12-24 17:47:20 +0100231 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
232 if (!http_err_msgs[msg]) {
233 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
234 abort();
235 }
236
237 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
238 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
239 }
Willy Tarreau42250582007-04-01 01:30:43 +0200240
241 /* initialize the log header encoding map : '{|}"#' should be encoded with
242 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
243 * URL encoding only requires '"', '#' to be encoded as well as non-
244 * printable characters above.
245 */
246 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
247 memset(url_encode_map, 0, sizeof(url_encode_map));
248 for (i = 0; i < 32; i++) {
249 FD_SET(i, hdr_encode_map);
250 FD_SET(i, url_encode_map);
251 }
252 for (i = 127; i < 256; i++) {
253 FD_SET(i, hdr_encode_map);
254 FD_SET(i, url_encode_map);
255 }
256
257 tmp = "\"#{|}";
258 while (*tmp) {
259 FD_SET(*tmp, hdr_encode_map);
260 tmp++;
261 }
262
263 tmp = "\"#";
264 while (*tmp) {
265 FD_SET(*tmp, url_encode_map);
266 tmp++;
267 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200268
269 /* memory allocations */
270 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200271 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
392 int size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200393 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100394 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100395 line,
396 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +0200397 s->req->buf.data, s->req->buf.size, s->req->l, s->req->w, s->req->r, s->req->buf.p, s->req->buf.o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398 write(-1, trash, size);
399 size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200400 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100401 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100402 line,
403 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +0200404 s->rep->buf.data, s->rep->buf.size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf.p, s->rep->buf.o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405
406 write(-1, trash, size);
407}
408#else
409#define http_silent_debug(l,s) do { } while (0)
410#endif
411
412/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100413 * Adds a header and its CRLF at the tail of the message's buffer, just before
414 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100415 * The header is also automatically added to the index <hdr_idx>, and the end
416 * of headers is automatically adjusted. The number of bytes added is returned
417 * on success, otherwise <0 is returned indicating an error.
418 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100419int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100420{
421 int bytes, len;
422
423 len = strlen(text);
Willy Tarreauaf819352012-08-27 22:08:00 +0200424 bytes = buffer_insert_line2(&msg->buf->buf, msg->buf->buf.p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100425 if (!bytes)
426 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100427 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100428 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
429}
430
431/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100432 * Adds a header and its CRLF at the tail of the message's buffer, just before
433 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434 * the buffer is only opened and the space reserved, but nothing is copied.
435 * The header is also automatically added to the index <hdr_idx>, and the end
436 * of headers is automatically adjusted. The number of bytes added is returned
437 * on success, otherwise <0 is returned indicating an error.
438 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100439int http_header_add_tail2(struct http_msg *msg,
440 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100441{
442 int bytes;
443
Willy Tarreauaf819352012-08-27 22:08:00 +0200444 bytes = buffer_insert_line2(&msg->buf->buf, msg->buf->buf.p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100445 if (!bytes)
446 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100447 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
449}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450
451/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100452 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
453 * If so, returns the position of the first non-space character relative to
454 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
455 * to return a pointer to the place after the first space. Returns 0 if the
456 * header name does not match. Checks are case-insensitive.
457 */
458int http_header_match2(const char *hdr, const char *end,
459 const char *name, int len)
460{
461 const char *val;
462
463 if (hdr + len >= end)
464 return 0;
465 if (hdr[len] != ':')
466 return 0;
467 if (strncasecmp(hdr, name, len) != 0)
468 return 0;
469 val = hdr + len + 1;
470 while (val < end && HTTP_IS_SPHT(*val))
471 val++;
472 if ((val >= end) && (len + 2 <= end - hdr))
473 return len + 2; /* we may replace starting from second space */
474 return val - hdr;
475}
476
Willy Tarreau68085d82010-01-18 14:54:04 +0100477/* Find the end of the header value contained between <s> and <e>. See RFC2616,
478 * par 2.2 for more information. Note that it requires a valid header to return
479 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100481char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482{
483 int quoted, qdpair;
484
485 quoted = qdpair = 0;
486 for (; s < e; s++) {
487 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200488 else if (quoted) {
489 if (*s == '\\') qdpair = 1;
490 else if (*s == '"') quoted = 0;
491 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 else if (*s == '"') quoted = 1;
493 else if (*s == ',') return s;
494 }
495 return s;
496}
497
498/* Find the first or next occurrence of header <name> in message buffer <sol>
499 * using headers index <idx>, and return it in the <ctx> structure. This
500 * structure holds everything necessary to use the header and find next
501 * occurrence. If its <idx> member is 0, the header is searched from the
502 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 * 1 when it finds a value, and 0 when there is no more. It is designed to work
504 * with headers defined as comma-separated lists. As a special case, if ctx->val
505 * is NULL when searching for a new values of a header, the current header is
506 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 */
508int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
Willy Tarreau68085d82010-01-18 14:54:04 +0100512 char *eol, *sov;
513 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515 cur_idx = ctx->idx;
516 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 /* We have previously returned a value, let's search
518 * another one on the same line.
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200521 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100522 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200523 eol = sol + idx->v[cur_idx].len;
524
525 if (sov >= eol)
526 /* no more values in this header */
527 goto next_hdr;
528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 /* values remaining for this header, skip the comma but save it
530 * for later use (eg: for header deletion).
531 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 sov++;
533 while (sov < eol && http_is_lws[(unsigned char)*sov])
534 sov++;
535
536 goto return_hdr;
537 }
538
539 /* first request for this header */
540 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100541 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 while (cur_idx) {
544 eol = sol + idx->v[cur_idx].len;
545
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200546 if (len == 0) {
547 /* No argument was passed, we want any header.
548 * To achieve this, we simply build a fake request. */
549 while (sol + len < eol && sol[len] != ':')
550 len++;
551 name = sol;
552 }
553
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 if ((len < eol - sol) &&
555 (sol[len] == ':') &&
556 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100557 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200558 sov = sol + len + 1;
559 while (sov < eol && http_is_lws[(unsigned char)*sov])
560 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 ctx->prev = old_idx;
564 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 ctx->idx = cur_idx;
566 ctx->val = sov - sol;
567
568 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200570 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200571 eol--;
572 ctx->tws++;
573 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 ctx->vlen = eol - sov;
575 return 1;
576 }
577 next_hdr:
578 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100579 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 cur_idx = idx->v[cur_idx].next;
581 }
582 return 0;
583}
584
585int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100586 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200587 struct hdr_ctx *ctx)
588{
589 return http_find_header2(name, strlen(name), sol, idx, ctx);
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Remove one value of a header. This only works on a <ctx> returned by one of
593 * the http_find_header functions. The value is removed, as well as surrounding
594 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100595 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100596 * message <msg>. The new index is returned. If it is zero, it means there is
597 * no more header, so any processing may stop. The ctx is always left in a form
598 * that can be handled by http_find_header2() to find next occurrence.
599 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100600int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100601{
602 int cur_idx = ctx->idx;
603 char *sol = ctx->line;
604 struct hdr_idx_elem *hdr;
605 int delta, skip_comma;
606
607 if (!cur_idx)
608 return 0;
609
610 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauaf819352012-08-27 22:08:00 +0200613 delta = buffer_replace2(&msg->buf->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100614 http_msg_move_end(msg, delta);
615 idx->used--;
616 hdr->len = 0; /* unused entry */
617 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100618 if (idx->tail == ctx->idx)
619 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 ctx->idx = ctx->prev; /* walk back to the end of previous header */
621 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
622 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200623 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 return ctx->idx;
625 }
626
627 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200628 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
629 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 */
631
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauaf819352012-08-27 22:08:00 +0200633 delta = buffer_replace2(&msg->buf->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 NULL, 0);
636 hdr->len += delta;
637 http_msg_move_end(msg, delta);
638 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200639 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 return ctx->idx;
641}
642
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100643/* This function handles a server error at the stream interface level. The
644 * stream interface is assumed to be already in a closed state. An optional
645 * message is copied into the input buffer, and an HTTP status code stored.
646 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100647 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100649static void http_server_error(struct session *t, struct stream_interface *si,
650 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100652 buffer_auto_read(si->ob);
653 buffer_abort(si->ob);
654 buffer_auto_close(si->ob);
655 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200656 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100657 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100658 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100659 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200660 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200661 }
662 if (!(t->flags & SN_ERR_MASK))
663 t->flags |= err;
664 if (!(t->flags & SN_FINST_MASK))
665 t->flags |= finst;
666}
667
Willy Tarreau80587432006-12-24 17:47:20 +0100668/* This function returns the appropriate error location for the given session
669 * and message.
670 */
671
672struct chunk *error_message(struct session *s, int msgnum)
673{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200674 if (s->be->errmsg[msgnum].str)
675 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100676 else if (s->fe->errmsg[msgnum].str)
677 return &s->fe->errmsg[msgnum];
678 else
679 return &http_err_chunks[msgnum];
680}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682/*
683 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
684 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
685 */
686static http_meth_t find_http_meth(const char *str, const int len)
687{
688 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100689 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100690
691 m = ((unsigned)*str - 'A');
692
693 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100694 for (h = http_methods[m]; h->len > 0; h++) {
695 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100697 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 };
700 return HTTP_METH_OTHER;
701 }
702 return HTTP_METH_NONE;
703
704}
705
Willy Tarreau21d2af32008-02-14 20:25:24 +0100706/* Parse the URI from the given transaction (which is assumed to be in request
707 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
708 * It is returned otherwise.
709 */
710static char *
711http_get_path(struct http_txn *txn)
712{
713 char *ptr, *end;
714
Willy Tarreau572bf902012-07-02 17:01:20 +0200715 ptr = txn->req.buf->buf.p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100716 end = ptr + txn->req.sl.rq.u_l;
717
718 if (ptr >= end)
719 return NULL;
720
721 /* RFC2616, par. 5.1.2 :
722 * Request-URI = "*" | absuri | abspath | authority
723 */
724
725 if (*ptr == '*')
726 return NULL;
727
728 if (isalpha((unsigned char)*ptr)) {
729 /* this is a scheme as described by RFC3986, par. 3.1 */
730 ptr++;
731 while (ptr < end &&
732 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
733 ptr++;
734 /* skip '://' */
735 if (ptr == end || *ptr++ != ':')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 if (ptr == end || *ptr++ != '/')
740 return NULL;
741 }
742 /* skip [user[:passwd]@]host[:[port]] */
743
744 while (ptr < end && *ptr != '/')
745 ptr++;
746
747 if (ptr == end)
748 return NULL;
749
750 /* OK, we got the '/' ! */
751 return ptr;
752}
753
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754/* Returns a 302 for a redirectable request. This may only be called just after
755 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
756 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200757 * NOTE: this function is designed to support being called once data are scheduled
758 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100761{
762 struct http_txn *txn;
763 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100765 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200766 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100767
768 /* 1: create the response header */
769 rdr.len = strlen(HTTP_302);
770 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200771 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772 memcpy(rdr.str, HTTP_302, rdr.len);
773
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 srv = target_srv(&s->target);
775
Willy Tarreauefb453c2008-10-26 20:49:47 +0100776 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100780 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100781 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
782 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
783 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786 /* 3: add the request URI. Since it was already forwarded, we need
787 * to temporarily rewind the buffer.
788 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 txn = &s->txn;
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200790 b_rew(&s->req->buf, rewind = s->req->buf.o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 path = http_get_path(txn);
Willy Tarreau572bf902012-07-02 17:01:20 +0200793 len = buffer_count(&s->req->buf, path, b_ptr(&s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200794
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200795 b_adv(&s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200796
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 if (!path)
798 return;
799
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200800 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801 return;
802
803 memcpy(rdr.str + rdr.len, path, len);
804 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100805
806 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
807 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
808 rdr.len += 29;
809 } else {
810 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
811 rdr.len += 23;
812 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200815 si_shutr(si);
816 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 si->err_type = SI_ET_NONE;
818 si->err_loc = NULL;
819 si->state = SI_ST_CLO;
820
821 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823
824 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 if (srv)
826 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827}
828
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100829/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 * that the server side is closed. Note that err_type is actually a
831 * bitmask, where almost only aborts may be cumulated with other
832 * values. We consider that aborted operations are more important
833 * than timeouts or errors due to the fact that nobody else in the
834 * logs might explain incomplete retries. All others should avoid
835 * being cumulated. It should normally not be possible to have multiple
836 * aborts at once, but just in case, the first one in sequence is reported.
837 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100838void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841
842 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
844 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
847 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
850 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
853 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100855 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
856 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100858 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
859 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100860 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100861 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
862 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100863}
864
Willy Tarreau42250582007-04-01 01:30:43 +0200865extern const char sess_term_cond[8];
866extern const char sess_fin_state[8];
867extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200868struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200869struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100870struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871
Willy Tarreau117f59e2007-03-04 18:17:17 +0100872/*
873 * Capture headers from message starting at <som> according to header list
874 * <cap_hdr>, and fill the <idx> structure appropriately.
875 */
876void capture_headers(char *som, struct hdr_idx *idx,
877 char **cap, struct cap_hdr *cap_hdr)
878{
879 char *eol, *sol, *col, *sov;
880 int cur_idx;
881 struct cap_hdr *h;
882 int len;
883
884 sol = som + hdr_idx_first_pos(idx);
885 cur_idx = hdr_idx_first_idx(idx);
886
887 while (cur_idx) {
888 eol = sol + idx->v[cur_idx].len;
889
890 col = sol;
891 while (col < eol && *col != ':')
892 col++;
893
894 sov = col + 1;
895 while (sov < eol && http_is_lws[(unsigned char)*sov])
896 sov++;
897
898 for (h = cap_hdr; h; h = h->next) {
899 if ((h->namelen == col - sol) &&
900 (strncasecmp(sol, h->name, h->namelen) == 0)) {
901 if (cap[h->index] == NULL)
902 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200903 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100904
905 if (cap[h->index] == NULL) {
906 Alert("HTTP capture : out of memory.\n");
907 continue;
908 }
909
910 len = eol - sov;
911 if (len > h->len)
912 len = h->len;
913
914 memcpy(cap[h->index], sov, len);
915 cap[h->index][len]=0;
916 }
917 }
918 sol = eol + idx->v[cur_idx].cr + 1;
919 cur_idx = idx->v[cur_idx].next;
920 }
921}
922
923
Willy Tarreau42250582007-04-01 01:30:43 +0200924/* either we find an LF at <ptr> or we jump to <bad>.
925 */
926#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
927
928/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
929 * otherwise to <http_msg_ood> with <state> set to <st>.
930 */
931#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
932 ptr++; \
933 if (likely(ptr < end)) \
934 goto good; \
935 else { \
936 state = (st); \
937 goto http_msg_ood; \
938 } \
939 } while (0)
940
941
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100943 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
945 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
946 * will give undefined results.
947 * Note that it is upon the caller's responsibility to ensure that ptr < end,
948 * and that msg->sol points to the beginning of the response.
949 * If a complete line is found (which implies that at least one CR or LF is
950 * found before <end>, the updated <ptr> is returned, otherwise NULL is
951 * returned indicating an incomplete line (which does not mean that parts have
952 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
953 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
954 * upon next call.
955 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200956 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
958 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200959 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200961const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100962 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100963 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100964{
Willy Tarreau572bf902012-07-02 17:01:20 +0200965 const char *msg_start = msg->buf->buf.p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100966
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100970 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100974 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
976 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100977 state = HTTP_MSG_ERROR;
978 break;
979
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200981 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100983 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 goto http_msg_rpcode;
985 }
986 if (likely(HTTP_IS_SPHT(*ptr)))
987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
988 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100989 state = HTTP_MSG_ERROR;
990 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200993 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 if (likely(!HTTP_IS_LWS(*ptr)))
995 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
996
997 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100998 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1000 }
1001
1002 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 http_msg_rsp_reason:
1005 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001006 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 msg->sl.st.r_l = 0;
1008 goto http_msg_rpline_eol;
1009
Willy Tarreau8973c702007-01-21 23:58:29 +01001010 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001011 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001012 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 goto http_msg_rpreason;
1015 }
1016 if (likely(HTTP_IS_SPHT(*ptr)))
1017 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1018 /* so it's a CR/LF, so there is no reason phrase */
1019 goto http_msg_rsp_reason;
1020
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001022 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001023 if (likely(!HTTP_IS_CRLF(*ptr)))
1024 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001025 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001026 http_msg_rpline_eol:
1027 /* We have seen the end of line. Note that we do not
1028 * necessarily have the \n yet, but at least we know that we
1029 * have EITHER \r OR \n, otherwise the response would not be
1030 * complete. We can then record the response length and return
1031 * to the caller which will be able to register it.
1032 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001033 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001034 return ptr;
1035
1036#ifdef DEBUG_FULL
1037 default:
1038 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1039 exit(1);
1040#endif
1041 }
1042
1043 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001044 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 if (ret_state)
1046 *ret_state = state;
1047 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001048 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001049 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001050}
1051
Willy Tarreau8973c702007-01-21 23:58:29 +01001052/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001053 * This function parses a request line between <ptr> and <end>, starting with
1054 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1055 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1056 * will give undefined results.
1057 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1058 * and that msg->sol points to the beginning of the request.
1059 * If a complete line is found (which implies that at least one CR or LF is
1060 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1061 * returned indicating an incomplete line (which does not mean that parts have
1062 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1063 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1064 * upon next call.
1065 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001066 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1068 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001069 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001071const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001072 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001073 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074{
Willy Tarreau572bf902012-07-02 17:01:20 +02001075 const char *msg_start = msg->buf->buf.p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001079 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 if (likely(HTTP_IS_TOKEN(*ptr)))
1081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 if (likely(HTTP_IS_CRLF(*ptr))) {
1089 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 msg->sl.rq.v_l = 0;
1098 goto http_msg_rqline_eol;
1099 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001100 state = HTTP_MSG_ERROR;
1101 break;
1102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001104 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 goto http_msg_rquri;
1108 }
1109 if (likely(HTTP_IS_SPHT(*ptr)))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1111 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1112 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001115 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001116 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001119 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001120 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001125 /* non-ASCII chars are forbidden unless option
1126 * accept-invalid-http-request is enabled in the frontend.
1127 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001129 if (msg->err_pos < -1)
1130 goto invalid_char;
1131 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001132 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1134 }
1135
1136 if (likely(HTTP_IS_CRLF(*ptr))) {
1137 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1138 goto http_msg_req09_uri_e;
1139 }
1140
1141 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001142 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001143 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001144 state = HTTP_MSG_ERROR;
1145 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001148 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001150 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 goto http_msg_rqver;
1152 }
1153 if (likely(HTTP_IS_SPHT(*ptr)))
1154 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1155 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1156 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001159 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001162
1163 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001165 http_msg_rqline_eol:
1166 /* We have seen the end of line. Note that we do not
1167 * necessarily have the \n yet, but at least we know that we
1168 * have EITHER \r OR \n, otherwise the request would not be
1169 * complete. We can then record the request length and return
1170 * to the caller which will be able to register it.
1171 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001172 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001173 return ptr;
1174 }
1175
1176 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001177 state = HTTP_MSG_ERROR;
1178 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001180#ifdef DEBUG_FULL
1181 default:
1182 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1183 exit(1);
1184#endif
1185 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001188 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189 if (ret_state)
1190 *ret_state = state;
1191 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001192 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001195
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001196/*
1197 * Returns the data from Authorization header. Function may be called more
1198 * than once so data is stored in txn->auth_data. When no header is found
1199 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1200 * searching again for something we are unable to find anyway.
1201 */
1202
1203char get_http_auth_buff[BUFSIZE];
1204
1205int
1206get_http_auth(struct session *s)
1207{
1208
1209 struct http_txn *txn = &s->txn;
1210 struct chunk auth_method;
1211 struct hdr_ctx ctx;
1212 char *h, *p;
1213 int len;
1214
1215#ifdef DEBUG_AUTH
1216 printf("Auth for session %p: %d\n", s, txn->auth.method);
1217#endif
1218
1219 if (txn->auth.method == HTTP_AUTH_WRONG)
1220 return 0;
1221
1222 if (txn->auth.method)
1223 return 1;
1224
1225 txn->auth.method = HTTP_AUTH_WRONG;
1226
1227 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001228
1229 if (txn->flags & TX_USE_PX_CONN) {
1230 h = "Proxy-Authorization";
1231 len = strlen(h);
1232 } else {
1233 h = "Authorization";
1234 len = strlen(h);
1235 }
1236
Willy Tarreau572bf902012-07-02 17:01:20 +02001237 if (!http_find_header2(h, len, s->req->buf.p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001238 return 0;
1239
1240 h = ctx.line + ctx.val;
1241
1242 p = memchr(h, ' ', ctx.vlen);
1243 if (!p || p == h)
1244 return 0;
1245
1246 chunk_initlen(&auth_method, h, 0, p-h);
1247 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1248
1249 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1250
1251 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1252 get_http_auth_buff, BUFSIZE - 1);
1253
1254 if (len < 0)
1255 return 0;
1256
1257
1258 get_http_auth_buff[len] = '\0';
1259
1260 p = strchr(get_http_auth_buff, ':');
1261
1262 if (!p)
1263 return 0;
1264
1265 txn->auth.user = get_http_auth_buff;
1266 *p = '\0';
1267 txn->auth.pass = p+1;
1268
1269 txn->auth.method = HTTP_AUTH_BASIC;
1270 return 1;
1271 }
1272
1273 return 0;
1274}
1275
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276
Willy Tarreau8973c702007-01-21 23:58:29 +01001277/*
1278 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001279 * depending on the initial msg->msg_state. The caller is responsible for
1280 * ensuring that the message does not wrap. The function can be preempted
1281 * everywhere when data are missing and recalled at the exact same location
1282 * with no information loss. The message may even be realigned between two
1283 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001285 * fields. Note that msg->sol will be initialized after completing the first
1286 * state, so that none of the msg pointers has to be initialized prior to the
1287 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001289void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001291 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001293 struct channel *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 state = msg->msg_state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001296 ptr = buf->buf.p + msg->next;
1297 end = buf->buf.p + buf->buf.i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(ptr >= end))
1300 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 /*
1304 * First, states that are specific to the response only.
1305 * We check them first so that request and headers are
1306 * closer to each other (accessed more often).
1307 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001309 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001311 /* we have a start of message, but we have to check
1312 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001313 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001314 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001315 if (unlikely(ptr != buf->buf.p)) {
1316 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001317 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001318 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001319 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 }
Willy Tarreau26927362012-05-18 23:22:52 +02001321 msg->sol = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 hdr_idx_init(idx);
1323 state = HTTP_MSG_RPVER;
1324 goto http_msg_rpver;
1325 }
1326
1327 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1328 goto http_msg_invalid;
1329
1330 if (unlikely(*ptr == '\n'))
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1333 /* stop here */
1334
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001336 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 EXPECT_LF_HERE(ptr, http_msg_invalid);
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1339 /* stop here */
1340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001342 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 case HTTP_MSG_RPVER_SP:
1344 case HTTP_MSG_RPCODE:
1345 case HTTP_MSG_RPCODE_SP:
1346 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001347 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001348 state, ptr, end,
1349 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (unlikely(!ptr))
1351 return;
1352
1353 /* we have a full response and we know that we have either a CR
1354 * or an LF at <ptr>.
1355 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1357
Willy Tarreau572bf902012-07-02 17:01:20 +02001358 msg->sol = ptr - buf->buf.p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 if (likely(*ptr == '\r'))
1360 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1361 goto http_msg_rpline_end;
1362
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 /* msg->sol must point to the first of CR or LF. */
1366 EXPECT_LF_HERE(ptr, http_msg_invalid);
1367 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1368 /* stop here */
1369
1370 /*
1371 * Second, states that are specific to the request only
1372 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001374 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 /* we have a start of message, but we have to check
1377 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001378 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001380 if (likely(ptr != buf->buf.p)) {
1381 if (buf->buf.o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001382 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001383 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau572bf902012-07-02 17:01:20 +02001384 bi_fast_delete(&buf->buf, ptr - buf->buf.p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 }
Willy Tarreau26927362012-05-18 23:22:52 +02001386 msg->sol = 0;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001387 /* we will need this when keep-alive will be supported
1388 hdr_idx_init(idx);
1389 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 state = HTTP_MSG_RQMETH;
1391 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1395 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (unlikely(*ptr == '\n'))
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 EXPECT_LF_HERE(ptr, http_msg_invalid);
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001406 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001409 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 case HTTP_MSG_RQMETH_SP:
1411 case HTTP_MSG_RQURI:
1412 case HTTP_MSG_RQURI_SP:
1413 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001414 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001415 state, ptr, end,
1416 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (unlikely(!ptr))
1418 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 /* we have a full request and we know that we have either a CR
1421 * or an LF at <ptr>.
1422 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau572bf902012-07-02 17:01:20 +02001425 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (likely(*ptr == '\r'))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 /* check for HTTP/0.9 request : no version information available.
1433 * msg->sol must point to the first of CR or LF.
1434 */
1435 if (unlikely(msg->sl.rq.v_l == 0))
1436 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 EXPECT_LF_HERE(ptr, http_msg_invalid);
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 /*
1443 * Common states below
1444 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_hdr_first:
Willy Tarreau572bf902012-07-02 17:01:20 +02001447 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(!HTTP_IS_CRLF(*ptr))) {
1449 goto http_msg_hdr_name;
1450 }
1451
1452 if (likely(*ptr == '\r'))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1454 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 /* assumes msg->sol points to the first char */
1459 if (likely(HTTP_IS_TOKEN(*ptr)))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001462 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465 if (likely(msg->err_pos < -1) || *ptr == '\n')
1466 goto http_msg_invalid;
1467
1468 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreau572bf902012-07-02 17:01:20 +02001469 msg->err_pos = ptr - buf->buf.p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001470
1471 /* and we still accept this non-token character */
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001475 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001476 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(HTTP_IS_SPHT(*ptr)))
1478 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 /* header value can be basically anything except CR/LF */
Willy Tarreau572bf902012-07-02 17:01:20 +02001481 msg->sov = ptr - buf->buf.p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (likely(!HTTP_IS_CRLF(*ptr))) {
1484 goto http_msg_hdr_val;
1485 }
1486
1487 if (likely(*ptr == '\r'))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1489 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EXPECT_LF_HERE(ptr, http_msg_invalid);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001497 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (likely(HTTP_IS_SPHT(*ptr))) {
1499 /* replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001500 for (; buf->buf.p + msg->sov < ptr; msg->sov++)
1501 buf->buf.p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 goto http_msg_hdr_l1_sp;
1503 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001504 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001505 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 goto http_msg_complete_header;
1507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001509 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001510 /* assumes msg->sol points to the first char, and msg->sov
1511 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 */
1513 if (likely(!HTTP_IS_CRLF(*ptr)))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau572bf902012-07-02 17:01:20 +02001516 msg->eol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 /* Note: we could also copy eol into ->eoh so that we have the
1518 * real header end in case it ends with lots of LWS, but is this
1519 * really needed ?
1520 */
1521 if (likely(*ptr == '\r'))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1523 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001531 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1533 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau572bf902012-07-02 17:01:20 +02001534 for (; buf->buf.p + msg->eol < ptr; msg->eol++)
1535 buf->buf.p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 goto http_msg_hdr_val;
1537 }
1538 http_msg_complete_header:
1539 /*
1540 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001541 * Assumes msg->sol points to the first char, msg->sov points
1542 * to the first character of the value and msg->eol to the
1543 * first CR or LF so we know how the line ends. We insert last
1544 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001546 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->buf.p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 idx, idx->tail) < 0))
1548 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001549
Willy Tarreau572bf902012-07-02 17:01:20 +02001550 msg->sol = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (likely(!HTTP_IS_CRLF(*ptr))) {
1552 goto http_msg_hdr_name;
1553 }
1554
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1557 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001560 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 /* Assumes msg->sol points to the first of either CR or LF */
1562 EXPECT_LF_HERE(ptr, http_msg_invalid);
1563 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001564 msg->sov = msg->next = ptr - buf->buf.p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001565 msg->eoh = msg->sol;
1566 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001567 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001569
1570 case HTTP_MSG_ERROR:
1571 /* this may only happen if we call http_msg_analyser() twice with an error */
1572 break;
1573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574#ifdef DEBUG_FULL
1575 default:
1576 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1577 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001578#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 }
1580 http_msg_ood:
1581 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 msg->msg_state = state;
Willy Tarreau572bf902012-07-02 17:01:20 +02001583 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 http_msg_invalid:
1587 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau572bf902012-07-02 17:01:20 +02001589 msg->next = ptr - buf->buf.p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 return;
1591}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001592
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1594 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1595 * nothing is done and 1 is returned.
1596 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001597static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598{
1599 int delta;
1600 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001601 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602
1603 if (msg->sl.rq.v_l != 0)
1604 return 1;
1605
Willy Tarreau572bf902012-07-02 17:01:20 +02001606 cur_end = msg->buf->buf.p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 delta = 0;
1608
1609 if (msg->sl.rq.u_l == 0) {
1610 /* if no URI was set, add "/" */
Willy Tarreauaf819352012-08-27 22:08:00 +02001611 delta = buffer_replace2(&msg->buf->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001613 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 }
1615 /* add HTTP version */
Willy Tarreauaf819352012-08-27 22:08:00 +02001616 delta = buffer_replace2(&msg->buf->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001617 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001618 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001619 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001620 HTTP_MSG_RQMETH,
Willy Tarreau572bf902012-07-02 17:01:20 +02001621 msg->buf->buf.p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 NULL, NULL);
1623 if (unlikely(!cur_end))
1624 return 0;
1625
1626 /* we have a full HTTP/1.0 request now and we know that
1627 * we have either a CR or an LF at <ptr>.
1628 */
1629 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1630 return 1;
1631}
1632
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001633/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001634 * and "keep-alive" values. If we already know that some headers may safely
1635 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001636 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1637 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1638 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1639 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1640 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001641 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001643void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001644{
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001646 const char *hdr_val = "Connection";
1647 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001648
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001650 return;
1651
Willy Tarreau88d349d2010-01-25 12:15:43 +01001652 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1653 hdr_val = "Proxy-Connection";
1654 hdr_len = 16;
1655 }
1656
Willy Tarreau5b154472009-12-21 20:11:07 +01001657 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001658 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001659 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1661 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001662 if (to_del & 2)
1663 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001664 else
1665 txn->flags |= TX_CON_KAL_SET;
1666 }
1667 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1668 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001669 if (to_del & 1)
1670 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 else
1672 txn->flags |= TX_CON_CLO_SET;
1673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001674 }
1675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676 txn->flags |= TX_HDR_CONN_PRS;
1677 return;
1678}
Willy Tarreau5b154472009-12-21 20:11:07 +01001679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680/* Apply desired changes on the Connection: header. Values may be removed and/or
1681 * added depending on the <wanted> flags, which are exclusively composed of
1682 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1683 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1684 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001685void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686{
1687 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688 const char *hdr_val = "Connection";
1689 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690
1691 ctx.idx = 0;
1692
Willy Tarreau88d349d2010-01-25 12:15:43 +01001693
1694 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1695 hdr_val = "Proxy-Connection";
1696 hdr_len = 16;
1697 }
1698
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau572bf902012-07-02 17:01:20 +02001700 while (http_find_header2(hdr_val, hdr_len, msg->buf->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1702 if (wanted & TX_CON_KAL_SET)
1703 txn->flags |= TX_CON_KAL_SET;
1704 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001705 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001706 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001707 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1708 if (wanted & TX_CON_CLO_SET)
1709 txn->flags |= TX_CON_CLO_SET;
1710 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001711 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001713 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001714
1715 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1716 return;
1717
1718 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1719 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001720 hdr_val = "Connection: close";
1721 hdr_len = 17;
1722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1723 hdr_val = "Proxy-Connection: close";
1724 hdr_len = 23;
1725 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001726 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001727 }
1728
1729 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1730 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001731 hdr_val = "Connection: keep-alive";
1732 hdr_len = 22;
1733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection: keep-alive";
1735 hdr_len = 28;
1736 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001737 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 }
1739 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001740}
1741
Willy Tarreaua458b672012-03-05 11:17:50 +01001742/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001744 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001745 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001747 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001748int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001749{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001750 const struct channel *buf = msg->buf;
Willy Tarreau572bf902012-07-02 17:01:20 +02001751 const char *ptr = b_ptr(&buf->buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001752 const char *ptr_old = ptr;
Willy Tarreau572bf902012-07-02 17:01:20 +02001753 const char *end = buf->buf.data + buf->buf.size;
1754 const char *stop = bi_end(&buf->buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 unsigned int chunk = 0;
1756
1757 /* The chunk size is in the following form, though we are only
1758 * interested in the size and CRLF :
1759 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1760 */
1761 while (1) {
1762 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001763 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 if (c < 0) /* not a hex digit anymore */
1767 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001769 ptr = buf->buf.data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001770 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001771 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001772 chunk = (chunk << 4) + c;
1773 }
1774
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001776 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001777 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778
1779 while (http_is_spht[(unsigned char)*ptr]) {
1780 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001781 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001782 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 }
1785
Willy Tarreaud98cf932009-12-27 22:54:55 +01001786 /* Up to there, we know that at least one byte is present at *ptr. Check
1787 * for the end of chunk size.
1788 */
1789 while (1) {
1790 if (likely(HTTP_IS_CRLF(*ptr))) {
1791 /* we now have a CR or an LF at ptr */
1792 if (likely(*ptr == '\r')) {
1793 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001794 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001795 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 return 0;
1797 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001798
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001800 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001802 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803 /* done */
1804 break;
1805 }
1806 else if (*ptr == ';') {
1807 /* chunk extension, ends at next CRLF */
1808 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001809 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001810 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001811 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812
1813 while (!HTTP_IS_CRLF(*ptr)) {
1814 if (++ptr >= end)
Willy Tarreau572bf902012-07-02 17:01:20 +02001815 ptr = buf->buf.data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001816 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 return 0;
1818 }
1819 /* we have a CRLF now, loop above */
1820 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001823 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001824 }
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001830 msg->sov += ptr - ptr_old;
Willy Tarreau572bf902012-07-02 17:01:20 +02001831 msg->next = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001832 msg->chunk_len = chunk;
1833 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001836 error:
Willy Tarreau572bf902012-07-02 17:01:20 +02001837 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001838 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001839}
1840
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001841/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001842 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001843 * the trailers is found, it is automatically scheduled to be forwarded,
1844 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1845 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001847 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001848 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001849 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1850 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001851 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001852 * matches the length of trailers already parsed and not forwarded. It is also
1853 * important to note that this function is designed to be able to parse wrapped
1854 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001856int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001858 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001859
Willy Tarreaua458b672012-03-05 11:17:50 +01001860 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001862 const char *p1 = NULL, *p2 = NULL;
Willy Tarreau572bf902012-07-02 17:01:20 +02001863 const char *ptr = b_ptr(&buf->buf, msg->next);
1864 const char *stop = bi_end(&buf->buf);
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) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001869 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870 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) {
Willy Tarreau572bf902012-07-02 17:01:20 +02001881 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
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++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001888 if (ptr >= buf->buf.data + buf->buf.size)
1889 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 }
1891
1892 /* after LF; point to beginning of next line */
1893 p2++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001894 if (p2 >= buf->buf.data + buf->buf.size)
1895 p2 = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
Willy Tarreau572bf902012-07-02 17:01:20 +02001897 bytes = p2 - b_ptr(&buf->buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 if (bytes < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02001899 bytes += buf->buf.size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001900
1901 /* schedule this line for forwarding */
1902 msg->sov += bytes;
Willy Tarreau572bf902012-07-02 17:01:20 +02001903 if (msg->sov >= buf->buf.size)
1904 msg->sov -= buf->buf.size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905
Willy Tarreau572bf902012-07-02 17:01:20 +02001906 if (p1 == b_ptr(&buf->buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001907 /* 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 Tarreau572bf902012-07-02 17:01:20 +02001911 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912 msg->msg_state = HTTP_MSG_DONE;
1913 return 1;
1914 }
1915 /* OK, next line then */
Willy Tarreau572bf902012-07-02 17:01:20 +02001916 msg->next = buffer_count(&buf->buf, buf->buf.p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 }
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,
Willy Tarreau26927362012-05-18 23:22:52 +02001922 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001923 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001924 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1925 * not enough data are available, the function does not change anything and
1926 * returns zero. If a parse error is encountered, the function returns < 0 and
1927 * does not change anything. Note: this function is designed to parse wrapped
1928 * CRLF at the end of the buffer.
1929 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001930int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001932 const struct channel *buf = msg->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001933 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 int bytes;
1935
1936 /* NB: we'll check data availabilty at the end. It's not a
1937 * problem because whatever we match first will be checked
1938 * against the correct length.
1939 */
1940 bytes = 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02001941 ptr = buf->buf.p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942 if (*ptr == '\r') {
1943 bytes++;
1944 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001945 if (ptr >= buf->buf.data + buf->buf.size)
1946 ptr = buf->buf.data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 }
1948
Willy Tarreau572bf902012-07-02 17:01:20 +02001949 if (bytes > buf->buf.i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 return 0;
1951
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 if (*ptr != '\n') {
Willy Tarreau572bf902012-07-02 17:01:20 +02001953 msg->err_pos = buffer_count(&buf->buf, buf->buf.p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956
1957 ptr++;
Willy Tarreau572bf902012-07-02 17:01:20 +02001958 if (ptr >= buf->buf.data + buf->buf.size)
1959 ptr = buf->buf.data;
Willy Tarreau26927362012-05-18 23:22:52 +02001960 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1961 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001962 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1964 return 1;
1965}
Willy Tarreau5b154472009-12-21 20:11:07 +01001966
Willy Tarreaud787e662009-07-07 10:14:51 +02001967/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1968 * processing can continue on next analysers, or zero if it either needs more
1969 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1970 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1971 * when it has nothing left to do, and may remove any analyser when it wants to
1972 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001974int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975{
Willy Tarreau59234e92008-11-30 23:51:27 +01001976 /*
1977 * We will parse the partial (or complete) lines.
1978 * We will check the request syntax, and also join multi-line
1979 * headers. An index of all the lines will be elaborated while
1980 * parsing.
1981 *
1982 * For the parsing, we use a 28 states FSM.
1983 *
1984 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02001985 * req->buf.p = beginning of request
1986 * req->buf.p + msg->eoh = end of processed headers / start of current one
1987 * req->buf.p + req->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001988 * msg->eol = end of current header or line (LF or CRLF)
1989 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001990 *
1991 * At end of parsing, we may perform a capture of the error (if any), and
1992 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1993 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1994 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001995 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001996
Willy Tarreau59234e92008-11-30 23:51:27 +01001997 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001998 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01001999 struct http_txn *txn = &s->txn;
2000 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002001 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002002
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002003 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002004 now_ms, __FUNCTION__,
2005 s,
2006 req,
2007 req->rex, req->wex,
2008 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002009 req->buf.i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002010 req->analysers);
2011
Willy Tarreau52a0c602009-08-16 22:45:38 +02002012 /* we're speaking HTTP here, so let's speak HTTP to the client */
2013 s->srv_error = http_return_srv_error;
2014
Willy Tarreau83e3af02009-12-28 17:39:57 +01002015 /* There's a protected area at the end of the buffer for rewriting
2016 * purposes. We don't want to start to parse the request if the
2017 * protected area is affected, because we may have to move processed
2018 * data later, which is much more complicated.
2019 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002020 if (buffer_not_empty(&req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002021 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002022 unlikely(channel_full(req) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002023 bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2024 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)) {
2025 if (req->buf.o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002026 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002027 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002028 /* some data has still not left the buffer, wake us once that's done */
2029 buffer_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002030 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002031 return 0;
2032 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002033 if (bi_end(&req->buf) < b_ptr(&req->buf, msg->next) ||
2034 bi_end(&req->buf) > req->buf.data + req->buf.size - global.tune.maxrewrite)
2035 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002036 }
2037
Willy Tarreau065e8332010-01-08 00:30:20 +01002038 /* Note that we have the same problem with the response ; we
2039 * may want to send a redirect, error or anything which requires
2040 * some spare space. So we'll ensure that we have at least
2041 * maxrewrite bytes available in the response buffer before
2042 * processing that one. This will only affect pipelined
2043 * keep-alive requests.
2044 */
2045 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002046 unlikely(channel_full(s->rep) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02002047 bi_end(&s->rep->buf) < b_ptr(&s->rep->buf, txn->rsp.next) ||
2048 bi_end(&s->rep->buf) > s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)) {
2049 if (s->rep->buf.o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002050 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002051 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002052 /* don't let a connection request be initiated */
2053 buffer_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002054 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002055 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002056 return 0;
2057 }
2058 }
2059
Willy Tarreau572bf902012-07-02 17:01:20 +02002060 if (likely(msg->next < req->buf.i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002061 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002062 }
2063
Willy Tarreau59234e92008-11-30 23:51:27 +01002064 /* 1: we might have to print this header in debug mode */
2065 if (unlikely((global.mode & MODE_DEBUG) &&
2066 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002067 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002068 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002069
Willy Tarreau572bf902012-07-02 17:01:20 +02002070 sol = req->buf.p;
Willy Tarreau59234e92008-11-30 23:51:27 +01002071 eol = sol + msg->sl.rq.l;
2072 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002073
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 sol += hdr_idx_first_pos(&txn->hdr_idx);
2075 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002076
Willy Tarreau59234e92008-11-30 23:51:27 +01002077 while (cur_idx) {
2078 eol = sol + txn->hdr_idx.v[cur_idx].len;
2079 debug_hdr("clihdr", s, sol, eol);
2080 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2081 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002082 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 }
2084
Willy Tarreau58f10d72006-12-04 02:26:12 +01002085
Willy Tarreau59234e92008-11-30 23:51:27 +01002086 /*
2087 * Now we quickly check if we have found a full valid request.
2088 * If not so, we check the FD and buffer states before leaving.
2089 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002090 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002091 * requests are checked first. When waiting for a second request
2092 * on a keep-alive session, if we encounter and error, close, t/o,
2093 * we note the error in the session flags but don't set any state.
2094 * Since the error will be noted there, it will not be counted by
2095 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002096 * Last, we may increase some tracked counters' http request errors on
2097 * the cases that are deliberately the client's fault. For instance,
2098 * a timeout or connection reset is not counted as an error. However
2099 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002101
Willy Tarreau655dce92009-11-08 13:10:58 +01002102 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002105 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002106 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002107 session_inc_http_req_ctr(s);
2108 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002109 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002111 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 /* 1: Since we are in header mode, if there's no space
2114 * left for headers, we won't be able to free more
2115 * later, so the session will never terminate. We
2116 * must terminate it now.
2117 */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002118 if (unlikely(buffer_full(&req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 /* FIXME: check if URI is set and return Status
2120 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002121 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002122 session_inc_http_req_ctr(s);
2123 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002124 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002125 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02002126 msg->err_pos = req->buf.i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 goto return_bad_req;
2128 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002131 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002132 if (!(s->flags & SN_ERR_MASK))
2133 s->flags |= SN_ERR_CLICL;
2134
Willy Tarreaufcffa692010-01-10 14:21:19 +01002135 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002136 goto failed_keep_alive;
2137
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002139 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002140 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002141 session_inc_http_err_ctr(s);
2142 }
2143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 msg->msg_state = HTTP_MSG_ERROR;
2145 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002146
Willy Tarreauda7ff642010-06-23 11:44:09 +02002147 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002148 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002149 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002150 if (s->listener->counters)
2151 s->listener->counters->failed_req++;
2152
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 if (!(s->flags & SN_FINST_MASK))
2154 s->flags |= SN_FINST_R;
2155 return 0;
2156 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002159 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002160 if (!(s->flags & SN_ERR_MASK))
2161 s->flags |= SN_ERR_CLITO;
2162
Willy Tarreaufcffa692010-01-10 14:21:19 +01002163 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002164 goto failed_keep_alive;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002168 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_err_ctr(s);
2170 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 txn->status = 408;
2172 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2173 msg->msg_state = HTTP_MSG_ERROR;
2174 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002175
Willy Tarreauda7ff642010-06-23 11:44:09 +02002176 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002177 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002178 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179 if (s->listener->counters)
2180 s->listener->counters->failed_req++;
2181
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 if (!(s->flags & SN_FINST_MASK))
2183 s->flags |= SN_FINST_R;
2184 return 0;
2185 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002188 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002189 if (!(s->flags & SN_ERR_MASK))
2190 s->flags |= SN_ERR_CLICL;
2191
Willy Tarreaufcffa692010-01-10 14:21:19 +01002192 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002193 goto failed_keep_alive;
2194
Willy Tarreau4076a152009-04-02 15:18:36 +02002195 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002196 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002197 txn->status = 400;
2198 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2199 msg->msg_state = HTTP_MSG_ERROR;
2200 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002201
Willy Tarreauda7ff642010-06-23 11:44:09 +02002202 session_inc_http_err_ctr(s);
2203 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002204 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002205 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002206 if (s->listener->counters)
2207 s->listener->counters->failed_req++;
2208
Willy Tarreau59234e92008-11-30 23:51:27 +01002209 if (!(s->flags & SN_FINST_MASK))
2210 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002211 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002212 }
2213
Willy Tarreau520d95e2009-09-19 21:04:57 +02002214 buffer_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002215 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2216 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002217#ifdef TCP_QUICKACK
Willy Tarreau572bf902012-07-02 17:01:20 +02002218 if (s->listener->options & LI_O_NOQUICKACK && req->buf.i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002219 /* We need more data, we have to re-enable quick-ack in case we
2220 * previously disabled it, otherwise we might cause the client
2221 * to delay next data.
2222 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002223 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002224 }
2225#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002226
Willy Tarreaufcffa692010-01-10 14:21:19 +01002227 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2228 /* If the client starts to talk, let's fall back to
2229 * request timeout processing.
2230 */
2231 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002232 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002233 }
2234
Willy Tarreau59234e92008-11-30 23:51:27 +01002235 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002236 if (!tick_isset(req->analyse_exp)) {
2237 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2238 (txn->flags & TX_WAIT_NEXT_RQ) &&
2239 tick_isset(s->be->timeout.httpka))
2240 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2241 else
2242 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002244
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 /* we're not ready yet */
2246 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002247
2248 failed_keep_alive:
2249 /* Here we process low-level errors for keep-alive requests. In
2250 * short, if the request is not the first one and it experiences
2251 * a timeout, read error or shutdown, we just silently close so
2252 * that the client can try again.
2253 */
2254 txn->status = 0;
2255 msg->msg_state = HTTP_MSG_RQBEFORE;
2256 req->analysers = 0;
2257 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002258 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002259 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002260 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002262
Willy Tarreaud787e662009-07-07 10:14:51 +02002263 /* OK now we have a complete HTTP request with indexed headers. Let's
2264 * complete the request parsing by setting a few fields we will need
Willy Tarreau572bf902012-07-02 17:01:20 +02002265 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002266 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002267 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002268 * byte after the last LF. msg->sov points to the first byte of data.
2269 * msg->eol cannot be trusted because it may have been left uninitialized
2270 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002271 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002272
Willy Tarreauda7ff642010-06-23 11:44:09 +02002273 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002274 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2275
Willy Tarreaub16a5742010-01-10 14:46:16 +01002276 if (txn->flags & TX_WAIT_NEXT_RQ) {
2277 /* kill the pending keep-alive timeout */
2278 txn->flags &= ~TX_WAIT_NEXT_RQ;
2279 req->analyse_exp = TICK_ETERNITY;
2280 }
2281
2282
Willy Tarreaud787e662009-07-07 10:14:51 +02002283 /* Maybe we found in invalid header name while we were configured not
2284 * to block on that, so we have to capture it now.
2285 */
2286 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002287 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002288
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 /*
2290 * 1: identify the method
2291 */
Willy Tarreau572bf902012-07-02 17:01:20 +02002292 txn->meth = find_http_meth(req->buf.p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002293
2294 /* we can make use of server redirect on GET and HEAD */
2295 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2296 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002297
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 /*
2299 * 2: check if the URI matches the monitor_uri.
2300 * We have to do this for every request which gets in, because
2301 * the monitor-uri is defined by the frontend.
2302 */
2303 if (unlikely((s->fe->monitor_uri_len != 0) &&
2304 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002305 !memcmp(req->buf.p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 s->fe->monitor_uri,
2307 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002308 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002314 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002317 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002318 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002319
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 ret = acl_pass(ret);
2321 if (cond->pol == ACL_COND_UNLESS)
2322 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002323
Willy Tarreau59234e92008-11-30 23:51:27 +01002324 if (ret) {
2325 /* we fail this request, let's return 503 service unavail */
2326 txn->status = 503;
2327 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2328 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002329 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002330 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* nothing to fail, let's reply normaly */
2333 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002334 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 goto return_prx_cond;
2336 }
2337
2338 /*
2339 * 3: Maybe we have to copy the original REQURI for the logs ?
2340 * Note: we cannot log anymore if the request has been
2341 * classified as invalid.
2342 */
2343 if (unlikely(s->logs.logwait & LW_REQ)) {
2344 /* we have a complete HTTP request that we must log */
2345 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2346 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 if (urilen >= REQURI_LEN)
2349 urilen = REQURI_LEN - 1;
Willy Tarreau572bf902012-07-02 17:01:20 +02002350 memcpy(txn->uri, req->buf.p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 if (!(s->logs.logwait &= ~LW_REQ))
2354 s->do_log(s);
2355 } else {
2356 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002357 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 }
Willy Tarreau06619262006-12-17 08:37:22 +01002359
William Lallemanda73203e2012-03-12 12:48:57 +01002360 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2361 s->unique_id = pool_alloc2(pool2_uniqueid);
2362 }
2363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002365 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002366 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002367
Willy Tarreau5b154472009-12-21 20:11:07 +01002368 /* ... and check if the request is HTTP/1.1 or above */
2369 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002370 ((req->buf.p[msg->sl.rq.v + 5] > '1') ||
2371 ((req->buf.p[msg->sl.rq.v + 5] == '1') &&
2372 (req->buf.p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002373 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002374
2375 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002376 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002377
Willy Tarreau88d349d2010-01-25 12:15:43 +01002378 /* if the frontend has "option http-use-proxy-header", we'll check if
2379 * we have what looks like a proxied connection instead of a connection,
2380 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2381 * Note that this is *not* RFC-compliant, however browsers and proxies
2382 * happen to do that despite being non-standard :-(
2383 * We consider that a request not beginning with either '/' or '*' is
2384 * a proxied connection, which covers both "scheme://location" and
2385 * CONNECT ip:port.
2386 */
2387 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002388 req->buf.p[msg->sl.rq.u] != '/' && req->buf.p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002389 txn->flags |= TX_USE_PX_CONN;
2390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002391 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002392 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002393
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002395 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02002396 capture_headers(req->buf.p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002398
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002399 /* 6: determine the transfer-length.
2400 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2401 * the presence of a message-body in a REQUEST and its transfer length
2402 * must be determined that way (in order of precedence) :
2403 * 1. The presence of a message-body in a request is signaled by the
2404 * inclusion of a Content-Length or Transfer-Encoding header field
2405 * in the request's header fields. When a request message contains
2406 * both a message-body of non-zero length and a method that does
2407 * not define any semantics for that request message-body, then an
2408 * origin server SHOULD either ignore the message-body or respond
2409 * with an appropriate error message (e.g., 413). A proxy or
2410 * gateway, when presented the same request, SHOULD either forward
2411 * the request inbound with the message- body or ignore the
2412 * message-body when determining a response.
2413 *
2414 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2415 * and the "chunked" transfer-coding (Section 6.2) is used, the
2416 * transfer-length is defined by the use of this transfer-coding.
2417 * If a Transfer-Encoding header field is present and the "chunked"
2418 * transfer-coding is not present, the transfer-length is defined
2419 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002420 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002421 * 3. If a Content-Length header field is present, its decimal value in
2422 * OCTETs represents both the entity-length and the transfer-length.
2423 * If a message is received with both a Transfer-Encoding header
2424 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002425 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426 * 4. By the server closing the connection. (Closing the connection
2427 * cannot be used to indicate the end of a request body, since that
2428 * would leave no possibility for the server to send back a response.)
2429 *
2430 * Whenever a transfer-coding is applied to a message-body, the set of
2431 * transfer-codings MUST include "chunked", unless the message indicates
2432 * it is terminated by closing the connection. When the "chunked"
2433 * transfer-coding is used, it MUST be the last transfer-coding applied
2434 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002435 */
2436
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002437 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002438 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002439 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002440 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002441 http_find_header2("Transfer-Encoding", 17, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002442 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002443 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2444 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 /* bad transfer-encoding (chunked followed by something else) */
2446 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002447 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002448 break;
2449 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002450 }
2451
Willy Tarreau32b47f42009-10-18 20:55:02 +02002452 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002453 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02002454 http_find_header2("Content-Length", 14, req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002455 signed long long cl;
2456
Willy Tarreauad14f752011-09-02 20:33:27 +02002457 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002458 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002460 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002461
Willy Tarreauad14f752011-09-02 20:33:27 +02002462 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002463 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002465 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002466
Willy Tarreauad14f752011-09-02 20:33:27 +02002467 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002468 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002469 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002470 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002471
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002472 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002473 msg->err_pos = ctx.line + ctx.val - req->buf.p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002475 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002477 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002478 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002479 }
2480
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 /* bodyless requests have a known length */
2482 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002483 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484
Willy Tarreaud787e662009-07-07 10:14:51 +02002485 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002486 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 req->analyse_exp = TICK_ETERNITY;
2488 return 1;
2489
2490 return_bad_req:
2491 /* We centralize bad requests processing here */
2492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2493 /* we detected a parsing error. We want to archive this request
2494 * in the dedicated proxy area for later troubleshooting.
2495 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002496 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002497 }
2498
2499 txn->req.msg_state = HTTP_MSG_ERROR;
2500 txn->status = 400;
2501 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002503 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504 if (s->listener->counters)
2505 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002506
2507 return_prx_cond:
2508 if (!(s->flags & SN_ERR_MASK))
2509 s->flags |= SN_ERR_PRXCOND;
2510 if (!(s->flags & SN_FINST_MASK))
2511 s->flags |= SN_FINST_R;
2512
2513 req->analysers = 0;
2514 req->analyse_exp = TICK_ETERNITY;
2515 return 0;
2516}
2517
Cyril Bonté70be45d2010-10-12 00:14:35 +02002518/* We reached the stats page through a POST request.
2519 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002520 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002521 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002522int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002523{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002524 struct proxy *px = NULL;
2525 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002526
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002527 char key[LINESIZE];
2528 int action = ST_ADM_ACTION_NONE;
2529 int reprocess = 0;
2530
2531 int total_servers = 0;
2532 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002533
2534 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002535 char *st_cur_param = NULL;
2536 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002537
Willy Tarreau572bf902012-07-02 17:01:20 +02002538 first_param = req->buf.p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002539 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002540
2541 cur_param = next_param = end_params;
2542
Willy Tarreau572bf902012-07-02 17:01:20 +02002543 if (end_params >= req->buf.data + req->buf.size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002544 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002545 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002546 return 1;
2547 }
Willy Tarreau572bf902012-07-02 17:01:20 +02002548 else if (end_params > req->buf.p + req->buf.i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002549 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002550 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002551 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002552 }
2553
2554 *end_params = '\0';
2555
Willy Tarreau295a8372011-03-10 11:25:07 +01002556 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002557
2558 /*
2559 * Parse the parameters in reverse order to only store the last value.
2560 * From the html form, the backend and the action are at the end.
2561 */
2562 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002563 char *value;
2564 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565
2566 cur_param--;
2567 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002568 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002570 poffset = (cur_param != first_param ? 1 : 0);
2571 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2572 if ((plen > 0) && (plen <= sizeof(key))) {
2573 strncpy(key, cur_param + poffset, plen);
2574 key[plen - 1] = '\0';
2575 } else {
2576 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2577 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002578 }
2579
2580 /* Parse the value */
2581 value = key;
2582 while (*value != '\0' && *value != '=') {
2583 value++;
2584 }
2585 if (*value == '=') {
2586 /* Ok, a value is found, we can mark the end of the key */
2587 *value++ = '\0';
2588 }
2589
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002590 if (!url_decode(key) || !url_decode(value))
2591 break;
2592
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002594 if (!px && (strcmp(key, "b") == 0)) {
2595 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2596 /* the backend name is unknown or ambiguous (duplicate names) */
2597 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2598 goto out;
2599 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002600 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002601 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002602 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002603 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604 }
2605 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002606 action = ST_ADM_ACTION_ENABLE;
2607 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002608 else if (strcmp(value, "stop") == 0) {
2609 action = ST_ADM_ACTION_STOP;
2610 }
2611 else if (strcmp(value, "start") == 0) {
2612 action = ST_ADM_ACTION_START;
2613 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002614 else if (strcmp(value, "shutdown") == 0) {
2615 action = ST_ADM_ACTION_SHUTDOWN;
2616 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002617 else {
2618 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2619 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620 }
2621 }
2622 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002623 if (!(px && action)) {
2624 /*
2625 * Indicates that we'll need to reprocess the parameters
2626 * as soon as backend and action are known
2627 */
2628 if (!reprocess) {
2629 st_cur_param = cur_param;
2630 st_next_param = next_param;
2631 }
2632 reprocess = 1;
2633 }
2634 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002635 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002636 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002637 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002638 /* Not already in maintenance, we can change the server state */
2639 sv->state |= SRV_MAINTAIN;
2640 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002641 altered_servers++;
2642 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 }
2644 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002645 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002646 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002647 /* Already in maintenance, we can change the server state */
2648 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002649 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002650 altered_servers++;
2651 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002652 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002653 break;
2654 case ST_ADM_ACTION_STOP:
2655 case ST_ADM_ACTION_START:
2656 if (action == ST_ADM_ACTION_START)
2657 sv->uweight = sv->iweight;
2658 else
2659 sv->uweight = 0;
2660
2661 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2662 /* we must take care of not pushing the server to full throttle during slow starts */
2663 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2664 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2665 else
2666 sv->eweight = BE_WEIGHT_SCALE;
2667 sv->eweight *= sv->uweight;
2668 } else {
2669 sv->eweight = sv->uweight;
2670 }
2671
2672 /* static LB algorithms are a bit harder to update */
2673 if (px->lbprm.update_server_eweight)
2674 px->lbprm.update_server_eweight(sv);
2675 else if (sv->eweight) {
2676 if (px->lbprm.set_server_status_up)
2677 px->lbprm.set_server_status_up(sv);
2678 }
2679 else {
2680 if (px->lbprm.set_server_status_down)
2681 px->lbprm.set_server_status_down(sv);
2682 }
2683 altered_servers++;
2684 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002685 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002686 case ST_ADM_ACTION_SHUTDOWN:
2687 if (px->state != PR_STSTOPPED) {
2688 struct session *sess, *sess_bck;
2689
2690 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2691 if (sess->srv_conn == sv)
2692 session_shutdown(sess, SN_ERR_KILLED);
2693
2694 altered_servers++;
2695 total_servers++;
2696 }
2697 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002698 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002699 } else {
2700 /* the server name is unknown or ambiguous (duplicate names) */
2701 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002702 }
2703 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002704 if (reprocess && px && action) {
2705 /* Now, we know the backend and the action chosen by the user.
2706 * We can safely restart from the first server parameter
2707 * to reprocess them
2708 */
2709 cur_param = st_cur_param;
2710 next_param = st_next_param;
2711 reprocess = 0;
2712 goto reprocess_servers;
2713 }
2714
Cyril Bonté70be45d2010-10-12 00:14:35 +02002715 next_param = cur_param;
2716 }
2717 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002718
2719 if (total_servers == 0) {
2720 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2721 }
2722 else if (altered_servers == 0) {
2723 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2724 }
2725 else if (altered_servers == total_servers) {
2726 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2727 }
2728 else {
2729 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2730 }
2731 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002732 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002733}
2734
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002735/* returns a pointer to the first rule which forbids access (deny or http_auth),
2736 * or NULL if everything's OK.
2737 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002738static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002739http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2740{
Willy Tarreauff011f22011-01-06 17:51:27 +01002741 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002742
Willy Tarreauff011f22011-01-06 17:51:27 +01002743 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002744 int ret = 1;
2745
Willy Tarreauff011f22011-01-06 17:51:27 +01002746 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002747 continue;
2748
2749 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002750 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002751 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002752 ret = acl_pass(ret);
2753
Willy Tarreauff011f22011-01-06 17:51:27 +01002754 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002755 ret = !ret;
2756 }
2757
2758 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002759 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002760 return NULL; /* no problem */
2761 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002762 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002763 }
2764 }
2765 return NULL;
2766}
2767
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768/* This stream analyser runs all HTTP request processing which is common to
2769 * frontends and backends, which means blocking ACLs, filters, connection-close,
2770 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 * either needs more data or wants to immediately abort the request (eg: deny,
2773 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002774 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002775int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002776{
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 struct http_txn *txn = &s->txn;
2778 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002780 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002782 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002783 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002784
Willy Tarreau655dce92009-11-08 13:10:58 +01002785 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002786 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002787 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002788 return 0;
2789 }
2790
Willy Tarreau3a816292009-07-07 10:55:49 +02002791 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002792 req->analyse_exp = TICK_ETERNITY;
2793
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002794 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02002795 now_ms, __FUNCTION__,
2796 s,
2797 req,
2798 req->rex, req->wex,
2799 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02002800 req->buf.i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002801 req->analysers);
2802
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 /* first check whether we have some ACLs set to block this request */
2804 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002805 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002806
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807 ret = acl_pass(ret);
2808 if (cond->pol == ACL_COND_UNLESS)
2809 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002810
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 if (ret) {
2812 txn->status = 403;
2813 /* let's log the request time */
2814 s->logs.tv_request = now;
2815 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002816 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002817 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002819 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002820
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002821 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002822 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002823
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002824 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002825 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002826 do_stats = stats_check_uri(s->rep->prod, txn, px);
2827 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002828 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 +01002829 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002830 else
2831 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002832
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002833 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002834 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002835 txn->status = 403;
2836 s->logs.tv_request = now;
2837 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002839 s->fe->fe_counters.denied_req++;
2840 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2841 s->be->be_counters.denied_req++;
2842 if (s->listener->counters)
2843 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002844 goto return_prx_cond;
2845 }
2846
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847 /* try headers filters */
2848 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002849 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002850 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002851
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 /* has the request been denied ? */
2853 if (txn->flags & TX_CLDENY) {
2854 /* no need to go further */
2855 txn->status = 403;
2856 /* let's log the request time */
2857 s->logs.tv_request = now;
2858 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002859 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 goto return_prx_cond;
2861 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002862
2863 /* When a connection is tarpitted, we use the tarpit timeout,
2864 * which may be the same as the connect timeout if unspecified.
2865 * If unset, then set it to zero because we really want it to
2866 * eventually expire. We build the tarpit as an analyser.
2867 */
2868 if (txn->flags & TX_CLTARPIT) {
2869 buffer_erase(s->req);
2870 /* wipe the request out so that we can drop the connection early
2871 * if the client closes first.
2872 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002873 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002874 req->analysers = 0; /* remove switching rules etc... */
2875 req->analysers |= AN_REQ_HTTP_TARPIT;
2876 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2877 if (!req->analyse_exp)
2878 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002879 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002880 return 1;
2881 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002882 }
Willy Tarreau06619262006-12-17 08:37:22 +01002883
Willy Tarreau5b154472009-12-21 20:11:07 +01002884 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2885 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002886 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2887 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002888 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2889 * avoid to redo the same work if FE and BE have the same settings (common).
2890 * The method consists in checking if options changed between the two calls
2891 * (implying that either one is non-null, or one of them is non-null and we
2892 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002893 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002894
Willy Tarreaudc008c52010-02-01 16:20:08 +01002895 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2896 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2897 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2898 (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 +01002899 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002900
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002901 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2902 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002903 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002904 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2905 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002906 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002907 tmp = TX_CON_WANT_CLO;
2908
Willy Tarreau5b154472009-12-21 20:11:07 +01002909 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2910 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002911
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002912 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2913 /* parse the Connection header and possibly clean it */
2914 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002915 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002916 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2917 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002918 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002919 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002920 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002921 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002922 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002923
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002924 /* check if client or config asks for explicit close in KAL/SCL */
2925 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2927 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002928 (!(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 +01002929 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002930 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002931 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002932 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2933 }
Willy Tarreau78599912009-10-17 20:12:21 +02002934
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002935 /* we can be blocked here because the request needs to be authenticated,
2936 * either to pass or to access stats.
2937 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002938 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002939 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002940 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002941
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002942 if (!realm)
2943 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2944
Willy Tarreau844a7e72010-01-31 21:46:18 +01002945 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02002946 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002947 txn->status = 401;
2948 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002949 /* on 401 we still count one error, because normal browsing
2950 * won't significantly increase the counter but brute force
2951 * attempts will.
2952 */
2953 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002954 goto return_prx_cond;
2955 }
2956
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002957 /* add request headers from the rule sets in the same order */
2958 list_for_each_entry(wl, &px->req_add, list) {
2959 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002960 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002961 ret = acl_pass(ret);
2962 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2963 ret = !ret;
2964 if (!ret)
2965 continue;
2966 }
2967
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002968 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002969 goto return_bad_req;
2970 }
2971
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002972 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002973 struct stats_admin_rule *stats_admin_rule;
2974
2975 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002976 * FIXME!!! that one is rather dangerous, we want to
2977 * make it follow standard rules (eg: clear req->analysers).
2978 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002979
Cyril Bonté474be412010-10-12 00:14:36 +02002980 /* now check whether we have some admin rules for this request */
2981 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2982 int ret = 1;
2983
2984 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002985 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Cyril Bonté474be412010-10-12 00:14:36 +02002986 ret = acl_pass(ret);
2987 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2988 ret = !ret;
2989 }
2990
2991 if (ret) {
2992 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002993 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002994 break;
2995 }
2996 }
2997
Cyril Bonté70be45d2010-10-12 00:14:35 +02002998 /* Was the status page requested with a POST ? */
2999 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003000 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003001 if (msg->msg_state < HTTP_MSG_100_SENT) {
3002 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3003 * send an HTTP/1.1 100 Continue intermediate response.
3004 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003005 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003006 struct hdr_ctx ctx;
3007 ctx.idx = 0;
3008 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003009 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003010 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003011 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003012 }
3013 }
3014 msg->msg_state = HTTP_MSG_100_SENT;
3015 s->logs.tv_request = now; /* update the request timer to reflect full request */
3016 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003017 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003018 /* we need more data */
3019 req->analysers |= an_bit;
3020 buffer_dont_connect(req);
3021 return 0;
3022 }
Cyril Bonté474be412010-10-12 00:14:36 +02003023 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003024 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003025 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003026 }
3027
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003028 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003029 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003030 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003031 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +02003032 s->rep->prod->conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003033 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003034 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003035 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3036 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003037
3038 return 0;
3039
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003040 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003041
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 /* check whether we have some ACLs set to redirect this request */
3043 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003044 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003045
Willy Tarreauf285f542010-01-03 20:03:03 +01003046 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003047 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003048 ret = acl_pass(ret);
3049 if (rule->cond->pol == ACL_COND_UNLESS)
3050 ret = !ret;
3051 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003054 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 /* build redirect message */
3058 switch(rule->code) {
3059 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 msg_fmt = HTTP_303;
3061 break;
3062 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 msg_fmt = HTTP_301;
3064 break;
3065 case 302:
3066 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 msg_fmt = HTTP_302;
3068 break;
3069 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003070
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003071 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003072 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 switch(rule->type) {
3075 case REDIRECT_TYPE_PREFIX: {
3076 const char *path;
3077 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003078
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 path = http_get_path(txn);
3080 /* build message using path */
3081 if (path) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003082 pathlen = txn->req.sl.rq.u_l + (req->buf.p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3084 int qs = 0;
3085 while (qs < pathlen) {
3086 if (path[qs] == '?') {
3087 pathlen = qs;
3088 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003089 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003091 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003092 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 } else {
3094 path = "/";
3095 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003096 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003097
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003098 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003099 goto return_bad_req;
3100
3101 /* add prefix. Note that if prefix == "/", we don't want to
3102 * add anything, otherwise it makes it hard for the user to
3103 * configure a self-redirection.
3104 */
3105 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003106 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3107 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003108 }
3109
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 /* add path */
3111 memcpy(rdr.str + rdr.len, path, pathlen);
3112 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003113
3114 /* append a slash at the end of the location is needed and missing */
3115 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3116 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3117 if (rdr.len > rdr.size - 5)
3118 goto return_bad_req;
3119 rdr.str[rdr.len] = '/';
3120 rdr.len++;
3121 }
3122
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003123 break;
3124 }
3125 case REDIRECT_TYPE_LOCATION:
3126 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003127 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003128 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003129
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 /* add location */
3131 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3132 rdr.len += rule->rdr_len;
3133 break;
3134 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003135
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 if (rule->cookie_len) {
3137 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3138 rdr.len += 14;
3139 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3140 rdr.len += rule->cookie_len;
3141 memcpy(rdr.str + rdr.len, "\r\n", 2);
3142 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003143 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003144
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003145 /* add end of headers and the keep-alive/close status.
3146 * We may choose to set keep-alive if the Location begins
3147 * with a slash, because the client will come back to the
3148 * same server.
3149 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003150 txn->status = rule->code;
3151 /* let's log the request time */
3152 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003153
3154 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003155 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3156 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003157 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3158 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3159 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003160 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003161 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3162 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3163 rdr.len += 30;
3164 } else {
3165 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3166 rdr.len += 24;
3167 }
Willy Tarreau75661452010-01-10 10:35:01 +01003168 }
3169 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3170 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003171 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003172 /* "eat" the request */
Willy Tarreau572bf902012-07-02 17:01:20 +02003173 bi_fast_delete(&req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003174 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003175 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003176 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3177 txn->req.msg_state = HTTP_MSG_CLOSED;
3178 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003179 break;
3180 } else {
3181 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003182 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3183 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3184 rdr.len += 29;
3185 } else {
3186 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3187 rdr.len += 23;
3188 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003189 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003190 goto return_prx_cond;
3191 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003192 }
3193 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003194
Willy Tarreau2be39392010-01-03 17:24:51 +01003195 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3196 * If this happens, then the data will not come immediately, so we must
3197 * send all what we have without waiting. Note that due to the small gain
3198 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003199 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003200 * itself once used.
3201 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003202 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 /* that's OK for us now, let's move on to next analysers */
3205 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003207 return_bad_req:
3208 /* We centralize bad requests processing here */
3209 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3210 /* we detected a parsing error. We want to archive this request
3211 * in the dedicated proxy area for later troubleshooting.
3212 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003213 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003214 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003215
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003216 txn->req.msg_state = HTTP_MSG_ERROR;
3217 txn->status = 400;
3218 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003219
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003220 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003221 if (s->listener->counters)
3222 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003223
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 return_prx_cond:
3225 if (!(s->flags & SN_ERR_MASK))
3226 s->flags |= SN_ERR_PRXCOND;
3227 if (!(s->flags & SN_FINST_MASK))
3228 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003230 req->analysers = 0;
3231 req->analyse_exp = TICK_ETERNITY;
3232 return 0;
3233}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003234
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003235/* This function performs all the processing enabled for the current request.
3236 * It returns 1 if the processing can continue on next analysers, or zero if it
3237 * needs more data, encounters an error, or wants to immediately abort the
3238 * request. It relies on buffers flags, and updates s->req->analysers.
3239 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003240int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003241{
3242 struct http_txn *txn = &s->txn;
3243 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003244
Willy Tarreau655dce92009-11-08 13:10:58 +01003245 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003246 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003247 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003248 return 0;
3249 }
3250
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003251 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003252 now_ms, __FUNCTION__,
3253 s,
3254 req,
3255 req->rex, req->wex,
3256 req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003257 req->buf.i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003258 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003259
Willy Tarreau59234e92008-11-30 23:51:27 +01003260 /*
3261 * Right now, we know that we have processed the entire headers
3262 * and that unwanted requests have been filtered out. We can do
3263 * whatever we want with the remaining request. Also, now we
3264 * may have separate values for ->fe, ->be.
3265 */
Willy Tarreau06619262006-12-17 08:37:22 +01003266
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 /*
3268 * If HTTP PROXY is set we simply get remote server address
3269 * parsing incoming request.
3270 */
3271 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003272 url2sa(req->buf.p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003273 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003274
Willy Tarreau59234e92008-11-30 23:51:27 +01003275 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003276 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003277 * Note that doing so might move headers in the request, but
3278 * the fields will stay coherent and the URI will not move.
3279 * This should only be performed in the backend.
3280 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003281 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003282 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3283 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003284
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003286 * 8: the appsession cookie was looked up very early in 1.2,
3287 * so let's do the same now.
3288 */
3289
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003290 /* It needs to look into the URI unless persistence must be ignored */
3291 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02003292 get_srv_from_appsession(s, req->buf.p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003293 }
3294
William Lallemanda73203e2012-03-12 12:48:57 +01003295 /* add unique-id if "header-unique-id" is specified */
3296
3297 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3298 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3299
3300 if (s->fe->header_unique_id && s->unique_id) {
3301 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3302 if (ret < 0 || ret > global.tune.bufsize)
3303 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003304 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003305 goto return_bad_req;
3306 }
3307
Cyril Bontéb21570a2009-11-29 20:04:48 +01003308 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 * 9: add X-Forwarded-For if either the frontend or the backend
3310 * asks for it.
3311 */
3312 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003313 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003314 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003315 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3316 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau572bf902012-07-02 17:01:20 +02003317 req->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003318 /* The header is set to be added only if none is present
3319 * and we found it, so don't do anything.
3320 */
3321 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003322 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003323 /* Add an X-Forwarded-For header unless the source IP is
3324 * in the 'except' network range.
3325 */
3326 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003327 (((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 +01003328 != s->fe->except_net.s_addr) &&
3329 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003330 (((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 +01003331 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003332 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003334 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003335
3336 /* Note: we rely on the backend to get the header name to be used for
3337 * x-forwarded-for, because the header is really meant for the backends.
3338 * However, if the backend did not specify any option, we have to rely
3339 * on the frontend's header name.
3340 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003341 if (s->be->fwdfor_hdr_len) {
3342 len = s->be->fwdfor_hdr_len;
3343 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003344 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 len = s->fe->fwdfor_hdr_len;
3346 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003347 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003348 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003349
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003350 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003351 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003352 }
3353 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003354 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003355 /* FIXME: for the sake of completeness, we should also support
3356 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003357 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003358 int len;
3359 char pn[INET6_ADDRSTRLEN];
3360 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003361 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003362 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003363
Willy Tarreau59234e92008-11-30 23:51:27 +01003364 /* Note: we rely on the backend to get the header name to be used for
3365 * x-forwarded-for, because the header is really meant for the backends.
3366 * However, if the backend did not specify any option, we have to rely
3367 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003368 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003369 if (s->be->fwdfor_hdr_len) {
3370 len = s->be->fwdfor_hdr_len;
3371 memcpy(trash, s->be->fwdfor_hdr_name, len);
3372 } else {
3373 len = s->fe->fwdfor_hdr_len;
3374 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003375 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003376 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003377
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003378 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003379 goto return_bad_req;
3380 }
3381 }
3382
3383 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003384 * 10: add X-Original-To if either the frontend or the backend
3385 * asks for it.
3386 */
3387 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3388
3389 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003390 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003391 /* Add an X-Original-To header unless the destination IP is
3392 * in the 'except' network range.
3393 */
Willy Tarreau59b94792012-05-11 16:16:40 +02003394 si_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003395
Willy Tarreau6471afb2011-09-23 10:54:59 +02003396 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003397 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003398 (((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 +02003399 != s->fe->except_to.s_addr) &&
3400 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003401 (((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 +02003402 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003403 int len;
3404 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003405 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003406
3407 /* Note: we rely on the backend to get the header name to be used for
3408 * x-original-to, because the header is really meant for the backends.
3409 * However, if the backend did not specify any option, we have to rely
3410 * on the frontend's header name.
3411 */
3412 if (s->be->orgto_hdr_len) {
3413 len = s->be->orgto_hdr_len;
3414 memcpy(trash, s->be->orgto_hdr_name, len);
3415 } else {
3416 len = s->fe->orgto_hdr_len;
3417 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003418 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003419 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3420
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003421 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003422 goto return_bad_req;
3423 }
3424 }
3425 }
3426
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003427 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3428 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003429 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003430 unsigned int want_flags = 0;
3431
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003432 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003433 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3434 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3435 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003436 want_flags |= TX_CON_CLO_SET;
3437 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003438 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3439 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3440 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003441 want_flags |= TX_CON_KAL_SET;
3442 }
3443
3444 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003445 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003446 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003447
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003448
Willy Tarreau522d6c02009-12-06 18:49:18 +01003449 /* If we have no server assigned yet and we're balancing on url_param
3450 * with a POST request, we may be interested in checking the body for
3451 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003452 */
3453 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3454 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003455 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003456 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003457 buffer_dont_connect(req);
3458 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003459 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003460
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003461 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003462 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003463#ifdef TCP_QUICKACK
3464 /* We expect some data from the client. Unless we know for sure
3465 * we already have a full request, we have to re-enable quick-ack
3466 * in case we previously disabled it, otherwise we might cause
3467 * the client to delay further data.
3468 */
3469 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003470 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02003471 (msg->body_len > req->buf.i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003472 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003473#endif
3474 }
Willy Tarreau03945942009-12-22 16:50:27 +01003475
Willy Tarreau59234e92008-11-30 23:51:27 +01003476 /*************************************************************
3477 * OK, that's finished for the headers. We have done what we *
3478 * could. Let's switch to the DATA state. *
3479 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003480 req->analyse_exp = TICK_ETERNITY;
3481 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003482
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003483 /* if the server closes the connection, we want to immediately react
3484 * and close the socket to save packets and syscalls.
3485 */
3486 req->cons->flags |= SI_FL_NOHALF;
3487
Willy Tarreau59234e92008-11-30 23:51:27 +01003488 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003489 /* OK let's go on with the BODY now */
3490 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003491
Willy Tarreau59234e92008-11-30 23:51:27 +01003492 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003493 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003494 /* we detected a parsing error. We want to archive this request
3495 * in the dedicated proxy area for later troubleshooting.
3496 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003497 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003498 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 txn->req.msg_state = HTTP_MSG_ERROR;
3501 txn->status = 400;
3502 req->analysers = 0;
3503 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003504
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003505 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003506 if (s->listener->counters)
3507 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003508
Willy Tarreau59234e92008-11-30 23:51:27 +01003509 if (!(s->flags & SN_ERR_MASK))
3510 s->flags |= SN_ERR_PRXCOND;
3511 if (!(s->flags & SN_FINST_MASK))
3512 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003513 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003514}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003515
Willy Tarreau60b85b02008-11-30 23:28:40 +01003516/* This function is an analyser which processes the HTTP tarpit. It always
3517 * returns zero, at the beginning because it prevents any other processing
3518 * from occurring, and at the end because it terminates the request.
3519 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003520int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003521{
3522 struct http_txn *txn = &s->txn;
3523
3524 /* This connection is being tarpitted. The CLIENT side has
3525 * already set the connect expiration date to the right
3526 * timeout. We just have to check that the client is still
3527 * there and that the timeout has not expired.
3528 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003529 buffer_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003530 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003531 !tick_is_expired(req->analyse_exp, now_ms))
3532 return 0;
3533
3534 /* We will set the queue timer to the time spent, just for
3535 * logging purposes. We fake a 500 server error, so that the
3536 * attacker will not suspect his connection has been tarpitted.
3537 * It will not cause trouble to the logs because we can exclude
3538 * the tarpitted connections by filtering on the 'PT' status flags.
3539 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003540 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3541
3542 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003543 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau60b85b02008-11-30 23:28:40 +01003544 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3545
3546 req->analysers = 0;
3547 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003548
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003549 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003550 if (s->listener->counters)
3551 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003552
Willy Tarreau60b85b02008-11-30 23:28:40 +01003553 if (!(s->flags & SN_ERR_MASK))
3554 s->flags |= SN_ERR_PRXCOND;
3555 if (!(s->flags & SN_FINST_MASK))
3556 s->flags |= SN_FINST_T;
3557 return 0;
3558}
3559
Willy Tarreaud34af782008-11-30 23:36:37 +01003560/* This function is an analyser which processes the HTTP request body. It looks
3561 * for parameters to be used for the load balancing algorithm (url_param). It
3562 * must only be called after the standard HTTP request processing has occurred,
3563 * because it expects the request to be parsed. It returns zero if it needs to
3564 * read more data, or 1 once it has completed its analysis.
3565 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003566int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003567{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003568 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003569 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003570 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003571
3572 /* We have to parse the HTTP request body to find any required data.
3573 * "balance url_param check_post" should have been the only way to get
3574 * into this. We were brought here after HTTP header analysis, so all
3575 * related structures are ready.
3576 */
3577
Willy Tarreau522d6c02009-12-06 18:49:18 +01003578 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3579 goto missing_data;
3580
3581 if (msg->msg_state < HTTP_MSG_100_SENT) {
3582 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3583 * send an HTTP/1.1 100 Continue intermediate response.
3584 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003585 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003586 struct hdr_ctx ctx;
3587 ctx.idx = 0;
3588 /* Expect is allowed in 1.1, look for it */
Willy Tarreau572bf902012-07-02 17:01:20 +02003589 if (http_find_header2("Expect", 6, req->buf.p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003591 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003592 }
3593 }
3594 msg->msg_state = HTTP_MSG_100_SENT;
3595 }
3596
3597 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003598 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02003599 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003600 * is still null. We must save the body in msg->next because it
3601 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003602 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003603 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003604
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003605 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003606 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3607 else
3608 msg->msg_state = HTTP_MSG_DATA;
3609 }
3610
3611 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003612 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003613 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003614 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003615 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003616 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003617
Willy Tarreau115acb92009-12-26 13:56:06 +01003618 if (!ret)
3619 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003620 else if (ret < 0) {
3621 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003623 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003624 }
3625
Willy Tarreaud98cf932009-12-27 22:54:55 +01003626 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003627 * We have the first data byte is in msg->sov. We're waiting for at
3628 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003629 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630
Willy Tarreau124d9912011-03-01 20:30:48 +01003631 if (msg->body_len < limit)
3632 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633
Willy Tarreau572bf902012-07-02 17:01:20 +02003634 if (req->buf.i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003635 goto http_end;
3636
3637 missing_data:
3638 /* we get here if we need to wait for more data */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02003639 if (buffer_full(&req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003640 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003641 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003642 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003643
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003644 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003645 txn->status = 408;
3646 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003647
3648 if (!(s->flags & SN_ERR_MASK))
3649 s->flags |= SN_ERR_CLITO;
3650 if (!(s->flags & SN_FINST_MASK))
3651 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003652 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003653 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654
3655 /* we get here if we need to wait for more data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003656 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(&req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003657 /* Not enough data. We'll re-use the http-request
3658 * timeout here. Ideally, we should set the timeout
3659 * relative to the accept() date. We just set the
3660 * request timeout once at the beginning of the
3661 * request.
3662 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003663 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003664 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003665 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003666 return 0;
3667 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003668
3669 http_end:
3670 /* The situation will not evolve, so let's give up on the analysis. */
3671 s->logs.tv_request = now; /* update the request timer to reflect full request */
3672 req->analysers &= ~an_bit;
3673 req->analyse_exp = TICK_ETERNITY;
3674 return 1;
3675
3676 return_bad_req: /* let's centralize all bad requests */
3677 txn->req.msg_state = HTTP_MSG_ERROR;
3678 txn->status = 400;
3679 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3680
Willy Tarreau79ebac62010-06-07 13:47:49 +02003681 if (!(s->flags & SN_ERR_MASK))
3682 s->flags |= SN_ERR_PRXCOND;
3683 if (!(s->flags & SN_FINST_MASK))
3684 s->flags |= SN_FINST_R;
3685
Willy Tarreau522d6c02009-12-06 18:49:18 +01003686 return_err_msg:
3687 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003688 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003689 if (s->listener->counters)
3690 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003691 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003692}
3693
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003694/* send a server's name with an outgoing request over an established connection.
3695 * Note: this function is designed to be called once the request has been scheduled
3696 * for being forwarded. This is the reason why it rewinds the buffer before
3697 * proceeding.
3698 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003699int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003700
3701 struct hdr_ctx ctx;
3702
Mark Lamourinec2247f02012-01-04 13:02:01 -05003703 char *hdr_name = be->server_id_hdr_name;
3704 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau7421efb2012-07-02 15:11:27 +02003705 struct channel *req = txn->req.buf;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003706 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003707 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003708
William Lallemandd9e90662012-01-30 17:27:17 +01003709 ctx.idx = 0;
3710
Willy Tarreau572bf902012-07-02 17:01:20 +02003711 old_o = req->buf.o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003712 if (old_o) {
3713 /* The request was already skipped, let's restore it */
Willy Tarreaua75bcef2012-08-24 22:56:11 +02003714 b_rew(&req->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003715 }
3716
Willy Tarreau572bf902012-07-02 17:01:20 +02003717 old_i = req->buf.i;
3718 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->buf.p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003719 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003720 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003721 }
3722
3723 /* Add the new header requested with the server value */
3724 hdr_val = trash;
3725 memcpy(hdr_val, hdr_name, hdr_name_len);
3726 hdr_val += hdr_name_len;
3727 *hdr_val++ = ':';
3728 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003729 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003730 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003731
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003732 if (old_o) {
3733 /* If this was a forwarded request, we must readjust the amount of
3734 * data to be forwarded in order to take into account the size
3735 * variations.
3736 */
Willy Tarreaua75bcef2012-08-24 22:56:11 +02003737 b_adv(&req->buf, old_o + req->buf.i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003738 }
3739
Mark Lamourinec2247f02012-01-04 13:02:01 -05003740 return 0;
3741}
3742
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743/* Terminate current transaction and prepare a new one. This is very tricky
3744 * right now but it works.
3745 */
3746void http_end_txn_clean_session(struct session *s)
3747{
3748 /* FIXME: We need a more portable way of releasing a backend's and a
3749 * server's connections. We need a safer way to reinitialize buffer
3750 * flags. We also need a more accurate method for computing per-request
3751 * data.
3752 */
3753 http_silent_debug(__LINE__, s);
3754
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003755 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003756 si_shutr(s->req->cons);
3757 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003758
3759 http_silent_debug(__LINE__, s);
3760
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003761 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003763 if (unlikely(s->srv_conn))
3764 sess_change_server(s, NULL);
3765 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003766
3767 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3768 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003769 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770
3771 if (s->txn.status) {
3772 int n;
3773
3774 n = s->txn.status / 100;
3775 if (n < 1 || n > 5)
3776 n = 0;
3777
3778 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003779 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780
Willy Tarreau24657792010-02-26 10:30:28 +01003781 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003783 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 }
3785
3786 /* don't count other requests' data */
Willy Tarreau572bf902012-07-02 17:01:20 +02003787 s->logs.bytes_in -= s->req->buf.i;
3788 s->logs.bytes_out -= s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789
3790 /* let's do a final log if we need it */
3791 if (s->logs.logwait &&
3792 !(s->flags & SN_MONITOR) &&
3793 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3794 s->do_log(s);
3795 }
3796
3797 s->logs.accept_date = date; /* user-visible date for logging */
3798 s->logs.tv_accept = now; /* corrected date for internal use */
3799 tv_zero(&s->logs.tv_request);
3800 s->logs.t_queue = -1;
3801 s->logs.t_connect = -1;
3802 s->logs.t_data = -1;
3803 s->logs.t_close = 0;
3804 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3805 s->logs.srv_queue_size = 0; /* we will get this number soon */
3806
Willy Tarreau572bf902012-07-02 17:01:20 +02003807 s->logs.bytes_in = s->req->total = s->req->buf.i;
3808 s->logs.bytes_out = s->rep->total = s->rep->buf.i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809
3810 if (s->pend_pos)
3811 pendconn_free(s->pend_pos);
3812
Willy Tarreau827aee92011-03-10 16:55:02 +01003813 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 if (s->flags & SN_CURR_SESS) {
3815 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003816 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003818 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3819 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 }
3821
Willy Tarreau9e000c62011-03-10 14:03:36 +01003822 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823
3824 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003825 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +02003826 s->req->cons->conn.flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003828 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 s->req->cons->err_loc = NULL;
3830 s->req->cons->exp = TICK_ETERNITY;
3831 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003832 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
3833 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003834 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 +01003835 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3836 s->txn.meth = 0;
3837 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003838 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003839 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840 s->req->cons->flags |= SI_FL_INDEP_STR;
3841
Willy Tarreau96e31212011-05-30 18:10:30 +02003842 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003843 s->req->flags |= CF_NEVER_WAIT;
3844 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003845 }
3846
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847 /* if the request buffer is not empty, it means we're
3848 * about to process another request, so send pending
3849 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003850 * Just don't do this if the buffer is close to be full,
3851 * because the request will wait for it to flush a little
3852 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 */
Willy Tarreau572bf902012-07-02 17:01:20 +02003854 if (s->req->buf.i) {
3855 if (s->rep->buf.o &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02003856 !buffer_full(&s->rep->buf, global.tune.maxrewrite) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02003857 bi_end(&s->rep->buf) <= s->rep->buf.data + s->rep->buf.size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003858 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01003859 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003860
3861 /* we're removing the analysers, we MUST re-enable events detection */
3862 buffer_auto_read(s->req);
3863 buffer_auto_close(s->req);
3864 buffer_auto_read(s->rep);
3865 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866
Willy Tarreau342b11c2010-11-24 16:22:09 +01003867 s->req->analysers = s->listener->analysers;
3868 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869 s->rep->analysers = 0;
3870
3871 http_silent_debug(__LINE__, s);
3872}
3873
3874
3875/* This function updates the request state machine according to the response
3876 * state machine and buffer flags. It returns 1 if it changes anything (flag
3877 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3878 * it is only used to find when a request/response couple is complete. Both
3879 * this function and its equivalent should loop until both return zero. It
3880 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3881 */
3882int http_sync_req_state(struct session *s)
3883{
Willy Tarreau7421efb2012-07-02 15:11:27 +02003884 struct channel *buf = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 struct http_txn *txn = &s->txn;
3886 unsigned int old_flags = buf->flags;
3887 unsigned int old_state = txn->req.msg_state;
3888
3889 http_silent_debug(__LINE__, s);
3890 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3891 return 0;
3892
3893 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003894 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003895 * We can shut the read side unless we want to abort_on_close,
3896 * or we have a POST request. The issue with POST requests is
3897 * that some browsers still send a CRLF after the request, and
3898 * this CRLF must be read so that it does not remain in the kernel
3899 * buffers, otherwise a close could cause an RST on some systems
3900 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003901 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003902 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003903 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904
3905 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3906 goto wait_other_side;
3907
3908 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3909 /* The server has not finished to respond, so we
3910 * don't want to move in order not to upset it.
3911 */
3912 goto wait_other_side;
3913 }
3914
3915 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3916 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003917 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 txn->req.msg_state = HTTP_MSG_TUNNEL;
3919 goto wait_other_side;
3920 }
3921
3922 /* When we get here, it means that both the request and the
3923 * response have finished receiving. Depending on the connection
3924 * mode, we'll have to wait for the last bytes to leave in either
3925 * direction, and sometimes for a close to be effective.
3926 */
3927
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003928 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3929 /* Server-close mode : queue a connection close to the server */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003930 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003931 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003932 }
3933 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3934 /* Option forceclose is set, or either side wants to close,
3935 * let's enforce it now that we're not expecting any new
3936 * data to come. The caller knows the session is complete
3937 * once both states are CLOSED.
3938 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003939 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 buffer_shutr_now(buf);
3941 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003943 }
3944 else {
3945 /* The last possible modes are keep-alive and tunnel. Since tunnel
3946 * mode does not set the body analyser, we can't reach this place
3947 * in tunnel mode, so we're left with keep-alive only.
3948 * This mode is currently not implemented, we switch to tunnel mode.
3949 */
3950 buffer_auto_read(buf);
3951 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 }
3953
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003954 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003955 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003956 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3957
Willy Tarreau8e21bb92012-08-24 22:40:29 +02003958 if (!channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003959 txn->req.msg_state = HTTP_MSG_CLOSING;
3960 goto http_msg_closing;
3961 }
3962 else {
3963 txn->req.msg_state = HTTP_MSG_CLOSED;
3964 goto http_msg_closed;
3965 }
3966 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003967 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968 }
3969
3970 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3971 http_msg_closing:
3972 /* nothing else to forward, just waiting for the output buffer
3973 * to be empty and for the shutw_now to take effect.
3974 */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02003975 if (channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 txn->req.msg_state = HTTP_MSG_CLOSED;
3977 goto http_msg_closed;
3978 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003979 else if (buf->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 txn->req.msg_state = HTTP_MSG_ERROR;
3981 goto wait_other_side;
3982 }
3983 }
3984
3985 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3986 http_msg_closed:
3987 goto wait_other_side;
3988 }
3989
3990 wait_other_side:
3991 http_silent_debug(__LINE__, s);
3992 return txn->req.msg_state != old_state || buf->flags != old_flags;
3993}
3994
3995
3996/* This function updates the response state machine according to the request
3997 * state machine and buffer flags. It returns 1 if it changes anything (flag
3998 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3999 * it is only used to find when a request/response couple is complete. Both
4000 * this function and its equivalent should loop until both return zero. It
4001 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4002 */
4003int http_sync_res_state(struct session *s)
4004{
Willy Tarreau7421efb2012-07-02 15:11:27 +02004005 struct channel *buf = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 struct http_txn *txn = &s->txn;
4007 unsigned int old_flags = buf->flags;
4008 unsigned int old_state = txn->rsp.msg_state;
4009
4010 http_silent_debug(__LINE__, s);
4011 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4012 return 0;
4013
4014 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4015 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004016 * still monitor the server connection for a possible close
4017 * while the request is being uploaded, so we don't disable
4018 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004020 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021
4022 if (txn->req.msg_state == HTTP_MSG_ERROR)
4023 goto wait_other_side;
4024
4025 if (txn->req.msg_state < HTTP_MSG_DONE) {
4026 /* The client seems to still be sending data, probably
4027 * because we got an error response during an upload.
4028 * We have the choice of either breaking the connection
4029 * or letting it pass through. Let's do the later.
4030 */
4031 goto wait_other_side;
4032 }
4033
4034 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4035 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004036 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4038 goto wait_other_side;
4039 }
4040
4041 /* When we get here, it means that both the request and the
4042 * response have finished receiving. Depending on the connection
4043 * mode, we'll have to wait for the last bytes to leave in either
4044 * direction, and sometimes for a close to be effective.
4045 */
4046
4047 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4048 /* Server-close mode : shut read and wait for the request
4049 * side to close its output buffer. The caller will detect
4050 * when we're in DONE and the other is in CLOSED and will
4051 * catch that for the final cleanup.
4052 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004053 if (!(buf->flags & (CF_SHUTR|CF_SHUTR_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004055 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004056 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4057 /* Option forceclose is set, or either side wants to close,
4058 * let's enforce it now that we're not expecting any new
4059 * data to come. The caller knows the session is complete
4060 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004062 if (!(buf->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004063 buffer_shutr_now(buf);
4064 buffer_shutw_now(buf);
4065 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 }
4067 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004068 /* The last possible modes are keep-alive and tunnel. Since tunnel
4069 * mode does not set the body analyser, we can't reach this place
4070 * in tunnel mode, so we're left with keep-alive only.
4071 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004073 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075 }
4076
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004077 if (buf->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 /* if we've just closed an output, let's switch */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004079 if (!channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4081 goto http_msg_closing;
4082 }
4083 else {
4084 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4085 goto http_msg_closed;
4086 }
4087 }
4088 goto wait_other_side;
4089 }
4090
4091 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4092 http_msg_closing:
4093 /* nothing else to forward, just waiting for the output buffer
4094 * to be empty and for the shutw_now to take effect.
4095 */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02004096 if (channel_is_empty(buf)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4098 goto http_msg_closed;
4099 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004100 else if (buf->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004101 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004102 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004103 if (target_srv(&s->target))
4104 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 goto wait_other_side;
4106 }
4107 }
4108
4109 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4110 http_msg_closed:
4111 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004112 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004114 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 goto wait_other_side;
4116 }
4117
4118 wait_other_side:
4119 http_silent_debug(__LINE__, s);
4120 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4121}
4122
4123
4124/* Resync the request and response state machines. Return 1 if either state
4125 * changes.
4126 */
4127int http_resync_states(struct session *s)
4128{
4129 struct http_txn *txn = &s->txn;
4130 int old_req_state = txn->req.msg_state;
4131 int old_res_state = txn->rsp.msg_state;
4132
4133 http_silent_debug(__LINE__, s);
4134 http_sync_req_state(s);
4135 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004136 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 if (!http_sync_res_state(s))
4138 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004139 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140 if (!http_sync_req_state(s))
4141 break;
4142 }
4143 http_silent_debug(__LINE__, s);
4144 /* OK, both state machines agree on a compatible state.
4145 * There are a few cases we're interested in :
4146 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4147 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4148 * directions, so let's simply disable both analysers.
4149 * - HTTP_MSG_CLOSED on the response only means we must abort the
4150 * request.
4151 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4152 * with server-close mode means we've completed one request and we
4153 * must re-initialize the server connection.
4154 */
4155
4156 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4157 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4158 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4159 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4160 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004161 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004162 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004163 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004164 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004165 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004167 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4168 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004169 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004170 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004171 s->rep->analysers = 0;
4172 buffer_auto_close(s->rep);
4173 buffer_auto_read(s->rep);
4174 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 buffer_abort(s->req);
4176 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004177 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004178 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 }
4180 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4181 txn->rsp.msg_state == HTTP_MSG_DONE &&
4182 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4183 /* server-close: terminate this server connection and
4184 * reinitialize a fresh-new transaction.
4185 */
4186 http_end_txn_clean_session(s);
4187 }
4188
4189 http_silent_debug(__LINE__, s);
4190 return txn->req.msg_state != old_req_state ||
4191 txn->rsp.msg_state != old_res_state;
4192}
4193
Willy Tarreaud98cf932009-12-27 22:54:55 +01004194/* This function is an analyser which forwards request body (including chunk
4195 * sizes if any). It is called as soon as we must forward, even if we forward
4196 * zero byte. The only situation where it must not be called is when we're in
4197 * tunnel mode and we want to forward till the close. It's used both to forward
4198 * remaining data and to resync after end of body. It expects the msg_state to
4199 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4200 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004201 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004202 * bytes of pending data + the headers if not already done (between sol and sov).
4203 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004204 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004205int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004206{
4207 struct http_txn *txn = &s->txn;
4208 struct http_msg *msg = &s->txn.req;
4209
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004210 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4211 return 0;
4212
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004213 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
4214 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf.o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004215 /* Output closed while we were sending data. We must abort and
4216 * wake the other side up.
4217 */
4218 msg->msg_state = HTTP_MSG_ERROR;
4219 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004220 return 1;
4221 }
4222
Willy Tarreau4fe41902010-06-07 22:27:41 +02004223 /* in most states, we should abort in case of early close */
4224 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004225
4226 /* Note that we don't have to send 100-continue back because we don't
4227 * need the data to complete our job, and it's up to the server to
4228 * decide whether to return 100, 417 or anything else in return of
4229 * an "Expect: 100-continue" header.
4230 */
4231
4232 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004233 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02004234 * req->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004235 * is still null. We must save the body in msg->next because it
4236 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004238 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004239
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004240 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4242 else {
4243 msg->msg_state = HTTP_MSG_DATA;
4244 }
4245 }
4246
Willy Tarreaud98cf932009-12-27 22:54:55 +01004247 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004248 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004249
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004251 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004252 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004253 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004254 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004255 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004256 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004257 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004258 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004259
Willy Tarreaucaabe412010-01-03 23:08:28 +01004260 if (msg->msg_state == HTTP_MSG_DATA) {
4261 /* must still forward */
4262 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004263 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004264
4265 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004266 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004267 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004269 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004270 }
4271 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004272 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004273 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274 * TRAILERS state.
4275 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004276 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004277
4278 if (!ret)
4279 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004280 else if (ret < 0) {
4281 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004282 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004283 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004284 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004285 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004286 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004287 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004288 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4289 /* we want the CRLF after the data */
4290 int ret;
4291
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004292 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004293
4294 if (ret == 0)
4295 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004296 else if (ret < 0) {
4297 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004298 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004299 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004300 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004301 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004302 /* we're in MSG_CHUNK_SIZE now */
4303 }
4304 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004305 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004306
4307 if (ret == 0)
4308 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004309 else if (ret < 0) {
4310 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004311 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004312 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004313 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004314 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004315 /* we're in HTTP_MSG_DONE now */
4316 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004318 int old_state = msg->msg_state;
4319
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004321 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004322 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4323 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4324 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325 if (http_resync_states(s)) {
4326 /* some state changes occurred, maybe the analyser
4327 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004328 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004329 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004330 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004331 /* request errors are most likely due to
4332 * the server aborting the transfer.
4333 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004334 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004335 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004336 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004337 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004339 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004341 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004342
4343 /* If "option abortonclose" is set on the backend, we
4344 * want to monitor the client's connection and forward
4345 * any shutdown notification to the server, which will
4346 * decide whether to close or to go on processing the
4347 * request.
4348 */
4349 if (s->be->options & PR_O_ABRT_CLOSE) {
4350 buffer_auto_read(req);
4351 buffer_auto_close(req);
4352 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004353 else if (s->txn.meth == HTTP_METH_POST) {
4354 /* POST requests may require to read extra CRLF
4355 * sent by broken browsers and which could cause
4356 * an RST to be sent upon close on some systems
4357 * (eg: Linux).
4358 */
4359 buffer_auto_read(req);
4360 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004361
Willy Tarreau610ecce2010-01-04 21:15:02 +01004362 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004363 }
4364 }
4365
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004367 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004368 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004369 if (!(s->flags & SN_ERR_MASK))
4370 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004371 if (!(s->flags & SN_FINST_MASK)) {
4372 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4373 s->flags |= SN_FINST_H;
4374 else
4375 s->flags |= SN_FINST_D;
4376 }
4377
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004378 s->fe->fe_counters.cli_aborts++;
4379 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004380 if (target_srv(&s->target))
4381 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004382
4383 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004384 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004385
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004386 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004387 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004388 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004390 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004391 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004392 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004393 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004394 buffer_dont_close(req);
4395
Willy Tarreau5c620922011-05-11 19:56:11 +02004396 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004397 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004398 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004399 * modes are already handled by the stream sock layer. We must not do
4400 * this in content-length mode because it could present the MSG_MORE
4401 * flag with the last block of forwarded data, which would cause an
4402 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004403 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004404 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004405 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004406
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004408 return 0;
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004411 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004412 if (s->listener->counters)
4413 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004414 return_bad_req_stats_ok:
4415 txn->req.msg_state = HTTP_MSG_ERROR;
4416 if (txn->status) {
4417 /* Note: we don't send any error if some data were already sent */
4418 stream_int_retnclose(req->prod, NULL);
4419 } else {
4420 txn->status = 400;
4421 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4422 }
4423 req->analysers = 0;
4424 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004425
4426 if (!(s->flags & SN_ERR_MASK))
4427 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004428 if (!(s->flags & SN_FINST_MASK)) {
4429 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4430 s->flags |= SN_FINST_H;
4431 else
4432 s->flags |= SN_FINST_D;
4433 }
4434 return 0;
4435
4436 aborted_xfer:
4437 txn->req.msg_state = HTTP_MSG_ERROR;
4438 if (txn->status) {
4439 /* Note: we don't send any error if some data were already sent */
4440 stream_int_retnclose(req->prod, NULL);
4441 } else {
4442 txn->status = 502;
4443 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4444 }
4445 req->analysers = 0;
4446 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4447
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004448 s->fe->fe_counters.srv_aborts++;
4449 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004450 if (target_srv(&s->target))
4451 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452
4453 if (!(s->flags & SN_ERR_MASK))
4454 s->flags |= SN_ERR_SRVCL;
4455 if (!(s->flags & SN_FINST_MASK)) {
4456 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4457 s->flags |= SN_FINST_H;
4458 else
4459 s->flags |= SN_FINST_D;
4460 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004461 return 0;
4462}
4463
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4465 * processing can continue on next analysers, or zero if it either needs more
4466 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4467 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4468 * when it has nothing left to do, and may remove any analyser when it wants to
4469 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004470 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004471int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004472{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004473 struct http_txn *txn = &s->txn;
4474 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004475 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004476 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004478 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004479
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004480 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004481 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004483 rep,
4484 rep->rex, rep->wex,
4485 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004486 rep->buf.i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004487 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 /*
4490 * Now parse the partial (or complete) lines.
4491 * We will check the response syntax, and also join multi-line
4492 * headers. An index of all the lines will be elaborated while
4493 * parsing.
4494 *
4495 * For the parsing, we use a 28 states FSM.
4496 *
4497 * Here is the information we currently have :
Willy Tarreau572bf902012-07-02 17:01:20 +02004498 * rep->buf.p = beginning of response
4499 * rep->buf.p + msg->eoh = end of processed headers / start of current one
4500 * rep->buf.p + rep->buf.i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004501 * msg->eol = end of current header or line (LF or CRLF)
4502 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503 */
4504
Willy Tarreau83e3af02009-12-28 17:39:57 +01004505 /* There's a protected area at the end of the buffer for rewriting
4506 * purposes. We don't want to start to parse the request if the
4507 * protected area is affected, because we may have to move processed
4508 * data later, which is much more complicated.
4509 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004510 if (buffer_not_empty(&rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004511 if (unlikely(channel_full(rep) ||
Willy Tarreau572bf902012-07-02 17:01:20 +02004512 bi_end(&rep->buf) < b_ptr(&rep->buf, msg->next) ||
4513 bi_end(&rep->buf) > rep->buf.data + rep->buf.size - global.tune.maxrewrite)) {
4514 if (rep->buf.o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004515 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004516 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004517 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004518 buffer_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004519 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004520 return 0;
4521 }
Willy Tarreau572bf902012-07-02 17:01:20 +02004522 if (rep->buf.i <= rep->buf.size - global.tune.maxrewrite)
4523 buffer_slow_realign(&msg->buf->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004524 }
4525
Willy Tarreau572bf902012-07-02 17:01:20 +02004526 if (likely(msg->next < rep->buf.i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004527 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004528 }
4529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 /* 1: we might have to print this header in debug mode */
4531 if (unlikely((global.mode & MODE_DEBUG) &&
4532 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004533 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004535
Willy Tarreau572bf902012-07-02 17:01:20 +02004536 sol = rep->buf.p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004537 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004539
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 sol += hdr_idx_first_pos(&txn->hdr_idx);
4541 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004542
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543 while (cur_idx) {
4544 eol = sol + txn->hdr_idx.v[cur_idx].len;
4545 debug_hdr("srvhdr", s, sol, eol);
4546 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4547 cur_idx = txn->hdr_idx.v[cur_idx].next;
4548 }
4549 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 /*
4552 * Now we quickly check if we have found a full valid response.
4553 * If not so, we check the FD and buffer states before leaving.
4554 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004555 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 * responses are checked first.
4557 *
4558 * Depending on whether the client is still there or not, we
4559 * may send an error response back or not. Note that normally
4560 * we should only check for HTTP status there, and check I/O
4561 * errors somewhere else.
4562 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004563
Willy Tarreau655dce92009-11-08 13:10:58 +01004564 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 /* Invalid response */
4566 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4567 /* we detected a parsing error. We want to archive this response
4568 * in the dedicated proxy area for later troubleshooting.
4569 */
4570 hdr_response_bad:
4571 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004572 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004574 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004575 if (target_srv(&s->target)) {
4576 target_srv(&s->target)->counters.failed_resp++;
4577 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004578 }
Willy Tarreau64648412010-03-05 10:41:54 +01004579 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004580 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 rep->analysers = 0;
4582 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004583 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004584 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4586
4587 if (!(s->flags & SN_ERR_MASK))
4588 s->flags |= SN_ERR_PRXCOND;
4589 if (!(s->flags & SN_FINST_MASK))
4590 s->flags |= SN_FINST_H;
4591
4592 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004593 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 /* too large response does not fit in buffer. */
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004596 else if (buffer_full(&rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004597 if (msg->err_pos < 0)
Willy Tarreau572bf902012-07-02 17:01:20 +02004598 msg->err_pos = rep->buf.i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 goto hdr_response_bad;
4600 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004601
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004602 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004603 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004605 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004606
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004607 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004608 if (target_srv(&s->target)) {
4609 target_srv(&s->target)->counters.failed_resp++;
4610 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004611 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004612
Willy Tarreau90deb182010-01-07 00:20:41 +01004613 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 rep->analysers = 0;
4615 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004616 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004617 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004619
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620 if (!(s->flags & SN_ERR_MASK))
4621 s->flags |= SN_ERR_SRVCL;
4622 if (!(s->flags & SN_FINST_MASK))
4623 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004624 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004625 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004626
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004628 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004630 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004631
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004632 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004633 if (target_srv(&s->target)) {
4634 target_srv(&s->target)->counters.failed_resp++;
4635 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004636 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004637
Willy Tarreau90deb182010-01-07 00:20:41 +01004638 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 rep->analysers = 0;
4640 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004641 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004642 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004644
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 if (!(s->flags & SN_ERR_MASK))
4646 s->flags |= SN_ERR_SRVTO;
4647 if (!(s->flags & SN_FINST_MASK))
4648 s->flags |= SN_FINST_H;
4649 return 0;
4650 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004651
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004652 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004653 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004654 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004655 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004656
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004657 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004658 if (target_srv(&s->target)) {
4659 target_srv(&s->target)->counters.failed_resp++;
4660 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004661 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004662
Willy Tarreau90deb182010-01-07 00:20:41 +01004663 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664 rep->analysers = 0;
4665 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004666 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004667 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004669
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670 if (!(s->flags & SN_ERR_MASK))
4671 s->flags |= SN_ERR_SRVCL;
4672 if (!(s->flags & SN_FINST_MASK))
4673 s->flags |= SN_FINST_H;
4674 return 0;
4675 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004676
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004678 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004680 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004681
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004682 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004684 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004685
4686 if (!(s->flags & SN_ERR_MASK))
4687 s->flags |= SN_ERR_CLICL;
4688 if (!(s->flags & SN_FINST_MASK))
4689 s->flags |= SN_FINST_H;
4690
4691 /* process_session() will take care of the error */
4692 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004693 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004694
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004695 buffer_dont_close(rep);
4696 return 0;
4697 }
4698
4699 /* More interesting part now : we know that we have a complete
4700 * response which at least looks like HTTP. We have an indicator
4701 * of each header's length, so we can parse them quickly.
4702 */
4703
4704 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004705 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 /*
4708 * 1: get the status code
4709 */
Willy Tarreau572bf902012-07-02 17:01:20 +02004710 n = rep->buf.p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 if (n < 1 || n > 5)
4712 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004713 /* when the client triggers a 4xx from the server, it's most often due
4714 * to a missing object or permission. These events should be tracked
4715 * because if they happen often, it may indicate a brute force or a
4716 * vulnerability scan.
4717 */
4718 if (n == 4)
4719 session_inc_http_err_ctr(s);
4720
Willy Tarreau827aee92011-03-10 16:55:02 +01004721 if (target_srv(&s->target))
4722 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723
Willy Tarreau5b154472009-12-21 20:11:07 +01004724 /* check if the response is HTTP/1.1 or above */
4725 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004726 ((rep->buf.p[5] > '1') ||
4727 ((rep->buf.p[5] == '1') && (rep->buf.p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004728 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004729
4730 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004731 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 +01004732
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004733 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004734 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004735
Willy Tarreau572bf902012-07-02 17:01:20 +02004736 txn->status = strl2ui(rep->buf.p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004737
Willy Tarreau39650402010-03-15 19:44:39 +01004738 /* Adjust server's health based on status code. Note: status codes 501
4739 * and 505 are triggered on demand by client request, so we must not
4740 * count them as server failures.
4741 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004742 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004743 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004744 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004745 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004746 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004747 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004748
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 /*
4750 * 2: check for cacheability.
4751 */
4752
4753 switch (txn->status) {
4754 case 200:
4755 case 203:
4756 case 206:
4757 case 300:
4758 case 301:
4759 case 410:
4760 /* RFC2616 @13.4:
4761 * "A response received with a status code of
4762 * 200, 203, 206, 300, 301 or 410 MAY be stored
4763 * by a cache (...) unless a cache-control
4764 * directive prohibits caching."
4765 *
4766 * RFC2616 @9.5: POST method :
4767 * "Responses to this method are not cacheable,
4768 * unless the response includes appropriate
4769 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02004772 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4774 break;
4775 default:
4776 break;
4777 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 /*
4780 * 3: we may need to capture headers
4781 */
4782 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004783 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau572bf902012-07-02 17:01:20 +02004784 capture_headers(rep->buf.p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 txn->rsp.cap, s->fe->rsp_cap);
4786
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004787 /* 4: determine the transfer-length.
4788 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4789 * the presence of a message-body in a RESPONSE and its transfer length
4790 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004791 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004792 * All responses to the HEAD request method MUST NOT include a
4793 * message-body, even though the presence of entity-header fields
4794 * might lead one to believe they do. All 1xx (informational), 204
4795 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4796 * message-body. All other responses do include a message-body,
4797 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004798 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004799 * 1. Any response which "MUST NOT" include a message-body (such as the
4800 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4801 * always terminated by the first empty line after the header fields,
4802 * regardless of the entity-header fields present in the message.
4803 *
4804 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4805 * the "chunked" transfer-coding (Section 6.2) is used, the
4806 * transfer-length is defined by the use of this transfer-coding.
4807 * If a Transfer-Encoding header field is present and the "chunked"
4808 * transfer-coding is not present, the transfer-length is defined by
4809 * the sender closing the connection.
4810 *
4811 * 3. If a Content-Length header field is present, its decimal value in
4812 * OCTETs represents both the entity-length and the transfer-length.
4813 * If a message is received with both a Transfer-Encoding header
4814 * field and a Content-Length header field, the latter MUST be ignored.
4815 *
4816 * 4. If the message uses the media type "multipart/byteranges", and
4817 * the transfer-length is not otherwise specified, then this self-
4818 * delimiting media type defines the transfer-length. This media
4819 * type MUST NOT be used unless the sender knows that the recipient
4820 * can parse it; the presence in a request of a Range header with
4821 * multiple byte-range specifiers from a 1.1 client implies that the
4822 * client can parse multipart/byteranges responses.
4823 *
4824 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004825 */
4826
4827 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004828 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004829 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004830 * FIXME: should we parse anyway and return an error on chunked encoding ?
4831 */
4832 if (txn->meth == HTTP_METH_HEAD ||
4833 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004834 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004835 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004836 goto skip_content_length;
4837 }
4838
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004839 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004840 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004841 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004842 http_find_header2("Transfer-Encoding", 17, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004843 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004844 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4845 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004846 /* bad transfer-encoding (chunked followed by something else) */
4847 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004848 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004849 break;
4850 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004851 }
4852
4853 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4854 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004855 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau572bf902012-07-02 17:01:20 +02004856 http_find_header2("Content-Length", 14, rep->buf.p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004857 signed long long cl;
4858
Willy Tarreauad14f752011-09-02 20:33:27 +02004859 if (!ctx.vlen) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004860 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004861 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004862 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004863
Willy Tarreauad14f752011-09-02 20:33:27 +02004864 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004865 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004866 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004867 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004868
Willy Tarreauad14f752011-09-02 20:33:27 +02004869 if (cl < 0) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004870 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004871 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004872 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004873
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004874 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau572bf902012-07-02 17:01:20 +02004875 msg->err_pos = ctx.line + ctx.val - rep->buf.p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004876 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004877 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004878
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004879 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004880 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004881 }
4882
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004883 /* FIXME: we should also implement the multipart/byterange method.
4884 * For now on, we resort to close mode in this case (unknown length).
4885 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004886skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004887
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004888 /* end of job, return OK */
4889 rep->analysers &= ~an_bit;
4890 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004891 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004892 return 1;
4893}
4894
4895/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004896 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4897 * and updates t->rep->analysers. It might make sense to explode it into several
4898 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004899 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004900int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004901{
4902 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004903 struct http_msg *msg = &txn->rsp;
4904 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004905 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004906
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004907 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004908 now_ms, __FUNCTION__,
4909 t,
4910 rep,
4911 rep->rex, rep->wex,
4912 rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02004913 rep->buf.i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004914 rep->analysers);
4915
Willy Tarreau655dce92009-11-08 13:10:58 +01004916 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004917 return 0;
4918
4919 rep->analysers &= ~an_bit;
4920 rep->analyse_exp = TICK_ETERNITY;
4921
Willy Tarreau5b154472009-12-21 20:11:07 +01004922 /* Now we have to check if we need to modify the Connection header.
4923 * This is more difficult on the response than it is on the request,
4924 * because we can have two different HTTP versions and we don't know
4925 * how the client will interprete a response. For instance, let's say
4926 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4927 * HTTP/1.1 response without any header. Maybe it will bound itself to
4928 * HTTP/1.0 because it only knows about it, and will consider the lack
4929 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4930 * the lack of header as a keep-alive. Thus we will use two flags
4931 * indicating how a request MAY be understood by the client. In case
4932 * of multiple possibilities, we'll fix the header to be explicit. If
4933 * ambiguous cases such as both close and keepalive are seen, then we
4934 * will fall back to explicit close. Note that we won't take risks with
4935 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004936 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004937 */
4938
Willy Tarreaudc008c52010-02-01 16:20:08 +01004939 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4940 txn->status == 101)) {
4941 /* Either we've established an explicit tunnel, or we're
4942 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004943 * to understand the next protocols. We have to switch to tunnel
4944 * mode, so that we transfer the request and responses then let
4945 * this protocol pass unmodified. When we later implement specific
4946 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004947 * header which contains information about that protocol for
4948 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004949 */
4950 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4951 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004952 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4953 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4954 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004955 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004956
Willy Tarreau60466522010-01-18 19:08:45 +01004957 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004959 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4960 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004961
Willy Tarreau60466522010-01-18 19:08:45 +01004962 /* now adjust header transformations depending on current state */
4963 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4964 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4965 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004966 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004967 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004968 }
Willy Tarreau60466522010-01-18 19:08:45 +01004969 else { /* SCL / KAL */
4970 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004971 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004972 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004973 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004974
Willy Tarreau60466522010-01-18 19:08:45 +01004975 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004976 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004977
Willy Tarreau60466522010-01-18 19:08:45 +01004978 /* Some keep-alive responses are converted to Server-close if
4979 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004980 */
Willy Tarreau60466522010-01-18 19:08:45 +01004981 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4982 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004983 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004984 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004985 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004986 }
4987
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004988 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004989 /*
4990 * 3: we will have to evaluate the filters.
4991 * As opposed to version 1.2, now they will be evaluated in the
4992 * filters order and not in the header order. This means that
4993 * each filter has to be validated among all headers.
4994 *
4995 * Filters are tried with ->be first, then with ->fe if it is
4996 * different from ->be.
4997 */
4998
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004999 cur_proxy = t->be;
5000 while (1) {
5001 struct proxy *rule_set = cur_proxy;
5002
5003 /* try headers filters */
5004 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005005 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005006 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005007 if (target_srv(&t->target)) {
5008 target_srv(&t->target)->counters.failed_resp++;
5009 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005010 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005011 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005012 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005013 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005014 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005015 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005016 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005017 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005018 if (!(t->flags & SN_ERR_MASK))
5019 t->flags |= SN_ERR_PRXCOND;
5020 if (!(t->flags & SN_FINST_MASK))
5021 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005022 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005023 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005024 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005025
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005026 /* has the response been denied ? */
5027 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005028 if (target_srv(&t->target))
5029 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005030
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005031 t->be->be_counters.denied_resp++;
5032 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005033 if (t->listener->counters)
5034 t->listener->counters->denied_resp++;
5035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005036 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005037 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005038
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005039 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005040 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005041 if (txn->status < 200)
5042 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005043 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005044 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005045 ret = acl_pass(ret);
5046 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5047 ret = !ret;
5048 if (!ret)
5049 continue;
5050 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005051 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005053 }
5054
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 /* check whether we're already working on the frontend */
5056 if (cur_proxy == t->fe)
5057 break;
5058 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005059 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005060
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005062 * We may be facing a 100-continue response, in which case this
5063 * is not the right response, and we're waiting for the next one.
5064 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005065 * next one.
5066 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005067 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005068 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau09d1e252012-05-18 22:36:34 +02005069 msg->next -= buffer_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005070 msg->msg_state = HTTP_MSG_RPBEFORE;
5071 txn->status = 0;
5072 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5073 return 1;
5074 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005075 else if (unlikely(txn->status < 200))
5076 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005077
5078 /* we don't have any 1xx status code now */
5079
5080 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081 * 4: check for server cookie.
5082 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005083 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5084 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005085 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005086
Willy Tarreaubaaee002006-06-26 02:48:02 +02005087
Willy Tarreaua15645d2007-03-18 16:22:39 +01005088 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005090 */
Willy Tarreau67402132012-05-31 20:40:20 +02005091 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005092 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 /*
5095 * 6: add server cookie in the response if needed
5096 */
Willy Tarreau67402132012-05-31 20:40:20 +02005097 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5098 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005099 (!(t->flags & SN_DIRECT) ||
5100 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5101 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5102 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5103 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005104 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005105 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005106 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005107 /* the server is known, it's not the one the client requested, or the
5108 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005109 * insert a set-cookie here, except if we want to insert only on POST
5110 * requests and this one isn't. Note that servers which don't have cookies
5111 * (eg: some backup servers) will return a full cookie removal request.
5112 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005113 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005114 len = sprintf(trash,
5115 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5116 t->be->cookie_name);
5117 }
5118 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005119 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005120
5121 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5122 /* emit last_date, which is mandatory */
5123 trash[len++] = COOKIE_DELIM_DATE;
5124 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5125 if (t->be->cookie_maxlife) {
5126 /* emit first_date, which is either the original one or
5127 * the current date.
5128 */
5129 trash[len++] = COOKIE_DELIM_DATE;
5130 s30tob64(txn->cookie_first_date ?
5131 txn->cookie_first_date >> 2 :
5132 (date.tv_sec+3) >> 2, trash + len);
5133 len += 5;
5134 }
5135 }
5136 len += sprintf(trash + len, "; path=/");
5137 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 if (t->be->cookie_domain)
5140 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005141
Willy Tarreau4992dd22012-05-31 21:02:17 +02005142 if (t->be->ck_opts & PR_CK_HTTPONLY)
5143 len += sprintf(trash+len, "; HttpOnly");
5144
5145 if (t->be->ck_opts & PR_CK_SECURE)
5146 len += sprintf(trash+len, "; Secure");
5147
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005148 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005150
Willy Tarreauf1348312010-10-07 15:54:11 +02005151 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005152 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005153 /* the server did not change, only the date was updated */
5154 txn->flags |= TX_SCK_UPDATED;
5155 else
5156 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005157
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005158 /* Here, we will tell an eventual cache on the client side that we don't
5159 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5160 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5161 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5162 */
Willy Tarreau67402132012-05-31 20:40:20 +02005163 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005164
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5166
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005167 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005168 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005170 }
5171 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005172
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 /*
5174 * 7: check if result will be cacheable with a cookie.
5175 * We'll block the response if security checks have caught
5176 * nasty things such as a cacheable cookie.
5177 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005178 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5179 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005180 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005181
5182 /* we're in presence of a cacheable response containing
5183 * a set-cookie header. We'll block it as requested by
5184 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005185 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005186 if (target_srv(&t->target))
5187 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005188
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005189 t->be->be_counters.denied_resp++;
5190 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005191 if (t->listener->counters)
5192 t->listener->counters->denied_resp++;
5193
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005194 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005195 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005196 send_log(t->be, LOG_ALERT,
5197 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005198 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199 goto return_srv_prx_502;
5200 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005201
5202 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005203 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005204 */
Willy Tarreau60466522010-01-18 19:08:45 +01005205 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5206 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5207 unsigned int want_flags = 0;
5208
5209 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5210 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5211 /* we want a keep-alive response here. Keep-alive header
5212 * required if either side is not 1.1.
5213 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005214 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005215 want_flags |= TX_CON_KAL_SET;
5216 }
5217 else {
5218 /* we want a close response here. Close header required if
5219 * the server is 1.1, regardless of the client.
5220 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005221 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005222 want_flags |= TX_CON_CLO_SET;
5223 }
5224
5225 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005226 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005227 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005228
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005229 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005230 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005231 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005232 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005233
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005234 /*************************************************************
5235 * OK, that's finished for the headers. We have done what we *
5236 * could. Let's switch to the DATA state. *
5237 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005238
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005240
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005241 /* if the user wants to log as soon as possible, without counting
5242 * bytes from the server, then this is the right moment. We have
5243 * to temporarily assign bytes_out to log what we currently have.
5244 */
5245 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5246 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5247 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005248 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249 t->logs.bytes_out = 0;
5250 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005251
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005252 /* Note: we must not try to cheat by jumping directly to DATA,
5253 * otherwise we would not let the client side wake up.
5254 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005255
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005256 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005257 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005258 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005260
Willy Tarreaud98cf932009-12-27 22:54:55 +01005261/* This function is an analyser which forwards response body (including chunk
5262 * sizes if any). It is called as soon as we must forward, even if we forward
5263 * zero byte. The only situation where it must not be called is when we're in
5264 * tunnel mode and we want to forward till the close. It's used both to forward
5265 * remaining data and to resync after end of body. It expects the msg_state to
5266 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5267 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005268 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005269 * bytes of pending data + the headers if not already done (between sol and sov).
5270 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005271 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005272int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005273{
5274 struct http_txn *txn = &s->txn;
5275 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005276 unsigned int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005278 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5279 return 0;
5280
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005281 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
5282 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf.o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005283 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005284 /* Output closed while we were sending data. We must abort and
5285 * wake the other side up.
5286 */
5287 msg->msg_state = HTTP_MSG_ERROR;
5288 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005289 return 1;
5290 }
5291
Willy Tarreau4fe41902010-06-07 22:27:41 +02005292 /* in most states, we should abort in case of early close */
5293 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005294
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005296 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau572bf902012-07-02 17:01:20 +02005297 * rep->buf.p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02005298 * is still null. We must save the body in msg->next because it
5299 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005301 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005302
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005303 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005304 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5305 else {
5306 msg->msg_state = HTTP_MSG_DATA;
5307 }
5308 }
5309
Willy Tarreaud98cf932009-12-27 22:54:55 +01005310 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005312 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005313 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005314 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005315 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005316 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005317 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005318 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005319 }
5320
Willy Tarreaucaabe412010-01-03 23:08:28 +01005321 if (msg->msg_state == HTTP_MSG_DATA) {
5322 /* must still forward */
5323 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005324 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005325
5326 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005327 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005328 msg->msg_state = HTTP_MSG_DATA_CRLF;
5329 else
5330 msg->msg_state = HTTP_MSG_DONE;
5331 }
5332 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005333 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005334 * set ->sov and ->next to point to the body and switch to DATA or
5335 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005336 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005337 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005338
5339 if (!ret)
5340 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005341 else if (ret < 0) {
5342 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005343 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005344 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005345 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005346 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005348 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5349 /* we want the CRLF after the data */
5350 int ret;
5351
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005352 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005353
5354 if (!ret)
5355 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005356 else if (ret < 0) {
5357 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005358 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005359 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005360 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005361 /* we're in MSG_CHUNK_SIZE now */
5362 }
5363 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005364 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005365
Willy Tarreaud98cf932009-12-27 22:54:55 +01005366 if (ret == 0)
5367 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005368 else if (ret < 0) {
5369 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005370 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005372 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005373 /* we're in HTTP_MSG_DONE now */
5374 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005376 int old_state = msg->msg_state;
5377
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005379 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005380 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5381 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5382 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005383 if (http_resync_states(s)) {
5384 http_silent_debug(__LINE__, s);
5385 /* some state changes occurred, maybe the analyser
5386 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005387 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005388 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005389 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005390 /* response errors are most likely due to
5391 * the client aborting the transfer.
5392 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005393 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005394 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005395 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005396 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005398 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005400 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005402 }
5403 }
5404
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005406 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005407 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005408 if (!(s->flags & SN_ERR_MASK))
5409 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005410 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005411 if (target_srv(&s->target))
5412 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005413 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005414 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005415
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005416 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005417 goto aborted_xfer;
5418
Willy Tarreau40dba092010-03-04 18:14:51 +01005419 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420 if (!s->req->analysers)
5421 goto return_bad_res;
5422
Willy Tarreauea953162012-05-18 23:41:28 +02005423 /* forward any data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02005424 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005425 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02005426 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005427 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02005428 msg->chunk_len += bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005429 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430 }
5431
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005432 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005433 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005434 * Similarly, with keep-alive on the client side, we don't want to forward a
5435 * close.
5436 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005437 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005438 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5439 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5440 buffer_dont_close(res);
5441
Willy Tarreau5c620922011-05-11 19:56:11 +02005442 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005443 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005444 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005445 * modes are already handled by the stream sock layer. We must not do
5446 * this in content-length mode because it could present the MSG_MORE
5447 * flag with the last block of forwarded data, which would cause an
5448 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005449 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005450 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005451 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005452
Willy Tarreaud98cf932009-12-27 22:54:55 +01005453 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005454 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005455 return 0;
5456
Willy Tarreau40dba092010-03-04 18:14:51 +01005457 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005458 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005459 if (target_srv(&s->target))
5460 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005461
5462 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005464 /* don't send any error message as we're in the body */
5465 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005466 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005467 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005468 if (target_srv(&s->target))
5469 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005470
5471 if (!(s->flags & SN_ERR_MASK))
5472 s->flags |= SN_ERR_PRXCOND;
5473 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005474 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005475 return 0;
5476
5477 aborted_xfer:
5478 txn->rsp.msg_state = HTTP_MSG_ERROR;
5479 /* don't send any error message as we're in the body */
5480 stream_int_retnclose(res->cons, NULL);
5481 res->analysers = 0;
5482 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5483
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005484 s->fe->fe_counters.cli_aborts++;
5485 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005486 if (target_srv(&s->target))
5487 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005488
5489 if (!(s->flags & SN_ERR_MASK))
5490 s->flags |= SN_ERR_CLICL;
5491 if (!(s->flags & SN_FINST_MASK))
5492 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005493 return 0;
5494}
5495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005496/* Iterate the same filter through all request headers.
5497 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005498 * Since it can manage the switch to another backend, it updates the per-proxy
5499 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005500 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005501int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005502{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005503 char term;
5504 char *cur_ptr, *cur_end, *cur_next;
5505 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005506 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 struct hdr_idx_elem *cur_hdr;
5508 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005510 last_hdr = 0;
5511
Willy Tarreau572bf902012-07-02 17:01:20 +02005512 cur_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005513 old_idx = 0;
5514
5515 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005516 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005518 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005519 (exp->action == ACT_ALLOW ||
5520 exp->action == ACT_DENY ||
5521 exp->action == ACT_TARPIT))
5522 return 0;
5523
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005524 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005525 if (!cur_idx)
5526 break;
5527
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005528 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005529 cur_ptr = cur_next;
5530 cur_end = cur_ptr + cur_hdr->len;
5531 cur_next = cur_end + cur_hdr->cr + 1;
5532
5533 /* Now we have one header between cur_ptr and cur_end,
5534 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535 */
5536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005537 /* The annoying part is that pattern matching needs
5538 * that we modify the contents to null-terminate all
5539 * strings before testing them.
5540 */
5541
5542 term = *cur_end;
5543 *cur_end = '\0';
5544
5545 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5546 switch (exp->action) {
5547 case ACT_SETBE:
5548 /* It is not possible to jump a second time.
5549 * FIXME: should we return an HTTP/500 here so that
5550 * the admin knows there's a problem ?
5551 */
5552 if (t->be != t->fe)
5553 break;
5554
5555 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005556 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005557 last_hdr = 1;
5558 break;
5559
5560 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005561 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005562 last_hdr = 1;
5563 break;
5564
5565 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005566 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005567 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005568
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005569 t->fe->fe_counters.denied_req++;
5570 if (t->fe != t->be)
5571 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005572 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005573 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 break;
5576
5577 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005578 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005579 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005580
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005581 t->fe->fe_counters.denied_req++;
5582 if (t->fe != t->be)
5583 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005584 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005585 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005587 break;
5588
5589 case ACT_REPLACE:
5590 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02005591 delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 /* FIXME: if the user adds a newline in the replacement, the
5593 * index will not be recalculated for now, and the new line
5594 * will not be counted as a new header.
5595 */
5596
5597 cur_end += delta;
5598 cur_next += delta;
5599 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005600 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005601 break;
5602
5603 case ACT_REMOVE:
Willy Tarreauaf819352012-08-27 22:08:00 +02005604 delta = buffer_replace2(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005605 cur_next += delta;
5606
Willy Tarreaufa355d42009-11-29 18:12:29 +01005607 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005608 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5609 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005610 cur_hdr->len = 0;
5611 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005612 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005613 break;
5614
5615 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005616 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 if (cur_end)
5618 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005620 /* keep the link from this header to next one in case of later
5621 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005622 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 old_idx = cur_idx;
5624 }
5625 return 0;
5626}
5627
5628
5629/* Apply the filter to the request line.
5630 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5631 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005632 * Since it can manage the switch to another backend, it updates the per-proxy
5633 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005635int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636{
5637 char term;
5638 char *cur_ptr, *cur_end;
5639 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005640 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005641 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005642
Willy Tarreau58f10d72006-12-04 02:26:12 +01005643
Willy Tarreau3d300592007-03-18 18:34:41 +01005644 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005646 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 (exp->action == ACT_ALLOW ||
5648 exp->action == ACT_DENY ||
5649 exp->action == ACT_TARPIT))
5650 return 0;
5651 else if (exp->action == ACT_REMOVE)
5652 return 0;
5653
5654 done = 0;
5655
Willy Tarreau572bf902012-07-02 17:01:20 +02005656 cur_ptr = req->buf.p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005657 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658
5659 /* Now we have the request line between cur_ptr and cur_end */
5660
5661 /* The annoying part is that pattern matching needs
5662 * that we modify the contents to null-terminate all
5663 * strings before testing them.
5664 */
5665
5666 term = *cur_end;
5667 *cur_end = '\0';
5668
5669 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5670 switch (exp->action) {
5671 case ACT_SETBE:
5672 /* It is not possible to jump a second time.
5673 * FIXME: should we return an HTTP/500 here so that
5674 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005675 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 if (t->be != t->fe)
5677 break;
5678
5679 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005680 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005681 done = 1;
5682 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005685 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 done = 1;
5687 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005689 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005690 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005691
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005692 t->fe->fe_counters.denied_req++;
5693 if (t->fe != t->be)
5694 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005695 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005696 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 done = 1;
5699 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005703
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005704 t->fe->fe_counters.denied_req++;
5705 if (t->fe != t->be)
5706 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005707 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005708 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 done = 1;
5711 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 case ACT_REPLACE:
5714 *cur_end = term; /* restore the string terminator */
5715 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02005716 delta = buffer_replace2(&req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 /* FIXME: if the user adds a newline in the replacement, the
5718 * index will not be recalculated for now, and the new line
5719 * will not be counted as a new header.
5720 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005721
Willy Tarreaufa355d42009-11-29 18:12:29 +01005722 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005724 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725 HTTP_MSG_RQMETH,
5726 cur_ptr, cur_end + 1,
5727 NULL, NULL);
5728 if (unlikely(!cur_end))
5729 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 /* we have a full request and we know that we have either a CR
5732 * or an LF at <ptr>.
5733 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005734 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5735 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 /* there is no point trying this regex on headers */
5737 return 1;
5738 }
5739 }
5740 *cur_end = term; /* restore the string terminator */
5741 return done;
5742}
Willy Tarreau97de6242006-12-27 17:18:38 +01005743
Willy Tarreau58f10d72006-12-04 02:26:12 +01005744
Willy Tarreau58f10d72006-12-04 02:26:12 +01005745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005747 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005749 * unparsable request. Since it can manage the switch to another backend, it
5750 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005752int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005754 struct http_txn *txn = &s->txn;
5755 struct hdr_exp *exp;
5756
5757 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005760 /*
5761 * The interleaving of transformations and verdicts
5762 * makes it difficult to decide to continue or stop
5763 * the evaluation.
5764 */
5765
Willy Tarreau6c123b12010-01-28 20:22:06 +01005766 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5767 break;
5768
Willy Tarreau3d300592007-03-18 18:34:41 +01005769 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005771 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005773
5774 /* if this filter had a condition, evaluate it now and skip to
5775 * next filter if the condition does not match.
5776 */
5777 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005778 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005779 ret = acl_pass(ret);
5780 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5781 ret = !ret;
5782
5783 if (!ret)
5784 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 }
5786
5787 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005788 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 if (unlikely(ret < 0))
5790 return -1;
5791
5792 if (likely(ret == 0)) {
5793 /* The filter did not match the request, it can be
5794 * iterated through all headers.
5795 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005796 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005797 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005798 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005799 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800}
5801
5802
Willy Tarreaua15645d2007-03-18 16:22:39 +01005803
Willy Tarreau58f10d72006-12-04 02:26:12 +01005804/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005805 * Try to retrieve the server associated to the appsession.
5806 * If the server is found, it's assigned to the session.
5807 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005808void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005809 struct http_txn *txn = &t->txn;
5810 appsess *asession = NULL;
5811 char *sessid_temp = NULL;
5812
Cyril Bontéb21570a2009-11-29 20:04:48 +01005813 if (len > t->be->appsession_len) {
5814 len = t->be->appsession_len;
5815 }
5816
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005817 if (t->be->options2 & PR_O2_AS_REQL) {
5818 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005819 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005820 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005821 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005822 }
5823
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005824 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005825 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5826 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5827 return;
5828 }
5829
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005830 memcpy(txn->sessid, buf, len);
5831 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005832 }
5833
5834 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5835 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5836 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5837 return;
5838 }
5839
Cyril Bontéb21570a2009-11-29 20:04:48 +01005840 memcpy(sessid_temp, buf, len);
5841 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005842
5843 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5844 /* free previously allocated memory */
5845 pool_free2(apools.sessid, sessid_temp);
5846
5847 if (asession != NULL) {
5848 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5849 if (!(t->be->options2 & PR_O2_AS_REQL))
5850 asession->request_count++;
5851
5852 if (asession->serverid != NULL) {
5853 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005854
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005855 while (srv) {
5856 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005857 if ((srv->state & SRV_RUNNING) ||
5858 (t->be->options & PR_O_PERSIST) ||
5859 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005860 /* we found the server and it's usable */
5861 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005862 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005863 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005864 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005865
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005866 break;
5867 } else {
5868 txn->flags &= ~TX_CK_MASK;
5869 txn->flags |= TX_CK_DOWN;
5870 }
5871 }
5872 srv = srv->next;
5873 }
5874 }
5875 }
5876}
5877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005878/* Find the end of a cookie value contained between <s> and <e>. It works the
5879 * same way as with headers above except that the semi-colon also ends a token.
5880 * See RFC2965 for more information. Note that it requires a valid header to
5881 * return a valid result.
5882 */
5883char *find_cookie_value_end(char *s, const char *e)
5884{
5885 int quoted, qdpair;
5886
5887 quoted = qdpair = 0;
5888 for (; s < e; s++) {
5889 if (qdpair) qdpair = 0;
5890 else if (quoted) {
5891 if (*s == '\\') qdpair = 1;
5892 else if (*s == '"') quoted = 0;
5893 }
5894 else if (*s == '"') quoted = 1;
5895 else if (*s == ',' || *s == ';') return s;
5896 }
5897 return s;
5898}
5899
5900/* Delete a value in a header between delimiters <from> and <next> in buffer
5901 * <buf>. The number of characters displaced is returned, and the pointer to
5902 * the first delimiter is updated if required. The function tries as much as
5903 * possible to respect the following principles :
5904 * - replace <from> delimiter by the <next> one unless <from> points to a
5905 * colon, in which case <next> is simply removed
5906 * - set exactly one space character after the new first delimiter, unless
5907 * there are not enough characters in the block being moved to do so.
5908 * - remove unneeded spaces before the previous delimiter and after the new
5909 * one.
5910 *
5911 * It is the caller's responsibility to ensure that :
5912 * - <from> points to a valid delimiter or the colon ;
5913 * - <next> points to a valid delimiter or the final CR/LF ;
5914 * - there are non-space chars before <from> ;
5915 * - there is a CR/LF at or after <next>.
5916 */
Willy Tarreauaf819352012-08-27 22:08:00 +02005917int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918{
5919 char *prev = *from;
5920
5921 if (*prev == ':') {
5922 /* We're removing the first value, preserve the colon and add a
5923 * space if possible.
5924 */
5925 if (!http_is_crlf[(unsigned char)*next])
5926 next++;
5927 prev++;
5928 if (prev < next)
5929 *prev++ = ' ';
5930
5931 while (http_is_spht[(unsigned char)*next])
5932 next++;
5933 } else {
5934 /* Remove useless spaces before the old delimiter. */
5935 while (http_is_spht[(unsigned char)*(prev-1)])
5936 prev--;
5937 *from = prev;
5938
5939 /* copy the delimiter and if possible a space if we're
5940 * not at the end of the line.
5941 */
5942 if (!http_is_crlf[(unsigned char)*next]) {
5943 *prev++ = *next++;
5944 if (prev + 1 < next)
5945 *prev++ = ' ';
5946 while (http_is_spht[(unsigned char)*next])
5947 next++;
5948 }
5949 }
5950 return buffer_replace2(buf, prev, next, NULL, 0);
5951}
5952
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005953/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005954 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005955 * desirable to call it only when needed. This code is quite complex because
5956 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5957 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005958 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005959void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005960{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005961 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005962 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005964 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5965 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005967 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005968 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02005969 hdr_next = req->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005970
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005971 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005972 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005973 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005974
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005975 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005976 hdr_beg = hdr_next;
5977 hdr_end = hdr_beg + cur_hdr->len;
5978 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005980 /* We have one full header between hdr_beg and hdr_end, and the
5981 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005982 * "Cookie:" headers.
5983 */
5984
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005985 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005986 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005987 old_idx = cur_idx;
5988 continue;
5989 }
5990
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005991 del_from = NULL; /* nothing to be deleted */
5992 preserve_hdr = 0; /* assume we may kill the whole header */
5993
Willy Tarreau58f10d72006-12-04 02:26:12 +01005994 /* Now look for cookies. Conforming to RFC2109, we have to support
5995 * attributes whose name begin with a '$', and associate them with
5996 * the right cookie, if we want to delete this cookie.
5997 * So there are 3 cases for each cookie read :
5998 * 1) it's a special attribute, beginning with a '$' : ignore it.
5999 * 2) it's a server id cookie that we *MAY* want to delete : save
6000 * some pointers on it (last semi-colon, beginning of cookie...)
6001 * 3) it's an application cookie : we *MAY* have to delete a previous
6002 * "special" cookie.
6003 * At the end of loop, if a "special" cookie remains, we may have to
6004 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006005 * *MUST* delete it.
6006 *
6007 * Note: RFC2965 is unclear about the processing of spaces around
6008 * the equal sign in the ATTR=VALUE form. A careful inspection of
6009 * the RFC explicitly allows spaces before it, and not within the
6010 * tokens (attrs or values). An inspection of RFC2109 allows that
6011 * too but section 10.1.3 lets one think that spaces may be allowed
6012 * after the equal sign too, resulting in some (rare) buggy
6013 * implementations trying to do that. So let's do what servers do.
6014 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6015 * allowed quoted strings in values, with any possible character
6016 * after a backslash, including control chars and delimitors, which
6017 * causes parsing to become ambiguous. Browsers also allow spaces
6018 * within values even without quotes.
6019 *
6020 * We have to keep multiple pointers in order to support cookie
6021 * removal at the beginning, middle or end of header without
6022 * corrupting the header. All of these headers are valid :
6023 *
6024 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6025 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6026 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6027 * | | | | | | | | |
6028 * | | | | | | | | hdr_end <--+
6029 * | | | | | | | +--> next
6030 * | | | | | | +----> val_end
6031 * | | | | | +-----------> val_beg
6032 * | | | | +--------------> equal
6033 * | | | +----------------> att_end
6034 * | | +---------------------> att_beg
6035 * | +--------------------------> prev
6036 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037 */
6038
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006039 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6040 /* Iterate through all cookies on this line */
6041
6042 /* find att_beg */
6043 att_beg = prev + 1;
6044 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6045 att_beg++;
6046
6047 /* find att_end : this is the first character after the last non
6048 * space before the equal. It may be equal to hdr_end.
6049 */
6050 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 while (equal < hdr_end) {
6053 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 if (http_is_spht[(unsigned char)*equal++])
6056 continue;
6057 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058 }
6059
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006060 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6061 * is between <att_beg> and <equal>, both may be identical.
6062 */
6063
6064 /* look for end of cookie if there is an equal sign */
6065 if (equal < hdr_end && *equal == '=') {
6066 /* look for the beginning of the value */
6067 val_beg = equal + 1;
6068 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6069 val_beg++;
6070
6071 /* find the end of the value, respecting quotes */
6072 next = find_cookie_value_end(val_beg, hdr_end);
6073
6074 /* make val_end point to the first white space or delimitor after the value */
6075 val_end = next;
6076 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6077 val_end--;
6078 } else {
6079 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006081
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006082 /* We have nothing to do with attributes beginning with '$'. However,
6083 * they will automatically be removed if a header before them is removed,
6084 * since they're supposed to be linked together.
6085 */
6086 if (*att_beg == '$')
6087 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 /* Ignore cookies with no equal sign */
6090 if (equal == next) {
6091 /* This is not our cookie, so we must preserve it. But if we already
6092 * scheduled another cookie for removal, we cannot remove the
6093 * complete header, but we can remove the previous block itself.
6094 */
6095 preserve_hdr = 1;
6096 if (del_from != NULL) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006097 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098 val_end += delta;
6099 next += delta;
6100 hdr_end += delta;
6101 hdr_next += delta;
6102 cur_hdr->len += delta;
6103 http_msg_move_end(&txn->req, delta);
6104 prev = del_from;
6105 del_from = NULL;
6106 }
6107 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 /* if there are spaces around the equal sign, we need to
6111 * strip them otherwise we'll get trouble for cookie captures,
6112 * or even for rewrites. Since this happens extremely rarely,
6113 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6116 int stripped_before = 0;
6117 int stripped_after = 0;
6118
6119 if (att_end != equal) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006120 stripped_before = buffer_replace2(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 equal += stripped_before;
6122 val_beg += stripped_before;
6123 }
6124
6125 if (val_beg > equal + 1) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006126 stripped_after = buffer_replace2(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 val_beg += stripped_after;
6128 stripped_before += stripped_after;
6129 }
6130
6131 val_end += stripped_before;
6132 next += stripped_before;
6133 hdr_end += stripped_before;
6134 hdr_next += stripped_before;
6135 cur_hdr->len += stripped_before;
6136 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006137 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006138 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006140 /* First, let's see if we want to capture this cookie. We check
6141 * that we don't already have a client side cookie, because we
6142 * can only capture one. Also as an optimisation, we ignore
6143 * cookies shorter than the declared name.
6144 */
6145 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6146 (val_end - att_beg >= t->fe->capture_namelen) &&
6147 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6148 int log_len = val_end - att_beg;
6149
6150 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6151 Alert("HTTP logging : out of memory.\n");
6152 } else {
6153 if (log_len > t->fe->capture_len)
6154 log_len = t->fe->capture_len;
6155 memcpy(txn->cli_cookie, att_beg, log_len);
6156 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006157 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006158 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006159
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 /* Persistence cookies in passive, rewrite or insert mode have the
6161 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006162 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006163 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006165 * For cookies in prefix mode, the form is :
6166 *
6167 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006168 */
6169 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6170 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6171 struct server *srv = t->be->srv;
6172 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006173
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006174 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6175 * have the server ID between val_beg and delim, and the original cookie between
6176 * delim+1 and val_end. Otherwise, delim==val_end :
6177 *
6178 * Cookie: NAME=SRV; # in all but prefix modes
6179 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6180 * | || || | |+-> next
6181 * | || || | +--> val_end
6182 * | || || +---------> delim
6183 * | || |+------------> val_beg
6184 * | || +-------------> att_end = equal
6185 * | |+-----------------> att_beg
6186 * | +------------------> prev
6187 * +-------------------------> hdr_beg
6188 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006189
Willy Tarreau67402132012-05-31 20:40:20 +02006190 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006191 for (delim = val_beg; delim < val_end; delim++)
6192 if (*delim == COOKIE_DELIM)
6193 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006194 } else {
6195 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006197 /* Now check if the cookie contains a date field, which would
6198 * appear after a vertical bar ('|') just after the server name
6199 * and before the delimiter.
6200 */
6201 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6202 if (vbar1) {
6203 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006204 * right is the last seen date. It is a base64 encoded
6205 * 30-bit value representing the UNIX date since the
6206 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006207 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006208 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006209 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006210 if (val_end - vbar1 >= 5) {
6211 val = b64tos30(vbar1);
6212 if (val > 0)
6213 txn->cookie_last_date = val << 2;
6214 }
6215 /* look for a second vertical bar */
6216 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6217 if (vbar1 && (val_end - vbar1 > 5)) {
6218 val = b64tos30(vbar1 + 1);
6219 if (val > 0)
6220 txn->cookie_first_date = val << 2;
6221 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006222 }
6223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224
Willy Tarreauf64d1412010-10-07 20:06:11 +02006225 /* if the cookie has an expiration date and the proxy wants to check
6226 * it, then we do that now. We first check if the cookie is too old,
6227 * then only if it has expired. We detect strict overflow because the
6228 * time resolution here is not great (4 seconds). Cookies with dates
6229 * in the future are ignored if their offset is beyond one day. This
6230 * allows an admin to fix timezone issues without expiring everyone
6231 * and at the same time avoids keeping unwanted side effects for too
6232 * long.
6233 */
6234 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006235 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6236 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006237 txn->flags &= ~TX_CK_MASK;
6238 txn->flags |= TX_CK_OLD;
6239 delim = val_beg; // let's pretend we have not found the cookie
6240 txn->cookie_first_date = 0;
6241 txn->cookie_last_date = 0;
6242 }
6243 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006244 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6245 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006246 txn->flags &= ~TX_CK_MASK;
6247 txn->flags |= TX_CK_EXPIRED;
6248 delim = val_beg; // let's pretend we have not found the cookie
6249 txn->cookie_first_date = 0;
6250 txn->cookie_last_date = 0;
6251 }
6252
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 /* Here, we'll look for the first running server which supports the cookie.
6254 * This allows to share a same cookie between several servers, for example
6255 * to dedicate backup servers to specific servers only.
6256 * However, to prevent clients from sticking to cookie-less backup server
6257 * when they have incidentely learned an empty cookie, we simply ignore
6258 * empty cookies and mark them as invalid.
6259 * The same behaviour is applied when persistence must be ignored.
6260 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006261 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 while (srv) {
6265 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6266 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6267 if ((srv->state & SRV_RUNNING) ||
6268 (t->be->options & PR_O_PERSIST) ||
6269 (t->flags & SN_FORCE_PRST)) {
6270 /* we found the server and we can use it */
6271 txn->flags &= ~TX_CK_MASK;
6272 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6273 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006274 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 break;
6276 } else {
6277 /* we found a server, but it's down,
6278 * mark it as such and go on in case
6279 * another one is available.
6280 */
6281 txn->flags &= ~TX_CK_MASK;
6282 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 srv = srv->next;
6286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreauf64d1412010-10-07 20:06:11 +02006288 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006289 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006291 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6292 txn->flags |= TX_CK_UNUSED;
6293 else
6294 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 /* depending on the cookie mode, we may have to either :
6298 * - delete the complete cookie if we're in insert+indirect mode, so that
6299 * the server never sees it ;
6300 * - remove the server id from the cookie value, and tag the cookie as an
6301 * application cookie so that it does not get accidentely removed later,
6302 * if we're in cookie prefix mode
6303 */
Willy Tarreau67402132012-05-31 20:40:20 +02006304 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006305 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306
Willy Tarreauaf819352012-08-27 22:08:00 +02006307 delta = buffer_replace2(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006308 val_end += delta;
6309 next += delta;
6310 hdr_end += delta;
6311 hdr_next += delta;
6312 cur_hdr->len += delta;
6313 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 del_from = NULL;
6316 preserve_hdr = 1; /* we want to keep this cookie */
6317 }
6318 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006319 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 del_from = prev;
6321 }
6322 } else {
6323 /* This is not our cookie, so we must preserve it. But if we already
6324 * scheduled another cookie for removal, we cannot remove the
6325 * complete header, but we can remove the previous block itself.
6326 */
6327 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 if (del_from != NULL) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006330 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006331 if (att_beg >= del_from)
6332 att_beg += delta;
6333 if (att_end >= del_from)
6334 att_end += delta;
6335 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 val_end += delta;
6337 next += delta;
6338 hdr_end += delta;
6339 hdr_next += delta;
6340 cur_hdr->len += delta;
6341 http_msg_move_end(&txn->req, delta);
6342 prev = del_from;
6343 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 /* Look for the appsession cookie unless persistence must be ignored */
6348 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6349 int cmp_len, value_len;
6350 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006351
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 if (t->be->options2 & PR_O2_AS_PFX) {
6353 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6354 value_begin = att_beg + t->be->appsession_name_len;
6355 value_len = val_end - att_beg - t->be->appsession_name_len;
6356 } else {
6357 cmp_len = att_end - att_beg;
6358 value_begin = val_beg;
6359 value_len = val_end - val_beg;
6360 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006361
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006362 /* let's see if the cookie is our appcookie */
6363 if (cmp_len == t->be->appsession_name_len &&
6364 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6365 manage_client_side_appsession(t, value_begin, value_len);
6366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006367 }
6368
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 /* continue with next cookie on this header line */
6370 att_beg = next;
6371 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006373 /* There are no more cookies on this line.
6374 * We may still have one (or several) marked for deletion at the
6375 * end of the line. We must do this now in two ways :
6376 * - if some cookies must be preserved, we only delete from the
6377 * mark to the end of line ;
6378 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 if (preserve_hdr) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006383 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006384 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385 cur_hdr->len += delta;
6386 } else {
Willy Tarreauaf819352012-08-27 22:08:00 +02006387 delta = buffer_replace2(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388
6389 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006390 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6391 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006393 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006394 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006395 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006396 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
6398
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006402}
6403
6404
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405/* Iterate the same filter through all response headers contained in <rtr>.
6406 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6407 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006408int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006409{
6410 char term;
6411 char *cur_ptr, *cur_end, *cur_next;
6412 int cur_idx, old_idx, last_hdr;
6413 struct http_txn *txn = &t->txn;
6414 struct hdr_idx_elem *cur_hdr;
6415 int len, delta;
6416
6417 last_hdr = 0;
6418
Willy Tarreau572bf902012-07-02 17:01:20 +02006419 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006420 old_idx = 0;
6421
6422 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006423 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006424 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006425 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006426 (exp->action == ACT_ALLOW ||
6427 exp->action == ACT_DENY))
6428 return 0;
6429
6430 cur_idx = txn->hdr_idx.v[old_idx].next;
6431 if (!cur_idx)
6432 break;
6433
6434 cur_hdr = &txn->hdr_idx.v[cur_idx];
6435 cur_ptr = cur_next;
6436 cur_end = cur_ptr + cur_hdr->len;
6437 cur_next = cur_end + cur_hdr->cr + 1;
6438
6439 /* Now we have one header between cur_ptr and cur_end,
6440 * and the next header starts at cur_next.
6441 */
6442
6443 /* The annoying part is that pattern matching needs
6444 * that we modify the contents to null-terminate all
6445 * strings before testing them.
6446 */
6447
6448 term = *cur_end;
6449 *cur_end = '\0';
6450
6451 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6452 switch (exp->action) {
6453 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006454 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455 last_hdr = 1;
6456 break;
6457
6458 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006459 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 last_hdr = 1;
6461 break;
6462
6463 case ACT_REPLACE:
6464 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02006465 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 /* FIXME: if the user adds a newline in the replacement, the
6467 * index will not be recalculated for now, and the new line
6468 * will not be counted as a new header.
6469 */
6470
6471 cur_end += delta;
6472 cur_next += delta;
6473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006474 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006475 break;
6476
6477 case ACT_REMOVE:
Willy Tarreauaf819352012-08-27 22:08:00 +02006478 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006479 cur_next += delta;
6480
Willy Tarreaufa355d42009-11-29 18:12:29 +01006481 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006482 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6483 txn->hdr_idx.used--;
6484 cur_hdr->len = 0;
6485 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006486 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 break;
6488
6489 }
6490 }
6491 if (cur_end)
6492 *cur_end = term; /* restore the string terminator */
6493
6494 /* keep the link from this header to next one in case of later
6495 * removal of next header.
6496 */
6497 old_idx = cur_idx;
6498 }
6499 return 0;
6500}
6501
6502
6503/* Apply the filter to the status line in the response buffer <rtr>.
6504 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6505 * or -1 if a replacement resulted in an invalid status line.
6506 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006507int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508{
6509 char term;
6510 char *cur_ptr, *cur_end;
6511 int done;
6512 struct http_txn *txn = &t->txn;
6513 int len, delta;
6514
6515
Willy Tarreau3d300592007-03-18 18:34:41 +01006516 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 (exp->action == ACT_ALLOW ||
6520 exp->action == ACT_DENY))
6521 return 0;
6522 else if (exp->action == ACT_REMOVE)
6523 return 0;
6524
6525 done = 0;
6526
Willy Tarreau572bf902012-07-02 17:01:20 +02006527 cur_ptr = rtr->buf.p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006528 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529
6530 /* Now we have the status line between cur_ptr and cur_end */
6531
6532 /* The annoying part is that pattern matching needs
6533 * that we modify the contents to null-terminate all
6534 * strings before testing them.
6535 */
6536
6537 term = *cur_end;
6538 *cur_end = '\0';
6539
6540 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6541 switch (exp->action) {
6542 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006543 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544 done = 1;
6545 break;
6546
6547 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006548 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 done = 1;
6550 break;
6551
6552 case ACT_REPLACE:
6553 *cur_end = term; /* restore the string terminator */
6554 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreauaf819352012-08-27 22:08:00 +02006555 delta = buffer_replace2(&rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 /* FIXME: if the user adds a newline in the replacement, the
6557 * index will not be recalculated for now, and the new line
6558 * will not be counted as a new header.
6559 */
6560
Willy Tarreaufa355d42009-11-29 18:12:29 +01006561 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006563 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006564 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565 cur_ptr, cur_end + 1,
6566 NULL, NULL);
6567 if (unlikely(!cur_end))
6568 return -1;
6569
6570 /* we have a full respnse and we know that we have either a CR
6571 * or an LF at <ptr>.
6572 */
Willy Tarreau572bf902012-07-02 17:01:20 +02006573 txn->status = strl2ui(rtr->buf.p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006574 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575 /* there is no point trying this regex on headers */
6576 return 1;
6577 }
6578 }
6579 *cur_end = term; /* restore the string terminator */
6580 return done;
6581}
6582
6583
6584
6585/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006586 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6588 * unparsable response.
6589 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006590int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006592 struct http_txn *txn = &s->txn;
6593 struct hdr_exp *exp;
6594
6595 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 int ret;
6597
6598 /*
6599 * The interleaving of transformations and verdicts
6600 * makes it difficult to decide to continue or stop
6601 * the evaluation.
6602 */
6603
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006604 if (txn->flags & TX_SVDENY)
6605 break;
6606
Willy Tarreau3d300592007-03-18 18:34:41 +01006607 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6609 exp->action == ACT_PASS)) {
6610 exp = exp->next;
6611 continue;
6612 }
6613
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006614 /* if this filter had a condition, evaluate it now and skip to
6615 * next filter if the condition does not match.
6616 */
6617 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006618 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006619 ret = acl_pass(ret);
6620 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6621 ret = !ret;
6622 if (!ret)
6623 continue;
6624 }
6625
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006627 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 if (unlikely(ret < 0))
6629 return -1;
6630
6631 if (likely(ret == 0)) {
6632 /* The filter did not match the response, it can be
6633 * iterated through all headers.
6634 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006635 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637 }
6638 return 0;
6639}
6640
6641
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006643 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 * desirable to call it only when needed. This function is also used when we
6645 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006646 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006647void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648{
6649 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006650 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006651 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 char *hdr_beg, *hdr_end, *hdr_next;
6654 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 /* Iterate through the headers.
6657 * we start with the start line.
6658 */
6659 old_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02006660 hdr_next = res->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006661
6662 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6663 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006664 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665
6666 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006667 hdr_beg = hdr_next;
6668 hdr_end = hdr_beg + cur_hdr->len;
6669 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670
Willy Tarreau24581ba2010-08-31 22:39:35 +02006671 /* We have one full header between hdr_beg and hdr_end, and the
6672 * next header starts at hdr_next. We're only interested in
6673 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 */
6675
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676 is_cookie2 = 0;
6677 prev = hdr_beg + 10;
6678 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006679 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006680 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6681 if (!val) {
6682 old_idx = cur_idx;
6683 continue;
6684 }
6685 is_cookie2 = 1;
6686 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687 }
6688
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6690 * <prev> points to the colon.
6691 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006692 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693
Willy Tarreau24581ba2010-08-31 22:39:35 +02006694 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6695 * check-cache is enabled) and we are not interested in checking
6696 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006697 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006698 if (t->be->cookie_name == NULL &&
6699 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006700 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 return;
6702
Willy Tarreau24581ba2010-08-31 22:39:35 +02006703 /* OK so now we know we have to process this response cookie.
6704 * The format of the Set-Cookie header is slightly different
6705 * from the format of the Cookie header in that it does not
6706 * support the comma as a cookie delimiter (thus the header
6707 * cannot be folded) because the Expires attribute described in
6708 * the original Netscape's spec may contain an unquoted date
6709 * with a comma inside. We have to live with this because
6710 * many browsers don't support Max-Age and some browsers don't
6711 * support quoted strings. However the Set-Cookie2 header is
6712 * clean.
6713 *
6714 * We have to keep multiple pointers in order to support cookie
6715 * removal at the beginning, middle or end of header without
6716 * corrupting the header (in case of set-cookie2). A special
6717 * pointer, <scav> points to the beginning of the set-cookie-av
6718 * fields after the first semi-colon. The <next> pointer points
6719 * either to the end of line (set-cookie) or next unquoted comma
6720 * (set-cookie2). All of these headers are valid :
6721 *
6722 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6723 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6724 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6725 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6726 * | | | | | | | | | |
6727 * | | | | | | | | +-> next hdr_end <--+
6728 * | | | | | | | +------------> scav
6729 * | | | | | | +--------------> val_end
6730 * | | | | | +--------------------> val_beg
6731 * | | | | +----------------------> equal
6732 * | | | +------------------------> att_end
6733 * | | +----------------------------> att_beg
6734 * | +------------------------------> prev
6735 * +-----------------------------------------> hdr_beg
6736 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreau24581ba2010-08-31 22:39:35 +02006738 for (; prev < hdr_end; prev = next) {
6739 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
Willy Tarreau24581ba2010-08-31 22:39:35 +02006741 /* find att_beg */
6742 att_beg = prev + 1;
6743 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6744 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 /* find att_end : this is the first character after the last non
6747 * space before the equal. It may be equal to hdr_end.
6748 */
6749 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
Willy Tarreau24581ba2010-08-31 22:39:35 +02006751 while (equal < hdr_end) {
6752 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6753 break;
6754 if (http_is_spht[(unsigned char)*equal++])
6755 continue;
6756 att_end = equal;
6757 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758
Willy Tarreau24581ba2010-08-31 22:39:35 +02006759 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6760 * is between <att_beg> and <equal>, both may be identical.
6761 */
6762
6763 /* look for end of cookie if there is an equal sign */
6764 if (equal < hdr_end && *equal == '=') {
6765 /* look for the beginning of the value */
6766 val_beg = equal + 1;
6767 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6768 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769
Willy Tarreau24581ba2010-08-31 22:39:35 +02006770 /* find the end of the value, respecting quotes */
6771 next = find_cookie_value_end(val_beg, hdr_end);
6772
6773 /* make val_end point to the first white space or delimitor after the value */
6774 val_end = next;
6775 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6776 val_end--;
6777 } else {
6778 /* <equal> points to next comma, semi-colon or EOL */
6779 val_beg = val_end = next = equal;
6780 }
6781
6782 if (next < hdr_end) {
6783 /* Set-Cookie2 supports multiple cookies, and <next> points to
6784 * a colon or semi-colon before the end. So skip all attr-value
6785 * pairs and look for the next comma. For Set-Cookie, since
6786 * commas are permitted in values, skip to the end.
6787 */
6788 if (is_cookie2)
6789 next = find_hdr_value_end(next, hdr_end);
6790 else
6791 next = hdr_end;
6792 }
6793
6794 /* Now everything is as on the diagram above */
6795
6796 /* Ignore cookies with no equal sign */
6797 if (equal == val_end)
6798 continue;
6799
6800 /* If there are spaces around the equal sign, we need to
6801 * strip them otherwise we'll get trouble for cookie captures,
6802 * or even for rewrites. Since this happens extremely rarely,
6803 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6806 int stripped_before = 0;
6807 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
Willy Tarreau24581ba2010-08-31 22:39:35 +02006809 if (att_end != equal) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006810 stripped_before = buffer_replace2(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 equal += stripped_before;
6812 val_beg += stripped_before;
6813 }
6814
6815 if (val_beg > equal + 1) {
Willy Tarreauaf819352012-08-27 22:08:00 +02006816 stripped_after = buffer_replace2(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 val_beg += stripped_after;
6818 stripped_before += stripped_after;
6819 }
6820
6821 val_end += stripped_before;
6822 next += stripped_before;
6823 hdr_end += stripped_before;
6824 hdr_next += stripped_before;
6825 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006826 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006827 }
6828
6829 /* First, let's see if we want to capture this cookie. We check
6830 * that we don't already have a server side cookie, because we
6831 * can only capture one. Also as an optimisation, we ignore
6832 * cookies shorter than the declared name.
6833 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006834 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006835 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006836 (val_end - att_beg >= t->fe->capture_namelen) &&
6837 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6838 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006839 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 Alert("HTTP logging : out of memory.\n");
6841 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006842 else {
6843 if (log_len > t->fe->capture_len)
6844 log_len = t->fe->capture_len;
6845 memcpy(txn->srv_cookie, att_beg, log_len);
6846 txn->srv_cookie[log_len] = 0;
6847 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 }
6849
Willy Tarreau827aee92011-03-10 16:55:02 +01006850 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 if (!(t->flags & SN_IGNORE_PRST) &&
6853 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6854 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006855 /* assume passive cookie by default */
6856 txn->flags &= ~TX_SCK_MASK;
6857 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858
6859 /* If the cookie is in insert mode on a known server, we'll delete
6860 * this occurrence because we'll insert another one later.
6861 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006862 * a direct access.
6863 */
Willy Tarreau67402132012-05-31 20:40:20 +02006864 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006865 /* The "preserve" flag was set, we don't want to touch the
6866 * server's cookie.
6867 */
6868 }
Willy Tarreau67402132012-05-31 20:40:20 +02006869 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
6870 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 /* this cookie must be deleted */
6872 if (*prev == ':' && next == hdr_end) {
6873 /* whole header */
Willy Tarreauaf819352012-08-27 22:08:00 +02006874 delta = buffer_replace2(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6876 txn->hdr_idx.used--;
6877 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006878 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006879 hdr_next += delta;
6880 http_msg_move_end(&txn->rsp, delta);
6881 /* note: while both invalid now, <next> and <hdr_end>
6882 * are still equal, so the for() will stop as expected.
6883 */
6884 } else {
6885 /* just remove the value */
Willy Tarreauaf819352012-08-27 22:08:00 +02006886 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 next = prev;
6888 hdr_end += delta;
6889 hdr_next += delta;
6890 cur_hdr->len += delta;
6891 http_msg_move_end(&txn->rsp, delta);
6892 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006893 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006894 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896 }
Willy Tarreau67402132012-05-31 20:40:20 +02006897 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899 * with this server since we know it.
6900 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006901 delta = buffer_replace2(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006902 next += delta;
6903 hdr_end += delta;
6904 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006906 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
Willy Tarreauf1348312010-10-07 15:54:11 +02006908 txn->flags &= ~TX_SCK_MASK;
6909 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 }
Willy Tarreaua0590312012-06-06 16:07:00 +02006911 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006913 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006915 delta = buffer_replace2(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 next += delta;
6917 hdr_end += delta;
6918 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006920 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921
Willy Tarreau827aee92011-03-10 16:55:02 +01006922 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006923 txn->flags &= ~TX_SCK_MASK;
6924 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006925 }
6926 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006927 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6928 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006929 int cmp_len, value_len;
6930 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931
Cyril Bontéb21570a2009-11-29 20:04:48 +01006932 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006933 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6934 value_begin = att_beg + t->be->appsession_name_len;
6935 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006936 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 cmp_len = att_end - att_beg;
6938 value_begin = val_beg;
6939 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006940 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006941
Cyril Bonté17530c32010-04-06 21:11:10 +02006942 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6944 /* free a possibly previously allocated memory */
6945 pool_free2(apools.sessid, txn->sessid);
6946
Cyril Bontéb21570a2009-11-29 20:04:48 +01006947 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006948 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006949 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6950 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6951 return;
6952 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006953 memcpy(txn->sessid, value_begin, value_len);
6954 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006956 }
6957 /* that's done for this cookie, check the next one on the same
6958 * line when next != hdr_end (only if is_cookie2).
6959 */
6960 }
6961 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006963 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006964
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006965 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006966 appsess *asession = NULL;
6967 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006968 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006970 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006971 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6972 Alert("Not enough Memory process_srv():asession:calloc().\n");
6973 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6974 return;
6975 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006976 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6977
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006978 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6979 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6980 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006981 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006982 return;
6983 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006984 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006985 asession->sessid[t->be->appsession_len] = 0;
6986
Willy Tarreau827aee92011-03-10 16:55:02 +01006987 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006988 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006989 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006990 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006991 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006992 return;
6993 }
6994 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006995 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006996
6997 asession->request_count = 0;
6998 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6999 }
7000
7001 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7002 asession->request_count++;
7003 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004}
7005
7006
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007/*
7008 * Check if response is cacheable or not. Updates t->flags.
7009 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007010void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011{
7012 struct http_txn *txn = &t->txn;
7013 char *p1, *p2;
7014
7015 char *cur_ptr, *cur_end, *cur_next;
7016 int cur_idx;
7017
Willy Tarreau5df51872007-11-25 16:20:08 +01007018 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019 return;
7020
7021 /* Iterate through the headers.
7022 * we start with the start line.
7023 */
7024 cur_idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007025 cur_next = rtr->buf.p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026
7027 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7028 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007029 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030
7031 cur_hdr = &txn->hdr_idx.v[cur_idx];
7032 cur_ptr = cur_next;
7033 cur_end = cur_ptr + cur_hdr->len;
7034 cur_next = cur_end + cur_hdr->cr + 1;
7035
7036 /* We have one full header between cur_ptr and cur_end, and the
7037 * next header starts at cur_next. We're only interested in
7038 * "Cookie:" headers.
7039 */
7040
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007041 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7042 if (val) {
7043 if ((cur_end - (cur_ptr + val) >= 8) &&
7044 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7045 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7046 return;
7047 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 }
7049
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007050 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7051 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 continue;
7053
7054 /* OK, right now we know we have a cache-control header at cur_ptr */
7055
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007056 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057
7058 if (p1 >= cur_end) /* no more info */
7059 continue;
7060
7061 /* p1 is at the beginning of the value */
7062 p2 = p1;
7063
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007064 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 p2++;
7066
7067 /* we have a complete value between p1 and p2 */
7068 if (p2 < cur_end && *p2 == '=') {
7069 /* we have something of the form no-cache="set-cookie" */
7070 if ((cur_end - p1 >= 21) &&
7071 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7072 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007073 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007074 continue;
7075 }
7076
7077 /* OK, so we know that either p2 points to the end of string or to a comma */
7078 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7079 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7080 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7081 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007082 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 return;
7084 }
7085
7086 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007087 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088 continue;
7089 }
7090 }
7091}
7092
7093
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094/*
7095 * Try to retrieve a known appsession in the URI, then the associated server.
7096 * If the server is found, it's assigned to the session.
7097 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007098void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007100 char *end_params, *first_param, *cur_param, *next_param;
7101 char separator;
7102 int value_len;
7103
7104 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007106 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007107 (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 +01007108 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110
Cyril Bontéb21570a2009-11-29 20:04:48 +01007111 first_param = NULL;
7112 switch (mode) {
7113 case PR_O2_AS_M_PP:
7114 first_param = memchr(begin, ';', len);
7115 break;
7116 case PR_O2_AS_M_QS:
7117 first_param = memchr(begin, '?', len);
7118 break;
7119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007120
Cyril Bontéb21570a2009-11-29 20:04:48 +01007121 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124
Cyril Bontéb21570a2009-11-29 20:04:48 +01007125 switch (mode) {
7126 case PR_O2_AS_M_PP:
7127 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7128 end_params = (char *) begin + len;
7129 }
7130 separator = ';';
7131 break;
7132 case PR_O2_AS_M_QS:
7133 end_params = (char *) begin + len;
7134 separator = '&';
7135 break;
7136 default:
7137 /* unknown mode, shouldn't happen */
7138 return;
7139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007140
Cyril Bontéb21570a2009-11-29 20:04:48 +01007141 cur_param = next_param = end_params;
7142 while (cur_param > first_param) {
7143 cur_param--;
7144 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7145 /* let's see if this is the appsession parameter */
7146 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7147 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7148 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7149 /* Cool... it's the right one */
7150 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7151 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7152 if (value_len > 0) {
7153 manage_client_side_appsession(t, cur_param, value_len);
7154 }
7155 break;
7156 }
7157 next_param = cur_param;
7158 }
7159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007161 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007162 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164}
7165
Willy Tarreaub2513902006-12-17 14:52:38 +01007166/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007167 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007168 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007169 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007170 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007171 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007172 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007173 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007174 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007175int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007176{
7177 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007178 struct http_msg *msg = &txn->req;
Willy Tarreau572bf902012-07-02 17:01:20 +02007179 const char *uri = msg->buf->buf.p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007180 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007181
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007182 if (!uri_auth)
7183 return 0;
7184
Cyril Bonté70be45d2010-10-12 00:14:35 +02007185 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007186 return 0;
7187
Willy Tarreau295a8372011-03-10 11:25:07 +01007188 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007189 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007191 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007192 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007193 return 0;
7194
Willy Tarreau3a215be2012-03-09 21:39:51 +01007195 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007196 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007197 return 0;
7198
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007199 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007200 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007201 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007202 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007203 break;
7204 }
7205 h++;
7206 }
7207
7208 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007209 h = uri + uri_auth->uri_len;
7210 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007211 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007212 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007213 break;
7214 }
7215 h++;
7216 }
7217 }
7218
Willy Tarreau3a215be2012-03-09 21:39:51 +01007219 h = uri + uri_auth->uri_len;
7220 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007221 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007222 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007223 break;
7224 }
7225 h++;
7226 }
7227
Willy Tarreau3a215be2012-03-09 21:39:51 +01007228 h = uri + uri_auth->uri_len;
7229 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007230 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007231 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007232 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007233 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007234 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7235 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7236 si->applet.ctx.stats.st_code = i;
7237 break;
7238 }
7239 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007240 break;
7241 }
7242 h++;
7243 }
7244
Willy Tarreau295a8372011-03-10 11:25:07 +01007245 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007246
Willy Tarreaub2513902006-12-17 14:52:38 +01007247 return 1;
7248}
7249
Willy Tarreau4076a152009-04-02 15:18:36 +02007250/*
7251 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007252 * By default it tries to report the error position as msg->err_pos. However if
7253 * this one is not set, it will then report msg->next, which is the last known
7254 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreau572bf902012-07-02 17:01:20 +02007255 * displays buffers as a contiguous area starting at buf->buf.p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007256 */
7257void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007258 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007259 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007260{
Willy Tarreau7421efb2012-07-02 15:11:27 +02007261 struct channel *buf = msg->buf;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007262 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007263
Willy Tarreau572bf902012-07-02 17:01:20 +02007264 es->len = MIN(buf->buf.i, sizeof(es->buf));
7265 len1 = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007266 len1 = MIN(len1, es->len);
7267 len2 = es->len - len1; /* remaining data if buffer wraps */
7268
Willy Tarreau572bf902012-07-02 17:01:20 +02007269 memcpy(es->buf, buf->buf.p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007270 if (len2)
Willy Tarreau572bf902012-07-02 17:01:20 +02007271 memcpy(es->buf + len1, buf->buf.data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007272
Willy Tarreau4076a152009-04-02 15:18:36 +02007273 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007274 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007275 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007276 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007277
Willy Tarreau4076a152009-04-02 15:18:36 +02007278 es->when = date; // user-visible date
7279 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007280 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007281 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007282 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007283 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007284 es->ev_id = error_snapshot_id++;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007285 es->b_flags = buf->flags;
7286 es->s_flags = s->flags;
7287 es->t_flags = s->txn.flags;
7288 es->m_flags = msg->flags;
Willy Tarreau572bf902012-07-02 17:01:20 +02007289 es->b_out = buf->buf.o;
7290 es->b_wrap = buf->buf.data + buf->buf.size - buf->buf.p;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007291 es->b_tot = buf->total;
7292 es->m_clen = msg->chunk_len;
7293 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007294}
Willy Tarreaub2513902006-12-17 14:52:38 +01007295
Willy Tarreau294c4732011-12-16 21:35:50 +01007296/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7297 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7298 * performed over the whole headers. Otherwise it must contain a valid header
7299 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7300 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7301 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7302 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7303 * -1.
7304 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007305 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007306unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007307 struct hdr_idx *idx, int occ,
7308 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007309{
Willy Tarreau294c4732011-12-16 21:35:50 +01007310 struct hdr_ctx local_ctx;
7311 char *ptr_hist[MAX_HDR_HISTORY];
7312 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007313 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007314 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007315
Willy Tarreau294c4732011-12-16 21:35:50 +01007316 if (!ctx) {
7317 local_ctx.idx = 0;
7318 ctx = &local_ctx;
7319 }
7320
Willy Tarreaubce70882009-09-07 11:51:47 +02007321 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007322 /* search from the beginning */
Willy Tarreau572bf902012-07-02 17:01:20 +02007323 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007324 occ--;
7325 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007326 *vptr = ctx->line + ctx->val;
7327 *vlen = ctx->vlen;
7328 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007329 }
7330 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007331 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007332 }
7333
7334 /* negative occurrence, we scan all the list then walk back */
7335 if (-occ > MAX_HDR_HISTORY)
7336 return 0;
7337
Willy Tarreau294c4732011-12-16 21:35:50 +01007338 found = hist_ptr = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007339 while (http_find_header2(hname, hlen, msg->buf->buf.p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007340 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7341 len_hist[hist_ptr] = ctx->vlen;
7342 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007343 hist_ptr = 0;
7344 found++;
7345 }
7346 if (-occ > found)
7347 return 0;
7348 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7349 * find occurrence -occ, so we have to check [hist_ptr+occ].
7350 */
7351 hist_ptr += occ;
7352 if (hist_ptr >= MAX_HDR_HISTORY)
7353 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007354 *vptr = ptr_hist[hist_ptr];
7355 *vlen = len_hist[hist_ptr];
7356 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007357}
7358
Willy Tarreaubaaee002006-06-26 02:48:02 +02007359/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360 * Print a debug line with a header
7361 */
7362void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7363{
7364 int len, max;
7365 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007366 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007367 max = end - start;
David du Colombier7af46052012-05-16 14:16:48 +02007368 UBOUND(max, trashlen - len - 1);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007369 len += strlcpy2(trash + len, start, max + 1);
7370 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007371 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372}
7373
Willy Tarreau0937bc42009-12-22 15:03:09 +01007374/*
7375 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7376 * the required fields are properly allocated and that we only need to (re)init
7377 * them. This should be used before processing any new request.
7378 */
7379void http_init_txn(struct session *s)
7380{
7381 struct http_txn *txn = &s->txn;
7382 struct proxy *fe = s->fe;
7383
7384 txn->flags = 0;
7385 txn->status = -1;
7386
William Lallemand5f232402012-04-05 18:02:55 +02007387 global.req_count++;
7388
Willy Tarreauf64d1412010-10-07 20:06:11 +02007389 txn->cookie_first_date = 0;
7390 txn->cookie_last_date = 0;
7391
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007392 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007393 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007394 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007395 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007396 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007397 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007398 txn->req.chunk_len = 0LL;
7399 txn->req.body_len = 0LL;
7400 txn->rsp.chunk_len = 0LL;
7401 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007402 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7403 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007404 txn->req.buf = s->req;
7405 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007406
7407 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007408
7409 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7410 if (fe->options2 & PR_O2_REQBUG_OK)
7411 txn->req.err_pos = -1; /* let buggy requests pass */
7412
Willy Tarreau46023632010-01-07 22:51:47 +01007413 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007414 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7415
Willy Tarreau46023632010-01-07 22:51:47 +01007416 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007417 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7418
7419 if (txn->hdr_idx.v)
7420 hdr_idx_init(&txn->hdr_idx);
7421}
7422
7423/* to be used at the end of a transaction */
7424void http_end_txn(struct session *s)
7425{
7426 struct http_txn *txn = &s->txn;
7427
7428 /* these ones will have been dynamically allocated */
7429 pool_free2(pool2_requri, txn->uri);
7430 pool_free2(pool2_capture, txn->cli_cookie);
7431 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007432 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007433 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007434
William Lallemanda73203e2012-03-12 12:48:57 +01007435 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007436 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007437 txn->uri = NULL;
7438 txn->srv_cookie = NULL;
7439 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007440
7441 if (txn->req.cap) {
7442 struct cap_hdr *h;
7443 for (h = s->fe->req_cap; h; h = h->next)
7444 pool_free2(h->pool, txn->req.cap[h->index]);
7445 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7446 }
7447
7448 if (txn->rsp.cap) {
7449 struct cap_hdr *h;
7450 for (h = s->fe->rsp_cap; h; h = h->next)
7451 pool_free2(h->pool, txn->rsp.cap[h->index]);
7452 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7453 }
7454
Willy Tarreau0937bc42009-12-22 15:03:09 +01007455}
7456
7457/* to be used at the end of a transaction to prepare a new one */
7458void http_reset_txn(struct session *s)
7459{
7460 http_end_txn(s);
7461 http_init_txn(s);
7462
7463 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007464 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007465 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007466 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007467 /* re-init store persistence */
7468 s->store_count = 0;
7469
Willy Tarreau0937bc42009-12-22 15:03:09 +01007470 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007471
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007472 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007473
Willy Tarreau739cfba2010-01-25 23:11:14 +01007474 /* We must trim any excess data from the response buffer, because we
7475 * may have blocked an invalid response from a server that we don't
7476 * want to accidentely forward once we disable the analysers, nor do
7477 * we want those data to come along with next response. A typical
7478 * example of such data would be from a buggy server responding to
7479 * a HEAD with some data, or sending more than the advertised
7480 * content-length.
7481 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007482 if (unlikely(s->rep->buf.i))
7483 s->rep->buf.i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007484
Willy Tarreau0937bc42009-12-22 15:03:09 +01007485 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007486 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007487
Willy Tarreaud04e8582010-05-31 12:31:35 +02007488 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007489 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007490
7491 s->req->rex = TICK_ETERNITY;
7492 s->req->wex = TICK_ETERNITY;
7493 s->req->analyse_exp = TICK_ETERNITY;
7494 s->rep->rex = TICK_ETERNITY;
7495 s->rep->wex = TICK_ETERNITY;
7496 s->rep->analyse_exp = TICK_ETERNITY;
7497}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007498
Willy Tarreauff011f22011-01-06 17:51:27 +01007499void free_http_req_rules(struct list *r) {
7500 struct http_req_rule *tr, *pr;
7501
7502 list_for_each_entry_safe(pr, tr, r, list) {
7503 LIST_DEL(&pr->list);
7504 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7505 free(pr->http_auth.realm);
7506
7507 free(pr);
7508 }
7509}
7510
7511struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7512{
7513 struct http_req_rule *rule;
7514 int cur_arg;
7515
7516 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7517 if (!rule) {
7518 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7519 return NULL;
7520 }
7521
7522 if (!*args[0]) {
7523 goto req_error_parsing;
7524 } else if (!strcmp(args[0], "allow")) {
7525 rule->action = HTTP_REQ_ACT_ALLOW;
7526 cur_arg = 1;
7527 } else if (!strcmp(args[0], "deny")) {
7528 rule->action = HTTP_REQ_ACT_DENY;
7529 cur_arg = 1;
7530 } else if (!strcmp(args[0], "auth")) {
7531 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7532 cur_arg = 1;
7533
7534 while(*args[cur_arg]) {
7535 if (!strcmp(args[cur_arg], "realm")) {
7536 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7537 cur_arg+=2;
7538 continue;
7539 } else
7540 break;
7541 }
7542 } else {
7543req_error_parsing:
7544 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7545 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7546 return NULL;
7547 }
7548
7549 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7550 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007551 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007552
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007553 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7554 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7555 file, linenum, args[0], errmsg);
7556 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007557 return NULL;
7558 }
7559 rule->cond = cond;
7560 }
7561 else if (*args[cur_arg]) {
7562 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7563 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7564 file, linenum, args[0], args[cur_arg]);
7565 return NULL;
7566 }
7567
7568 return rule;
7569}
7570
Willy Tarreau8797c062007-05-07 00:55:35 +02007571/************************************************************************/
7572/* The code below is dedicated to ACL parsing and matching */
7573/************************************************************************/
7574
7575
Willy Tarreau14174bc2012-04-16 14:34:04 +02007576/* This function ensures that the prerequisites for an L7 fetch are ready,
7577 * which means that a request or response is ready. If some data is missing,
7578 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007579 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7580 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007581 *
7582 * The function returns :
7583 * 0 if some data is missing or if the requested data cannot be fetched
7584 * -1 if it is certain that we'll never have any HTTP message there
7585 * 1 if an HTTP message is ready
7586 */
7587static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007588acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007589 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007590{
7591 struct http_txn *txn = l7;
7592 struct http_msg *msg = &txn->req;
7593
7594 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7595 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7596 */
7597
7598 if (unlikely(!s || !txn))
7599 return 0;
7600
7601 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007602 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007603
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007604 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007605 if (unlikely(!s->req))
7606 return 0;
7607
7608 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007609 if ((msg->msg_state == HTTP_MSG_ERROR) ||
7610 buffer_full(&s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007611 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007612 return -1;
7613 }
7614
7615 /* Try to decode HTTP request */
Willy Tarreau572bf902012-07-02 17:01:20 +02007616 if (likely(msg->next < s->req->buf.i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007617 http_msg_analyzer(msg, &txn->hdr_idx);
7618
7619 /* Still no valid request ? */
7620 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007621 if ((msg->msg_state == HTTP_MSG_ERROR) ||
7622 buffer_full(&s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007623 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007624 return -1;
7625 }
7626 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007627 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007628 return 0;
7629 }
7630
7631 /* OK we just got a valid HTTP request. We have some minor
7632 * preparation to perform so that further checks can rely
7633 * on HTTP tests.
7634 */
Willy Tarreau572bf902012-07-02 17:01:20 +02007635 txn->meth = find_http_meth(msg->buf->buf.p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007636 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7637 s->flags |= SN_REDIRECTABLE;
7638
7639 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007640 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007641 return -1;
7642 }
7643 }
7644
Willy Tarreau24e32d82012-04-23 23:55:44 +02007645 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007646 return 0; /* data might have moved and indexes changed */
7647
7648 /* otherwise everything's ready for the request */
7649 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007650 else {
7651 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007652 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7653 return 0;
7654 }
7655
7656 /* everything's OK */
7657 return 1;
7658}
Willy Tarreau8797c062007-05-07 00:55:35 +02007659
Willy Tarreauc0239e02012-04-16 14:42:55 +02007660#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007661 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02007662
Willy Tarreau24e32d82012-04-23 23:55:44 +02007663#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007664 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02007665
Willy Tarreau8797c062007-05-07 00:55:35 +02007666
7667/* 1. Check on METHOD
7668 * We use the pre-parsed method if it is known, and store its number as an
7669 * integer. If it is unknown, we use the pointer and the length.
7670 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007671static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007672{
7673 int len, meth;
7674
Willy Tarreauae8b7962007-06-09 23:10:04 +02007675 len = strlen(*text);
7676 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007677
7678 pattern->val.i = meth;
7679 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007680 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007681 if (!pattern->ptr.str) {
7682 if (err)
7683 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007684 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007685 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007686 pattern->len = len;
7687 }
7688 return 1;
7689}
7690
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007691/* This function fetches the method of current HTTP request and stores
7692 * it in the global pattern struct as a chunk. There are two possibilities :
7693 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7694 * in <len> and <ptr> is NULL ;
7695 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7696 * <len> to its length.
7697 * This is intended to be used with acl_match_meth() only.
7698 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007699static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007700acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007701 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007702{
7703 int meth;
7704 struct http_txn *txn = l7;
7705
Willy Tarreau24e32d82012-04-23 23:55:44 +02007706 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007707
Willy Tarreau8797c062007-05-07 00:55:35 +02007708 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007709 smp->type = SMP_T_UINT;
7710 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007711 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007712 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7713 /* ensure the indexes are not affected */
7714 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007715 smp->type = SMP_T_CSTR;
7716 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007717 smp->data.str.str = txn->req.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007718 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007719 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007720 return 1;
7721}
7722
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007723/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02007724static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02007725{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007726 int icase;
7727
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007728
Willy Tarreauf853c462012-04-23 18:53:56 +02007729 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007730 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02007731 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007732 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007733 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007734 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007735
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007736 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7737 if (pattern->val.i != HTTP_METH_OTHER)
7738 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007739
7740 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02007741 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007742 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007743
7744 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02007745 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
7746 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007747 return ACL_PAT_FAIL;
7748 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007749}
7750
7751/* 2. Check on Request/Status Version
7752 * We simply compare strings here.
7753 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007754static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007755{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007756 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007757 if (!pattern->ptr.str) {
7758 if (err)
7759 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007760 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007761 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02007762 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007763 return 1;
7764}
7765
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007766static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007767acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007768 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007769{
7770 struct http_txn *txn = l7;
7771 char *ptr;
7772 int len;
7773
Willy Tarreauc0239e02012-04-16 14:42:55 +02007774 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007775
Willy Tarreau8797c062007-05-07 00:55:35 +02007776 len = txn->req.sl.rq.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007777 ptr = txn->req.buf->buf.p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007778
7779 while ((len-- > 0) && (*ptr++ != '/'));
7780 if (len <= 0)
7781 return 0;
7782
Willy Tarreauf853c462012-04-23 18:53:56 +02007783 smp->type = SMP_T_CSTR;
7784 smp->data.str.str = ptr;
7785 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007786
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007787 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007788 return 1;
7789}
7790
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007791static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007792acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007793 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007794{
7795 struct http_txn *txn = l7;
7796 char *ptr;
7797 int len;
7798
Willy Tarreauc0239e02012-04-16 14:42:55 +02007799 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007800
Willy Tarreau8797c062007-05-07 00:55:35 +02007801 len = txn->rsp.sl.st.v_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007802 ptr = txn->rsp.buf->buf.p;
Willy Tarreau8797c062007-05-07 00:55:35 +02007803
7804 while ((len-- > 0) && (*ptr++ != '/'));
7805 if (len <= 0)
7806 return 0;
7807
Willy Tarreauf853c462012-04-23 18:53:56 +02007808 smp->type = SMP_T_CSTR;
7809 smp->data.str.str = ptr;
7810 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007811
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007812 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007813 return 1;
7814}
7815
7816/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007817static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007818acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007819 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007820{
7821 struct http_txn *txn = l7;
7822 char *ptr;
7823 int len;
7824
Willy Tarreauc0239e02012-04-16 14:42:55 +02007825 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007826
Willy Tarreau8797c062007-05-07 00:55:35 +02007827 len = txn->rsp.sl.st.c_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007828 ptr = txn->rsp.buf->buf.p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007829
Willy Tarreauf853c462012-04-23 18:53:56 +02007830 smp->type = SMP_T_UINT;
7831 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02007832 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007833 return 1;
7834}
7835
7836/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007837static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007838smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007839 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007840{
7841 struct http_txn *txn = l7;
7842
Willy Tarreauc0239e02012-04-16 14:42:55 +02007843 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007844
Willy Tarreauf853c462012-04-23 18:53:56 +02007845 smp->type = SMP_T_CSTR;
7846 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau572bf902012-07-02 17:01:20 +02007847 smp->data.str.str = txn->req.buf->buf.p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02007848 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007849 return 1;
7850}
7851
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007852static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007853smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007854 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007855{
7856 struct http_txn *txn = l7;
7857
Willy Tarreauc0239e02012-04-16 14:42:55 +02007858 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007859
7860 /* Parse HTTP request */
Willy Tarreau572bf902012-07-02 17:01:20 +02007861 url2sa(txn->req.buf->buf.p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01007862 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7863 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02007864 smp->type = SMP_T_IPV4;
7865 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007866
7867 /*
7868 * If we are parsing url in frontend space, we prepare backend stage
7869 * to not parse again the same url ! optimization lazyness...
7870 */
7871 if (px->options & PR_O_HTTP_PROXY)
7872 l4->flags |= SN_ADDR_SET;
7873
Willy Tarreau37406352012-04-23 16:16:37 +02007874 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007875 return 1;
7876}
7877
7878static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02007879smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007880 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007881{
7882 struct http_txn *txn = l7;
7883
Willy Tarreauc0239e02012-04-16 14:42:55 +02007884 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007885
7886 /* Same optimization as url_ip */
Willy Tarreau572bf902012-07-02 17:01:20 +02007887 url2sa(txn->req.buf->buf.p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02007888 smp->type = SMP_T_UINT;
7889 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007890
7891 if (px->options & PR_O_HTTP_PROXY)
7892 l4->flags |= SN_ADDR_SET;
7893
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02007894 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007895 return 1;
7896}
7897
Willy Tarreau185b5c42012-04-26 15:11:51 +02007898/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
7899 * Accepts an optional argument of type string containing the header field name,
7900 * and an optional argument of type signed or unsigned integer to request an
7901 * explicit occurrence of the header. Note that in the event of a missing name,
7902 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007903 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007904static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007905smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007906 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007907{
7908 struct http_txn *txn = l7;
7909 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02007910 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007911 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02007912 int occ = 0;
7913 const char *name_str = NULL;
7914 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007915
Willy Tarreau185b5c42012-04-26 15:11:51 +02007916 if (args) {
7917 if (args[0].type != ARGT_STR)
7918 return 0;
7919 name_str = args[0].data.str.str;
7920 name_len = args[0].data.str.len;
7921
7922 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
7923 occ = args[1].data.uint;
7924 }
Willy Tarreau34db1082012-04-19 17:16:54 +02007925
Willy Tarreaue333ec92012-04-16 16:26:40 +02007926 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007927
Willy Tarreau185b5c42012-04-26 15:11:51 +02007928 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007929 /* search for header from the beginning */
7930 ctx->idx = 0;
7931
Willy Tarreau185b5c42012-04-26 15:11:51 +02007932 if (!occ && !(opt & SMP_OPT_ITERATE))
7933 /* no explicit occurrence and single fetch => last header by default */
7934 occ = -1;
7935
7936 if (!occ)
7937 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02007938 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01007939
Willy Tarreau185b5c42012-04-26 15:11:51 +02007940 smp->type = SMP_T_CSTR;
7941 smp->flags |= SMP_F_VOL_HDR;
7942 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007943 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007944
Willy Tarreau37406352012-04-23 16:16:37 +02007945 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007946 return 0;
7947}
7948
Willy Tarreauc11416f2007-06-17 16:58:38 +02007949/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007950 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007951 */
7952static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007953smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007954 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007955{
7956 struct http_txn *txn = l7;
7957 struct hdr_idx *idx = &txn->hdr_idx;
7958 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007959 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007960 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007961
Willy Tarreau24e32d82012-04-23 23:55:44 +02007962 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02007963 return 0;
7964
Willy Tarreaue333ec92012-04-16 16:26:40 +02007965 CHECK_HTTP_MESSAGE_FIRST();
7966
Willy Tarreau33a7e692007-06-10 19:45:56 +02007967 ctx.idx = 0;
7968 cnt = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02007969 while (http_find_header2(args->data.str.str, args->data.str.len, msg->buf->buf.p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007970 cnt++;
7971
Willy Tarreauf853c462012-04-23 18:53:56 +02007972 smp->type = SMP_T_UINT;
7973 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02007974 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007975 return 1;
7976}
7977
Willy Tarreau185b5c42012-04-26 15:11:51 +02007978/* Fetch an HTTP header's integer value. The integer value is returned. It
7979 * takes a mandatory argument of type string and an optional one of type int
7980 * to designate a specific occurrence. It returns an unsigned integer, which
7981 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007982 */
7983static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02007984smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007985 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02007986{
Willy Tarreau185b5c42012-04-26 15:11:51 +02007987 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007988
Willy Tarreauf853c462012-04-23 18:53:56 +02007989 if (ret > 0) {
7990 smp->type = SMP_T_UINT;
7991 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
7992 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02007993
Willy Tarreaud53e2422012-04-16 17:21:11 +02007994 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007995}
7996
Willy Tarreau185b5c42012-04-26 15:11:51 +02007997/* Fetch an HTTP header's integer value. The integer value is returned. It
7998 * takes a mandatory argument of type string and an optional one of type int
7999 * to designate a specific occurrence. It returns an IPv4 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008000 */
8001static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008002smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008003 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008004{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008005 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008006
Willy Tarreau185b5c42012-04-26 15:11:51 +02008007 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Willy Tarreauf853c462012-04-23 18:53:56 +02008008 smp->type = SMP_T_IPV4;
8009 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4))
Willy Tarreaud53e2422012-04-16 17:21:11 +02008010 break;
8011 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008012 if (!(smp->flags & SMP_F_NOT_LAST))
8013 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008014 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008015 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008016}
8017
Willy Tarreau737b0c12007-06-10 21:28:46 +02008018/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8019 * the first '/' after the possible hostname, and ends before the possible '?'.
8020 */
8021static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008022smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008023 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008024{
8025 struct http_txn *txn = l7;
8026 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008027
Willy Tarreauc0239e02012-04-16 14:42:55 +02008028 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008029
Willy Tarreau572bf902012-07-02 17:01:20 +02008030 end = txn->req.buf->buf.p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008031 ptr = http_get_path(txn);
8032 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008033 return 0;
8034
8035 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008036 smp->type = SMP_T_CSTR;
8037 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008038
8039 while (ptr < end && *ptr != '?')
8040 ptr++;
8041
Willy Tarreauf853c462012-04-23 18:53:56 +02008042 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008043 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008044 return 1;
8045}
8046
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008047/* This produces a concatenation of the first occurrence of the Host header
8048 * followed by the path component if it begins with a slash ('/'). This means
8049 * that '*' will not be added, resulting in exactly the first Host entry.
8050 * If no Host header is found, then the path is returned as-is. The returned
8051 * value is stored in the trash so it does not need to be marked constant.
8052 */
8053static int
8054smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8055 const struct arg *args, struct sample *smp)
8056{
8057 struct http_txn *txn = l7;
8058 char *ptr, *end, *beg;
8059 struct hdr_ctx ctx;
8060
8061 CHECK_HTTP_MESSAGE_FIRST();
8062
8063 ctx.idx = 0;
Willy Tarreau572bf902012-07-02 17:01:20 +02008064 if (!http_find_header2("Host", 4, txn->req.buf->buf.p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008065 !ctx.vlen)
8066 return smp_fetch_path(px, l4, l7, opt, args, smp);
8067
8068 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8069 memcpy(trash, ctx.line + ctx.val, ctx.vlen);
8070 smp->type = SMP_T_STR;
8071 smp->data.str.str = trash;
8072 smp->data.str.len = ctx.vlen;
8073
8074 /* now retrieve the path */
Willy Tarreau572bf902012-07-02 17:01:20 +02008075 end = txn->req.buf->buf.p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008076 beg = http_get_path(txn);
8077 if (!beg)
8078 beg = end;
8079
8080 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8081
8082 if (beg < ptr && *beg == '/') {
8083 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8084 smp->data.str.len += ptr - beg;
8085 }
8086
8087 smp->flags = SMP_F_VOL_1ST;
8088 return 1;
8089}
8090
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008091static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008092acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008093 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008094{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008095 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8096 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8097 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008098
Willy Tarreau24e32d82012-04-23 23:55:44 +02008099 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008100
Willy Tarreauf853c462012-04-23 18:53:56 +02008101 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008102 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008103 return 1;
8104}
8105
Willy Tarreau7f18e522010-10-22 20:04:13 +02008106/* return a valid test if the current request is the first one on the connection */
8107static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008108acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008109 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008110{
8111 if (!s)
8112 return 0;
8113
Willy Tarreauf853c462012-04-23 18:53:56 +02008114 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008115 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008116 return 1;
8117}
8118
Willy Tarreau34db1082012-04-19 17:16:54 +02008119/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008120static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008121acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008122 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008123{
8124
Willy Tarreau24e32d82012-04-23 23:55:44 +02008125 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008126 return 0;
8127
Willy Tarreauc0239e02012-04-16 14:42:55 +02008128 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008129
Willy Tarreauc0239e02012-04-16 14:42:55 +02008130 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008131 return 0;
8132
Willy Tarreauf853c462012-04-23 18:53:56 +02008133 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008134 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008135 return 1;
8136}
Willy Tarreau8797c062007-05-07 00:55:35 +02008137
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008138/* Accepts exactly 1 argument of type userlist */
8139static int
8140acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8141 const struct arg *args, struct sample *smp)
8142{
8143
8144 if (!args || args->type != ARGT_USR)
8145 return 0;
8146
8147 CHECK_HTTP_MESSAGE_FIRST();
8148
8149 if (!get_http_auth(l4))
8150 return 0;
8151
8152 /* acl_match_auth() will need several information at once */
8153 smp->ctx.a[0] = args->data.usr; /* user list */
8154 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8155 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8156
8157 /* if the user does not belong to the userlist or has a wrong password,
8158 * report that it unconditionally does not match. Otherwise we return
8159 * a non-zero integer which will be ignored anyway since all the params
8160 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8161 */
8162 smp->type = SMP_T_BOOL;
8163 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8164 if (smp->data.uint)
8165 smp->type = SMP_T_UINT;
8166
8167 return 1;
8168}
8169
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008170/* Try to find the next occurrence of a cookie name in a cookie header value.
8171 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8172 * the cookie value is returned into *value and *value_l, and the function
8173 * returns a pointer to the next pointer to search from if the value was found.
8174 * Otherwise if the cookie was not found, NULL is returned and neither value
8175 * nor value_l are touched. The input <hdr> string should first point to the
8176 * header's value, and the <hdr_end> pointer must point to the first character
8177 * not part of the value. <list> must be non-zero if value may represent a list
8178 * of values (cookie headers). This makes it faster to abort parsing when no
8179 * list is expected.
8180 */
8181static char *
8182extract_cookie_value(char *hdr, const char *hdr_end,
8183 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008184 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008185{
8186 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8187 char *next;
8188
8189 /* we search at least a cookie name followed by an equal, and more
8190 * generally something like this :
8191 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8192 */
8193 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8194 /* Iterate through all cookies on this line */
8195
8196 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8197 att_beg++;
8198
8199 /* find att_end : this is the first character after the last non
8200 * space before the equal. It may be equal to hdr_end.
8201 */
8202 equal = att_end = att_beg;
8203
8204 while (equal < hdr_end) {
8205 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8206 break;
8207 if (http_is_spht[(unsigned char)*equal++])
8208 continue;
8209 att_end = equal;
8210 }
8211
8212 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8213 * is between <att_beg> and <equal>, both may be identical.
8214 */
8215
8216 /* look for end of cookie if there is an equal sign */
8217 if (equal < hdr_end && *equal == '=') {
8218 /* look for the beginning of the value */
8219 val_beg = equal + 1;
8220 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8221 val_beg++;
8222
8223 /* find the end of the value, respecting quotes */
8224 next = find_cookie_value_end(val_beg, hdr_end);
8225
8226 /* make val_end point to the first white space or delimitor after the value */
8227 val_end = next;
8228 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8229 val_end--;
8230 } else {
8231 val_beg = val_end = next = equal;
8232 }
8233
8234 /* We have nothing to do with attributes beginning with '$'. However,
8235 * they will automatically be removed if a header before them is removed,
8236 * since they're supposed to be linked together.
8237 */
8238 if (*att_beg == '$')
8239 continue;
8240
8241 /* Ignore cookies with no equal sign */
8242 if (equal == next)
8243 continue;
8244
8245 /* Now we have the cookie name between att_beg and att_end, and
8246 * its value between val_beg and val_end.
8247 */
8248
8249 if (att_end - att_beg == cookie_name_l &&
8250 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8251 /* let's return this value and indicate where to go on from */
8252 *value = val_beg;
8253 *value_l = val_end - val_beg;
8254 return next + 1;
8255 }
8256
8257 /* Set-Cookie headers only have the name in the first attr=value part */
8258 if (!list)
8259 break;
8260 }
8261
8262 return NULL;
8263}
8264
Willy Tarreaue333ec92012-04-16 16:26:40 +02008265/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008266 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8267 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008268 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008269 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008270 * Accepts exactly 1 argument of type string. If the input options indicate
8271 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008272 */
8273static int
Willy Tarreau51539362012-05-08 12:46:28 +02008274smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8275 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008276{
8277 struct http_txn *txn = l7;
8278 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008279 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008280 const struct http_msg *msg;
8281 const char *hdr_name;
8282 int hdr_name_len;
8283 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008284 int occ = 0;
8285 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008286
Willy Tarreau24e32d82012-04-23 23:55:44 +02008287 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008288 return 0;
8289
Willy Tarreaue333ec92012-04-16 16:26:40 +02008290 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008291
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008292 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008293 msg = &txn->req;
8294 hdr_name = "Cookie";
8295 hdr_name_len = 6;
8296 } else {
8297 msg = &txn->rsp;
8298 hdr_name = "Set-Cookie";
8299 hdr_name_len = 10;
8300 }
8301
Willy Tarreau28376d62012-04-26 21:26:10 +02008302 if (!occ && !(opt & SMP_OPT_ITERATE))
8303 /* no explicit occurrence and single fetch => last cookie by default */
8304 occ = -1;
8305
8306 /* OK so basically here, either we want only one value and it's the
8307 * last one, or we want to iterate over all of them and we fetch the
8308 * next one.
8309 */
8310
Willy Tarreau572bf902012-07-02 17:01:20 +02008311 sol = msg->buf->buf.p;
Willy Tarreau37406352012-04-23 16:16:37 +02008312 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008313 /* search for the header from the beginning, we must first initialize
8314 * the search parameters.
8315 */
Willy Tarreau37406352012-04-23 16:16:37 +02008316 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008317 ctx->idx = 0;
8318 }
8319
Willy Tarreau28376d62012-04-26 21:26:10 +02008320 smp->flags |= SMP_F_VOL_HDR;
8321
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008322 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008323 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8324 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008325 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8326 goto out;
8327
Willy Tarreau24e32d82012-04-23 23:55:44 +02008328 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008329 continue;
8330
Willy Tarreau37406352012-04-23 16:16:37 +02008331 smp->ctx.a[0] = ctx->line + ctx->val;
8332 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008333 }
8334
Willy Tarreauf853c462012-04-23 18:53:56 +02008335 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008336 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008337 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008338 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008339 &smp->data.str.str,
8340 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008341 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008342 found = 1;
8343 if (occ >= 0) {
8344 /* one value was returned into smp->data.str.{str,len} */
8345 smp->flags |= SMP_F_NOT_LAST;
8346 return 1;
8347 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008348 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008349 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008350 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008351 /* all cookie headers and values were scanned. If we're looking for the
8352 * last occurrence, we may return it now.
8353 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008354 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008355 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008356 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008357}
8358
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008359/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008360 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008361 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008362 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008363 */
8364static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008365acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008366 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008367{
8368 struct http_txn *txn = l7;
8369 struct hdr_idx *idx = &txn->hdr_idx;
8370 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008371 const struct http_msg *msg;
8372 const char *hdr_name;
8373 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008374 int cnt;
8375 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008376 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008377
Willy Tarreau24e32d82012-04-23 23:55:44 +02008378 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008379 return 0;
8380
Willy Tarreaue333ec92012-04-16 16:26:40 +02008381 CHECK_HTTP_MESSAGE_FIRST();
8382
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008383 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008384 msg = &txn->req;
8385 hdr_name = "Cookie";
8386 hdr_name_len = 6;
8387 } else {
8388 msg = &txn->rsp;
8389 hdr_name = "Set-Cookie";
8390 hdr_name_len = 10;
8391 }
8392
Willy Tarreau572bf902012-07-02 17:01:20 +02008393 sol = msg->buf->buf.p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008394 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008395 ctx.idx = 0;
8396 cnt = 0;
8397
8398 while (1) {
8399 /* Note: val_beg == NULL every time we need to fetch a new header */
8400 if (!val_beg) {
8401 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8402 break;
8403
Willy Tarreau24e32d82012-04-23 23:55:44 +02008404 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008405 continue;
8406
8407 val_beg = ctx.line + ctx.val;
8408 val_end = val_beg + ctx.vlen;
8409 }
8410
Willy Tarreauf853c462012-04-23 18:53:56 +02008411 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008412 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008413 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008414 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008415 &smp->data.str.str,
8416 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008417 cnt++;
8418 }
8419 }
8420
Willy Tarreauf853c462012-04-23 18:53:56 +02008421 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008422 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008423 return 1;
8424}
8425
Willy Tarreau51539362012-05-08 12:46:28 +02008426/* Fetch an cookie's integer value. The integer value is returned. It
8427 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8428 */
8429static int
8430smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8431 const struct arg *args, struct sample *smp)
8432{
8433 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8434
8435 if (ret > 0) {
8436 smp->type = SMP_T_UINT;
8437 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8438 }
8439
8440 return ret;
8441}
8442
Willy Tarreau8797c062007-05-07 00:55:35 +02008443/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008444/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008445/************************************************************************/
8446
David Cournapeau16023ee2010-12-23 20:55:41 +09008447/*
8448 * Given a path string and its length, find the position of beginning of the
8449 * query string. Returns NULL if no query string is found in the path.
8450 *
8451 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8452 *
8453 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8454 */
8455static inline char *find_query_string(char *path, size_t path_l)
8456{
8457 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008458
David Cournapeau16023ee2010-12-23 20:55:41 +09008459 p = memchr(path, '?', path_l);
8460 return p ? p + 1 : NULL;
8461}
8462
8463static inline int is_param_delimiter(char c)
8464{
8465 return c == '&' || c == ';';
8466}
8467
8468/*
8469 * Given a url parameter, find the starting position of the first occurence,
8470 * or NULL if the parameter is not found.
8471 *
8472 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8473 * the function will return query_string+8.
8474 */
8475static char*
8476find_url_param_pos(char* query_string, size_t query_string_l,
8477 char* url_param_name, size_t url_param_name_l)
8478{
8479 char *pos, *last;
8480
8481 pos = query_string;
8482 last = query_string + query_string_l - url_param_name_l - 1;
8483
8484 while (pos <= last) {
8485 if (pos[url_param_name_l] == '=') {
8486 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8487 return pos;
8488 pos += url_param_name_l + 1;
8489 }
8490 while (pos <= last && !is_param_delimiter(*pos))
8491 pos++;
8492 pos++;
8493 }
8494 return NULL;
8495}
8496
8497/*
8498 * Given a url parameter name, returns its value and size into *value and
8499 * *value_l respectively, and returns non-zero. If the parameter is not found,
8500 * zero is returned and value/value_l are not touched.
8501 */
8502static int
8503find_url_param_value(char* path, size_t path_l,
8504 char* url_param_name, size_t url_param_name_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008505 char** value, int* value_l)
David Cournapeau16023ee2010-12-23 20:55:41 +09008506{
8507 char *query_string, *qs_end;
8508 char *arg_start;
8509 char *value_start, *value_end;
8510
8511 query_string = find_query_string(path, path_l);
8512 if (!query_string)
8513 return 0;
8514
8515 qs_end = path + path_l;
8516 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8517 url_param_name, url_param_name_l);
8518 if (!arg_start)
8519 return 0;
8520
8521 value_start = arg_start + url_param_name_l + 1;
8522 value_end = value_start;
8523
8524 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8525 value_end++;
8526
8527 *value = value_start;
8528 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008529 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008530}
8531
8532static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008533smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8534 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008535{
8536 struct http_txn *txn = l7;
8537 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008538
8539 if (!args || args->type != ARGT_STR)
8540 return 0;
8541
8542 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008543
Willy Tarreau572bf902012-07-02 17:01:20 +02008544 if (!find_url_param_value(msg->buf->buf.p + msg->sl.rq.u, msg->sl.rq.u_l,
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008545 args->data.str.str, args->data.str.len,
8546 &smp->data.str.str, &smp->data.str.len))
David Cournapeau16023ee2010-12-23 20:55:41 +09008547 return 0;
8548
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008549 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008550 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008551 return 1;
8552}
8553
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008554/* Return the signed integer value for the specified url parameter (see url_param
8555 * above).
8556 */
8557static int
8558smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8559 const struct arg *args, struct sample *smp)
8560{
8561 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8562
8563 if (ret > 0) {
8564 smp->type = SMP_T_UINT;
8565 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8566 }
8567
8568 return ret;
8569}
8570
Willy Tarreau185b5c42012-04-26 15:11:51 +02008571/* This function is used to validate the arguments passed to any "hdr" fetch
8572 * keyword. These keywords support an optional positive or negative occurrence
8573 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8574 * is assumed that the types are already the correct ones. Returns 0 on error,
8575 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8576 * error message in case of error, that the caller is responsible for freeing.
8577 * The initial location must either be freeable or NULL.
8578 */
8579static int val_hdr(struct arg *arg, char **err_msg)
8580{
8581 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
8582 if (err_msg)
8583 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
8584 return 0;
8585 }
8586 return 1;
8587}
8588
Willy Tarreau4a568972010-05-12 08:08:50 +02008589/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008590/* All supported ACL keywords must be declared here. */
8591/************************************************************************/
8592
8593/* Note: must not be declared <const> as its list will be overwritten.
8594 * Please take care of keeping this list alphabetically sorted.
8595 */
8596static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008597 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8598 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8599 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8600 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8601 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8602 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8603 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8604 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8605
Willy Tarreau51539362012-05-08 12:46:28 +02008606 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8607 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008608 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008609 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8610 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8611 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8612 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8613 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8614 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8615 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008616
Willy Tarreau185b5c42012-04-26 15:11:51 +02008617 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8618 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8619 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8620 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8621 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8622 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8623 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8624 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8625 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8626 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8627 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008628
8629 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008630 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008631 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8632
8633 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8634
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008635 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8636 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8637 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8638 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8639 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8640 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8641 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8642 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008643
8644 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8645 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8646 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8647
Willy Tarreau51539362012-05-08 12:46:28 +02008648 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8649 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008650 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008651 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8652 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8653 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8654 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8655 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8656 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8657 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008658
Willy Tarreau185b5c42012-04-26 15:11:51 +02008659 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8660 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8661 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8662 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8663 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8664 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8665 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8666 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8667 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8668 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8669 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008670
8671 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8672
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008673 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8674 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8675 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8676 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8677 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8678 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8679 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8680 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8681 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8682 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008683
8684 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8685 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8686 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8687 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8688 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8689 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8690 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8691 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8692 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008693 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008694
8695 { NULL, NULL, NULL, NULL },
8696}};
8697
8698/************************************************************************/
8699/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008700/************************************************************************/
8701/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02008702static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008703 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008704 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008705 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8706 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_REQ },
8707 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_REQ },
8708 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_REQ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008709 { "url_param", smp_fetch_url_param, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ },
Willy Tarreau51539362012-05-08 12:46:28 +02008710 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_REQ|SMP_CAP_RES },
8711 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02008712 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008713}};
8714
Willy Tarreau8797c062007-05-07 00:55:35 +02008715
8716__attribute__((constructor))
8717static void __http_protocol_init(void)
8718{
8719 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02008720 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008721}
8722
8723
Willy Tarreau58f10d72006-12-04 02:26:12 +01008724/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008725 * Local variables:
8726 * c-indent-level: 8
8727 * c-basic-offset: 8
8728 * End:
8729 */