blob: 2fa75980f64d86168cc6e786b4289ba82d36767f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
96const char *HTTP_401_fmt =
97 "HTTP/1.0 401 Unauthorized\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200100 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
102 "\r\n"
103 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
104
Willy Tarreau844a7e72010-01-31 21:46:18 +0100105const char *HTTP_407_fmt =
106 "HTTP/1.0 407 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
109 "Content-Type: text/html\r\n"
110 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200116 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100117 [HTTP_ERR_400] = 400,
118 [HTTP_ERR_403] = 403,
119 [HTTP_ERR_408] = 408,
120 [HTTP_ERR_500] = 500,
121 [HTTP_ERR_502] = 502,
122 [HTTP_ERR_503] = 503,
123 [HTTP_ERR_504] = 504,
124};
125
Willy Tarreau80587432006-12-24 17:47:20 +0100126static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200127 [HTTP_ERR_200] =
128 "HTTP/1.0 200 OK\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "\r\n"
133 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Cyril Bonté19979e12012-04-04 12:57:21 +0200193/* status codes available for the stats admin page (strictly 4 chars length) */
194const char *stat_status_codes[STAT_STATUS_SIZE] = {
195 [STAT_STATUS_DENY] = "DENY",
196 [STAT_STATUS_DONE] = "DONE",
197 [STAT_STATUS_ERRP] = "ERRP",
198 [STAT_STATUS_EXCD] = "EXCD",
199 [STAT_STATUS_NONE] = "NONE",
200 [STAT_STATUS_PART] = "PART",
201 [STAT_STATUS_UNKN] = "UNKN",
202};
203
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205/* We must put the messages here since GCC cannot initialize consts depending
206 * on strlen().
207 */
208struct chunk http_err_chunks[HTTP_ERR_SIZE];
209
Willy Tarreau42250582007-04-01 01:30:43 +0200210#define FD_SETS_ARE_BITFIELDS
211#ifdef FD_SETS_ARE_BITFIELDS
212/*
213 * This map is used with all the FD_* macros to check whether a particular bit
214 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
215 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
216 * byte should be encoded. Be careful to always pass bytes from 0 to 255
217 * exclusively to the macros.
218 */
219fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
221
222#else
223#error "Check if your OS uses bitfields for fd_sets"
224#endif
225
Willy Tarreau80587432006-12-24 17:47:20 +0100226void init_proto_http()
227{
Willy Tarreau42250582007-04-01 01:30:43 +0200228 int i;
229 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100230 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200231
Willy Tarreau80587432006-12-24 17:47:20 +0100232 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
233 if (!http_err_msgs[msg]) {
234 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
235 abort();
236 }
237
238 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
239 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
240 }
Willy Tarreau42250582007-04-01 01:30:43 +0200241
242 /* initialize the log header encoding map : '{|}"#' should be encoded with
243 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
244 * URL encoding only requires '"', '#' to be encoded as well as non-
245 * printable characters above.
246 */
247 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
248 memset(url_encode_map, 0, sizeof(url_encode_map));
249 for (i = 0; i < 32; i++) {
250 FD_SET(i, hdr_encode_map);
251 FD_SET(i, url_encode_map);
252 }
253 for (i = 127; i < 256; i++) {
254 FD_SET(i, hdr_encode_map);
255 FD_SET(i, url_encode_map);
256 }
257
258 tmp = "\"#{|}";
259 while (*tmp) {
260 FD_SET(*tmp, hdr_encode_map);
261 tmp++;
262 }
263
264 tmp = "\"#";
265 while (*tmp) {
266 FD_SET(*tmp, url_encode_map);
267 tmp++;
268 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200269
270 /* memory allocations */
271 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100392 chunk_printf(&trash,
393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
397 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100399 chunk_printf(&trash,
400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
404 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405}
406#else
407#define http_silent_debug(l,s) do { } while (0)
408#endif
409
410/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100411 * Adds a header and its CRLF at the tail of the message's buffer, just before
412 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100417int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418{
419 int bytes, len;
420
421 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200422 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
428
429/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100430 * Adds a header and its CRLF at the tail of the message's buffer, just before
431 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100432 * the buffer is only opened and the space reserved, but nothing is copied.
433 * The header is also automatically added to the index <hdr_idx>, and the end
434 * of headers is automatically adjusted. The number of bytes added is returned
435 * on success, otherwise <0 is returned indicating an error.
436 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100437int http_header_add_tail2(struct http_msg *msg,
438 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439{
440 int bytes;
441
Willy Tarreau9b28e032012-10-12 23:49:43 +0200442 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 if (!bytes)
444 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100445 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
447}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448
449/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100450 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
451 * If so, returns the position of the first non-space character relative to
452 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
453 * to return a pointer to the place after the first space. Returns 0 if the
454 * header name does not match. Checks are case-insensitive.
455 */
456int http_header_match2(const char *hdr, const char *end,
457 const char *name, int len)
458{
459 const char *val;
460
461 if (hdr + len >= end)
462 return 0;
463 if (hdr[len] != ':')
464 return 0;
465 if (strncasecmp(hdr, name, len) != 0)
466 return 0;
467 val = hdr + len + 1;
468 while (val < end && HTTP_IS_SPHT(*val))
469 val++;
470 if ((val >= end) && (len + 2 <= end - hdr))
471 return len + 2; /* we may replace starting from second space */
472 return val - hdr;
473}
474
Willy Tarreau68085d82010-01-18 14:54:04 +0100475/* Find the end of the header value contained between <s> and <e>. See RFC2616,
476 * par 2.2 for more information. Note that it requires a valid header to return
477 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200478 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100479char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480{
481 int quoted, qdpair;
482
483 quoted = qdpair = 0;
484 for (; s < e; s++) {
485 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200486 else if (quoted) {
487 if (*s == '\\') qdpair = 1;
488 else if (*s == '"') quoted = 0;
489 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490 else if (*s == '"') quoted = 1;
491 else if (*s == ',') return s;
492 }
493 return s;
494}
495
496/* Find the first or next occurrence of header <name> in message buffer <sol>
497 * using headers index <idx>, and return it in the <ctx> structure. This
498 * structure holds everything necessary to use the header and find next
499 * occurrence. If its <idx> member is 0, the header is searched from the
500 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100501 * 1 when it finds a value, and 0 when there is no more. It is designed to work
502 * with headers defined as comma-separated lists. As a special case, if ctx->val
503 * is NULL when searching for a new values of a header, the current header is
504 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 */
506int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100507 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 struct hdr_ctx *ctx)
509{
Willy Tarreau68085d82010-01-18 14:54:04 +0100510 char *eol, *sov;
511 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200512
Willy Tarreau68085d82010-01-18 14:54:04 +0100513 cur_idx = ctx->idx;
514 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515 /* We have previously returned a value, let's search
516 * another one on the same line.
517 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200519 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100520 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 eol = sol + idx->v[cur_idx].len;
522
523 if (sov >= eol)
524 /* no more values in this header */
525 goto next_hdr;
526
Willy Tarreau68085d82010-01-18 14:54:04 +0100527 /* values remaining for this header, skip the comma but save it
528 * for later use (eg: for header deletion).
529 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 sov++;
531 while (sov < eol && http_is_lws[(unsigned char)*sov])
532 sov++;
533
534 goto return_hdr;
535 }
536
537 /* first request for this header */
538 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200540 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 while (cur_idx) {
542 eol = sol + idx->v[cur_idx].len;
543
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200544 if (len == 0) {
545 /* No argument was passed, we want any header.
546 * To achieve this, we simply build a fake request. */
547 while (sol + len < eol && sol[len] != ':')
548 len++;
549 name = sol;
550 }
551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 if ((len < eol - sol) &&
553 (sol[len] == ':') &&
554 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 sov = sol + len + 1;
557 while (sov < eol && http_is_lws[(unsigned char)*sov])
558 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 ctx->prev = old_idx;
562 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 ctx->idx = cur_idx;
564 ctx->val = sov - sol;
565
566 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200567 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200568 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 eol--;
570 ctx->tws++;
571 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 ctx->vlen = eol - sov;
573 return 1;
574 }
575 next_hdr:
576 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200578 cur_idx = idx->v[cur_idx].next;
579 }
580 return 0;
581}
582
583int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100584 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200585 struct hdr_ctx *ctx)
586{
587 return http_find_header2(name, strlen(name), sol, idx, ctx);
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Remove one value of a header. This only works on a <ctx> returned by one of
591 * the http_find_header functions. The value is removed, as well as surrounding
592 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100593 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100594 * message <msg>. The new index is returned. If it is zero, it means there is
595 * no more header, so any processing may stop. The ctx is always left in a form
596 * that can be handled by http_find_header2() to find next occurrence.
597 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100598int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100599{
600 int cur_idx = ctx->idx;
601 char *sol = ctx->line;
602 struct hdr_idx_elem *hdr;
603 int delta, skip_comma;
604
605 if (!cur_idx)
606 return 0;
607
608 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200609 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200611 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 http_msg_move_end(msg, delta);
613 idx->used--;
614 hdr->len = 0; /* unused entry */
615 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100616 if (idx->tail == ctx->idx)
617 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 ctx->idx = ctx->prev; /* walk back to the end of previous header */
619 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
620 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 return ctx->idx;
623 }
624
625 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200626 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
627 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 */
629
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200630 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200631 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 NULL, 0);
634 hdr->len += delta;
635 http_msg_move_end(msg, delta);
636 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200637 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100638 return ctx->idx;
639}
640
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641/* This function handles a server error at the stream interface level. The
642 * stream interface is assumed to be already in a closed state. An optional
643 * message is copied into the input buffer, and an HTTP status code stored.
644 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100645 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100647static void http_server_error(struct session *t, struct stream_interface *si,
648 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200650 channel_auto_read(si->ob);
651 channel_abort(si->ob);
652 channel_auto_close(si->ob);
653 channel_erase(si->ob);
654 channel_auto_close(si->ib);
655 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100656 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100657 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200658 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659 }
660 if (!(t->flags & SN_ERR_MASK))
661 t->flags |= err;
662 if (!(t->flags & SN_FINST_MASK))
663 t->flags |= finst;
664}
665
Willy Tarreau80587432006-12-24 17:47:20 +0100666/* This function returns the appropriate error location for the given session
667 * and message.
668 */
669
Willy Tarreau783f2582012-09-04 12:19:04 +0200670struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100671{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200672 if (s->be->errmsg[msgnum].str)
673 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100674 else if (s->fe->errmsg[msgnum].str)
675 return &s->fe->errmsg[msgnum];
676 else
677 return &http_err_chunks[msgnum];
678}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680/*
681 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
682 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
683 */
684static http_meth_t find_http_meth(const char *str, const int len)
685{
686 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100687 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100688
689 m = ((unsigned)*str - 'A');
690
691 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 for (h = http_methods[m]; h->len > 0; h++) {
693 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100694 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 };
698 return HTTP_METH_OTHER;
699 }
700 return HTTP_METH_NONE;
701
702}
703
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704/* Parse the URI from the given transaction (which is assumed to be in request
705 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
706 * It is returned otherwise.
707 */
708static char *
709http_get_path(struct http_txn *txn)
710{
711 char *ptr, *end;
712
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100714 end = ptr + txn->req.sl.rq.u_l;
715
716 if (ptr >= end)
717 return NULL;
718
719 /* RFC2616, par. 5.1.2 :
720 * Request-URI = "*" | absuri | abspath | authority
721 */
722
723 if (*ptr == '*')
724 return NULL;
725
726 if (isalpha((unsigned char)*ptr)) {
727 /* this is a scheme as described by RFC3986, par. 3.1 */
728 ptr++;
729 while (ptr < end &&
730 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
731 ptr++;
732 /* skip '://' */
733 if (ptr == end || *ptr++ != ':')
734 return NULL;
735 if (ptr == end || *ptr++ != '/')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 }
740 /* skip [user[:passwd]@]host[:[port]] */
741
742 while (ptr < end && *ptr != '/')
743 ptr++;
744
745 if (ptr == end)
746 return NULL;
747
748 /* OK, we got the '/' ! */
749 return ptr;
750}
751
Willy Tarreau71241ab2012-12-27 11:30:54 +0100752/* Returns a 302 for a redirectable request that reaches a server working in
753 * in redirect mode. This may only be called just after the stream interface
754 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
755 * follow normal proxy processing. NOTE: this function is designed to support
756 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100758void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200763 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100766 trash.len = strlen(HTTP_302);
767 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100768
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100769 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100770
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100772 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100775 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100777 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
778 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200781 /* 3: add the request URI. Since it was already forwarded, we need
782 * to temporarily rewind the buffer.
783 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200785 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200788 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200789
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 if (!path)
793 return;
794
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100795 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 return;
797
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100798 memcpy(trash.str + trash.len, path, len);
799 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100800
801 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100802 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
803 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100804 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100805 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
806 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100807 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808
809 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200810 si_shutr(si);
811 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 si->err_type = SI_ET_NONE;
813 si->err_loc = NULL;
814 si->state = SI_ST_CLO;
815
816 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100817 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100820 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821}
822
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100823/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824 * that the server side is closed. Note that err_type is actually a
825 * bitmask, where almost only aborts may be cumulated with other
826 * values. We consider that aborted operations are more important
827 * than timeouts or errors due to the fact that nobody else in the
828 * logs might explain incomplete retries. All others should avoid
829 * being cumulated. It should normally not be possible to have multiple
830 * aborts at once, but just in case, the first one in sequence is reported.
831 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100832void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100834 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835
836 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200838 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200841 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100843 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200844 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100845 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100846 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200847 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100849 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200850 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100852 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200853 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100854 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100855 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200856 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100857}
858
Willy Tarreau42250582007-04-01 01:30:43 +0200859extern const char sess_term_cond[8];
860extern const char sess_fin_state[8];
861extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200862struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100863struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100864struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100865
Willy Tarreau117f59e2007-03-04 18:17:17 +0100866/*
867 * Capture headers from message starting at <som> according to header list
868 * <cap_hdr>, and fill the <idx> structure appropriately.
869 */
870void capture_headers(char *som, struct hdr_idx *idx,
871 char **cap, struct cap_hdr *cap_hdr)
872{
873 char *eol, *sol, *col, *sov;
874 int cur_idx;
875 struct cap_hdr *h;
876 int len;
877
878 sol = som + hdr_idx_first_pos(idx);
879 cur_idx = hdr_idx_first_idx(idx);
880
881 while (cur_idx) {
882 eol = sol + idx->v[cur_idx].len;
883
884 col = sol;
885 while (col < eol && *col != ':')
886 col++;
887
888 sov = col + 1;
889 while (sov < eol && http_is_lws[(unsigned char)*sov])
890 sov++;
891
892 for (h = cap_hdr; h; h = h->next) {
893 if ((h->namelen == col - sol) &&
894 (strncasecmp(sol, h->name, h->namelen) == 0)) {
895 if (cap[h->index] == NULL)
896 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200897 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100898
899 if (cap[h->index] == NULL) {
900 Alert("HTTP capture : out of memory.\n");
901 continue;
902 }
903
904 len = eol - sov;
905 if (len > h->len)
906 len = h->len;
907
908 memcpy(cap[h->index], sov, len);
909 cap[h->index][len]=0;
910 }
911 }
912 sol = eol + idx->v[cur_idx].cr + 1;
913 cur_idx = idx->v[cur_idx].next;
914 }
915}
916
917
Willy Tarreau42250582007-04-01 01:30:43 +0200918/* either we find an LF at <ptr> or we jump to <bad>.
919 */
920#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
921
922/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
923 * otherwise to <http_msg_ood> with <state> set to <st>.
924 */
925#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
926 ptr++; \
927 if (likely(ptr < end)) \
928 goto good; \
929 else { \
930 state = (st); \
931 goto http_msg_ood; \
932 } \
933 } while (0)
934
935
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100937 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100938 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
939 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
940 * will give undefined results.
941 * Note that it is upon the caller's responsibility to ensure that ptr < end,
942 * and that msg->sol points to the beginning of the response.
943 * If a complete line is found (which implies that at least one CR or LF is
944 * found before <end>, the updated <ptr> is returned, otherwise NULL is
945 * returned indicating an incomplete line (which does not mean that parts have
946 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
947 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
948 * upon next call.
949 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
952 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200953 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200955const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100956 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100957 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100958{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200959 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100960
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200963 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100964 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
966
967 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100968 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100969 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
970 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100971 state = HTTP_MSG_ERROR;
972 break;
973
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200975 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100976 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100977 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 goto http_msg_rpcode;
979 }
980 if (likely(HTTP_IS_SPHT(*ptr)))
981 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
982 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100983 state = HTTP_MSG_ERROR;
984 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100985
Willy Tarreau8973c702007-01-21 23:58:29 +0100986 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200987 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100988 if (likely(!HTTP_IS_LWS(*ptr)))
989 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
990
991 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100992 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100993 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
994 }
995
996 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100997 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998 http_msg_rsp_reason:
999 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001000 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 msg->sl.st.r_l = 0;
1002 goto http_msg_rpline_eol;
1003
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001005 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001006 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001007 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001008 goto http_msg_rpreason;
1009 }
1010 if (likely(HTTP_IS_SPHT(*ptr)))
1011 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1012 /* so it's a CR/LF, so there is no reason phrase */
1013 goto http_msg_rsp_reason;
1014
Willy Tarreau8973c702007-01-21 23:58:29 +01001015 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001016 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 if (likely(!HTTP_IS_CRLF(*ptr)))
1018 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001019 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001020 http_msg_rpline_eol:
1021 /* We have seen the end of line. Note that we do not
1022 * necessarily have the \n yet, but at least we know that we
1023 * have EITHER \r OR \n, otherwise the response would not be
1024 * complete. We can then record the response length and return
1025 * to the caller which will be able to register it.
1026 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001027 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001028 return ptr;
1029
1030#ifdef DEBUG_FULL
1031 default:
1032 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1033 exit(1);
1034#endif
1035 }
1036
1037 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001038 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 if (ret_state)
1040 *ret_state = state;
1041 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001042 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001043 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044}
1045
Willy Tarreau8973c702007-01-21 23:58:29 +01001046/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001047 * This function parses a request line between <ptr> and <end>, starting with
1048 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1049 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1050 * will give undefined results.
1051 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1052 * and that msg->sol points to the beginning of the request.
1053 * If a complete line is found (which implies that at least one CR or LF is
1054 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1055 * returned indicating an incomplete line (which does not mean that parts have
1056 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1057 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1058 * upon next call.
1059 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001060 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001061 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1062 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001063 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001065const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001066 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001067 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001069 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001073 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (likely(HTTP_IS_TOKEN(*ptr)))
1075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001078 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 if (likely(HTTP_IS_CRLF(*ptr))) {
1083 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 msg->sl.rq.v_l = 0;
1092 goto http_msg_rqline_eol;
1093 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001094 state = HTTP_MSG_ERROR;
1095 break;
1096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001098 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001100 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 goto http_msg_rquri;
1102 }
1103 if (likely(HTTP_IS_SPHT(*ptr)))
1104 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1105 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1106 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001109 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001110 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001114 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1116 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001119 /* non-ASCII chars are forbidden unless option
1120 * accept-invalid-http-request is enabled in the frontend.
1121 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001122 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001123 if (msg->err_pos < -1)
1124 goto invalid_char;
1125 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001126 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1128 }
1129
1130 if (likely(HTTP_IS_CRLF(*ptr))) {
1131 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1132 goto http_msg_req09_uri_e;
1133 }
1134
1135 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001136 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001137 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001138 state = HTTP_MSG_ERROR;
1139 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001142 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001144 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001145 goto http_msg_rqver;
1146 }
1147 if (likely(HTTP_IS_SPHT(*ptr)))
1148 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1149 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1150 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001153 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001154 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001156
1157 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001158 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001159 http_msg_rqline_eol:
1160 /* We have seen the end of line. Note that we do not
1161 * necessarily have the \n yet, but at least we know that we
1162 * have EITHER \r OR \n, otherwise the request would not be
1163 * complete. We can then record the request length and return
1164 * to the caller which will be able to register it.
1165 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001166 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001167 return ptr;
1168 }
1169
1170 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001171 state = HTTP_MSG_ERROR;
1172 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001173
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174#ifdef DEBUG_FULL
1175 default:
1176 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1177 exit(1);
1178#endif
1179 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001181 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001182 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 if (ret_state)
1184 *ret_state = state;
1185 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001186 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001190/*
1191 * Returns the data from Authorization header. Function may be called more
1192 * than once so data is stored in txn->auth_data. When no header is found
1193 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1194 * searching again for something we are unable to find anyway.
1195 */
1196
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001197char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001198
1199int
1200get_http_auth(struct session *s)
1201{
1202
1203 struct http_txn *txn = &s->txn;
1204 struct chunk auth_method;
1205 struct hdr_ctx ctx;
1206 char *h, *p;
1207 int len;
1208
1209#ifdef DEBUG_AUTH
1210 printf("Auth for session %p: %d\n", s, txn->auth.method);
1211#endif
1212
1213 if (txn->auth.method == HTTP_AUTH_WRONG)
1214 return 0;
1215
1216 if (txn->auth.method)
1217 return 1;
1218
1219 txn->auth.method = HTTP_AUTH_WRONG;
1220
1221 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001222
1223 if (txn->flags & TX_USE_PX_CONN) {
1224 h = "Proxy-Authorization";
1225 len = strlen(h);
1226 } else {
1227 h = "Authorization";
1228 len = strlen(h);
1229 }
1230
Willy Tarreau9b28e032012-10-12 23:49:43 +02001231 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001232 return 0;
1233
1234 h = ctx.line + ctx.val;
1235
1236 p = memchr(h, ' ', ctx.vlen);
1237 if (!p || p == h)
1238 return 0;
1239
1240 chunk_initlen(&auth_method, h, 0, p-h);
1241 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1242
1243 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1244
1245 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001246 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001247
1248 if (len < 0)
1249 return 0;
1250
1251
1252 get_http_auth_buff[len] = '\0';
1253
1254 p = strchr(get_http_auth_buff, ':');
1255
1256 if (!p)
1257 return 0;
1258
1259 txn->auth.user = get_http_auth_buff;
1260 *p = '\0';
1261 txn->auth.pass = p+1;
1262
1263 txn->auth.method = HTTP_AUTH_BASIC;
1264 return 1;
1265 }
1266
1267 return 0;
1268}
1269
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8973c702007-01-21 23:58:29 +01001271/*
1272 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001273 * depending on the initial msg->msg_state. The caller is responsible for
1274 * ensuring that the message does not wrap. The function can be preempted
1275 * everywhere when data are missing and recalled at the exact same location
1276 * with no information loss. The message may even be realigned between two
1277 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001278 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001279 * fields. Note that msg->sol will be initialized after completing the first
1280 * state, so that none of the msg pointers has to be initialized prior to the
1281 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001283void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001285 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001287 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001289 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001290 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001291 ptr = buf->p + msg->next;
1292 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001294 if (unlikely(ptr >= end))
1295 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 /*
1299 * First, states that are specific to the response only.
1300 * We check them first so that request and headers are
1301 * closer to each other (accessed more often).
1302 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001304 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001306 /* we have a start of message, but we have to check
1307 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001308 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001310 if (unlikely(ptr != buf->p)) {
1311 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001312 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001313 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001314 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 }
Willy Tarreau26927362012-05-18 23:22:52 +02001316 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001317 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 hdr_idx_init(idx);
1319 state = HTTP_MSG_RPVER;
1320 goto http_msg_rpver;
1321 }
1322
1323 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1324 goto http_msg_invalid;
1325
1326 if (unlikely(*ptr == '\n'))
1327 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1329 /* stop here */
1330
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001332 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 EXPECT_LF_HERE(ptr, http_msg_invalid);
1334 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1335 /* stop here */
1336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPVER_SP:
1340 case HTTP_MSG_RPCODE:
1341 case HTTP_MSG_RPCODE_SP:
1342 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001343 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001344 state, ptr, end,
1345 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 if (unlikely(!ptr))
1347 return;
1348
1349 /* we have a full response and we know that we have either a CR
1350 * or an LF at <ptr>.
1351 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1353
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001354 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 if (likely(*ptr == '\r'))
1356 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1357 goto http_msg_rpline_end;
1358
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001360 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 /* msg->sol must point to the first of CR or LF. */
1362 EXPECT_LF_HERE(ptr, http_msg_invalid);
1363 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1364 /* stop here */
1365
1366 /*
1367 * Second, states that are specific to the request only
1368 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001370 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 /* we have a start of message, but we have to check
1373 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001374 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001375 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001376 if (likely(ptr != buf->p)) {
1377 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001378 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001379 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001380 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 }
Willy Tarreau26927362012-05-18 23:22:52 +02001382 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001383 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 state = HTTP_MSG_RQMETH;
1385 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1389 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 if (unlikely(*ptr == '\n'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001397 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 EXPECT_LF_HERE(ptr, http_msg_invalid);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
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_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 case HTTP_MSG_RQMETH_SP:
1405 case HTTP_MSG_RQURI:
1406 case HTTP_MSG_RQURI_SP:
1407 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001408 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001409 state, ptr, end,
1410 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (unlikely(!ptr))
1412 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 /* we have a full request and we know that we have either a CR
1415 * or an LF at <ptr>.
1416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001418
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001419 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 if (likely(*ptr == '\r'))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001425 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 /* check for HTTP/0.9 request : no version information available.
1427 * msg->sol must point to the first of CR or LF.
1428 */
1429 if (unlikely(msg->sl.rq.v_l == 0))
1430 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 EXPECT_LF_HERE(ptr, http_msg_invalid);
1433 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001435
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 /*
1437 * Common states below
1438 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001441 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (likely(!HTTP_IS_CRLF(*ptr))) {
1443 goto http_msg_hdr_name;
1444 }
1445
1446 if (likely(*ptr == '\r'))
1447 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1448 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 /* assumes msg->sol points to the first char */
1453 if (likely(HTTP_IS_TOKEN(*ptr)))
1454 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001456 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001458
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001459 if (likely(msg->err_pos < -1) || *ptr == '\n')
1460 goto http_msg_invalid;
1461
1462 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001463 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001464
1465 /* and we still accept this non-token character */
1466 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001469 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001470 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 if (likely(HTTP_IS_SPHT(*ptr)))
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001475 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(!HTTP_IS_CRLF(*ptr))) {
1478 goto http_msg_hdr_val;
1479 }
1480
1481 if (likely(*ptr == '\r'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1483 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001486 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 EXPECT_LF_HERE(ptr, http_msg_invalid);
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 if (likely(HTTP_IS_SPHT(*ptr))) {
1493 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001494 for (; buf->p + msg->sov < ptr; msg->sov++)
1495 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 goto http_msg_hdr_l1_sp;
1497 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001498 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001499 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 goto http_msg_complete_header;
1501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001503 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001504 /* assumes msg->sol points to the first char, and msg->sov
1505 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 */
1507 if (likely(!HTTP_IS_CRLF(*ptr)))
1508 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001510 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 /* Note: we could also copy eol into ->eoh so that we have the
1512 * real header end in case it ends with lots of LWS, but is this
1513 * really needed ?
1514 */
1515 if (likely(*ptr == '\r'))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1517 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001520 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 EXPECT_LF_HERE(ptr, http_msg_invalid);
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1527 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001528 for (; buf->p + msg->eol < ptr; msg->eol++)
1529 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 goto http_msg_hdr_val;
1531 }
1532 http_msg_complete_header:
1533 /*
1534 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001535 * Assumes msg->sol points to the first char, msg->sov points
1536 * to the first character of the value and msg->eol to the
1537 * first CR or LF so we know how the line ends. We insert last
1538 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001540 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 idx, idx->tail) < 0))
1542 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001543
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 if (likely(!HTTP_IS_CRLF(*ptr))) {
1546 goto http_msg_hdr_name;
1547 }
1548
1549 if (likely(*ptr == '\r'))
1550 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1551 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 /* Assumes msg->sol points to the first of either CR or LF */
1556 EXPECT_LF_HERE(ptr, http_msg_invalid);
1557 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001558 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001559 msg->eoh = msg->sol;
1560 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001561 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001563
1564 case HTTP_MSG_ERROR:
1565 /* this may only happen if we call http_msg_analyser() twice with an error */
1566 break;
1567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568#ifdef DEBUG_FULL
1569 default:
1570 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1571 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001572#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 }
1574 http_msg_ood:
1575 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001576 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001577 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_invalid:
1581 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001583 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 return;
1585}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001586
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001587/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1588 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1589 * nothing is done and 1 is returned.
1590 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001591static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001592{
1593 int delta;
1594 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001595 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001596
1597 if (msg->sl.rq.v_l != 0)
1598 return 1;
1599
Willy Tarreau9b28e032012-10-12 23:49:43 +02001600 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601 delta = 0;
1602
1603 if (msg->sl.rq.u_l == 0) {
1604 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001605 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001607 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 }
1609 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001610 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001611 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001613 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001615 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001616 NULL, NULL);
1617 if (unlikely(!cur_end))
1618 return 0;
1619
1620 /* we have a full HTTP/1.0 request now and we know that
1621 * we have either a CR or an LF at <ptr>.
1622 */
1623 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1624 return 1;
1625}
1626
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001627/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001628 * and "keep-alive" values. If we already know that some headers may safely
1629 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001630 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1631 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001632 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001633 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1634 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1635 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001636 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001638void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001639{
Willy Tarreau5b154472009-12-21 20:11:07 +01001640 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001641 const char *hdr_val = "Connection";
1642 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001643
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001644 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001645 return;
1646
Willy Tarreau88d349d2010-01-25 12:15:43 +01001647 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1648 hdr_val = "Proxy-Connection";
1649 hdr_len = 16;
1650 }
1651
Willy Tarreau5b154472009-12-21 20:11:07 +01001652 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001653 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001654 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1656 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001657 if (to_del & 2)
1658 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 else
1660 txn->flags |= TX_CON_KAL_SET;
1661 }
1662 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1663 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001664 if (to_del & 1)
1665 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001666 else
1667 txn->flags |= TX_CON_CLO_SET;
1668 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001669 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1670 txn->flags |= TX_HDR_CONN_UPG;
1671 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001672 }
1673
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001674 txn->flags |= TX_HDR_CONN_PRS;
1675 return;
1676}
Willy Tarreau5b154472009-12-21 20:11:07 +01001677
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001678/* Apply desired changes on the Connection: header. Values may be removed and/or
1679 * added depending on the <wanted> flags, which are exclusively composed of
1680 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1681 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1682 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001683void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001684{
1685 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001686 const char *hdr_val = "Connection";
1687 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001688
1689 ctx.idx = 0;
1690
Willy Tarreau88d349d2010-01-25 12:15:43 +01001691
1692 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1693 hdr_val = "Proxy-Connection";
1694 hdr_len = 16;
1695 }
1696
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001697 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001698 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1700 if (wanted & TX_CON_KAL_SET)
1701 txn->flags |= TX_CON_KAL_SET;
1702 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001703 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001704 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001705 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1706 if (wanted & TX_CON_CLO_SET)
1707 txn->flags |= TX_CON_CLO_SET;
1708 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001709 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001710 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001711 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712
1713 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1714 return;
1715
1716 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1717 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001718 hdr_val = "Connection: close";
1719 hdr_len = 17;
1720 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1721 hdr_val = "Proxy-Connection: close";
1722 hdr_len = 23;
1723 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001724 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001725 }
1726
1727 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1728 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001729 hdr_val = "Connection: keep-alive";
1730 hdr_len = 22;
1731 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1732 hdr_val = "Proxy-Connection: keep-alive";
1733 hdr_len = 28;
1734 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001735 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001736 }
1737 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001738}
1739
Willy Tarreaua458b672012-03-05 11:17:50 +01001740/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001741 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001742 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001743 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001744 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001745 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001746static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001747{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001748 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001749 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001750 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 const char *end = buf->data + buf->size;
1752 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001753 unsigned int chunk = 0;
1754
1755 /* The chunk size is in the following form, though we are only
1756 * interested in the size and CRLF :
1757 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1758 */
1759 while (1) {
1760 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001761 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001762 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001763 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 if (c < 0) /* not a hex digit anymore */
1765 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001768 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001769 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001770 chunk = (chunk << 4) + c;
1771 }
1772
Willy Tarreaud98cf932009-12-27 22:54:55 +01001773 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001774 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001775 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001776
1777 while (http_is_spht[(unsigned char)*ptr]) {
1778 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001779 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001780 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001781 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 }
1783
Willy Tarreaud98cf932009-12-27 22:54:55 +01001784 /* Up to there, we know that at least one byte is present at *ptr. Check
1785 * for the end of chunk size.
1786 */
1787 while (1) {
1788 if (likely(HTTP_IS_CRLF(*ptr))) {
1789 /* we now have a CR or an LF at ptr */
1790 if (likely(*ptr == '\r')) {
1791 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001793 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001794 return 0;
1795 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001796
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001798 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001800 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 /* done */
1802 break;
1803 }
1804 else if (*ptr == ';') {
1805 /* chunk extension, ends at next CRLF */
1806 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001807 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001808 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001809 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810
1811 while (!HTTP_IS_CRLF(*ptr)) {
1812 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001814 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 return 0;
1816 }
1817 /* we have a CRLF now, loop above */
1818 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001819 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001820 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001821 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 }
1823
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001825 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001827 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001828 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001830 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001831 msg->next = buffer_count(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 Tarreaucdbdd522012-10-12 22:51:15 +02001837 msg->err_pos = buffer_count(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 Tarreau24e6d972012-10-26 00:49:52 +02001856static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001858 const struct buffer *buf = msg->chn->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 Tarreaucdbdd522012-10-12 22:51:15 +02001863 const char *ptr = b_ptr(buf, msg->next);
1864 const char *stop = bi_end(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 Tarreaucdbdd522012-10-12 22:51:15 +02001881 msg->err_pos = buffer_count(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 Tarreaucdbdd522012-10-12 22:51:15 +02001888 if (ptr >= buf->data + buf->size)
1889 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 }
1891
1892 /* after LF; point to beginning of next line */
1893 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001894 if (p2 >= buf->data + buf->size)
1895 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001900
1901 /* schedule this line for forwarding */
1902 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001903 if (msg->sov >= buf->size)
1904 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001905
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001906 if (p1 == b_ptr(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 Tarreaucdbdd522012-10-12 22:51:15 +02001911 msg->next = buffer_count(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 Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 }
1918}
1919
Willy Tarreau54d23df2012-10-25 19:04:45 +02001920/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * 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 Tarreau24e6d972012-10-26 00:49:52 +02001930static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001932 const struct buffer *buf = msg->chn->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 Tarreaucdbdd522012-10-12 22:51:15 +02001941 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001942 if (*ptr == '\r') {
1943 bytes++;
1944 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001945 if (ptr >= buf->data + buf->size)
1946 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 }
1948
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 if (bytes > 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 Tarreaucdbdd522012-10-12 22:51:15 +02001953 msg->err_pos = buffer_count(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 Tarreaucdbdd522012-10-12 22:51:15 +02001958 if (ptr >= buf->data + buf->size)
1959 ptr = 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
William Lallemand82fe75c2012-10-23 10:25:10 +02001967
1968/*
1969 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001970 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001971int select_compression_request_header(struct session *s, struct buffer *req)
1972{
1973 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001974 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001975 struct hdr_ctx ctx;
1976 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001977 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001978
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001979 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1980 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001981 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1982 */
1983 ctx.idx = 0;
1984 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1985 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001986 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1987 (ctx.vlen < 31 ||
1988 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1989 ctx.line[ctx.val + 30] < '6' ||
1990 (ctx.line[ctx.val + 30] == '6' &&
1991 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1992 s->comp_algo = NULL;
1993 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001994 }
1995
William Lallemand82fe75c2012-10-23 10:25:10 +02001996 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001997 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02001998 ctx.idx = 0;
1999 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002000 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002001 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2002 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002003
2004 /* remove all occurrences of the header when "compression offload" is set */
2005
2006 if ((s->be->comp && s->be->comp->offload) ||
2007 (s->fe->comp && s->fe->comp->offload)) {
2008 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2009 ctx.idx = 0;
2010 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2011 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2012 }
2013 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002014 return 1;
2015 }
2016 }
2017 }
2018 }
2019
2020 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002021 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2022 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002023 if (comp_algo->add_data == identity_add_data) {
2024 s->comp_algo = comp_algo;
2025 return 1;
2026 }
2027 }
2028 }
2029
2030 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002031 return 0;
2032}
2033
2034/*
2035 * Selects a comression algorithm depending of the server response.
2036 */
2037int select_compression_response_header(struct session *s, struct buffer *res)
2038{
2039 struct http_txn *txn = &s->txn;
2040 struct http_msg *msg = &txn->rsp;
2041 struct hdr_ctx ctx;
2042 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002043
2044 /* no common compression algorithm was found in request header */
2045 if (s->comp_algo == NULL)
2046 goto fail;
2047
2048 /* HTTP < 1.1 should not be compressed */
2049 if (!(msg->flags & HTTP_MSGF_VER_11))
2050 goto fail;
2051
William Lallemandd3002612012-11-26 14:34:47 +01002052 /* 200 only */
2053 if (txn->status != 200)
2054 goto fail;
2055
William Lallemand82fe75c2012-10-23 10:25:10 +02002056 /* Content-Length is null */
2057 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2058 goto fail;
2059
2060 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002061 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002062 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2063 goto fail;
2064
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002065 /* no compression when Cache-Control: no-transform is present in the message */
2066 ctx.idx = 0;
2067 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2068 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2069 goto fail;
2070 }
2071
William Lallemand82fe75c2012-10-23 10:25:10 +02002072 comp_type = NULL;
2073
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002074 /* we don't want to compress multipart content-types, nor content-types that are
2075 * not listed in the "compression type" directive if any. If no content-type was
2076 * found but configuration requires one, we don't compress either. Backend has
2077 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002078 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002079 ctx.idx = 0;
2080 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2081 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2082 goto fail;
2083
2084 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2085 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002086 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002087 if (ctx.vlen >= comp_type->name_len &&
2088 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002089 /* this Content-Type should be compressed */
2090 break;
2091 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002092 /* this Content-Type should not be compressed */
2093 if (comp_type == NULL)
2094 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002095 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002096 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002097 else { /* no content-type header */
2098 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2099 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002100 }
2101
William Lallemandd85f9172012-11-09 17:05:39 +01002102 /* limit compression rate */
2103 if (global.comp_rate_lim > 0)
2104 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2105 goto fail;
2106
William Lallemand072a2bf2012-11-20 17:01:01 +01002107 /* limit cpu usage */
2108 if (idle_pct < compress_min_idle)
2109 goto fail;
2110
William Lallemand4c49fae2012-11-07 15:00:23 +01002111 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002112 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002113 goto fail;
2114
William Lallemandec3e3892012-11-12 17:02:18 +01002115 s->flags |= SN_COMP_READY;
2116
William Lallemand82fe75c2012-10-23 10:25:10 +02002117 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002118 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002119 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2120 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2121
2122 /* add Transfer-Encoding header */
2123 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2124 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2125
2126 /*
2127 * Add Content-Encoding header when it's not identity encoding.
2128 * RFC 2616 : Identity encoding: This content-coding is used only in the
2129 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2130 * header.
2131 */
2132 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002133 trash.len = 18;
2134 memcpy(trash.str, "Content-Encoding: ", trash.len);
2135 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2136 trash.len += s->comp_algo->name_len;
2137 trash.str[trash.len] = '\0';
2138 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002139 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002140 return 1;
2141
2142fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002143 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002144 return 0;
2145}
2146
2147
Willy Tarreaud787e662009-07-07 10:14:51 +02002148/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2149 * processing can continue on next analysers, or zero if it either needs more
2150 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2151 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2152 * when it has nothing left to do, and may remove any analyser when it wants to
2153 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002154 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002155int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002156{
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 /*
2158 * We will parse the partial (or complete) lines.
2159 * We will check the request syntax, and also join multi-line
2160 * headers. An index of all the lines will be elaborated while
2161 * parsing.
2162 *
2163 * For the parsing, we use a 28 states FSM.
2164 *
2165 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002166 * req->buf->p = beginning of request
2167 * req->buf->p + msg->eoh = end of processed headers / start of current one
2168 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002169 * msg->eol = end of current header or line (LF or CRLF)
2170 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002171 *
2172 * At end of parsing, we may perform a capture of the error (if any), and
2173 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2174 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2175 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002176 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002177
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002179 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 struct http_txn *txn = &s->txn;
2181 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002182 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002183
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002184 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 +01002185 now_ms, __FUNCTION__,
2186 s,
2187 req,
2188 req->rex, req->wex,
2189 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002190 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002191 req->analysers);
2192
Willy Tarreau52a0c602009-08-16 22:45:38 +02002193 /* we're speaking HTTP here, so let's speak HTTP to the client */
2194 s->srv_error = http_return_srv_error;
2195
Willy Tarreau83e3af02009-12-28 17:39:57 +01002196 /* There's a protected area at the end of the buffer for rewriting
2197 * purposes. We don't want to start to parse the request if the
2198 * protected area is affected, because we may have to move processed
2199 * data later, which is much more complicated.
2200 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002201 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002202 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002203 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002204 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2205 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2206 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002207 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002208 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002209 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002210 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002211 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002212 return 0;
2213 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002214 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2215 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2216 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002217 }
2218
Willy Tarreau065e8332010-01-08 00:30:20 +01002219 /* Note that we have the same problem with the response ; we
2220 * may want to send a redirect, error or anything which requires
2221 * some spare space. So we'll ensure that we have at least
2222 * maxrewrite bytes available in the response buffer before
2223 * processing that one. This will only affect pipelined
2224 * keep-alive requests.
2225 */
2226 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002227 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002228 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2229 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2230 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002231 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002232 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002233 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002234 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002235 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002236 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002237 return 0;
2238 }
2239 }
2240
Willy Tarreau9b28e032012-10-12 23:49:43 +02002241 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002242 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002243 }
2244
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 /* 1: we might have to print this header in debug mode */
2246 if (unlikely((global.mode & MODE_DEBUG) &&
2247 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002248 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002250
Willy Tarreau9b28e032012-10-12 23:49:43 +02002251 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002252 /* this is a bit complex : in case of error on the request line,
2253 * we know that rq.l is still zero, so we display only the part
2254 * up to the end of the line (truncated by debug_hdr).
2255 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002256 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002258
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 sol += hdr_idx_first_pos(&txn->hdr_idx);
2260 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 while (cur_idx) {
2263 eol = sol + txn->hdr_idx.v[cur_idx].len;
2264 debug_hdr("clihdr", s, sol, eol);
2265 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2266 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002267 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 }
2269
Willy Tarreau58f10d72006-12-04 02:26:12 +01002270
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 /*
2272 * Now we quickly check if we have found a full valid request.
2273 * If not so, we check the FD and buffer states before leaving.
2274 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002275 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002276 * requests are checked first. When waiting for a second request
2277 * on a keep-alive session, if we encounter and error, close, t/o,
2278 * we note the error in the session flags but don't set any state.
2279 * Since the error will be noted there, it will not be counted by
2280 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002281 * Last, we may increase some tracked counters' http request errors on
2282 * the cases that are deliberately the client's fault. For instance,
2283 * a timeout or connection reset is not counted as an error. However
2284 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286
Willy Tarreau655dce92009-11-08 13:10:58 +01002287 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002288 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002291 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002292 session_inc_http_req_ctr(s);
2293 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002294 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002297
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 /* 1: Since we are in header mode, if there's no space
2299 * left for headers, we won't be able to free more
2300 * later, so the session will never terminate. We
2301 * must terminate it now.
2302 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002303 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 /* FIXME: check if URI is set and return Status
2305 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002306 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002307 session_inc_http_req_ctr(s);
2308 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002309 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002310 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002311 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 goto return_bad_req;
2313 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002316 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002317 if (!(s->flags & SN_ERR_MASK))
2318 s->flags |= SN_ERR_CLICL;
2319
Willy Tarreaufcffa692010-01-10 14:21:19 +01002320 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002321 goto failed_keep_alive;
2322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002324 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002325 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002326 session_inc_http_err_ctr(s);
2327 }
2328
Willy Tarreaudc979f22012-12-04 10:39:01 +01002329 txn->status = 400;
2330 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 msg->msg_state = HTTP_MSG_ERROR;
2332 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002333
Willy Tarreauda7ff642010-06-23 11:44:09 +02002334 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002335 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002336 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002337 if (s->listener->counters)
2338 s->listener->counters->failed_req++;
2339
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 if (!(s->flags & SN_FINST_MASK))
2341 s->flags |= SN_FINST_R;
2342 return 0;
2343 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002344
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002346 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002347 if (!(s->flags & SN_ERR_MASK))
2348 s->flags |= SN_ERR_CLITO;
2349
Willy Tarreaufcffa692010-01-10 14:21:19 +01002350 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002351 goto failed_keep_alive;
2352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002354 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002355 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002356 session_inc_http_err_ctr(s);
2357 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002359 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 msg->msg_state = HTTP_MSG_ERROR;
2361 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002362
Willy Tarreauda7ff642010-06-23 11:44:09 +02002363 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002364 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002365 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002366 if (s->listener->counters)
2367 s->listener->counters->failed_req++;
2368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 if (!(s->flags & SN_FINST_MASK))
2370 s->flags |= SN_FINST_R;
2371 return 0;
2372 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002375 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002376 if (!(s->flags & SN_ERR_MASK))
2377 s->flags |= SN_ERR_CLICL;
2378
Willy Tarreaufcffa692010-01-10 14:21:19 +01002379 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002380 goto failed_keep_alive;
2381
Willy Tarreau4076a152009-04-02 15:18:36 +02002382 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002383 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002385 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 msg->msg_state = HTTP_MSG_ERROR;
2387 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002388
Willy Tarreauda7ff642010-06-23 11:44:09 +02002389 session_inc_http_err_ctr(s);
2390 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002391 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002392 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002393 if (s->listener->counters)
2394 s->listener->counters->failed_req++;
2395
Willy Tarreau59234e92008-11-30 23:51:27 +01002396 if (!(s->flags & SN_FINST_MASK))
2397 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002398 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002399 }
2400
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002401 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002402 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2403 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002404#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002405 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002406 /* We need more data, we have to re-enable quick-ack in case we
2407 * previously disabled it, otherwise we might cause the client
2408 * to delay next data.
2409 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002410 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002411 }
2412#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002413
Willy Tarreaufcffa692010-01-10 14:21:19 +01002414 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2415 /* If the client starts to talk, let's fall back to
2416 * request timeout processing.
2417 */
2418 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002419 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002420 }
2421
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002423 if (!tick_isset(req->analyse_exp)) {
2424 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2425 (txn->flags & TX_WAIT_NEXT_RQ) &&
2426 tick_isset(s->be->timeout.httpka))
2427 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2428 else
2429 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 /* we're not ready yet */
2433 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002434
2435 failed_keep_alive:
2436 /* Here we process low-level errors for keep-alive requests. In
2437 * short, if the request is not the first one and it experiences
2438 * a timeout, read error or shutdown, we just silently close so
2439 * that the client can try again.
2440 */
2441 txn->status = 0;
2442 msg->msg_state = HTTP_MSG_RQBEFORE;
2443 req->analysers = 0;
2444 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002445 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002446 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002447 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002449
Willy Tarreaud787e662009-07-07 10:14:51 +02002450 /* OK now we have a complete HTTP request with indexed headers. Let's
2451 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002452 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002453 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002454 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002455 * byte after the last LF. msg->sov points to the first byte of data.
2456 * msg->eol cannot be trusted because it may have been left uninitialized
2457 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002458 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002459
Willy Tarreauda7ff642010-06-23 11:44:09 +02002460 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002461 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2462
Willy Tarreaub16a5742010-01-10 14:46:16 +01002463 if (txn->flags & TX_WAIT_NEXT_RQ) {
2464 /* kill the pending keep-alive timeout */
2465 txn->flags &= ~TX_WAIT_NEXT_RQ;
2466 req->analyse_exp = TICK_ETERNITY;
2467 }
2468
2469
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 /* Maybe we found in invalid header name while we were configured not
2471 * to block on that, so we have to capture it now.
2472 */
2473 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002474 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002475
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 /*
2477 * 1: identify the method
2478 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002479 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002480
2481 /* we can make use of server redirect on GET and HEAD */
2482 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2483 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002484
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 /*
2486 * 2: check if the URI matches the monitor_uri.
2487 * We have to do this for every request which gets in, because
2488 * the monitor-uri is defined by the frontend.
2489 */
2490 if (unlikely((s->fe->monitor_uri_len != 0) &&
2491 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 s->fe->monitor_uri,
2494 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002495 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002497 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002499
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002501 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002502
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002504 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002505 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 ret = acl_pass(ret);
2508 if (cond->pol == ACL_COND_UNLESS)
2509 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 if (ret) {
2512 /* we fail this request, let's return 503 service unavail */
2513 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002514 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002516 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002517 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* nothing to fail, let's reply normaly */
2520 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002521 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 goto return_prx_cond;
2523 }
2524
2525 /*
2526 * 3: Maybe we have to copy the original REQURI for the logs ?
2527 * Note: we cannot log anymore if the request has been
2528 * classified as invalid.
2529 */
2530 if (unlikely(s->logs.logwait & LW_REQ)) {
2531 /* we have a complete HTTP request that we must log */
2532 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2533 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 if (urilen >= REQURI_LEN)
2536 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002537 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002539
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002540 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 s->do_log(s);
2542 } else {
2543 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002544 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 }
Willy Tarreau06619262006-12-17 08:37:22 +01002546
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002547 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2548 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002551 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002552 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002553
Willy Tarreau5b154472009-12-21 20:11:07 +01002554 /* ... and check if the request is HTTP/1.1 or above */
2555 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2557 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2558 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002559 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002560
2561 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002562 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002563
Willy Tarreau88d349d2010-01-25 12:15:43 +01002564 /* if the frontend has "option http-use-proxy-header", we'll check if
2565 * we have what looks like a proxied connection instead of a connection,
2566 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2567 * Note that this is *not* RFC-compliant, however browsers and proxies
2568 * happen to do that despite being non-standard :-(
2569 * We consider that a request not beginning with either '/' or '*' is
2570 * a proxied connection, which covers both "scheme://location" and
2571 * CONNECT ip:port.
2572 */
2573 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002574 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002575 txn->flags |= TX_USE_PX_CONN;
2576
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002577 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002578 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002579
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002581 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002582 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002584
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002585 /* 6: determine the transfer-length.
2586 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2587 * the presence of a message-body in a REQUEST and its transfer length
2588 * must be determined that way (in order of precedence) :
2589 * 1. The presence of a message-body in a request is signaled by the
2590 * inclusion of a Content-Length or Transfer-Encoding header field
2591 * in the request's header fields. When a request message contains
2592 * both a message-body of non-zero length and a method that does
2593 * not define any semantics for that request message-body, then an
2594 * origin server SHOULD either ignore the message-body or respond
2595 * with an appropriate error message (e.g., 413). A proxy or
2596 * gateway, when presented the same request, SHOULD either forward
2597 * the request inbound with the message- body or ignore the
2598 * message-body when determining a response.
2599 *
2600 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2601 * and the "chunked" transfer-coding (Section 6.2) is used, the
2602 * transfer-length is defined by the use of this transfer-coding.
2603 * If a Transfer-Encoding header field is present and the "chunked"
2604 * transfer-coding is not present, the transfer-length is defined
2605 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002606 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002607 * 3. If a Content-Length header field is present, its decimal value in
2608 * OCTETs represents both the entity-length and the transfer-length.
2609 * If a message is received with both a Transfer-Encoding header
2610 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002611 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002612 * 4. By the server closing the connection. (Closing the connection
2613 * cannot be used to indicate the end of a request body, since that
2614 * would leave no possibility for the server to send back a response.)
2615 *
2616 * Whenever a transfer-coding is applied to a message-body, the set of
2617 * transfer-codings MUST include "chunked", unless the message indicates
2618 * it is terminated by closing the connection. When the "chunked"
2619 * transfer-coding is used, it MUST be the last transfer-coding applied
2620 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002621 */
2622
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002623 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002625 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002626 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002627 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002628 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002629 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2630 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002631 /* bad transfer-encoding (chunked followed by something else) */
2632 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002633 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002634 break;
2635 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002636 }
2637
Willy Tarreau32b47f42009-10-18 20:55:02 +02002638 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002639 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 signed long long cl;
2642
Willy Tarreauad14f752011-09-02 20:33:27 +02002643 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002644 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002645 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002646 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002647
Willy Tarreauad14f752011-09-02 20:33:27 +02002648 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002649 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002650 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002651 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002652
Willy Tarreauad14f752011-09-02 20:33:27 +02002653 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002654 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002656 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002658 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002659 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002660 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002661 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002662
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002663 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002664 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002665 }
2666
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002667 /* bodyless requests have a known length */
2668 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002669 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002670
Willy Tarreaud787e662009-07-07 10:14:51 +02002671 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002672 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002673 req->analyse_exp = TICK_ETERNITY;
2674 return 1;
2675
2676 return_bad_req:
2677 /* We centralize bad requests processing here */
2678 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2679 /* we detected a parsing error. We want to archive this request
2680 * in the dedicated proxy area for later troubleshooting.
2681 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002682 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002683 }
2684
2685 txn->req.msg_state = HTTP_MSG_ERROR;
2686 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002687 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002688
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002689 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002690 if (s->listener->counters)
2691 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002692
2693 return_prx_cond:
2694 if (!(s->flags & SN_ERR_MASK))
2695 s->flags |= SN_ERR_PRXCOND;
2696 if (!(s->flags & SN_FINST_MASK))
2697 s->flags |= SN_FINST_R;
2698
2699 req->analysers = 0;
2700 req->analyse_exp = TICK_ETERNITY;
2701 return 0;
2702}
2703
Cyril Bonté70be45d2010-10-12 00:14:35 +02002704/* We reached the stats page through a POST request.
2705 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002706 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002707 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002708int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002709{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002710 struct proxy *px = NULL;
2711 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002713 char key[LINESIZE];
2714 int action = ST_ADM_ACTION_NONE;
2715 int reprocess = 0;
2716
2717 int total_servers = 0;
2718 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719
2720 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002721 char *st_cur_param = NULL;
2722 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002723
Willy Tarreau9b28e032012-10-12 23:49:43 +02002724 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002725 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002726
2727 cur_param = next_param = end_params;
2728
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002730 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002731 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732 return 1;
2733 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002734 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002735 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002736 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002737 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002738 }
2739
2740 *end_params = '\0';
2741
Willy Tarreau295a8372011-03-10 11:25:07 +01002742 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002743
2744 /*
2745 * Parse the parameters in reverse order to only store the last value.
2746 * From the html form, the backend and the action are at the end.
2747 */
2748 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002749 char *value;
2750 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002751
2752 cur_param--;
2753 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002754 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002755 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002756 poffset = (cur_param != first_param ? 1 : 0);
2757 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2758 if ((plen > 0) && (plen <= sizeof(key))) {
2759 strncpy(key, cur_param + poffset, plen);
2760 key[plen - 1] = '\0';
2761 } else {
2762 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2763 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002764 }
2765
2766 /* Parse the value */
2767 value = key;
2768 while (*value != '\0' && *value != '=') {
2769 value++;
2770 }
2771 if (*value == '=') {
2772 /* Ok, a value is found, we can mark the end of the key */
2773 *value++ = '\0';
2774 }
2775
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002776 if (!url_decode(key) || !url_decode(value))
2777 break;
2778
Cyril Bonté70be45d2010-10-12 00:14:35 +02002779 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002780 if (!px && (strcmp(key, "b") == 0)) {
2781 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2782 /* the backend name is unknown or ambiguous (duplicate names) */
2783 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2784 goto out;
2785 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002786 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002787 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002788 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002789 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002790 }
2791 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002792 action = ST_ADM_ACTION_ENABLE;
2793 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002794 else if (strcmp(value, "stop") == 0) {
2795 action = ST_ADM_ACTION_STOP;
2796 }
2797 else if (strcmp(value, "start") == 0) {
2798 action = ST_ADM_ACTION_START;
2799 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002800 else if (strcmp(value, "shutdown") == 0) {
2801 action = ST_ADM_ACTION_SHUTDOWN;
2802 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002803 else {
2804 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2805 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002806 }
2807 }
2808 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002809 if (!(px && action)) {
2810 /*
2811 * Indicates that we'll need to reprocess the parameters
2812 * as soon as backend and action are known
2813 */
2814 if (!reprocess) {
2815 st_cur_param = cur_param;
2816 st_next_param = next_param;
2817 }
2818 reprocess = 1;
2819 }
2820 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002821 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002822 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002823 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002824 /* Not already in maintenance, we can change the server state */
2825 sv->state |= SRV_MAINTAIN;
2826 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002827 altered_servers++;
2828 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002829 }
2830 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002831 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002832 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002833 /* Already in maintenance, we can change the server state */
2834 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002835 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002836 altered_servers++;
2837 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002838 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002839 break;
2840 case ST_ADM_ACTION_STOP:
2841 case ST_ADM_ACTION_START:
2842 if (action == ST_ADM_ACTION_START)
2843 sv->uweight = sv->iweight;
2844 else
2845 sv->uweight = 0;
2846
2847 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2848 /* we must take care of not pushing the server to full throttle during slow starts */
2849 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2850 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2851 else
2852 sv->eweight = BE_WEIGHT_SCALE;
2853 sv->eweight *= sv->uweight;
2854 } else {
2855 sv->eweight = sv->uweight;
2856 }
2857
2858 /* static LB algorithms are a bit harder to update */
2859 if (px->lbprm.update_server_eweight)
2860 px->lbprm.update_server_eweight(sv);
2861 else if (sv->eweight) {
2862 if (px->lbprm.set_server_status_up)
2863 px->lbprm.set_server_status_up(sv);
2864 }
2865 else {
2866 if (px->lbprm.set_server_status_down)
2867 px->lbprm.set_server_status_down(sv);
2868 }
2869 altered_servers++;
2870 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002871 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002872 case ST_ADM_ACTION_SHUTDOWN:
2873 if (px->state != PR_STSTOPPED) {
2874 struct session *sess, *sess_bck;
2875
2876 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2877 if (sess->srv_conn == sv)
2878 session_shutdown(sess, SN_ERR_KILLED);
2879
2880 altered_servers++;
2881 total_servers++;
2882 }
2883 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002884 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002885 } else {
2886 /* the server name is unknown or ambiguous (duplicate names) */
2887 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002888 }
2889 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002890 if (reprocess && px && action) {
2891 /* Now, we know the backend and the action chosen by the user.
2892 * We can safely restart from the first server parameter
2893 * to reprocess them
2894 */
2895 cur_param = st_cur_param;
2896 next_param = st_next_param;
2897 reprocess = 0;
2898 goto reprocess_servers;
2899 }
2900
Cyril Bonté70be45d2010-10-12 00:14:35 +02002901 next_param = cur_param;
2902 }
2903 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002904
2905 if (total_servers == 0) {
2906 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2907 }
2908 else if (altered_servers == 0) {
2909 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2910 }
2911 else if (altered_servers == total_servers) {
2912 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2913 }
2914 else {
2915 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2916 }
2917 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002918 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002919}
2920
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002921/* This function checks whether we need to enable a POST analyser to parse a
2922 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002923 * if it needs to come back again, otherwise non-zero if it finishes. In the
2924 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002925 */
2926int http_handle_stats(struct session *s, struct channel *req)
2927{
2928 struct stats_admin_rule *stats_admin_rule;
2929 struct stream_interface *si = s->rep->prod;
2930 struct http_txn *txn = &s->txn;
2931 struct http_msg *msg = &txn->req;
2932 struct uri_auth *uri = s->be->uri_auth;
2933
2934 /* now check whether we have some admin rules for this request */
2935 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2936 int ret = 1;
2937
2938 if (stats_admin_rule->cond) {
2939 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2940 ret = acl_pass(ret);
2941 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2942 ret = !ret;
2943 }
2944
2945 if (ret) {
2946 /* no rule, or the rule matches */
2947 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2948 break;
2949 }
2950 }
2951
2952 /* Was the status page requested with a POST ? */
2953 if (unlikely(txn->meth == HTTP_METH_POST)) {
2954 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2955 if (msg->msg_state < HTTP_MSG_100_SENT) {
2956 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2957 * send an HTTP/1.1 100 Continue intermediate response.
2958 */
2959 if (msg->flags & HTTP_MSGF_VER_11) {
2960 struct hdr_ctx ctx;
2961 ctx.idx = 0;
2962 /* Expect is allowed in 1.1, look for it */
2963 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2964 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2965 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2966 }
2967 }
2968 msg->msg_state = HTTP_MSG_100_SENT;
2969 s->logs.tv_request = now; /* update the request timer to reflect full request */
2970 }
2971 if (!http_process_req_stat_post(si, txn, req))
2972 return 0; /* we need more data */
2973 }
2974 else
2975 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
2976
2977 /* We don't want to land on the posted stats page because a refresh will
2978 * repost the data. We don't want this to happen on accident so we redirect
2979 * the browse to the stats page with a GET.
2980 */
2981 chunk_printf(&trash,
2982 "HTTP/1.0 303 See Other\r\n"
2983 "Cache-Control: no-cache\r\n"
2984 "Content-Type: text/plain\r\n"
2985 "Connection: close\r\n"
2986 "Location: %s;st=%s\r\n"
2987 "\r\n",
2988 uri->uri_prefix,
2989 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
2990 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
2991 stat_status_codes[si->applet.ctx.stats.st_code]) ?
2992 stat_status_codes[si->applet.ctx.stats.st_code] :
2993 stat_status_codes[STAT_STATUS_UNKN]);
2994
2995 s->txn.status = 303;
2996 s->logs.tv_request = now;
2997 stream_int_retnclose(req->prod, &trash);
2998 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
2999
3000 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3001 s->fe->fe_counters.intercepted_req++;
3002
3003 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3004 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3005 if (!(s->flags & SN_FINST_MASK))
3006 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003007 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003008 return 1;
3009 }
3010
3011 /* OK, let's go on now */
3012
3013 chunk_printf(&trash,
3014 "HTTP/1.0 200 OK\r\n"
3015 "Cache-Control: no-cache\r\n"
3016 "Connection: close\r\n"
3017 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003018 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003019
3020 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3021 chunk_appendf(&trash, "Refresh: %d\r\n",
3022 uri->refresh);
3023
3024 chunk_appendf(&trash, "\r\n");
3025
3026 s->txn.status = 200;
3027 s->logs.tv_request = now;
3028
3029 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3030 s->fe->fe_counters.intercepted_req++;
3031
3032 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3033 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3034 if (!(s->flags & SN_FINST_MASK))
3035 s->flags |= SN_FINST_R;
3036
3037 if (s->txn.meth == HTTP_METH_HEAD) {
3038 /* that's all we return in case of HEAD request, so let's immediately close. */
3039 stream_int_retnclose(req->prod, &trash);
3040 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003041 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003042 return 1;
3043 }
3044
3045 /* OK, push the response and hand over to the stats I/O handler */
3046 bi_putchk(s->rep, &trash);
3047
3048 s->task->nice = -32; /* small boost for HTTP statistics */
3049 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3050 s->target = s->rep->prod->conn->target; // for logging only
3051 s->rep->prod->conn->xprt_ctx = s;
3052 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3053 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003054 return 1;
3055}
3056
Willy Tarreau20b0de52012-12-24 15:45:22 +01003057/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003058 * transaction <txn>. Returns the first rule that prevents further processing
3059 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3060 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3061 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003062 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003063static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003064http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003065{
Willy Tarreauff011f22011-01-06 17:51:27 +01003066 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003067 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003068
Willy Tarreauff011f22011-01-06 17:51:27 +01003069 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003070 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003071 continue;
3072
Willy Tarreau96257ec2012-12-27 10:46:37 +01003073 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003074 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003075 int ret;
3076
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003077 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078 ret = acl_pass(ret);
3079
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003082
3083 if (!ret) /* condition not matched */
3084 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003085 }
3086
Willy Tarreau20b0de52012-12-24 15:45:22 +01003087
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088 switch (rule->action) {
3089 case HTTP_REQ_ACT_ALLOW:
3090 return NULL; /* "allow" rules are OK */
3091
3092 case HTTP_REQ_ACT_DENY:
3093 txn->flags |= TX_CLDENY;
3094 return rule;
3095
Willy Tarreauccbcc372012-12-27 12:37:57 +01003096 case HTTP_REQ_ACT_TARPIT:
3097 txn->flags |= TX_CLTARPIT;
3098 return rule;
3099
Willy Tarreau96257ec2012-12-27 10:46:37 +01003100 case HTTP_REQ_ACT_AUTH:
3101 return rule;
3102
Willy Tarreau81499eb2012-12-27 12:19:02 +01003103 case HTTP_REQ_ACT_REDIR:
3104 return rule;
3105
Willy Tarreau96257ec2012-12-27 10:46:37 +01003106 case HTTP_REQ_ACT_SET_HDR:
3107 ctx.idx = 0;
3108 /* remove all occurrences of the header */
3109 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3110 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3111 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003112 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003113 /* now fall through to header addition */
3114
3115 case HTTP_REQ_ACT_ADD_HDR:
3116 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3117 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3118 trash.len = rule->arg.hdr_add.name_len;
3119 trash.str[trash.len++] = ':';
3120 trash.str[trash.len++] = ' ';
3121 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3122 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3123 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003124 }
3125 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003126
3127 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003128 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003129}
3130
Willy Tarreau71241ab2012-12-27 11:30:54 +01003131
3132/* Perform an HTTP redirect based on the information in <rule>. The function
3133 * returns non-zero on success, or zero in case of a, irrecoverable error such
3134 * as too large a request to build a valid response.
3135 */
3136static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3137{
3138 struct http_msg *msg = &txn->req;
3139 const char *msg_fmt;
3140
3141 /* build redirect message */
3142 switch(rule->code) {
3143 case 303:
3144 msg_fmt = HTTP_303;
3145 break;
3146 case 301:
3147 msg_fmt = HTTP_301;
3148 break;
3149 case 302:
3150 default:
3151 msg_fmt = HTTP_302;
3152 break;
3153 }
3154
3155 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3156 return 0;
3157
3158 switch(rule->type) {
3159 case REDIRECT_TYPE_SCHEME: {
3160 const char *path;
3161 const char *host;
3162 struct hdr_ctx ctx;
3163 int pathlen;
3164 int hostlen;
3165
3166 host = "";
3167 hostlen = 0;
3168 ctx.idx = 0;
3169 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3170 host = ctx.line + ctx.val;
3171 hostlen = ctx.vlen;
3172 }
3173
3174 path = http_get_path(txn);
3175 /* build message using path */
3176 if (path) {
3177 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3178 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3179 int qs = 0;
3180 while (qs < pathlen) {
3181 if (path[qs] == '?') {
3182 pathlen = qs;
3183 break;
3184 }
3185 qs++;
3186 }
3187 }
3188 } else {
3189 path = "/";
3190 pathlen = 1;
3191 }
3192
3193 /* check if we can add scheme + "://" + host + path */
3194 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3195 return 0;
3196
3197 /* add scheme */
3198 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3199 trash.len += rule->rdr_len;
3200
3201 /* add "://" */
3202 memcpy(trash.str + trash.len, "://", 3);
3203 trash.len += 3;
3204
3205 /* add host */
3206 memcpy(trash.str + trash.len, host, hostlen);
3207 trash.len += hostlen;
3208
3209 /* add path */
3210 memcpy(trash.str + trash.len, path, pathlen);
3211 trash.len += pathlen;
3212
3213 /* append a slash at the end of the location is needed and missing */
3214 if (trash.len && trash.str[trash.len - 1] != '/' &&
3215 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3216 if (trash.len > trash.size - 5)
3217 return 0;
3218 trash.str[trash.len] = '/';
3219 trash.len++;
3220 }
3221
3222 break;
3223 }
3224 case REDIRECT_TYPE_PREFIX: {
3225 const char *path;
3226 int pathlen;
3227
3228 path = http_get_path(txn);
3229 /* build message using path */
3230 if (path) {
3231 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3232 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3233 int qs = 0;
3234 while (qs < pathlen) {
3235 if (path[qs] == '?') {
3236 pathlen = qs;
3237 break;
3238 }
3239 qs++;
3240 }
3241 }
3242 } else {
3243 path = "/";
3244 pathlen = 1;
3245 }
3246
3247 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3248 return 0;
3249
3250 /* add prefix. Note that if prefix == "/", we don't want to
3251 * add anything, otherwise it makes it hard for the user to
3252 * configure a self-redirection.
3253 */
3254 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3255 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3256 trash.len += rule->rdr_len;
3257 }
3258
3259 /* add path */
3260 memcpy(trash.str + trash.len, path, pathlen);
3261 trash.len += pathlen;
3262
3263 /* append a slash at the end of the location is needed and missing */
3264 if (trash.len && trash.str[trash.len - 1] != '/' &&
3265 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3266 if (trash.len > trash.size - 5)
3267 return 0;
3268 trash.str[trash.len] = '/';
3269 trash.len++;
3270 }
3271
3272 break;
3273 }
3274 case REDIRECT_TYPE_LOCATION:
3275 default:
3276 if (trash.len + rule->rdr_len > trash.size - 4)
3277 return 0;
3278
3279 /* add location */
3280 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3281 trash.len += rule->rdr_len;
3282 break;
3283 }
3284
3285 if (rule->cookie_len) {
3286 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3287 trash.len += 14;
3288 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3289 trash.len += rule->cookie_len;
3290 memcpy(trash.str + trash.len, "\r\n", 2);
3291 trash.len += 2;
3292 }
3293
3294 /* add end of headers and the keep-alive/close status.
3295 * We may choose to set keep-alive if the Location begins
3296 * with a slash, because the client will come back to the
3297 * same server.
3298 */
3299 txn->status = rule->code;
3300 /* let's log the request time */
3301 s->logs.tv_request = now;
3302
3303 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3304 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3305 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3306 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3307 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3308 /* keep-alive possible */
3309 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3310 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3311 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3312 trash.len += 30;
3313 } else {
3314 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3315 trash.len += 24;
3316 }
3317 }
3318 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3319 trash.len += 4;
3320 bo_inject(txn->rsp.chn, trash.str, trash.len);
3321 /* "eat" the request */
3322 bi_fast_delete(txn->req.chn->buf, msg->sov);
3323 msg->sov = 0;
3324 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3325 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3326 txn->req.msg_state = HTTP_MSG_CLOSED;
3327 txn->rsp.msg_state = HTTP_MSG_DONE;
3328 } else {
3329 /* keep-alive not possible */
3330 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3331 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3332 trash.len += 29;
3333 } else {
3334 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3335 trash.len += 23;
3336 }
3337 stream_int_retnclose(txn->req.chn->prod, &trash);
3338 txn->req.chn->analysers = 0;
3339 }
3340
3341 if (!(s->flags & SN_ERR_MASK))
3342 s->flags |= SN_ERR_PRXCOND;
3343 if (!(s->flags & SN_FINST_MASK))
3344 s->flags |= SN_FINST_R;
3345
3346 return 1;
3347}
3348
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003349/* This stream analyser runs all HTTP request processing which is common to
3350 * frontends and backends, which means blocking ACLs, filters, connection-close,
3351 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003352 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003353 * either needs more data or wants to immediately abort the request (eg: deny,
3354 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003355 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003356int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003357{
Willy Tarreaud787e662009-07-07 10:14:51 +02003358 struct http_txn *txn = &s->txn;
3359 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003360 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003361 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003362 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003363 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003364 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003365
Willy Tarreau655dce92009-11-08 13:10:58 +01003366 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003367 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003368 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003369 return 0;
3370 }
3371
Willy Tarreau3a816292009-07-07 10:55:49 +02003372 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003373 req->analyse_exp = TICK_ETERNITY;
3374
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003375 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 +02003376 now_ms, __FUNCTION__,
3377 s,
3378 req,
3379 req->rex, req->wex,
3380 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003381 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003382 req->analysers);
3383
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003384 /* first check whether we have some ACLs set to block this request */
3385 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003386 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003387
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003388 ret = acl_pass(ret);
3389 if (cond->pol == ACL_COND_UNLESS)
3390 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003391
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003392 if (ret) {
3393 txn->status = 403;
3394 /* let's log the request time */
3395 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003396 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003397 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003400 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003401
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003402 /* just in case we have some per-backend tracking */
3403 session_inc_be_http_req_ctr(s);
3404
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003405 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003406 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003407
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003408 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003409 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003410 do_stats = stats_check_uri(s->rep->prod, txn, px);
3411 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003412 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003413 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003414 else
3415 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003416
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003417 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003418 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3419 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003420 txn->status = 403;
3421 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003422 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003423 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003424 s->fe->fe_counters.denied_req++;
3425 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3426 s->be->be_counters.denied_req++;
3427 if (s->listener->counters)
3428 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003429 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003430 }
3431 /* When a connection is tarpitted, we use the tarpit timeout,
3432 * which may be the same as the connect timeout if unspecified.
3433 * If unset, then set it to zero because we really want it to
3434 * eventually expire. We build the tarpit as an analyser.
3435 */
3436 if (txn->flags & TX_CLTARPIT) {
3437 channel_erase(s->req);
3438 /* wipe the request out so that we can drop the connection early
3439 * if the client closes first.
3440 */
3441 channel_dont_connect(req);
3442 req->analysers = 0; /* remove switching rules etc... */
3443 req->analysers |= AN_REQ_HTTP_TARPIT;
3444 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3445 if (!req->analyse_exp)
3446 req->analyse_exp = tick_add(now_ms, 0);
3447 session_inc_http_err_ctr(s);
3448 s->fe->fe_counters.denied_req++;
3449 if (s->fe != s->be)
3450 s->be->be_counters.denied_req++;
3451 if (s->listener->counters)
3452 s->listener->counters->denied_req++;
3453 return 1;
3454 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003455 }
3456
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457 /* try headers filters */
3458 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003459 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003461
Willy Tarreau59234e92008-11-30 23:51:27 +01003462 /* has the request been denied ? */
3463 if (txn->flags & TX_CLDENY) {
3464 /* no need to go further */
3465 txn->status = 403;
3466 /* let's log the request time */
3467 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003468 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003469 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 goto return_prx_cond;
3471 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003472
3473 /* When a connection is tarpitted, we use the tarpit timeout,
3474 * which may be the same as the connect timeout if unspecified.
3475 * If unset, then set it to zero because we really want it to
3476 * eventually expire. We build the tarpit as an analyser.
3477 */
3478 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003479 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003480 /* wipe the request out so that we can drop the connection early
3481 * if the client closes first.
3482 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003483 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003484 req->analysers = 0; /* remove switching rules etc... */
3485 req->analysers |= AN_REQ_HTTP_TARPIT;
3486 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3487 if (!req->analyse_exp)
3488 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003489 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003490 return 1;
3491 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003492 }
Willy Tarreau06619262006-12-17 08:37:22 +01003493
Willy Tarreau5b154472009-12-21 20:11:07 +01003494 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3495 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003496 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3497 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003498 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3499 * avoid to redo the same work if FE and BE have the same settings (common).
3500 * The method consists in checking if options changed between the two calls
3501 * (implying that either one is non-null, or one of them is non-null and we
3502 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003503 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003504
Willy Tarreaudc008c52010-02-01 16:20:08 +01003505 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3506 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3507 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3508 (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 +01003509 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003510
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003511 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3512 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003513 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003514 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3515 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003516 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003517 tmp = TX_CON_WANT_CLO;
3518
Willy Tarreau5b154472009-12-21 20:11:07 +01003519 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3520 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003521
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003522 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3523 /* parse the Connection header and possibly clean it */
3524 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003525 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003526 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3527 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003528 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003529 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003530 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003531 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003532 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003533
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003534 /* check if client or config asks for explicit close in KAL/SCL */
3535 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3536 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3537 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003538 (!(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 +01003539 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003540 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003541 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003542 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3543 }
Willy Tarreau78599912009-10-17 20:12:21 +02003544
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003545 /* we can be blocked here because the request needs to be authenticated,
3546 * either to pass or to access stats.
3547 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003548 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003549 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003550
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003551 if (!realm)
3552 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3553
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003554 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003555 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003556 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003557 /* on 401 we still count one error, because normal browsing
3558 * won't significantly increase the counter but brute force
3559 * attempts will.
3560 */
3561 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003562 goto return_prx_cond;
3563 }
3564
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003565 /* add request headers from the rule sets in the same order */
3566 list_for_each_entry(wl, &px->req_add, list) {
3567 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003568 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003569 ret = acl_pass(ret);
3570 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3571 ret = !ret;
3572 if (!ret)
3573 continue;
3574 }
3575
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003576 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003577 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003578 }
3579
3580 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3581 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3582 goto return_bad_req;
3583 req->analyse_exp = TICK_ETERNITY;
3584 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003585 }
3586
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003587 if (unlikely(do_stats)) {
3588 /* process the stats request now */
3589 if (!http_handle_stats(s, req)) {
3590 /* we need more data, let's come back here later */
3591 req->analysers |= an_bit;
3592 channel_dont_connect(req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003593 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003594 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003595 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003596
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003597 /* check whether we have some ACLs set to redirect this request */
3598 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003599 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003600 int ret;
3601
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003602 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003603 ret = acl_pass(ret);
3604 if (rule->cond->pol == ACL_COND_UNLESS)
3605 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003606 if (!ret)
3607 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003608 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003609 if (!http_apply_redirect_rule(rule, s, txn))
3610 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003611
Willy Tarreau71241ab2012-12-27 11:30:54 +01003612 req->analyse_exp = TICK_ETERNITY;
3613 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003615
Willy Tarreau2be39392010-01-03 17:24:51 +01003616 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3617 * If this happens, then the data will not come immediately, so we must
3618 * send all what we have without waiting. Note that due to the small gain
3619 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003620 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003621 * itself once used.
3622 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003623 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003624
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003625 /* that's OK for us now, let's move on to next analysers */
3626 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003627
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 return_bad_req:
3629 /* We centralize bad requests processing here */
3630 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3631 /* we detected a parsing error. We want to archive this request
3632 * in the dedicated proxy area for later troubleshooting.
3633 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003634 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003636
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003637 txn->req.msg_state = HTTP_MSG_ERROR;
3638 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003639 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003641 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003642 if (s->listener->counters)
3643 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003644
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003645 return_prx_cond:
3646 if (!(s->flags & SN_ERR_MASK))
3647 s->flags |= SN_ERR_PRXCOND;
3648 if (!(s->flags & SN_FINST_MASK))
3649 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003650
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003651 req->analysers = 0;
3652 req->analyse_exp = TICK_ETERNITY;
3653 return 0;
3654}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003655
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003656/* This function performs all the processing enabled for the current request.
3657 * It returns 1 if the processing can continue on next analysers, or zero if it
3658 * needs more data, encounters an error, or wants to immediately abort the
3659 * request. It relies on buffers flags, and updates s->req->analysers.
3660 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003661int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003662{
3663 struct http_txn *txn = &s->txn;
3664 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003665
Willy Tarreau655dce92009-11-08 13:10:58 +01003666 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003667 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003668 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003669 return 0;
3670 }
3671
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003672 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 +02003673 now_ms, __FUNCTION__,
3674 s,
3675 req,
3676 req->rex, req->wex,
3677 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003678 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003679 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003680
William Lallemand82fe75c2012-10-23 10:25:10 +02003681 if (s->fe->comp || s->be->comp)
3682 select_compression_request_header(s, req->buf);
3683
Willy Tarreau59234e92008-11-30 23:51:27 +01003684 /*
3685 * Right now, we know that we have processed the entire headers
3686 * and that unwanted requests have been filtered out. We can do
3687 * whatever we want with the remaining request. Also, now we
3688 * may have separate values for ->fe, ->be.
3689 */
Willy Tarreau06619262006-12-17 08:37:22 +01003690
Willy Tarreau59234e92008-11-30 23:51:27 +01003691 /*
3692 * If HTTP PROXY is set we simply get remote server address
3693 * parsing incoming request.
3694 */
3695 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003696 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003698
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003700 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 * Note that doing so might move headers in the request, but
3702 * the fields will stay coherent and the URI will not move.
3703 * This should only be performed in the backend.
3704 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003705 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003706 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3707 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003708
Willy Tarreau59234e92008-11-30 23:51:27 +01003709 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003710 * 8: the appsession cookie was looked up very early in 1.2,
3711 * so let's do the same now.
3712 */
3713
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003714 /* It needs to look into the URI unless persistence must be ignored */
3715 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003716 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 +01003717 }
3718
William Lallemanda73203e2012-03-12 12:48:57 +01003719 /* add unique-id if "header-unique-id" is specified */
3720
3721 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3722 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3723
3724 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003725 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3726 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003727 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003728 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003729 goto return_bad_req;
3730 }
3731
Cyril Bontéb21570a2009-11-29 20:04:48 +01003732 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003733 * 9: add X-Forwarded-For if either the frontend or the backend
3734 * asks for it.
3735 */
3736 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003737 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003738 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003739 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3740 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003741 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003742 /* The header is set to be added only if none is present
3743 * and we found it, so don't do anything.
3744 */
3745 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003746 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003747 /* Add an X-Forwarded-For header unless the source IP is
3748 * in the 'except' network range.
3749 */
3750 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003751 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003752 != s->fe->except_net.s_addr) &&
3753 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003754 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003756 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003757 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003758 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003759
3760 /* Note: we rely on the backend to get the header name to be used for
3761 * x-forwarded-for, because the header is really meant for the backends.
3762 * However, if the backend did not specify any option, we have to rely
3763 * on the frontend's header name.
3764 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003765 if (s->be->fwdfor_hdr_len) {
3766 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003767 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003768 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003770 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003771 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003772 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003774 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003775 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003776 }
3777 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003778 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003779 /* FIXME: for the sake of completeness, we should also support
3780 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003781 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003782 int len;
3783 char pn[INET6_ADDRSTRLEN];
3784 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003785 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003786 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003787
Willy Tarreau59234e92008-11-30 23:51:27 +01003788 /* Note: we rely on the backend to get the header name to be used for
3789 * x-forwarded-for, because the header is really meant for the backends.
3790 * However, if the backend did not specify any option, we have to rely
3791 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003792 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003793 if (s->be->fwdfor_hdr_len) {
3794 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003795 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003796 } else {
3797 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003798 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003799 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003800 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003802 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 goto return_bad_req;
3804 }
3805 }
3806
3807 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003808 * 10: add X-Original-To if either the frontend or the backend
3809 * asks for it.
3810 */
3811 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3812
3813 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003814 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003815 /* Add an X-Original-To header unless the destination IP is
3816 * in the 'except' network range.
3817 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003818 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003819
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003820 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003821 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003822 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003823 != s->fe->except_to.s_addr) &&
3824 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003825 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003826 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003827 int len;
3828 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003829 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003830
3831 /* Note: we rely on the backend to get the header name to be used for
3832 * x-original-to, because the header is really meant for the backends.
3833 * However, if the backend did not specify any option, we have to rely
3834 * on the frontend's header name.
3835 */
3836 if (s->be->orgto_hdr_len) {
3837 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003838 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003839 } else {
3840 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003841 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003842 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003843 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003844
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003845 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003846 goto return_bad_req;
3847 }
3848 }
3849 }
3850
Willy Tarreau50fc7772012-11-11 22:19:57 +01003851 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3852 * If an "Upgrade" token is found, the header is left untouched in order not to have
3853 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3854 * "Upgrade" is present in the Connection header.
3855 */
3856 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3857 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3858 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003859 unsigned int want_flags = 0;
3860
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003861 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003862 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3863 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3864 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003865 want_flags |= TX_CON_CLO_SET;
3866 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003867 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3868 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3869 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003870 want_flags |= TX_CON_KAL_SET;
3871 }
3872
3873 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003874 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003875 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003876
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003877
Willy Tarreau522d6c02009-12-06 18:49:18 +01003878 /* If we have no server assigned yet and we're balancing on url_param
3879 * with a POST request, we may be interested in checking the body for
3880 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003881 */
3882 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3883 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003884 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003885 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003886 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003887 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003888 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003889
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003890 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003891 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003892#ifdef TCP_QUICKACK
3893 /* We expect some data from the client. Unless we know for sure
3894 * we already have a full request, we have to re-enable quick-ack
3895 * in case we previously disabled it, otherwise we might cause
3896 * the client to delay further data.
3897 */
3898 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003899 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003900 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003901 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003902#endif
3903 }
Willy Tarreau03945942009-12-22 16:50:27 +01003904
Willy Tarreau59234e92008-11-30 23:51:27 +01003905 /*************************************************************
3906 * OK, that's finished for the headers. We have done what we *
3907 * could. Let's switch to the DATA state. *
3908 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003909 req->analyse_exp = TICK_ETERNITY;
3910 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003911
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003912 /* if the server closes the connection, we want to immediately react
3913 * and close the socket to save packets and syscalls.
3914 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003915 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3916 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003917
Willy Tarreau59234e92008-11-30 23:51:27 +01003918 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 /* OK let's go on with the BODY now */
3920 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003921
Willy Tarreau59234e92008-11-30 23:51:27 +01003922 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003923 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003924 /* we detected a parsing error. We want to archive this request
3925 * in the dedicated proxy area for later troubleshooting.
3926 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003927 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003928 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003929
Willy Tarreau59234e92008-11-30 23:51:27 +01003930 txn->req.msg_state = HTTP_MSG_ERROR;
3931 txn->status = 400;
3932 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003933 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003934
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003935 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003936 if (s->listener->counters)
3937 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003938
Willy Tarreau59234e92008-11-30 23:51:27 +01003939 if (!(s->flags & SN_ERR_MASK))
3940 s->flags |= SN_ERR_PRXCOND;
3941 if (!(s->flags & SN_FINST_MASK))
3942 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003943 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003944}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003945
Willy Tarreau60b85b02008-11-30 23:28:40 +01003946/* This function is an analyser which processes the HTTP tarpit. It always
3947 * returns zero, at the beginning because it prevents any other processing
3948 * from occurring, and at the end because it terminates the request.
3949 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003950int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003951{
3952 struct http_txn *txn = &s->txn;
3953
3954 /* This connection is being tarpitted. The CLIENT side has
3955 * already set the connect expiration date to the right
3956 * timeout. We just have to check that the client is still
3957 * there and that the timeout has not expired.
3958 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003959 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003960 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003961 !tick_is_expired(req->analyse_exp, now_ms))
3962 return 0;
3963
3964 /* We will set the queue timer to the time spent, just for
3965 * logging purposes. We fake a 500 server error, so that the
3966 * attacker will not suspect his connection has been tarpitted.
3967 * It will not cause trouble to the logs because we can exclude
3968 * the tarpitted connections by filtering on the 'PT' status flags.
3969 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003970 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3971
3972 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003973 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003974 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003975
3976 req->analysers = 0;
3977 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003978
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003979 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003980 if (s->listener->counters)
3981 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003982
Willy Tarreau60b85b02008-11-30 23:28:40 +01003983 if (!(s->flags & SN_ERR_MASK))
3984 s->flags |= SN_ERR_PRXCOND;
3985 if (!(s->flags & SN_FINST_MASK))
3986 s->flags |= SN_FINST_T;
3987 return 0;
3988}
3989
Willy Tarreaud34af782008-11-30 23:36:37 +01003990/* This function is an analyser which processes the HTTP request body. It looks
3991 * for parameters to be used for the load balancing algorithm (url_param). It
3992 * must only be called after the standard HTTP request processing has occurred,
3993 * because it expects the request to be parsed. It returns zero if it needs to
3994 * read more data, or 1 once it has completed its analysis.
3995 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003996int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003997{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003998 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003999 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004000 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004001
4002 /* We have to parse the HTTP request body to find any required data.
4003 * "balance url_param check_post" should have been the only way to get
4004 * into this. We were brought here after HTTP header analysis, so all
4005 * related structures are ready.
4006 */
4007
Willy Tarreau522d6c02009-12-06 18:49:18 +01004008 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4009 goto missing_data;
4010
4011 if (msg->msg_state < HTTP_MSG_100_SENT) {
4012 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4013 * send an HTTP/1.1 100 Continue intermediate response.
4014 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004015 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004016 struct hdr_ctx ctx;
4017 ctx.idx = 0;
4018 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004019 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004020 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004021 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004022 }
4023 }
4024 msg->msg_state = HTTP_MSG_100_SENT;
4025 }
4026
4027 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004028 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004029 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004030 * is still null. We must save the body in msg->next because it
4031 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004032 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004033 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004034
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004035 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004036 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4037 else
4038 msg->msg_state = HTTP_MSG_DATA;
4039 }
4040
4041 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004042 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004043 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004044 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004045 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004046 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004047
Willy Tarreau115acb92009-12-26 13:56:06 +01004048 if (!ret)
4049 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004050 else if (ret < 0) {
4051 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004052 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004053 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004054 }
4055
Willy Tarreaud98cf932009-12-27 22:54:55 +01004056 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004057 * We have the first data byte is in msg->sov. We're waiting for at
4058 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004059 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004060
Willy Tarreau124d9912011-03-01 20:30:48 +01004061 if (msg->body_len < limit)
4062 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004063
Willy Tarreau9b28e032012-10-12 23:49:43 +02004064 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004065 goto http_end;
4066
4067 missing_data:
4068 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004069 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004070 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004071 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004072 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004073
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004074 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004075 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004076 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004077
4078 if (!(s->flags & SN_ERR_MASK))
4079 s->flags |= SN_ERR_CLITO;
4080 if (!(s->flags & SN_FINST_MASK))
4081 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004082 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004083 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084
4085 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004086 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004087 /* Not enough data. We'll re-use the http-request
4088 * timeout here. Ideally, we should set the timeout
4089 * relative to the accept() date. We just set the
4090 * request timeout once at the beginning of the
4091 * request.
4092 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004093 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004094 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004095 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004096 return 0;
4097 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004098
4099 http_end:
4100 /* The situation will not evolve, so let's give up on the analysis. */
4101 s->logs.tv_request = now; /* update the request timer to reflect full request */
4102 req->analysers &= ~an_bit;
4103 req->analyse_exp = TICK_ETERNITY;
4104 return 1;
4105
4106 return_bad_req: /* let's centralize all bad requests */
4107 txn->req.msg_state = HTTP_MSG_ERROR;
4108 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004109 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004110
Willy Tarreau79ebac62010-06-07 13:47:49 +02004111 if (!(s->flags & SN_ERR_MASK))
4112 s->flags |= SN_ERR_PRXCOND;
4113 if (!(s->flags & SN_FINST_MASK))
4114 s->flags |= SN_FINST_R;
4115
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116 return_err_msg:
4117 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004118 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004119 if (s->listener->counters)
4120 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004121 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004122}
4123
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004124/* send a server's name with an outgoing request over an established connection.
4125 * Note: this function is designed to be called once the request has been scheduled
4126 * for being forwarded. This is the reason why it rewinds the buffer before
4127 * proceeding.
4128 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004129int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004130
4131 struct hdr_ctx ctx;
4132
Mark Lamourinec2247f02012-01-04 13:02:01 -05004133 char *hdr_name = be->server_id_hdr_name;
4134 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004135 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004136 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004137 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004138
William Lallemandd9e90662012-01-30 17:27:17 +01004139 ctx.idx = 0;
4140
Willy Tarreau9b28e032012-10-12 23:49:43 +02004141 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004142 if (old_o) {
4143 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004144 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004145 }
4146
Willy Tarreau9b28e032012-10-12 23:49:43 +02004147 old_i = chn->buf->i;
4148 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004149 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004150 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004151 }
4152
4153 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004154 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004155 memcpy(hdr_val, hdr_name, hdr_name_len);
4156 hdr_val += hdr_name_len;
4157 *hdr_val++ = ':';
4158 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004159 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4160 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004161
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004162 if (old_o) {
4163 /* If this was a forwarded request, we must readjust the amount of
4164 * data to be forwarded in order to take into account the size
4165 * variations.
4166 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004167 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004168 }
4169
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170 return 0;
4171}
4172
Willy Tarreau610ecce2010-01-04 21:15:02 +01004173/* Terminate current transaction and prepare a new one. This is very tricky
4174 * right now but it works.
4175 */
4176void http_end_txn_clean_session(struct session *s)
4177{
4178 /* FIXME: We need a more portable way of releasing a backend's and a
4179 * server's connections. We need a safer way to reinitialize buffer
4180 * flags. We also need a more accurate method for computing per-request
4181 * data.
4182 */
4183 http_silent_debug(__LINE__, s);
4184
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004185 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004186 si_shutr(s->req->cons);
4187 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188
4189 http_silent_debug(__LINE__, s);
4190
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004191 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004192 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004193 if (unlikely(s->srv_conn))
4194 sess_change_server(s, NULL);
4195 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196
4197 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4198 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004199 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200
4201 if (s->txn.status) {
4202 int n;
4203
4204 n = s->txn.status / 100;
4205 if (n < 1 || n > 5)
4206 n = 0;
4207
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004208 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004209 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004210 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004211 s->fe->fe_counters.p.http.comp_rsp++;
4212 }
Willy Tarreau24657792010-02-26 10:30:28 +01004213 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004214 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004215 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004216 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004217 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004218 s->be->be_counters.p.http.comp_rsp++;
4219 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 }
4221
4222 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004223 s->logs.bytes_in -= s->req->buf->i;
4224 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225
4226 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004227 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 !(s->flags & SN_MONITOR) &&
4229 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4230 s->do_log(s);
4231 }
4232
4233 s->logs.accept_date = date; /* user-visible date for logging */
4234 s->logs.tv_accept = now; /* corrected date for internal use */
4235 tv_zero(&s->logs.tv_request);
4236 s->logs.t_queue = -1;
4237 s->logs.t_connect = -1;
4238 s->logs.t_data = -1;
4239 s->logs.t_close = 0;
4240 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4241 s->logs.srv_queue_size = 0; /* we will get this number soon */
4242
Willy Tarreau9b28e032012-10-12 23:49:43 +02004243 s->logs.bytes_in = s->req->total = s->req->buf->i;
4244 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245
4246 if (s->pend_pos)
4247 pendconn_free(s->pend_pos);
4248
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004249 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 if (s->flags & SN_CURR_SESS) {
4251 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004252 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004254 if (may_dequeue_tasks(objt_server(s->target), s->be))
4255 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 }
4257
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004258 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259
4260 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004261 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4262 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004263 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004265 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 s->req->cons->err_loc = NULL;
4267 s->req->cons->exp = TICK_ETERNITY;
4268 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004269 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4270 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 +02004271 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 +01004272 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004273
4274 if (s->flags & SN_COMP_READY)
4275 s->comp_algo->end(&s->comp_ctx);
4276 s->comp_algo = NULL;
4277 s->flags &= ~SN_COMP_READY;
4278
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 s->txn.meth = 0;
4280 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004281 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004282 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004283 s->req->cons->flags |= SI_FL_INDEP_STR;
4284
Willy Tarreau96e31212011-05-30 18:10:30 +02004285 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004286 s->req->flags |= CF_NEVER_WAIT;
4287 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004288 }
4289
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290 /* if the request buffer is not empty, it means we're
4291 * about to process another request, so send pending
4292 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004293 * Just don't do this if the buffer is close to be full,
4294 * because the request will wait for it to flush a little
4295 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004297 if (s->req->buf->i) {
4298 if (s->rep->buf->o &&
4299 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4300 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004301 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004302 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004303
4304 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004305 channel_auto_read(s->req);
4306 channel_auto_close(s->req);
4307 channel_auto_read(s->rep);
4308 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309
Willy Tarreau342b11c2010-11-24 16:22:09 +01004310 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311 s->rep->analysers = 0;
4312
4313 http_silent_debug(__LINE__, s);
4314}
4315
4316
4317/* This function updates the request state machine according to the response
4318 * state machine and buffer flags. It returns 1 if it changes anything (flag
4319 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4320 * it is only used to find when a request/response couple is complete. Both
4321 * this function and its equivalent should loop until both return zero. It
4322 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4323 */
4324int http_sync_req_state(struct session *s)
4325{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004326 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004328 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 unsigned int old_state = txn->req.msg_state;
4330
4331 http_silent_debug(__LINE__, s);
4332 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4333 return 0;
4334
4335 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004336 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004337 * We can shut the read side unless we want to abort_on_close,
4338 * or we have a POST request. The issue with POST requests is
4339 * that some browsers still send a CRLF after the request, and
4340 * this CRLF must be read so that it does not remain in the kernel
4341 * buffers, otherwise a close could cause an RST on some systems
4342 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004343 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004344 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004345 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346
Willy Tarreau40f151a2012-12-20 12:10:09 +01004347 /* if the server closes the connection, we want to immediately react
4348 * and close the socket to save packets and syscalls.
4349 */
4350 chn->cons->flags |= SI_FL_NOHALF;
4351
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4353 goto wait_other_side;
4354
4355 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4356 /* The server has not finished to respond, so we
4357 * don't want to move in order not to upset it.
4358 */
4359 goto wait_other_side;
4360 }
4361
4362 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4363 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004364 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004366 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004367 goto wait_other_side;
4368 }
4369
4370 /* When we get here, it means that both the request and the
4371 * response have finished receiving. Depending on the connection
4372 * mode, we'll have to wait for the last bytes to leave in either
4373 * direction, and sometimes for a close to be effective.
4374 */
4375
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004376 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4377 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004378 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4379 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004380 }
4381 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4382 /* Option forceclose is set, or either side wants to close,
4383 * let's enforce it now that we're not expecting any new
4384 * data to come. The caller knows the session is complete
4385 * once both states are CLOSED.
4386 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004387 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4388 channel_shutr_now(chn);
4389 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004390 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004391 }
4392 else {
4393 /* The last possible modes are keep-alive and tunnel. Since tunnel
4394 * mode does not set the body analyser, we can't reach this place
4395 * in tunnel mode, so we're left with keep-alive only.
4396 * This mode is currently not implemented, we switch to tunnel mode.
4397 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004398 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004399 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004400 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 }
4402
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004403 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004405 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004406
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004407 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408 txn->req.msg_state = HTTP_MSG_CLOSING;
4409 goto http_msg_closing;
4410 }
4411 else {
4412 txn->req.msg_state = HTTP_MSG_CLOSED;
4413 goto http_msg_closed;
4414 }
4415 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004416 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417 }
4418
4419 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4420 http_msg_closing:
4421 /* nothing else to forward, just waiting for the output buffer
4422 * to be empty and for the shutw_now to take effect.
4423 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004424 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004425 txn->req.msg_state = HTTP_MSG_CLOSED;
4426 goto http_msg_closed;
4427 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004428 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 txn->req.msg_state = HTTP_MSG_ERROR;
4430 goto wait_other_side;
4431 }
4432 }
4433
4434 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4435 http_msg_closed:
4436 goto wait_other_side;
4437 }
4438
4439 wait_other_side:
4440 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004441 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442}
4443
4444
4445/* This function updates the response state machine according to the request
4446 * state machine and buffer flags. It returns 1 if it changes anything (flag
4447 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4448 * it is only used to find when a request/response couple is complete. Both
4449 * this function and its equivalent should loop until both return zero. It
4450 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4451 */
4452int http_sync_res_state(struct session *s)
4453{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004454 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004455 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004456 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 unsigned int old_state = txn->rsp.msg_state;
4458
4459 http_silent_debug(__LINE__, s);
4460 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4461 return 0;
4462
4463 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4464 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004465 * still monitor the server connection for a possible close
4466 * while the request is being uploaded, so we don't disable
4467 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004469 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470
4471 if (txn->req.msg_state == HTTP_MSG_ERROR)
4472 goto wait_other_side;
4473
4474 if (txn->req.msg_state < HTTP_MSG_DONE) {
4475 /* The client seems to still be sending data, probably
4476 * because we got an error response during an upload.
4477 * We have the choice of either breaking the connection
4478 * or letting it pass through. Let's do the later.
4479 */
4480 goto wait_other_side;
4481 }
4482
4483 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4484 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004485 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004487 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 goto wait_other_side;
4489 }
4490
4491 /* When we get here, it means that both the request and the
4492 * response have finished receiving. Depending on the connection
4493 * mode, we'll have to wait for the last bytes to leave in either
4494 * direction, and sometimes for a close to be effective.
4495 */
4496
4497 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4498 /* Server-close mode : shut read and wait for the request
4499 * side to close its output buffer. The caller will detect
4500 * when we're in DONE and the other is in CLOSED and will
4501 * catch that for the final cleanup.
4502 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004503 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4504 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004506 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4507 /* Option forceclose is set, or either side wants to close,
4508 * let's enforce it now that we're not expecting any new
4509 * data to come. The caller knows the session is complete
4510 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004512 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4513 channel_shutr_now(chn);
4514 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004515 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004516 }
4517 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004518 /* The last possible modes are keep-alive and tunnel. Since tunnel
4519 * mode does not set the body analyser, we can't reach this place
4520 * in tunnel mode, so we're left with keep-alive only.
4521 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004523 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004524 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004525 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004526 }
4527
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004528 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004529 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004530 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4532 goto http_msg_closing;
4533 }
4534 else {
4535 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4536 goto http_msg_closed;
4537 }
4538 }
4539 goto wait_other_side;
4540 }
4541
4542 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4543 http_msg_closing:
4544 /* nothing else to forward, just waiting for the output buffer
4545 * to be empty and for the shutw_now to take effect.
4546 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004547 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4549 goto http_msg_closed;
4550 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004551 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004552 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004553 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004554 if (objt_server(s->target))
4555 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004556 goto wait_other_side;
4557 }
4558 }
4559
4560 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4561 http_msg_closed:
4562 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 bi_erase(chn);
4564 channel_auto_close(chn);
4565 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 goto wait_other_side;
4567 }
4568
4569 wait_other_side:
4570 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004571 /* We force the response to leave immediately if we're waiting for the
4572 * other side, since there is no pending shutdown to push it out.
4573 */
4574 if (!channel_is_empty(chn))
4575 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004576 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577}
4578
4579
4580/* Resync the request and response state machines. Return 1 if either state
4581 * changes.
4582 */
4583int http_resync_states(struct session *s)
4584{
4585 struct http_txn *txn = &s->txn;
4586 int old_req_state = txn->req.msg_state;
4587 int old_res_state = txn->rsp.msg_state;
4588
4589 http_silent_debug(__LINE__, s);
4590 http_sync_req_state(s);
4591 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004592 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004593 if (!http_sync_res_state(s))
4594 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004595 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 if (!http_sync_req_state(s))
4597 break;
4598 }
4599 http_silent_debug(__LINE__, s);
4600 /* OK, both state machines agree on a compatible state.
4601 * There are a few cases we're interested in :
4602 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4603 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4604 * directions, so let's simply disable both analysers.
4605 * - HTTP_MSG_CLOSED on the response only means we must abort the
4606 * request.
4607 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4608 * with server-close mode means we've completed one request and we
4609 * must re-initialize the server connection.
4610 */
4611
4612 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4613 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4614 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4615 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4616 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004617 channel_auto_close(s->req);
4618 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004620 channel_auto_close(s->rep);
4621 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004623 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4624 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004625 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004626 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004627 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004628 channel_auto_close(s->rep);
4629 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004630 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004631 channel_abort(s->req);
4632 channel_auto_close(s->req);
4633 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004634 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 }
4636 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4637 txn->rsp.msg_state == HTTP_MSG_DONE &&
4638 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4639 /* server-close: terminate this server connection and
4640 * reinitialize a fresh-new transaction.
4641 */
4642 http_end_txn_clean_session(s);
4643 }
4644
4645 http_silent_debug(__LINE__, s);
4646 return txn->req.msg_state != old_req_state ||
4647 txn->rsp.msg_state != old_res_state;
4648}
4649
Willy Tarreaud98cf932009-12-27 22:54:55 +01004650/* This function is an analyser which forwards request body (including chunk
4651 * sizes if any). It is called as soon as we must forward, even if we forward
4652 * zero byte. The only situation where it must not be called is when we're in
4653 * tunnel mode and we want to forward till the close. It's used both to forward
4654 * remaining data and to resync after end of body. It expects the msg_state to
4655 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4656 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004657 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004658 * bytes of pending data + the headers if not already done (between sol and sov).
4659 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004660 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004661int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004662{
4663 struct http_txn *txn = &s->txn;
4664 struct http_msg *msg = &s->txn.req;
4665
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004666 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4667 return 0;
4668
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004669 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004670 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004671 /* Output closed while we were sending data. We must abort and
4672 * wake the other side up.
4673 */
4674 msg->msg_state = HTTP_MSG_ERROR;
4675 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004676 return 1;
4677 }
4678
Willy Tarreau4fe41902010-06-07 22:27:41 +02004679 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004680 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004681
4682 /* Note that we don't have to send 100-continue back because we don't
4683 * need the data to complete our job, and it's up to the server to
4684 * decide whether to return 100, 417 or anything else in return of
4685 * an "Expect: 100-continue" header.
4686 */
4687
4688 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004689 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004690 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004691 * is still null. We must save the body in msg->next because it
4692 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004693 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004694 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004695
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004696 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004697 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004698 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004699 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004700 }
4701
Willy Tarreaud98cf932009-12-27 22:54:55 +01004702 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004703 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004704
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004706 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004707 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004708 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004709 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004710 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004711 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004712 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004713 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004714
Willy Tarreaucaabe412010-01-03 23:08:28 +01004715 if (msg->msg_state == HTTP_MSG_DATA) {
4716 /* must still forward */
4717 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004718 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004719
4720 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004721 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004722 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004723 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004724 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004725 }
4726 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004727 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004728 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004729 * TRAILERS state.
4730 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004731 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004732
Willy Tarreau54d23df2012-10-25 19:04:45 +02004733 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004734 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004735 else if (ret < 0) {
4736 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004737 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004738 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004739 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004740 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004741 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004742 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004743 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004745 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004746
4747 if (ret == 0)
4748 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004749 else if (ret < 0) {
4750 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004751 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004752 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004753 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004754 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004755 /* we're in MSG_CHUNK_SIZE now */
4756 }
4757 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004758 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004759
4760 if (ret == 0)
4761 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004762 else if (ret < 0) {
4763 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004764 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004765 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004767 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004768 /* we're in HTTP_MSG_DONE now */
4769 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004771 int old_state = msg->msg_state;
4772
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004774 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004775 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4776 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004777 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004778 if (http_resync_states(s)) {
4779 /* some state changes occurred, maybe the analyser
4780 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004781 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004782 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004783 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004784 /* request errors are most likely due to
4785 * the server aborting the transfer.
4786 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004787 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004788 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004789 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004790 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004792 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004794 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004795
4796 /* If "option abortonclose" is set on the backend, we
4797 * want to monitor the client's connection and forward
4798 * any shutdown notification to the server, which will
4799 * decide whether to close or to go on processing the
4800 * request.
4801 */
4802 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004803 channel_auto_read(req);
4804 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004805 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004806 else if (s->txn.meth == HTTP_METH_POST) {
4807 /* POST requests may require to read extra CRLF
4808 * sent by broken browsers and which could cause
4809 * an RST to be sent upon close on some systems
4810 * (eg: Linux).
4811 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004812 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004813 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004814
Willy Tarreau610ecce2010-01-04 21:15:02 +01004815 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 }
4817 }
4818
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004820 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004821 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004822 if (!(s->flags & SN_ERR_MASK))
4823 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004824 if (!(s->flags & SN_FINST_MASK)) {
4825 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4826 s->flags |= SN_FINST_H;
4827 else
4828 s->flags |= SN_FINST_D;
4829 }
4830
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004831 s->fe->fe_counters.cli_aborts++;
4832 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004833 if (objt_server(s->target))
4834 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004835
4836 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004837 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004838
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004839 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004840 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004841 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004842
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004843 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004844 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004845 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004846 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004848
Willy Tarreau5c620922011-05-11 19:56:11 +02004849 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004850 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004851 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004852 * modes are already handled by the stream sock layer. We must not do
4853 * this in content-length mode because it could present the MSG_MORE
4854 * flag with the last block of forwarded data, which would cause an
4855 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004856 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004857 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004858 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004859
Willy Tarreau610ecce2010-01-04 21:15:02 +01004860 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004861 return 0;
4862
Willy Tarreaud98cf932009-12-27 22:54:55 +01004863 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004864 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004865 if (s->listener->counters)
4866 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004867 return_bad_req_stats_ok:
4868 txn->req.msg_state = HTTP_MSG_ERROR;
4869 if (txn->status) {
4870 /* Note: we don't send any error if some data were already sent */
4871 stream_int_retnclose(req->prod, NULL);
4872 } else {
4873 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004874 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875 }
4876 req->analysers = 0;
4877 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004878
4879 if (!(s->flags & SN_ERR_MASK))
4880 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004881 if (!(s->flags & SN_FINST_MASK)) {
4882 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4883 s->flags |= SN_FINST_H;
4884 else
4885 s->flags |= SN_FINST_D;
4886 }
4887 return 0;
4888
4889 aborted_xfer:
4890 txn->req.msg_state = HTTP_MSG_ERROR;
4891 if (txn->status) {
4892 /* Note: we don't send any error if some data were already sent */
4893 stream_int_retnclose(req->prod, NULL);
4894 } else {
4895 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004896 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004897 }
4898 req->analysers = 0;
4899 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4900
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004901 s->fe->fe_counters.srv_aborts++;
4902 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004903 if (objt_server(s->target))
4904 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004905
4906 if (!(s->flags & SN_ERR_MASK))
4907 s->flags |= SN_ERR_SRVCL;
4908 if (!(s->flags & SN_FINST_MASK)) {
4909 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4910 s->flags |= SN_FINST_H;
4911 else
4912 s->flags |= SN_FINST_D;
4913 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914 return 0;
4915}
4916
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004917/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4918 * processing can continue on next analysers, or zero if it either needs more
4919 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4920 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4921 * when it has nothing left to do, and may remove any analyser when it wants to
4922 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004923 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004924int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004925{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 struct http_txn *txn = &s->txn;
4927 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004928 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004929 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004930 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004931 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004932
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004933 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 +02004934 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004935 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004936 rep,
4937 rep->rex, rep->wex,
4938 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004939 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004940 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004942 /*
4943 * Now parse the partial (or complete) lines.
4944 * We will check the response syntax, and also join multi-line
4945 * headers. An index of all the lines will be elaborated while
4946 * parsing.
4947 *
4948 * For the parsing, we use a 28 states FSM.
4949 *
4950 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004951 * rep->buf->p = beginning of response
4952 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4953 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004954 * msg->eol = end of current header or line (LF or CRLF)
4955 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004956 */
4957
Willy Tarreau83e3af02009-12-28 17:39:57 +01004958 /* There's a protected area at the end of the buffer for rewriting
4959 * purposes. We don't want to start to parse the request if the
4960 * protected area is affected, because we may have to move processed
4961 * data later, which is much more complicated.
4962 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004963 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004964 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004965 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4966 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4967 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004968 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004969 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004970 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004971 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004972 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004973 return 0;
4974 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004975 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4976 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004977 }
4978
Willy Tarreau9b28e032012-10-12 23:49:43 +02004979 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004980 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004981 }
4982
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004983 /* 1: we might have to print this header in debug mode */
4984 if (unlikely((global.mode & MODE_DEBUG) &&
4985 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004986 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004987 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988
Willy Tarreau9b28e032012-10-12 23:49:43 +02004989 sol = rep->buf->p;
4990 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004991 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 sol += hdr_idx_first_pos(&txn->hdr_idx);
4994 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004996 while (cur_idx) {
4997 eol = sol + txn->hdr_idx.v[cur_idx].len;
4998 debug_hdr("srvhdr", s, sol, eol);
4999 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5000 cur_idx = txn->hdr_idx.v[cur_idx].next;
5001 }
5002 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005003
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005004 /*
5005 * Now we quickly check if we have found a full valid response.
5006 * If not so, we check the FD and buffer states before leaving.
5007 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005008 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005009 * responses are checked first.
5010 *
5011 * Depending on whether the client is still there or not, we
5012 * may send an error response back or not. Note that normally
5013 * we should only check for HTTP status there, and check I/O
5014 * errors somewhere else.
5015 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005016
Willy Tarreau655dce92009-11-08 13:10:58 +01005017 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005018 /* Invalid response */
5019 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5020 /* we detected a parsing error. We want to archive this response
5021 * in the dedicated proxy area for later troubleshooting.
5022 */
5023 hdr_response_bad:
5024 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005025 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005026
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005027 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005028 if (objt_server(s->target)) {
5029 objt_server(s->target)->counters.failed_resp++;
5030 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005031 }
Willy Tarreau64648412010-03-05 10:41:54 +01005032 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005033 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005034 rep->analysers = 0;
5035 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005036 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005037 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005038 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039
5040 if (!(s->flags & SN_ERR_MASK))
5041 s->flags |= SN_ERR_PRXCOND;
5042 if (!(s->flags & SN_FINST_MASK))
5043 s->flags |= SN_FINST_H;
5044
5045 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005046 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005047
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005048 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005049 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005050 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005051 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 goto hdr_response_bad;
5053 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005056 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005058 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005060 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005061 if (objt_server(s->target)) {
5062 objt_server(s->target)->counters.failed_resp++;
5063 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005064 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005065
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005066 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 rep->analysers = 0;
5068 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005069 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005070 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005071 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 if (!(s->flags & SN_ERR_MASK))
5074 s->flags |= SN_ERR_SRVCL;
5075 if (!(s->flags & SN_FINST_MASK))
5076 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005077 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005081 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005083 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005084
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005085 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005086 if (objt_server(s->target)) {
5087 objt_server(s->target)->counters.failed_resp++;
5088 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005089 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005090
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005091 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 rep->analysers = 0;
5093 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005094 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005095 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005096 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005097
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005098 if (!(s->flags & SN_ERR_MASK))
5099 s->flags |= SN_ERR_SRVTO;
5100 if (!(s->flags & SN_FINST_MASK))
5101 s->flags |= SN_FINST_H;
5102 return 0;
5103 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005104
Willy Tarreauf003d372012-11-26 13:35:37 +01005105 /* client abort with an abortonclose */
5106 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5107 s->fe->fe_counters.cli_aborts++;
5108 s->be->be_counters.cli_aborts++;
5109 if (objt_server(s->target))
5110 objt_server(s->target)->counters.cli_aborts++;
5111
5112 rep->analysers = 0;
5113 channel_auto_close(rep);
5114
5115 txn->status = 400;
5116 bi_erase(rep);
5117 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5118
5119 if (!(s->flags & SN_ERR_MASK))
5120 s->flags |= SN_ERR_CLICL;
5121 if (!(s->flags & SN_FINST_MASK))
5122 s->flags |= SN_FINST_H;
5123
5124 /* process_session() will take care of the error */
5125 return 0;
5126 }
5127
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005128 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005129 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005130 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005131 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005133 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005134 if (objt_server(s->target)) {
5135 objt_server(s->target)->counters.failed_resp++;
5136 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005137 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005138
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005139 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140 rep->analysers = 0;
5141 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005142 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005143 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005144 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 if (!(s->flags & SN_ERR_MASK))
5147 s->flags |= SN_ERR_SRVCL;
5148 if (!(s->flags & SN_FINST_MASK))
5149 s->flags |= SN_FINST_H;
5150 return 0;
5151 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005154 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005155 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005156 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005157
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005158 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005160 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161
5162 if (!(s->flags & SN_ERR_MASK))
5163 s->flags |= SN_ERR_CLICL;
5164 if (!(s->flags & SN_FINST_MASK))
5165 s->flags |= SN_FINST_H;
5166
5167 /* process_session() will take care of the error */
5168 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005170
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005171 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 return 0;
5173 }
5174
5175 /* More interesting part now : we know that we have a complete
5176 * response which at least looks like HTTP. We have an indicator
5177 * of each header's length, so we can parse them quickly.
5178 */
5179
5180 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005181 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183 /*
5184 * 1: get the status code
5185 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005186 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005187 if (n < 1 || n > 5)
5188 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005189 /* when the client triggers a 4xx from the server, it's most often due
5190 * to a missing object or permission. These events should be tracked
5191 * because if they happen often, it may indicate a brute force or a
5192 * vulnerability scan.
5193 */
5194 if (n == 4)
5195 session_inc_http_err_ctr(s);
5196
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (objt_server(s->target))
5198 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199
Willy Tarreau5b154472009-12-21 20:11:07 +01005200 /* check if the response is HTTP/1.1 or above */
5201 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005202 ((rep->buf->p[5] > '1') ||
5203 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005204 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005205
5206 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005207 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005208
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005209 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005210 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005211
Willy Tarreau9b28e032012-10-12 23:49:43 +02005212 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005213
Willy Tarreau39650402010-03-15 19:44:39 +01005214 /* Adjust server's health based on status code. Note: status codes 501
5215 * and 505 are triggered on demand by client request, so we must not
5216 * count them as server failures.
5217 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005218 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005219 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005220 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005221 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005222 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005223 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005224
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 /*
5226 * 2: check for cacheability.
5227 */
5228
5229 switch (txn->status) {
5230 case 200:
5231 case 203:
5232 case 206:
5233 case 300:
5234 case 301:
5235 case 410:
5236 /* RFC2616 @13.4:
5237 * "A response received with a status code of
5238 * 200, 203, 206, 300, 301 or 410 MAY be stored
5239 * by a cache (...) unless a cache-control
5240 * directive prohibits caching."
5241 *
5242 * RFC2616 @9.5: POST method :
5243 * "Responses to this method are not cacheable,
5244 * unless the response includes appropriate
5245 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005246 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005248 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5250 break;
5251 default:
5252 break;
5253 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 /*
5256 * 3: we may need to capture headers
5257 */
5258 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005259 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005260 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005261 txn->rsp.cap, s->fe->rsp_cap);
5262
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005263 /* 4: determine the transfer-length.
5264 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5265 * the presence of a message-body in a RESPONSE and its transfer length
5266 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005267 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005268 * All responses to the HEAD request method MUST NOT include a
5269 * message-body, even though the presence of entity-header fields
5270 * might lead one to believe they do. All 1xx (informational), 204
5271 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5272 * message-body. All other responses do include a message-body,
5273 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005274 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005275 * 1. Any response which "MUST NOT" include a message-body (such as the
5276 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5277 * always terminated by the first empty line after the header fields,
5278 * regardless of the entity-header fields present in the message.
5279 *
5280 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5281 * the "chunked" transfer-coding (Section 6.2) is used, the
5282 * transfer-length is defined by the use of this transfer-coding.
5283 * If a Transfer-Encoding header field is present and the "chunked"
5284 * transfer-coding is not present, the transfer-length is defined by
5285 * the sender closing the connection.
5286 *
5287 * 3. If a Content-Length header field is present, its decimal value in
5288 * OCTETs represents both the entity-length and the transfer-length.
5289 * If a message is received with both a Transfer-Encoding header
5290 * field and a Content-Length header field, the latter MUST be ignored.
5291 *
5292 * 4. If the message uses the media type "multipart/byteranges", and
5293 * the transfer-length is not otherwise specified, then this self-
5294 * delimiting media type defines the transfer-length. This media
5295 * type MUST NOT be used unless the sender knows that the recipient
5296 * can parse it; the presence in a request of a Range header with
5297 * multiple byte-range specifiers from a 1.1 client implies that the
5298 * client can parse multipart/byteranges responses.
5299 *
5300 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005301 */
5302
5303 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005304 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005305 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005306 * FIXME: should we parse anyway and return an error on chunked encoding ?
5307 */
5308 if (txn->meth == HTTP_METH_HEAD ||
5309 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005310 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005311 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005312 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005313 goto skip_content_length;
5314 }
5315
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005316 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005317 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005318 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005319 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005320 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005321 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5322 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005323 /* bad transfer-encoding (chunked followed by something else) */
5324 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005325 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005326 break;
5327 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005328 }
5329
5330 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5331 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005332 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005333 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005334 signed long long cl;
5335
Willy Tarreauad14f752011-09-02 20:33:27 +02005336 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005337 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005338 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005339 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005340
Willy Tarreauad14f752011-09-02 20:33:27 +02005341 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005342 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005343 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005344 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005345
Willy Tarreauad14f752011-09-02 20:33:27 +02005346 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005347 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005348 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005349 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005350
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005351 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005352 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005353 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005354 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005355
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005356 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005357 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005358 }
5359
William Lallemand82fe75c2012-10-23 10:25:10 +02005360 if (s->fe->comp || s->be->comp)
5361 select_compression_response_header(s, rep->buf);
5362
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005363 /* FIXME: we should also implement the multipart/byterange method.
5364 * For now on, we resort to close mode in this case (unknown length).
5365 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005366skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005367
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005368 /* end of job, return OK */
5369 rep->analysers &= ~an_bit;
5370 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005371 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005372 return 1;
5373}
5374
5375/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005376 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5377 * and updates t->rep->analysers. It might make sense to explode it into several
5378 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005379 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005380int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005381{
5382 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005383 struct http_msg *msg = &txn->rsp;
5384 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005385 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005387 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 +02005388 now_ms, __FUNCTION__,
5389 t,
5390 rep,
5391 rep->rex, rep->wex,
5392 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005393 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394 rep->analysers);
5395
Willy Tarreau655dce92009-11-08 13:10:58 +01005396 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397 return 0;
5398
5399 rep->analysers &= ~an_bit;
5400 rep->analyse_exp = TICK_ETERNITY;
5401
Willy Tarreau5b154472009-12-21 20:11:07 +01005402 /* Now we have to check if we need to modify the Connection header.
5403 * This is more difficult on the response than it is on the request,
5404 * because we can have two different HTTP versions and we don't know
5405 * how the client will interprete a response. For instance, let's say
5406 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5407 * HTTP/1.1 response without any header. Maybe it will bound itself to
5408 * HTTP/1.0 because it only knows about it, and will consider the lack
5409 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5410 * the lack of header as a keep-alive. Thus we will use two flags
5411 * indicating how a request MAY be understood by the client. In case
5412 * of multiple possibilities, we'll fix the header to be explicit. If
5413 * ambiguous cases such as both close and keepalive are seen, then we
5414 * will fall back to explicit close. Note that we won't take risks with
5415 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005416 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005417 */
5418
Willy Tarreaudc008c52010-02-01 16:20:08 +01005419 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5420 txn->status == 101)) {
5421 /* Either we've established an explicit tunnel, or we're
5422 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005423 * to understand the next protocols. We have to switch to tunnel
5424 * mode, so that we transfer the request and responses then let
5425 * this protocol pass unmodified. When we later implement specific
5426 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005427 * header which contains information about that protocol for
5428 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005429 */
5430 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5431 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005432 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5433 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5434 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005435 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005436
Willy Tarreau60466522010-01-18 19:08:45 +01005437 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005438 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005439 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5440 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005441
Willy Tarreau60466522010-01-18 19:08:45 +01005442 /* now adjust header transformations depending on current state */
5443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5444 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5445 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005446 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005447 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005448 }
Willy Tarreau60466522010-01-18 19:08:45 +01005449 else { /* SCL / KAL */
5450 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005451 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005452 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005453 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005454
Willy Tarreau60466522010-01-18 19:08:45 +01005455 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005456 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005457
Willy Tarreau60466522010-01-18 19:08:45 +01005458 /* Some keep-alive responses are converted to Server-close if
5459 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005460 */
Willy Tarreau60466522010-01-18 19:08:45 +01005461 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5462 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005463 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005464 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005465 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005466 }
5467
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005468 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005469 /*
5470 * 3: we will have to evaluate the filters.
5471 * As opposed to version 1.2, now they will be evaluated in the
5472 * filters order and not in the header order. This means that
5473 * each filter has to be validated among all headers.
5474 *
5475 * Filters are tried with ->be first, then with ->fe if it is
5476 * different from ->be.
5477 */
5478
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005479 cur_proxy = t->be;
5480 while (1) {
5481 struct proxy *rule_set = cur_proxy;
5482
5483 /* try headers filters */
5484 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005485 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005486 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005487 if (objt_server(t->target)) {
5488 objt_server(t->target)->counters.failed_resp++;
5489 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005490 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005491 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005492 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005493 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005494 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005495 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005496 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005497 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005498 if (!(t->flags & SN_ERR_MASK))
5499 t->flags |= SN_ERR_PRXCOND;
5500 if (!(t->flags & SN_FINST_MASK))
5501 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005502 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005503 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005504 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005505
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005506 /* has the response been denied ? */
5507 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005508 if (objt_server(t->target))
5509 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005510
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005511 t->be->be_counters.denied_resp++;
5512 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005513 if (t->listener->counters)
5514 t->listener->counters->denied_resp++;
5515
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005516 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005517 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005518
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005519 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005520 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005521 if (txn->status < 200)
5522 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005523 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005524 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005525 ret = acl_pass(ret);
5526 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5527 ret = !ret;
5528 if (!ret)
5529 continue;
5530 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005531 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005532 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005533 }
5534
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005535 /* check whether we're already working on the frontend */
5536 if (cur_proxy == t->fe)
5537 break;
5538 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005539 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005542 * We may be facing a 100-continue response, in which case this
5543 * is not the right response, and we're waiting for the next one.
5544 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005545 * next one.
5546 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005547 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005548 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005549 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005550 msg->msg_state = HTTP_MSG_RPBEFORE;
5551 txn->status = 0;
5552 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5553 return 1;
5554 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005555 else if (unlikely(txn->status < 200))
5556 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005557
5558 /* we don't have any 1xx status code now */
5559
5560 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005561 * 4: check for server cookie.
5562 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005563 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5564 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005565 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005566
Willy Tarreaubaaee002006-06-26 02:48:02 +02005567
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005569 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005570 */
Willy Tarreau67402132012-05-31 20:40:20 +02005571 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005572 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005573
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005574 /*
5575 * 6: add server cookie in the response if needed
5576 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005577 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005578 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005579 (!(t->flags & SN_DIRECT) ||
5580 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5581 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5582 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5583 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005584 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005585 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005586 /* the server is known, it's not the one the client requested, or the
5587 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005588 * insert a set-cookie here, except if we want to insert only on POST
5589 * requests and this one isn't. Note that servers which don't have cookies
5590 * (eg: some backup servers) will return a full cookie removal request.
5591 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005592 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005593 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005594 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5595 t->be->cookie_name);
5596 }
5597 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005598 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005599
5600 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5601 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005602 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5603 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5604 trash.len += 5;
5605
Willy Tarreauef4f3912010-10-07 21:00:29 +02005606 if (t->be->cookie_maxlife) {
5607 /* emit first_date, which is either the original one or
5608 * the current date.
5609 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005610 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005611 s30tob64(txn->cookie_first_date ?
5612 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005613 (date.tv_sec+3) >> 2, trash.str + trash.len);
5614 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005615 }
5616 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005617 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005618 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005619
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005620 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005621 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005622
Willy Tarreau4992dd22012-05-31 21:02:17 +02005623 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005625
5626 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005627 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005628
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005629 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005630 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005631
Willy Tarreauf1348312010-10-07 15:54:11 +02005632 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005633 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005634 /* the server did not change, only the date was updated */
5635 txn->flags |= TX_SCK_UPDATED;
5636 else
5637 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005638
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005639 /* Here, we will tell an eventual cache on the client side that we don't
5640 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5641 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5642 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5643 */
Willy Tarreau67402132012-05-31 20:40:20 +02005644 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005645
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005646 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5647
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005648 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005649 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005650 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005651 }
5652 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005653
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005654 /*
5655 * 7: check if result will be cacheable with a cookie.
5656 * We'll block the response if security checks have caught
5657 * nasty things such as a cacheable cookie.
5658 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005659 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5660 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005661 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005662
5663 /* we're in presence of a cacheable response containing
5664 * a set-cookie header. We'll block it as requested by
5665 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005666 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005667 if (objt_server(t->target))
5668 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005669
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005670 t->be->be_counters.denied_resp++;
5671 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005672 if (t->listener->counters)
5673 t->listener->counters->denied_resp++;
5674
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005675 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005676 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005677 send_log(t->be, LOG_ALERT,
5678 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005679 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005680 goto return_srv_prx_502;
5681 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005682
5683 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005684 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005685 * If an "Upgrade" token is found, the header is left untouched in order
5686 * not to have to deal with some client bugs : some of them fail an upgrade
5687 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005688 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005689 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5690 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5691 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005692 unsigned int want_flags = 0;
5693
5694 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5695 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5696 /* we want a keep-alive response here. Keep-alive header
5697 * required if either side is not 1.1.
5698 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005699 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005700 want_flags |= TX_CON_KAL_SET;
5701 }
5702 else {
5703 /* we want a close response here. Close header required if
5704 * the server is 1.1, regardless of the client.
5705 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005706 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005707 want_flags |= TX_CON_CLO_SET;
5708 }
5709
5710 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005711 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005712 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005714 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005715 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005716 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005717 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005718
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005719 /*************************************************************
5720 * OK, that's finished for the headers. We have done what we *
5721 * could. Let's switch to the DATA state. *
5722 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005724 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005726 /* if the user wants to log as soon as possible, without counting
5727 * bytes from the server, then this is the right moment. We have
5728 * to temporarily assign bytes_out to log what we currently have.
5729 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005730 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005731 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5732 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005733 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005734 t->logs.bytes_out = 0;
5735 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005736
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737 /* Note: we must not try to cheat by jumping directly to DATA,
5738 * otherwise we would not let the client side wake up.
5739 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005740
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005741 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005742 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005743 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005744}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005745
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746/* This function is an analyser which forwards response body (including chunk
5747 * sizes if any). It is called as soon as we must forward, even if we forward
5748 * zero byte. The only situation where it must not be called is when we're in
5749 * tunnel mode and we want to forward till the close. It's used both to forward
5750 * remaining data and to resync after end of body. It expects the msg_state to
5751 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5752 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005753 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005754 * bytes of pending data + the headers if not already done (between sol and sov).
5755 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005757int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758{
5759 struct http_txn *txn = &s->txn;
5760 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005761 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005762 static struct buffer *tmpbuf = NULL;
5763 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005764 int consumed_data = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005765
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005766 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5767 return 0;
5768
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005769 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005770 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005771 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005772 /* Output closed while we were sending data. We must abort and
5773 * wake the other side up.
5774 */
5775 msg->msg_state = HTTP_MSG_ERROR;
5776 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005777 return 1;
5778 }
5779
Willy Tarreau4fe41902010-06-07 22:27:41 +02005780 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005781 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005782
William Lallemand82fe75c2012-10-23 10:25:10 +02005783 /* this is the first time we need the compression buffer */
5784 if (s->comp_algo != NULL && tmpbuf == NULL) {
5785 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5786 goto aborted_xfer; /* no memory */
5787 }
5788
Willy Tarreaud98cf932009-12-27 22:54:55 +01005789 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005790 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005791 * rep->buf.p still points to the beginning of the message and msg->sol
5792 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005793 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005794 channel_forward(res, msg->sov);
5795 msg->next = 0;
5796 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005797
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005798 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005799 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005800 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005801 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005802 }
5803
William Lallemand82fe75c2012-10-23 10:25:10 +02005804 if (s->comp_algo != NULL) {
5805 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5806 if (ret < 0)
5807 goto missing_data; /* not enough spaces in buffers */
5808 compressing = 1;
5809 }
5810
Willy Tarreaud98cf932009-12-27 22:54:55 +01005811 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005812 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005813 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005814 if (s->comp_algo == NULL) {
5815 bytes = msg->sov - msg->sol;
5816 if (msg->chunk_len || bytes) {
5817 msg->sol = msg->sov;
5818 msg->next -= bytes; /* will be forwarded */
5819 msg->chunk_len += bytes;
5820 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5821 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005822 }
5823
Willy Tarreaucaabe412010-01-03 23:08:28 +01005824 if (msg->msg_state == HTTP_MSG_DATA) {
William Lallemandbf3ae612012-11-19 12:35:37 +01005825 int ret;
5826
Willy Tarreaucaabe412010-01-03 23:08:28 +01005827 /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005828 if (compressing) {
5829 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5830 if (ret < 0)
5831 goto aborted_xfer;
5832 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005833
5834 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005835 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005836
5837 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005838 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005839 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005840 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005841 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005842 if (compressing && consumed_data) {
5843 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5844 compressing = 0;
5845 }
5846 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005847 }
5848 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005849 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005850 * set ->sov and ->next to point to the body and switch to DATA or
5851 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005852 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005853 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005854
Willy Tarreau54d23df2012-10-25 19:04:45 +02005855 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005856 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005857 else if (ret < 0) {
5858 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005859 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005860 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005861 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005862 if (compressing) {
5863 if (likely(msg->chunk_len > 0)) {
5864 /* skipping data if we are in compression mode */
5865 b_adv(res->buf, msg->next);
5866 msg->next = 0;
5867 msg->sov = 0;
5868 msg->sol = 0;
5869 } else {
5870 if (consumed_data) {
5871 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5872 compressing = 0;
5873 }
5874 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005875 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005876 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005877 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005878 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005879 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005880 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005881
Willy Tarreau54d23df2012-10-25 19:04:45 +02005882 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005883 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005884 else if (ret < 0) {
5885 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005886 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005887 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005888 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005889 /* skipping data in buffer for compression */
5890 if (compressing) {
5891 b_adv(res->buf, msg->next);
5892 msg->next = 0;
5893 msg->sov = 0;
5894 msg->sol = 0;
5895 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005896 /* we're in MSG_CHUNK_SIZE now */
5897 }
5898 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005899 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005900
Willy Tarreaud98cf932009-12-27 22:54:55 +01005901 if (ret == 0)
5902 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005903 else if (ret < 0) {
5904 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005905 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005906 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005907 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005908 if (s->comp_algo != NULL) {
5909 /* forwarding trailers */
5910 channel_forward(res, msg->next);
5911 msg->next = 0;
5912 }
5913 /* we're in HTTP_MSG_DONE now */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005914 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005915 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005916 int old_state = msg->msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005917 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005918 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005919 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5920 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005921 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005922 if (http_resync_states(s)) {
5923 http_silent_debug(__LINE__, s);
5924 /* some state changes occurred, maybe the analyser
5925 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005926 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005927 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005928 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005929 /* response errors are most likely due to
5930 * the client aborting the transfer.
5931 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005932 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005933 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005934 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005935 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005936 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005937 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005938 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005939 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005940 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005941 }
5942 }
5943
Willy Tarreaud98cf932009-12-27 22:54:55 +01005944 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005945 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005946 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5947 compressing = 0;
5948 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005949
5950 if (res->flags & CF_SHUTW)
5951 goto aborted_xfer;
5952
5953 /* stop waiting for data if the input is closed before the end. If the
5954 * client side was already closed, it means that the client has aborted,
5955 * so we don't want to count this as a server abort. Otherwise it's a
5956 * server abort.
5957 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005958 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005959 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5960 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005961 if (!(s->flags & SN_ERR_MASK))
5962 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005963 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005964 if (objt_server(s->target))
5965 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005966 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005967 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005968
Willy Tarreau40dba092010-03-04 18:14:51 +01005969 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005970 if (!s->req->analysers)
5971 goto return_bad_res;
5972
Willy Tarreauea953162012-05-18 23:41:28 +02005973 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005974 if (s->comp_algo == NULL) {
5975 bytes = msg->sov - msg->sol;
5976 if (msg->chunk_len || bytes) {
5977 msg->sol = msg->sov;
5978 msg->next -= bytes; /* will be forwarded */
5979 msg->chunk_len += bytes;
5980 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5981 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005982 }
5983
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005984 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005985 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005986 * Similarly, with keep-alive on the client side, we don't want to forward a
5987 * close.
5988 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005989 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005990 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5991 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005992 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005993
Willy Tarreau5c620922011-05-11 19:56:11 +02005994 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005995 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005996 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005997 * modes are already handled by the stream sock layer. We must not do
5998 * this in content-length mode because it could present the MSG_MORE
5999 * flag with the last block of forwarded data, which would cause an
6000 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006001 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006002 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006003 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006004
Willy Tarreaud98cf932009-12-27 22:54:55 +01006005 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006006 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006007 return 0;
6008
Willy Tarreau40dba092010-03-04 18:14:51 +01006009 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006010 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006011 if (objt_server(s->target))
6012 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006013
6014 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006015 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006016 /* don't send any error message as we're in the body */
6017 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006019 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006020 if (objt_server(s->target))
6021 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022
6023 if (!(s->flags & SN_ERR_MASK))
6024 s->flags |= SN_ERR_PRXCOND;
6025 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006026 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006027 return 0;
6028
6029 aborted_xfer:
6030 txn->rsp.msg_state = HTTP_MSG_ERROR;
6031 /* don't send any error message as we're in the body */
6032 stream_int_retnclose(res->cons, NULL);
6033 res->analysers = 0;
6034 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6035
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006036 s->fe->fe_counters.cli_aborts++;
6037 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006038 if (objt_server(s->target))
6039 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006040
6041 if (!(s->flags & SN_ERR_MASK))
6042 s->flags |= SN_ERR_CLICL;
6043 if (!(s->flags & SN_FINST_MASK))
6044 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045 return 0;
6046}
6047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006048/* Iterate the same filter through all request headers.
6049 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006050 * Since it can manage the switch to another backend, it updates the per-proxy
6051 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006052 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006053int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 char term;
6056 char *cur_ptr, *cur_end, *cur_next;
6057 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006058 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006060 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006062 last_hdr = 0;
6063
Willy Tarreau9b28e032012-10-12 23:49:43 +02006064 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006065 old_idx = 0;
6066
6067 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006068 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006069 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006070 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 (exp->action == ACT_ALLOW ||
6072 exp->action == ACT_DENY ||
6073 exp->action == ACT_TARPIT))
6074 return 0;
6075
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006076 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 if (!cur_idx)
6078 break;
6079
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006080 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 cur_ptr = cur_next;
6082 cur_end = cur_ptr + cur_hdr->len;
6083 cur_next = cur_end + cur_hdr->cr + 1;
6084
6085 /* Now we have one header between cur_ptr and cur_end,
6086 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 */
6088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 /* The annoying part is that pattern matching needs
6090 * that we modify the contents to null-terminate all
6091 * strings before testing them.
6092 */
6093
6094 term = *cur_end;
6095 *cur_end = '\0';
6096
6097 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6098 switch (exp->action) {
6099 case ACT_SETBE:
6100 /* It is not possible to jump a second time.
6101 * FIXME: should we return an HTTP/500 here so that
6102 * the admin knows there's a problem ?
6103 */
6104 if (t->be != t->fe)
6105 break;
6106
6107 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006108 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006109 last_hdr = 1;
6110 break;
6111
6112 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006113 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 last_hdr = 1;
6115 break;
6116
6117 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006118 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006120
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006121 t->fe->fe_counters.denied_req++;
6122 if (t->fe != t->be)
6123 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006124 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006125 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006127 break;
6128
6129 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006130 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006131 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006133 t->fe->fe_counters.denied_req++;
6134 if (t->fe != t->be)
6135 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006136 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006137 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 break;
6140
6141 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006142 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6143 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006144 /* FIXME: if the user adds a newline in the replacement, the
6145 * index will not be recalculated for now, and the new line
6146 * will not be counted as a new header.
6147 */
6148
6149 cur_end += delta;
6150 cur_next += delta;
6151 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006152 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006153 break;
6154
6155 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006156 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 cur_next += delta;
6158
Willy Tarreaufa355d42009-11-29 18:12:29 +01006159 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006160 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6161 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162 cur_hdr->len = 0;
6163 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006164 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006165 break;
6166
6167 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006168 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006169 if (cur_end)
6170 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006172 /* keep the link from this header to next one in case of later
6173 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006174 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006175 old_idx = cur_idx;
6176 }
6177 return 0;
6178}
6179
6180
6181/* Apply the filter to the request line.
6182 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6183 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006184 * Since it can manage the switch to another backend, it updates the per-proxy
6185 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006186 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006187int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006188{
6189 char term;
6190 char *cur_ptr, *cur_end;
6191 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006192 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006193 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194
Willy Tarreau3d300592007-03-18 18:34:41 +01006195 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006197 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006198 (exp->action == ACT_ALLOW ||
6199 exp->action == ACT_DENY ||
6200 exp->action == ACT_TARPIT))
6201 return 0;
6202 else if (exp->action == ACT_REMOVE)
6203 return 0;
6204
6205 done = 0;
6206
Willy Tarreau9b28e032012-10-12 23:49:43 +02006207 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006208 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006209
6210 /* Now we have the request line between cur_ptr and cur_end */
6211
6212 /* The annoying part is that pattern matching needs
6213 * that we modify the contents to null-terminate all
6214 * strings before testing them.
6215 */
6216
6217 term = *cur_end;
6218 *cur_end = '\0';
6219
6220 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6221 switch (exp->action) {
6222 case ACT_SETBE:
6223 /* It is not possible to jump a second time.
6224 * FIXME: should we return an HTTP/500 here so that
6225 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006227 if (t->be != t->fe)
6228 break;
6229
6230 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006231 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006232 done = 1;
6233 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006235 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006236 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006237 done = 1;
6238 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006240 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006241 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006242
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006243 t->fe->fe_counters.denied_req++;
6244 if (t->fe != t->be)
6245 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006246 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006247 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006249 done = 1;
6250 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006253 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006254
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006255 t->fe->fe_counters.denied_req++;
6256 if (t->fe != t->be)
6257 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006258 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006259 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006261 done = 1;
6262 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006264 case ACT_REPLACE:
6265 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006266 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6267 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006268 /* FIXME: if the user adds a newline in the replacement, the
6269 * index will not be recalculated for now, and the new line
6270 * will not be counted as a new header.
6271 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006272
Willy Tarreaufa355d42009-11-29 18:12:29 +01006273 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006274 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006275 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 HTTP_MSG_RQMETH,
6277 cur_ptr, cur_end + 1,
6278 NULL, NULL);
6279 if (unlikely(!cur_end))
6280 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006282 /* we have a full request and we know that we have either a CR
6283 * or an LF at <ptr>.
6284 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006285 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6286 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006287 /* there is no point trying this regex on headers */
6288 return 1;
6289 }
6290 }
6291 *cur_end = term; /* restore the string terminator */
6292 return done;
6293}
Willy Tarreau97de6242006-12-27 17:18:38 +01006294
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006297/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006298 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006299 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006300 * unparsable request. Since it can manage the switch to another backend, it
6301 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006302 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006303int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006305 struct http_txn *txn = &s->txn;
6306 struct hdr_exp *exp;
6307
6308 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006309 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006311 /*
6312 * The interleaving of transformations and verdicts
6313 * makes it difficult to decide to continue or stop
6314 * the evaluation.
6315 */
6316
Willy Tarreau6c123b12010-01-28 20:22:06 +01006317 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6318 break;
6319
Willy Tarreau3d300592007-03-18 18:34:41 +01006320 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006322 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006323 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006324
6325 /* if this filter had a condition, evaluate it now and skip to
6326 * next filter if the condition does not match.
6327 */
6328 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006329 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006330 ret = acl_pass(ret);
6331 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6332 ret = !ret;
6333
6334 if (!ret)
6335 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006336 }
6337
6338 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006339 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006340 if (unlikely(ret < 0))
6341 return -1;
6342
6343 if (likely(ret == 0)) {
6344 /* The filter did not match the request, it can be
6345 * iterated through all headers.
6346 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006347 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351}
6352
6353
Willy Tarreaua15645d2007-03-18 16:22:39 +01006354
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006356 * Try to retrieve the server associated to the appsession.
6357 * If the server is found, it's assigned to the session.
6358 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006359void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006360 struct http_txn *txn = &t->txn;
6361 appsess *asession = NULL;
6362 char *sessid_temp = NULL;
6363
Cyril Bontéb21570a2009-11-29 20:04:48 +01006364 if (len > t->be->appsession_len) {
6365 len = t->be->appsession_len;
6366 }
6367
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006368 if (t->be->options2 & PR_O2_AS_REQL) {
6369 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006370 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006371 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006372 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006373 }
6374
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006375 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006376 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6377 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6378 return;
6379 }
6380
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006381 memcpy(txn->sessid, buf, len);
6382 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006383 }
6384
6385 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6386 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6387 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6388 return;
6389 }
6390
Cyril Bontéb21570a2009-11-29 20:04:48 +01006391 memcpy(sessid_temp, buf, len);
6392 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006393
6394 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6395 /* free previously allocated memory */
6396 pool_free2(apools.sessid, sessid_temp);
6397
6398 if (asession != NULL) {
6399 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6400 if (!(t->be->options2 & PR_O2_AS_REQL))
6401 asession->request_count++;
6402
6403 if (asession->serverid != NULL) {
6404 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006405
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006406 while (srv) {
6407 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006408 if ((srv->state & SRV_RUNNING) ||
6409 (t->be->options & PR_O_PERSIST) ||
6410 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006411 /* we found the server and it's usable */
6412 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006413 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006414 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006415 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006416
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006417 break;
6418 } else {
6419 txn->flags &= ~TX_CK_MASK;
6420 txn->flags |= TX_CK_DOWN;
6421 }
6422 }
6423 srv = srv->next;
6424 }
6425 }
6426 }
6427}
6428
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429/* Find the end of a cookie value contained between <s> and <e>. It works the
6430 * same way as with headers above except that the semi-colon also ends a token.
6431 * See RFC2965 for more information. Note that it requires a valid header to
6432 * return a valid result.
6433 */
6434char *find_cookie_value_end(char *s, const char *e)
6435{
6436 int quoted, qdpair;
6437
6438 quoted = qdpair = 0;
6439 for (; s < e; s++) {
6440 if (qdpair) qdpair = 0;
6441 else if (quoted) {
6442 if (*s == '\\') qdpair = 1;
6443 else if (*s == '"') quoted = 0;
6444 }
6445 else if (*s == '"') quoted = 1;
6446 else if (*s == ',' || *s == ';') return s;
6447 }
6448 return s;
6449}
6450
6451/* Delete a value in a header between delimiters <from> and <next> in buffer
6452 * <buf>. The number of characters displaced is returned, and the pointer to
6453 * the first delimiter is updated if required. The function tries as much as
6454 * possible to respect the following principles :
6455 * - replace <from> delimiter by the <next> one unless <from> points to a
6456 * colon, in which case <next> is simply removed
6457 * - set exactly one space character after the new first delimiter, unless
6458 * there are not enough characters in the block being moved to do so.
6459 * - remove unneeded spaces before the previous delimiter and after the new
6460 * one.
6461 *
6462 * It is the caller's responsibility to ensure that :
6463 * - <from> points to a valid delimiter or the colon ;
6464 * - <next> points to a valid delimiter or the final CR/LF ;
6465 * - there are non-space chars before <from> ;
6466 * - there is a CR/LF at or after <next>.
6467 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006468int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006469{
6470 char *prev = *from;
6471
6472 if (*prev == ':') {
6473 /* We're removing the first value, preserve the colon and add a
6474 * space if possible.
6475 */
6476 if (!http_is_crlf[(unsigned char)*next])
6477 next++;
6478 prev++;
6479 if (prev < next)
6480 *prev++ = ' ';
6481
6482 while (http_is_spht[(unsigned char)*next])
6483 next++;
6484 } else {
6485 /* Remove useless spaces before the old delimiter. */
6486 while (http_is_spht[(unsigned char)*(prev-1)])
6487 prev--;
6488 *from = prev;
6489
6490 /* copy the delimiter and if possible a space if we're
6491 * not at the end of the line.
6492 */
6493 if (!http_is_crlf[(unsigned char)*next]) {
6494 *prev++ = *next++;
6495 if (prev + 1 < next)
6496 *prev++ = ' ';
6497 while (http_is_spht[(unsigned char)*next])
6498 next++;
6499 }
6500 }
6501 return buffer_replace2(buf, prev, next, NULL, 0);
6502}
6503
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006504/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006505 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 * desirable to call it only when needed. This code is quite complex because
6507 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6508 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006509 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006510void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006511{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006512 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006513 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6516 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006518 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006519 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006520 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006521
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006522 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006524 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006526 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 hdr_beg = hdr_next;
6528 hdr_end = hdr_beg + cur_hdr->len;
6529 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006531 /* We have one full header between hdr_beg and hdr_end, and the
6532 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533 * "Cookie:" headers.
6534 */
6535
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006537 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 old_idx = cur_idx;
6539 continue;
6540 }
6541
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006542 del_from = NULL; /* nothing to be deleted */
6543 preserve_hdr = 0; /* assume we may kill the whole header */
6544
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545 /* Now look for cookies. Conforming to RFC2109, we have to support
6546 * attributes whose name begin with a '$', and associate them with
6547 * the right cookie, if we want to delete this cookie.
6548 * So there are 3 cases for each cookie read :
6549 * 1) it's a special attribute, beginning with a '$' : ignore it.
6550 * 2) it's a server id cookie that we *MAY* want to delete : save
6551 * some pointers on it (last semi-colon, beginning of cookie...)
6552 * 3) it's an application cookie : we *MAY* have to delete a previous
6553 * "special" cookie.
6554 * At the end of loop, if a "special" cookie remains, we may have to
6555 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 * *MUST* delete it.
6557 *
6558 * Note: RFC2965 is unclear about the processing of spaces around
6559 * the equal sign in the ATTR=VALUE form. A careful inspection of
6560 * the RFC explicitly allows spaces before it, and not within the
6561 * tokens (attrs or values). An inspection of RFC2109 allows that
6562 * too but section 10.1.3 lets one think that spaces may be allowed
6563 * after the equal sign too, resulting in some (rare) buggy
6564 * implementations trying to do that. So let's do what servers do.
6565 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6566 * allowed quoted strings in values, with any possible character
6567 * after a backslash, including control chars and delimitors, which
6568 * causes parsing to become ambiguous. Browsers also allow spaces
6569 * within values even without quotes.
6570 *
6571 * We have to keep multiple pointers in order to support cookie
6572 * removal at the beginning, middle or end of header without
6573 * corrupting the header. All of these headers are valid :
6574 *
6575 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6576 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6577 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6578 * | | | | | | | | |
6579 * | | | | | | | | hdr_end <--+
6580 * | | | | | | | +--> next
6581 * | | | | | | +----> val_end
6582 * | | | | | +-----------> val_beg
6583 * | | | | +--------------> equal
6584 * | | | +----------------> att_end
6585 * | | +---------------------> att_beg
6586 * | +--------------------------> prev
6587 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 */
6589
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6591 /* Iterate through all cookies on this line */
6592
6593 /* find att_beg */
6594 att_beg = prev + 1;
6595 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6596 att_beg++;
6597
6598 /* find att_end : this is the first character after the last non
6599 * space before the equal. It may be equal to hdr_end.
6600 */
6601 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006603 while (equal < hdr_end) {
6604 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 if (http_is_spht[(unsigned char)*equal++])
6607 continue;
6608 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006609 }
6610
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6612 * is between <att_beg> and <equal>, both may be identical.
6613 */
6614
6615 /* look for end of cookie if there is an equal sign */
6616 if (equal < hdr_end && *equal == '=') {
6617 /* look for the beginning of the value */
6618 val_beg = equal + 1;
6619 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6620 val_beg++;
6621
6622 /* find the end of the value, respecting quotes */
6623 next = find_cookie_value_end(val_beg, hdr_end);
6624
6625 /* make val_end point to the first white space or delimitor after the value */
6626 val_end = next;
6627 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6628 val_end--;
6629 } else {
6630 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006632
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 /* We have nothing to do with attributes beginning with '$'. However,
6634 * they will automatically be removed if a header before them is removed,
6635 * since they're supposed to be linked together.
6636 */
6637 if (*att_beg == '$')
6638 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006639
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006640 /* Ignore cookies with no equal sign */
6641 if (equal == next) {
6642 /* This is not our cookie, so we must preserve it. But if we already
6643 * scheduled another cookie for removal, we cannot remove the
6644 * complete header, but we can remove the previous block itself.
6645 */
6646 preserve_hdr = 1;
6647 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006648 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006649 val_end += delta;
6650 next += delta;
6651 hdr_end += delta;
6652 hdr_next += delta;
6653 cur_hdr->len += delta;
6654 http_msg_move_end(&txn->req, delta);
6655 prev = del_from;
6656 del_from = NULL;
6657 }
6658 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006659 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 /* if there are spaces around the equal sign, we need to
6662 * strip them otherwise we'll get trouble for cookie captures,
6663 * or even for rewrites. Since this happens extremely rarely,
6664 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006665 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6667 int stripped_before = 0;
6668 int stripped_after = 0;
6669
6670 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006671 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672 equal += stripped_before;
6673 val_beg += stripped_before;
6674 }
6675
6676 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006677 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 val_beg += stripped_after;
6679 stripped_before += stripped_after;
6680 }
6681
6682 val_end += stripped_before;
6683 next += stripped_before;
6684 hdr_end += stripped_before;
6685 hdr_next += stripped_before;
6686 cur_hdr->len += stripped_before;
6687 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006688 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006690
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006691 /* First, let's see if we want to capture this cookie. We check
6692 * that we don't already have a client side cookie, because we
6693 * can only capture one. Also as an optimisation, we ignore
6694 * cookies shorter than the declared name.
6695 */
6696 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6697 (val_end - att_beg >= t->fe->capture_namelen) &&
6698 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6699 int log_len = val_end - att_beg;
6700
6701 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6702 Alert("HTTP logging : out of memory.\n");
6703 } else {
6704 if (log_len > t->fe->capture_len)
6705 log_len = t->fe->capture_len;
6706 memcpy(txn->cli_cookie, att_beg, log_len);
6707 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006708 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710
Willy Tarreaubca99692010-10-06 19:25:55 +02006711 /* Persistence cookies in passive, rewrite or insert mode have the
6712 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006714 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006716 * For cookies in prefix mode, the form is :
6717 *
6718 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 */
6720 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6721 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6722 struct server *srv = t->be->srv;
6723 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006724
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006725 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6726 * have the server ID between val_beg and delim, and the original cookie between
6727 * delim+1 and val_end. Otherwise, delim==val_end :
6728 *
6729 * Cookie: NAME=SRV; # in all but prefix modes
6730 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6731 * | || || | |+-> next
6732 * | || || | +--> val_end
6733 * | || || +---------> delim
6734 * | || |+------------> val_beg
6735 * | || +-------------> att_end = equal
6736 * | |+-----------------> att_beg
6737 * | +------------------> prev
6738 * +-------------------------> hdr_beg
6739 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740
Willy Tarreau67402132012-05-31 20:40:20 +02006741 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 for (delim = val_beg; delim < val_end; delim++)
6743 if (*delim == COOKIE_DELIM)
6744 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006745 } else {
6746 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006747 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006748 /* Now check if the cookie contains a date field, which would
6749 * appear after a vertical bar ('|') just after the server name
6750 * and before the delimiter.
6751 */
6752 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6753 if (vbar1) {
6754 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006755 * right is the last seen date. It is a base64 encoded
6756 * 30-bit value representing the UNIX date since the
6757 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006758 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006759 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006760 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006761 if (val_end - vbar1 >= 5) {
6762 val = b64tos30(vbar1);
6763 if (val > 0)
6764 txn->cookie_last_date = val << 2;
6765 }
6766 /* look for a second vertical bar */
6767 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6768 if (vbar1 && (val_end - vbar1 > 5)) {
6769 val = b64tos30(vbar1 + 1);
6770 if (val > 0)
6771 txn->cookie_first_date = val << 2;
6772 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006773 }
6774 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006775
Willy Tarreauf64d1412010-10-07 20:06:11 +02006776 /* if the cookie has an expiration date and the proxy wants to check
6777 * it, then we do that now. We first check if the cookie is too old,
6778 * then only if it has expired. We detect strict overflow because the
6779 * time resolution here is not great (4 seconds). Cookies with dates
6780 * in the future are ignored if their offset is beyond one day. This
6781 * allows an admin to fix timezone issues without expiring everyone
6782 * and at the same time avoids keeping unwanted side effects for too
6783 * long.
6784 */
6785 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006786 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6787 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006788 txn->flags &= ~TX_CK_MASK;
6789 txn->flags |= TX_CK_OLD;
6790 delim = val_beg; // let's pretend we have not found the cookie
6791 txn->cookie_first_date = 0;
6792 txn->cookie_last_date = 0;
6793 }
6794 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006795 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6796 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006797 txn->flags &= ~TX_CK_MASK;
6798 txn->flags |= TX_CK_EXPIRED;
6799 delim = val_beg; // let's pretend we have not found the cookie
6800 txn->cookie_first_date = 0;
6801 txn->cookie_last_date = 0;
6802 }
6803
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 /* Here, we'll look for the first running server which supports the cookie.
6805 * This allows to share a same cookie between several servers, for example
6806 * to dedicate backup servers to specific servers only.
6807 * However, to prevent clients from sticking to cookie-less backup server
6808 * when they have incidentely learned an empty cookie, we simply ignore
6809 * empty cookies and mark them as invalid.
6810 * The same behaviour is applied when persistence must be ignored.
6811 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006812 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006813 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006814
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006815 while (srv) {
6816 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6817 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6818 if ((srv->state & SRV_RUNNING) ||
6819 (t->be->options & PR_O_PERSIST) ||
6820 (t->flags & SN_FORCE_PRST)) {
6821 /* we found the server and we can use it */
6822 txn->flags &= ~TX_CK_MASK;
6823 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6824 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006825 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006826 break;
6827 } else {
6828 /* we found a server, but it's down,
6829 * mark it as such and go on in case
6830 * another one is available.
6831 */
6832 txn->flags &= ~TX_CK_MASK;
6833 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006834 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006835 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 srv = srv->next;
6837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838
Willy Tarreauf64d1412010-10-07 20:06:11 +02006839 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006840 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006841 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006842 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6843 txn->flags |= TX_CK_UNUSED;
6844 else
6845 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006847
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006848 /* depending on the cookie mode, we may have to either :
6849 * - delete the complete cookie if we're in insert+indirect mode, so that
6850 * the server never sees it ;
6851 * - remove the server id from the cookie value, and tag the cookie as an
6852 * application cookie so that it does not get accidentely removed later,
6853 * if we're in cookie prefix mode
6854 */
Willy Tarreau67402132012-05-31 20:40:20 +02006855 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006857
Willy Tarreau9b28e032012-10-12 23:49:43 +02006858 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006859 val_end += delta;
6860 next += delta;
6861 hdr_end += delta;
6862 hdr_next += delta;
6863 cur_hdr->len += delta;
6864 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 del_from = NULL;
6867 preserve_hdr = 1; /* we want to keep this cookie */
6868 }
6869 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006870 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 del_from = prev;
6872 }
6873 } else {
6874 /* This is not our cookie, so we must preserve it. But if we already
6875 * scheduled another cookie for removal, we cannot remove the
6876 * complete header, but we can remove the previous block itself.
6877 */
6878 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006879
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006880 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006881 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006882 if (att_beg >= del_from)
6883 att_beg += delta;
6884 if (att_end >= del_from)
6885 att_end += delta;
6886 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006887 val_end += delta;
6888 next += delta;
6889 hdr_end += delta;
6890 hdr_next += delta;
6891 cur_hdr->len += delta;
6892 http_msg_move_end(&txn->req, delta);
6893 prev = del_from;
6894 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006895 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 /* Look for the appsession cookie unless persistence must be ignored */
6899 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6900 int cmp_len, value_len;
6901 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006902
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 if (t->be->options2 & PR_O2_AS_PFX) {
6904 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6905 value_begin = att_beg + t->be->appsession_name_len;
6906 value_len = val_end - att_beg - t->be->appsession_name_len;
6907 } else {
6908 cmp_len = att_end - att_beg;
6909 value_begin = val_beg;
6910 value_len = val_end - val_beg;
6911 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 /* let's see if the cookie is our appcookie */
6914 if (cmp_len == t->be->appsession_name_len &&
6915 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6916 manage_client_side_appsession(t, value_begin, value_len);
6917 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006918 }
6919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 /* continue with next cookie on this header line */
6921 att_beg = next;
6922 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 /* There are no more cookies on this line.
6925 * We may still have one (or several) marked for deletion at the
6926 * end of the line. We must do this now in two ways :
6927 * - if some cookies must be preserved, we only delete from the
6928 * mark to the end of line ;
6929 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006931 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006932 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006934 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006936 cur_hdr->len += delta;
6937 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006938 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006939
6940 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006941 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6942 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006944 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006947 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006948 }
6949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006951 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006953}
6954
6955
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956/* Iterate the same filter through all response headers contained in <rtr>.
6957 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6958 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006959int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960{
6961 char term;
6962 char *cur_ptr, *cur_end, *cur_next;
6963 int cur_idx, old_idx, last_hdr;
6964 struct http_txn *txn = &t->txn;
6965 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006966 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967
6968 last_hdr = 0;
6969
Willy Tarreau9b28e032012-10-12 23:49:43 +02006970 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971 old_idx = 0;
6972
6973 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006974 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006976 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977 (exp->action == ACT_ALLOW ||
6978 exp->action == ACT_DENY))
6979 return 0;
6980
6981 cur_idx = txn->hdr_idx.v[old_idx].next;
6982 if (!cur_idx)
6983 break;
6984
6985 cur_hdr = &txn->hdr_idx.v[cur_idx];
6986 cur_ptr = cur_next;
6987 cur_end = cur_ptr + cur_hdr->len;
6988 cur_next = cur_end + cur_hdr->cr + 1;
6989
6990 /* Now we have one header between cur_ptr and cur_end,
6991 * and the next header starts at cur_next.
6992 */
6993
6994 /* The annoying part is that pattern matching needs
6995 * that we modify the contents to null-terminate all
6996 * strings before testing them.
6997 */
6998
6999 term = *cur_end;
7000 *cur_end = '\0';
7001
7002 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7003 switch (exp->action) {
7004 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007005 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 last_hdr = 1;
7007 break;
7008
7009 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007010 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 last_hdr = 1;
7012 break;
7013
7014 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007015 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7016 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017 /* FIXME: if the user adds a newline in the replacement, the
7018 * index will not be recalculated for now, and the new line
7019 * will not be counted as a new header.
7020 */
7021
7022 cur_end += delta;
7023 cur_next += delta;
7024 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007025 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026 break;
7027
7028 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007029 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 cur_next += delta;
7031
Willy Tarreaufa355d42009-11-29 18:12:29 +01007032 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7034 txn->hdr_idx.used--;
7035 cur_hdr->len = 0;
7036 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007037 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007038 break;
7039
7040 }
7041 }
7042 if (cur_end)
7043 *cur_end = term; /* restore the string terminator */
7044
7045 /* keep the link from this header to next one in case of later
7046 * removal of next header.
7047 */
7048 old_idx = cur_idx;
7049 }
7050 return 0;
7051}
7052
7053
7054/* Apply the filter to the status line in the response buffer <rtr>.
7055 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7056 * or -1 if a replacement resulted in an invalid status line.
7057 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007058int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059{
7060 char term;
7061 char *cur_ptr, *cur_end;
7062 int done;
7063 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007064 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065
7066
Willy Tarreau3d300592007-03-18 18:34:41 +01007067 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007069 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007070 (exp->action == ACT_ALLOW ||
7071 exp->action == ACT_DENY))
7072 return 0;
7073 else if (exp->action == ACT_REMOVE)
7074 return 0;
7075
7076 done = 0;
7077
Willy Tarreau9b28e032012-10-12 23:49:43 +02007078 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007079 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080
7081 /* Now we have the status line between cur_ptr and cur_end */
7082
7083 /* The annoying part is that pattern matching needs
7084 * that we modify the contents to null-terminate all
7085 * strings before testing them.
7086 */
7087
7088 term = *cur_end;
7089 *cur_end = '\0';
7090
7091 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7092 switch (exp->action) {
7093 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007094 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 done = 1;
7096 break;
7097
7098 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007099 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 done = 1;
7101 break;
7102
7103 case ACT_REPLACE:
7104 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007105 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7106 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107 /* FIXME: if the user adds a newline in the replacement, the
7108 * index will not be recalculated for now, and the new line
7109 * will not be counted as a new header.
7110 */
7111
Willy Tarreaufa355d42009-11-29 18:12:29 +01007112 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007114 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007115 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 cur_ptr, cur_end + 1,
7117 NULL, NULL);
7118 if (unlikely(!cur_end))
7119 return -1;
7120
7121 /* we have a full respnse and we know that we have either a CR
7122 * or an LF at <ptr>.
7123 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007124 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007125 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126 /* there is no point trying this regex on headers */
7127 return 1;
7128 }
7129 }
7130 *cur_end = term; /* restore the string terminator */
7131 return done;
7132}
7133
7134
7135
7136/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007137 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7139 * unparsable response.
7140 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007141int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007143 struct http_txn *txn = &s->txn;
7144 struct hdr_exp *exp;
7145
7146 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147 int ret;
7148
7149 /*
7150 * The interleaving of transformations and verdicts
7151 * makes it difficult to decide to continue or stop
7152 * the evaluation.
7153 */
7154
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007155 if (txn->flags & TX_SVDENY)
7156 break;
7157
Willy Tarreau3d300592007-03-18 18:34:41 +01007158 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7160 exp->action == ACT_PASS)) {
7161 exp = exp->next;
7162 continue;
7163 }
7164
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007165 /* if this filter had a condition, evaluate it now and skip to
7166 * next filter if the condition does not match.
7167 */
7168 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007169 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007170 ret = acl_pass(ret);
7171 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7172 ret = !ret;
7173 if (!ret)
7174 continue;
7175 }
7176
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007178 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 if (unlikely(ret < 0))
7180 return -1;
7181
7182 if (likely(ret == 0)) {
7183 /* The filter did not match the response, it can be
7184 * iterated through all headers.
7185 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007186 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 }
7189 return 0;
7190}
7191
7192
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007194 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007195 * desirable to call it only when needed. This function is also used when we
7196 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007198void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007199{
7200 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007201 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007202 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007204 char *hdr_beg, *hdr_end, *hdr_next;
7205 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 /* Iterate through the headers.
7208 * we start with the start line.
7209 */
7210 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007211 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212
7213 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7214 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007215 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216
7217 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007218 hdr_beg = hdr_next;
7219 hdr_end = hdr_beg + cur_hdr->len;
7220 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221
Willy Tarreau24581ba2010-08-31 22:39:35 +02007222 /* We have one full header between hdr_beg and hdr_end, and the
7223 * next header starts at hdr_next. We're only interested in
7224 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 */
7226
Willy Tarreau24581ba2010-08-31 22:39:35 +02007227 is_cookie2 = 0;
7228 prev = hdr_beg + 10;
7229 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007230 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007231 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7232 if (!val) {
7233 old_idx = cur_idx;
7234 continue;
7235 }
7236 is_cookie2 = 1;
7237 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 }
7239
Willy Tarreau24581ba2010-08-31 22:39:35 +02007240 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7241 * <prev> points to the colon.
7242 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007243 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7246 * check-cache is enabled) and we are not interested in checking
7247 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007248 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007249 if (t->be->cookie_name == NULL &&
7250 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007251 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007252 return;
7253
Willy Tarreau24581ba2010-08-31 22:39:35 +02007254 /* OK so now we know we have to process this response cookie.
7255 * The format of the Set-Cookie header is slightly different
7256 * from the format of the Cookie header in that it does not
7257 * support the comma as a cookie delimiter (thus the header
7258 * cannot be folded) because the Expires attribute described in
7259 * the original Netscape's spec may contain an unquoted date
7260 * with a comma inside. We have to live with this because
7261 * many browsers don't support Max-Age and some browsers don't
7262 * support quoted strings. However the Set-Cookie2 header is
7263 * clean.
7264 *
7265 * We have to keep multiple pointers in order to support cookie
7266 * removal at the beginning, middle or end of header without
7267 * corrupting the header (in case of set-cookie2). A special
7268 * pointer, <scav> points to the beginning of the set-cookie-av
7269 * fields after the first semi-colon. The <next> pointer points
7270 * either to the end of line (set-cookie) or next unquoted comma
7271 * (set-cookie2). All of these headers are valid :
7272 *
7273 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7274 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7275 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7276 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7277 * | | | | | | | | | |
7278 * | | | | | | | | +-> next hdr_end <--+
7279 * | | | | | | | +------------> scav
7280 * | | | | | | +--------------> val_end
7281 * | | | | | +--------------------> val_beg
7282 * | | | | +----------------------> equal
7283 * | | | +------------------------> att_end
7284 * | | +----------------------------> att_beg
7285 * | +------------------------------> prev
7286 * +-----------------------------------------> hdr_beg
7287 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288
Willy Tarreau24581ba2010-08-31 22:39:35 +02007289 for (; prev < hdr_end; prev = next) {
7290 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291
Willy Tarreau24581ba2010-08-31 22:39:35 +02007292 /* find att_beg */
7293 att_beg = prev + 1;
7294 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7295 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296
Willy Tarreau24581ba2010-08-31 22:39:35 +02007297 /* find att_end : this is the first character after the last non
7298 * space before the equal. It may be equal to hdr_end.
7299 */
7300 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301
Willy Tarreau24581ba2010-08-31 22:39:35 +02007302 while (equal < hdr_end) {
7303 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7304 break;
7305 if (http_is_spht[(unsigned char)*equal++])
7306 continue;
7307 att_end = equal;
7308 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
Willy Tarreau24581ba2010-08-31 22:39:35 +02007310 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7311 * is between <att_beg> and <equal>, both may be identical.
7312 */
7313
7314 /* look for end of cookie if there is an equal sign */
7315 if (equal < hdr_end && *equal == '=') {
7316 /* look for the beginning of the value */
7317 val_beg = equal + 1;
7318 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7319 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320
Willy Tarreau24581ba2010-08-31 22:39:35 +02007321 /* find the end of the value, respecting quotes */
7322 next = find_cookie_value_end(val_beg, hdr_end);
7323
7324 /* make val_end point to the first white space or delimitor after the value */
7325 val_end = next;
7326 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7327 val_end--;
7328 } else {
7329 /* <equal> points to next comma, semi-colon or EOL */
7330 val_beg = val_end = next = equal;
7331 }
7332
7333 if (next < hdr_end) {
7334 /* Set-Cookie2 supports multiple cookies, and <next> points to
7335 * a colon or semi-colon before the end. So skip all attr-value
7336 * pairs and look for the next comma. For Set-Cookie, since
7337 * commas are permitted in values, skip to the end.
7338 */
7339 if (is_cookie2)
7340 next = find_hdr_value_end(next, hdr_end);
7341 else
7342 next = hdr_end;
7343 }
7344
7345 /* Now everything is as on the diagram above */
7346
7347 /* Ignore cookies with no equal sign */
7348 if (equal == val_end)
7349 continue;
7350
7351 /* If there are spaces around the equal sign, we need to
7352 * strip them otherwise we'll get trouble for cookie captures,
7353 * or even for rewrites. Since this happens extremely rarely,
7354 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007356 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7357 int stripped_before = 0;
7358 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359
Willy Tarreau24581ba2010-08-31 22:39:35 +02007360 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007361 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007362 equal += stripped_before;
7363 val_beg += stripped_before;
7364 }
7365
7366 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007367 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 val_beg += stripped_after;
7369 stripped_before += stripped_after;
7370 }
7371
7372 val_end += stripped_before;
7373 next += stripped_before;
7374 hdr_end += stripped_before;
7375 hdr_next += stripped_before;
7376 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007377 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007378 }
7379
7380 /* First, let's see if we want to capture this cookie. We check
7381 * that we don't already have a server side cookie, because we
7382 * can only capture one. Also as an optimisation, we ignore
7383 * cookies shorter than the declared name.
7384 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007385 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007386 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007387 (val_end - att_beg >= t->fe->capture_namelen) &&
7388 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7389 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007390 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 Alert("HTTP logging : out of memory.\n");
7392 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007393 else {
7394 if (log_len > t->fe->capture_len)
7395 log_len = t->fe->capture_len;
7396 memcpy(txn->srv_cookie, att_beg, log_len);
7397 txn->srv_cookie[log_len] = 0;
7398 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399 }
7400
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007401 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 if (!(t->flags & SN_IGNORE_PRST) &&
7404 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7405 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007406 /* assume passive cookie by default */
7407 txn->flags &= ~TX_SCK_MASK;
7408 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409
7410 /* If the cookie is in insert mode on a known server, we'll delete
7411 * this occurrence because we'll insert another one later.
7412 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 * a direct access.
7414 */
Willy Tarreau67402132012-05-31 20:40:20 +02007415 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007416 /* The "preserve" flag was set, we don't want to touch the
7417 * server's cookie.
7418 */
7419 }
Willy Tarreau67402132012-05-31 20:40:20 +02007420 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7421 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007422 /* this cookie must be deleted */
7423 if (*prev == ':' && next == hdr_end) {
7424 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007425 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007426 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7427 txn->hdr_idx.used--;
7428 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007429 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007430 hdr_next += delta;
7431 http_msg_move_end(&txn->rsp, delta);
7432 /* note: while both invalid now, <next> and <hdr_end>
7433 * are still equal, so the for() will stop as expected.
7434 */
7435 } else {
7436 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007437 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007438 next = prev;
7439 hdr_end += delta;
7440 hdr_next += delta;
7441 cur_hdr->len += delta;
7442 http_msg_move_end(&txn->rsp, delta);
7443 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007444 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007445 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007446 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 }
Willy Tarreau67402132012-05-31 20:40:20 +02007448 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007449 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450 * with this server since we know it.
7451 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007452 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 next += delta;
7454 hdr_end += delta;
7455 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007457 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007458
Willy Tarreauf1348312010-10-07 15:54:11 +02007459 txn->flags &= ~TX_SCK_MASK;
7460 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007461 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007462 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007463 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007464 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007466 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 next += delta;
7468 hdr_end += delta;
7469 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007470 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007471 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007472
Willy Tarreau827aee92011-03-10 16:55:02 +01007473 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007474 txn->flags &= ~TX_SCK_MASK;
7475 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007476 }
7477 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007478 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7479 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007480 int cmp_len, value_len;
7481 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482
Cyril Bontéb21570a2009-11-29 20:04:48 +01007483 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007484 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7485 value_begin = att_beg + t->be->appsession_name_len;
7486 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007487 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 cmp_len = att_end - att_beg;
7489 value_begin = val_beg;
7490 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007491 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007492
Cyril Bonté17530c32010-04-06 21:11:10 +02007493 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007494 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7495 /* free a possibly previously allocated memory */
7496 pool_free2(apools.sessid, txn->sessid);
7497
Cyril Bontéb21570a2009-11-29 20:04:48 +01007498 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007499 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007500 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7501 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7502 return;
7503 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007504 memcpy(txn->sessid, value_begin, value_len);
7505 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007507 }
7508 /* that's done for this cookie, check the next one on the same
7509 * line when next != hdr_end (only if is_cookie2).
7510 */
7511 }
7512 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007514 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007515
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007516 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007517 appsess *asession = NULL;
7518 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007519 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007520 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007521 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007522 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7523 Alert("Not enough Memory process_srv():asession:calloc().\n");
7524 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7525 return;
7526 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007527 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7528
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007529 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7530 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7531 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007532 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007533 return;
7534 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007535 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007536 asession->sessid[t->be->appsession_len] = 0;
7537
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007538 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007539 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007540 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007541 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007542 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007543 return;
7544 }
7545 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007546 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007547
7548 asession->request_count = 0;
7549 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7550 }
7551
7552 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7553 asession->request_count++;
7554 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555}
7556
7557
Willy Tarreaua15645d2007-03-18 16:22:39 +01007558/*
7559 * Check if response is cacheable or not. Updates t->flags.
7560 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007561void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562{
7563 struct http_txn *txn = &t->txn;
7564 char *p1, *p2;
7565
7566 char *cur_ptr, *cur_end, *cur_next;
7567 int cur_idx;
7568
Willy Tarreau5df51872007-11-25 16:20:08 +01007569 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570 return;
7571
7572 /* Iterate through the headers.
7573 * we start with the start line.
7574 */
7575 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007576 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577
7578 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7579 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007580 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007581
7582 cur_hdr = &txn->hdr_idx.v[cur_idx];
7583 cur_ptr = cur_next;
7584 cur_end = cur_ptr + cur_hdr->len;
7585 cur_next = cur_end + cur_hdr->cr + 1;
7586
7587 /* We have one full header between cur_ptr and cur_end, and the
7588 * next header starts at cur_next. We're only interested in
7589 * "Cookie:" headers.
7590 */
7591
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007592 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7593 if (val) {
7594 if ((cur_end - (cur_ptr + val) >= 8) &&
7595 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7596 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7597 return;
7598 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599 }
7600
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007601 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7602 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 continue;
7604
7605 /* OK, right now we know we have a cache-control header at cur_ptr */
7606
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007607 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608
7609 if (p1 >= cur_end) /* no more info */
7610 continue;
7611
7612 /* p1 is at the beginning of the value */
7613 p2 = p1;
7614
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007615 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 p2++;
7617
7618 /* we have a complete value between p1 and p2 */
7619 if (p2 < cur_end && *p2 == '=') {
7620 /* we have something of the form no-cache="set-cookie" */
7621 if ((cur_end - p1 >= 21) &&
7622 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7623 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007624 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625 continue;
7626 }
7627
7628 /* OK, so we know that either p2 points to the end of string or to a comma */
7629 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7630 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7631 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7632 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007633 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007634 return;
7635 }
7636
7637 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007638 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639 continue;
7640 }
7641 }
7642}
7643
7644
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645/*
7646 * Try to retrieve a known appsession in the URI, then the associated server.
7647 * If the server is found, it's assigned to the session.
7648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007649void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007651 char *end_params, *first_param, *cur_param, *next_param;
7652 char separator;
7653 int value_len;
7654
7655 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007657 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007658 (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 +01007659 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007660 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661
Cyril Bontéb21570a2009-11-29 20:04:48 +01007662 first_param = NULL;
7663 switch (mode) {
7664 case PR_O2_AS_M_PP:
7665 first_param = memchr(begin, ';', len);
7666 break;
7667 case PR_O2_AS_M_QS:
7668 first_param = memchr(begin, '?', len);
7669 break;
7670 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671
Cyril Bontéb21570a2009-11-29 20:04:48 +01007672 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007674 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675
Cyril Bontéb21570a2009-11-29 20:04:48 +01007676 switch (mode) {
7677 case PR_O2_AS_M_PP:
7678 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7679 end_params = (char *) begin + len;
7680 }
7681 separator = ';';
7682 break;
7683 case PR_O2_AS_M_QS:
7684 end_params = (char *) begin + len;
7685 separator = '&';
7686 break;
7687 default:
7688 /* unknown mode, shouldn't happen */
7689 return;
7690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Cyril Bontéb21570a2009-11-29 20:04:48 +01007692 cur_param = next_param = end_params;
7693 while (cur_param > first_param) {
7694 cur_param--;
7695 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7696 /* let's see if this is the appsession parameter */
7697 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7698 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7699 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7700 /* Cool... it's the right one */
7701 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7702 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7703 if (value_len > 0) {
7704 manage_client_side_appsession(t, cur_param, value_len);
7705 }
7706 break;
7707 }
7708 next_param = cur_param;
7709 }
7710 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007711#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007712 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007713 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715}
7716
Willy Tarreaub2513902006-12-17 14:52:38 +01007717/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007718 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007719 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007720 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007721 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007722 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007723 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007724 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007725 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007726int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007727{
7728 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007729 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007730 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007731 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007732
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007733 if (!uri_auth)
7734 return 0;
7735
Cyril Bonté70be45d2010-10-12 00:14:35 +02007736 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007737 return 0;
7738
Willy Tarreau295a8372011-03-10 11:25:07 +01007739 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007740 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007741 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007743 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007744 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007745 return 0;
7746
Willy Tarreau3a215be2012-03-09 21:39:51 +01007747 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007748 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007749 return 0;
7750
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007751 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007752 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007753 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007754 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007755 break;
7756 }
7757 h++;
7758 }
7759
7760 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007761 h = uri + uri_auth->uri_len;
7762 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007763 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007764 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007765 break;
7766 }
7767 h++;
7768 }
7769 }
7770
Willy Tarreau3a215be2012-03-09 21:39:51 +01007771 h = uri + uri_auth->uri_len;
7772 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007773 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007774 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007775 break;
7776 }
7777 h++;
7778 }
7779
Willy Tarreau3a215be2012-03-09 21:39:51 +01007780 h = uri + uri_auth->uri_len;
7781 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007782 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007783 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007784 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007785 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007786 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7787 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7788 si->applet.ctx.stats.st_code = i;
7789 break;
7790 }
7791 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007792 break;
7793 }
7794 h++;
7795 }
Willy Tarreaub2513902006-12-17 14:52:38 +01007796 return 1;
7797}
7798
Willy Tarreau4076a152009-04-02 15:18:36 +02007799/*
7800 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007801 * By default it tries to report the error position as msg->err_pos. However if
7802 * this one is not set, it will then report msg->next, which is the last known
7803 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007804 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007805 */
7806void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007807 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007808 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007809{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007810 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007811 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007812
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 es->len = MIN(chn->buf->i, sizeof(es->buf));
7814 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007815 len1 = MIN(len1, es->len);
7816 len2 = es->len - len1; /* remaining data if buffer wraps */
7817
Willy Tarreau9b28e032012-10-12 23:49:43 +02007818 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007819 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007820 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007821
Willy Tarreau4076a152009-04-02 15:18:36 +02007822 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007823 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007824 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007825 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007826
Willy Tarreau4076a152009-04-02 15:18:36 +02007827 es->when = date; // user-visible date
7828 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007829 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007830 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007831 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007832 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007833 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007834 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007835 es->s_flags = s->flags;
7836 es->t_flags = s->txn.flags;
7837 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007838 es->b_out = chn->buf->o;
7839 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007840 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007841 es->m_clen = msg->chunk_len;
7842 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007843}
Willy Tarreaub2513902006-12-17 14:52:38 +01007844
Willy Tarreau294c4732011-12-16 21:35:50 +01007845/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7846 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7847 * performed over the whole headers. Otherwise it must contain a valid header
7848 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7849 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7850 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7851 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7852 * -1.
7853 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007854 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007855unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007856 struct hdr_idx *idx, int occ,
7857 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007858{
Willy Tarreau294c4732011-12-16 21:35:50 +01007859 struct hdr_ctx local_ctx;
7860 char *ptr_hist[MAX_HDR_HISTORY];
7861 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007862 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007863 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007864
Willy Tarreau294c4732011-12-16 21:35:50 +01007865 if (!ctx) {
7866 local_ctx.idx = 0;
7867 ctx = &local_ctx;
7868 }
7869
Willy Tarreaubce70882009-09-07 11:51:47 +02007870 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007871 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007872 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007873 occ--;
7874 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007875 *vptr = ctx->line + ctx->val;
7876 *vlen = ctx->vlen;
7877 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007878 }
7879 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007880 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007881 }
7882
7883 /* negative occurrence, we scan all the list then walk back */
7884 if (-occ > MAX_HDR_HISTORY)
7885 return 0;
7886
Willy Tarreau294c4732011-12-16 21:35:50 +01007887 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007888 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007889 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7890 len_hist[hist_ptr] = ctx->vlen;
7891 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007892 hist_ptr = 0;
7893 found++;
7894 }
7895 if (-occ > found)
7896 return 0;
7897 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7898 * find occurrence -occ, so we have to check [hist_ptr+occ].
7899 */
7900 hist_ptr += occ;
7901 if (hist_ptr >= MAX_HDR_HISTORY)
7902 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007903 *vptr = ptr_hist[hist_ptr];
7904 *vlen = len_hist[hist_ptr];
7905 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007906}
7907
Willy Tarreaubaaee002006-06-26 02:48:02 +02007908/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007909 * Print a debug line with a header. Always stop at the first CR or LF char,
7910 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7911 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912 */
7913void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7914{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007915 int max;
7916 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007917 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7918 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007919
7920 for (max = 0; start + max < end; max++)
7921 if (start[max] == '\r' || start[max] == '\n')
7922 break;
7923
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007924 UBOUND(max, trash.size - trash.len - 3);
7925 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7926 trash.str[trash.len++] = '\n';
7927 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007928}
7929
Willy Tarreau0937bc42009-12-22 15:03:09 +01007930/*
7931 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7932 * the required fields are properly allocated and that we only need to (re)init
7933 * them. This should be used before processing any new request.
7934 */
7935void http_init_txn(struct session *s)
7936{
7937 struct http_txn *txn = &s->txn;
7938 struct proxy *fe = s->fe;
7939
7940 txn->flags = 0;
7941 txn->status = -1;
7942
William Lallemand5f232402012-04-05 18:02:55 +02007943 global.req_count++;
7944
Willy Tarreauf64d1412010-10-07 20:06:11 +02007945 txn->cookie_first_date = 0;
7946 txn->cookie_last_date = 0;
7947
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007948 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007949 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007950 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007951 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007952 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007953 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007954 txn->req.chunk_len = 0LL;
7955 txn->req.body_len = 0LL;
7956 txn->rsp.chunk_len = 0LL;
7957 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007958 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7959 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007960 txn->req.chn = s->req;
7961 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007962
7963 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007964
7965 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7966 if (fe->options2 & PR_O2_REQBUG_OK)
7967 txn->req.err_pos = -1; /* let buggy requests pass */
7968
Willy Tarreau46023632010-01-07 22:51:47 +01007969 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007970 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7971
Willy Tarreau46023632010-01-07 22:51:47 +01007972 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007973 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7974
7975 if (txn->hdr_idx.v)
7976 hdr_idx_init(&txn->hdr_idx);
7977}
7978
7979/* to be used at the end of a transaction */
7980void http_end_txn(struct session *s)
7981{
7982 struct http_txn *txn = &s->txn;
7983
7984 /* these ones will have been dynamically allocated */
7985 pool_free2(pool2_requri, txn->uri);
7986 pool_free2(pool2_capture, txn->cli_cookie);
7987 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007988 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007989 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007990
William Lallemanda73203e2012-03-12 12:48:57 +01007991 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007992 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007993 txn->uri = NULL;
7994 txn->srv_cookie = NULL;
7995 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007996
7997 if (txn->req.cap) {
7998 struct cap_hdr *h;
7999 for (h = s->fe->req_cap; h; h = h->next)
8000 pool_free2(h->pool, txn->req.cap[h->index]);
8001 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8002 }
8003
8004 if (txn->rsp.cap) {
8005 struct cap_hdr *h;
8006 for (h = s->fe->rsp_cap; h; h = h->next)
8007 pool_free2(h->pool, txn->rsp.cap[h->index]);
8008 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8009 }
8010
Willy Tarreau0937bc42009-12-22 15:03:09 +01008011}
8012
8013/* to be used at the end of a transaction to prepare a new one */
8014void http_reset_txn(struct session *s)
8015{
8016 http_end_txn(s);
8017 http_init_txn(s);
8018
8019 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008020 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008021 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008022 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008023 /* re-init store persistence */
8024 s->store_count = 0;
8025
Willy Tarreau0937bc42009-12-22 15:03:09 +01008026 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008027
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008028 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008029
Willy Tarreau739cfba2010-01-25 23:11:14 +01008030 /* We must trim any excess data from the response buffer, because we
8031 * may have blocked an invalid response from a server that we don't
8032 * want to accidentely forward once we disable the analysers, nor do
8033 * we want those data to come along with next response. A typical
8034 * example of such data would be from a buggy server responding to
8035 * a HEAD with some data, or sending more than the advertised
8036 * content-length.
8037 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008038 if (unlikely(s->rep->buf->i))
8039 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008040
Willy Tarreau0937bc42009-12-22 15:03:09 +01008041 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008042 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008043
Willy Tarreaud04e8582010-05-31 12:31:35 +02008044 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008045 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008046
8047 s->req->rex = TICK_ETERNITY;
8048 s->req->wex = TICK_ETERNITY;
8049 s->req->analyse_exp = TICK_ETERNITY;
8050 s->rep->rex = TICK_ETERNITY;
8051 s->rep->wex = TICK_ETERNITY;
8052 s->rep->analyse_exp = TICK_ETERNITY;
8053}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008054
Willy Tarreauff011f22011-01-06 17:51:27 +01008055void free_http_req_rules(struct list *r) {
8056 struct http_req_rule *tr, *pr;
8057
8058 list_for_each_entry_safe(pr, tr, r, list) {
8059 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008060 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008061 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008062
8063 free(pr);
8064 }
8065}
8066
8067struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8068{
8069 struct http_req_rule *rule;
8070 int cur_arg;
8071
8072 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8073 if (!rule) {
8074 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008075 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008076 }
8077
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008078 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008079 rule->action = HTTP_REQ_ACT_ALLOW;
8080 cur_arg = 1;
8081 } else if (!strcmp(args[0], "deny")) {
8082 rule->action = HTTP_REQ_ACT_DENY;
8083 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008084 } else if (!strcmp(args[0], "tarpit")) {
8085 rule->action = HTTP_REQ_ACT_TARPIT;
8086 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008087 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008088 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008089 cur_arg = 1;
8090
8091 while(*args[cur_arg]) {
8092 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008093 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008094 cur_arg+=2;
8095 continue;
8096 } else
8097 break;
8098 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008099 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8100 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8101 cur_arg = 1;
8102
8103 if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) {
8104 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8105 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008106 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008107 }
8108
8109 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8110 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8111 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreau3bfeadb2013-03-24 07:33:22 +01008112 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008113 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008114 } else if (strcmp(args[0], "redirect") == 0) {
8115 struct redirect_rule *redir;
8116 char *errmsg;
8117
8118 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8119 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8120 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8121 goto out_err;
8122 }
8123
8124 /* this redirect rule might already contain a parsed condition which
8125 * we'll pass to the http-request rule.
8126 */
8127 rule->action = HTTP_REQ_ACT_REDIR;
8128 rule->arg.redir = redir;
8129 rule->cond = redir->cond;
8130 redir->cond = NULL;
8131 cur_arg = 2;
8132 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008133 } else {
Baptiste Assmann116eefe2013-01-05 16:02:07 +01008134 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008135 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008136 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008137 }
8138
8139 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8140 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008141 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008142
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008143 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8144 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8145 file, linenum, args[0], errmsg);
8146 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008147 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008148 }
8149 rule->cond = cond;
8150 }
8151 else if (*args[cur_arg]) {
8152 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8153 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8154 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008155 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008156 }
8157
8158 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008159 out_err:
8160 free(rule);
8161 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008162}
8163
Willy Tarreau4baae242012-12-27 12:00:31 +01008164/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8165 * with <err> filled with the error message.
8166 */
8167struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8168 const char **args, char **errmsg)
8169{
8170 struct redirect_rule *rule;
8171 int cur_arg;
8172 int type = REDIRECT_TYPE_NONE;
8173 int code = 302;
8174 const char *destination = NULL;
8175 const char *cookie = NULL;
8176 int cookie_set = 0;
8177 unsigned int flags = REDIRECT_FLAG_NONE;
8178 struct acl_cond *cond = NULL;
8179
8180 cur_arg = 0;
8181 while (*(args[cur_arg])) {
8182 if (strcmp(args[cur_arg], "location") == 0) {
8183 if (!*args[cur_arg + 1])
8184 goto missing_arg;
8185
8186 type = REDIRECT_TYPE_LOCATION;
8187 cur_arg++;
8188 destination = args[cur_arg];
8189 }
8190 else if (strcmp(args[cur_arg], "prefix") == 0) {
8191 if (!*args[cur_arg + 1])
8192 goto missing_arg;
8193
8194 type = REDIRECT_TYPE_PREFIX;
8195 cur_arg++;
8196 destination = args[cur_arg];
8197 }
8198 else if (strcmp(args[cur_arg], "scheme") == 0) {
8199 if (!*args[cur_arg + 1])
8200 goto missing_arg;
8201
8202 type = REDIRECT_TYPE_SCHEME;
8203 cur_arg++;
8204 destination = args[cur_arg];
8205 }
8206 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8207 if (!*args[cur_arg + 1])
8208 goto missing_arg;
8209
8210 cur_arg++;
8211 cookie = args[cur_arg];
8212 cookie_set = 1;
8213 }
8214 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8215 if (!*args[cur_arg + 1])
8216 goto missing_arg;
8217
8218 cur_arg++;
8219 cookie = args[cur_arg];
8220 cookie_set = 0;
8221 }
8222 else if (strcmp(args[cur_arg], "code") == 0) {
8223 if (!*args[cur_arg + 1])
8224 goto missing_arg;
8225
8226 cur_arg++;
8227 code = atol(args[cur_arg]);
8228 if (code < 301 || code > 303) {
8229 memprintf(errmsg,
8230 "'%s': unsupported HTTP code '%s' (must be a number between 301 and 303)",
8231 args[cur_arg - 1], args[cur_arg]);
8232 return NULL;
8233 }
8234 }
8235 else if (!strcmp(args[cur_arg],"drop-query")) {
8236 flags |= REDIRECT_FLAG_DROP_QS;
8237 }
8238 else if (!strcmp(args[cur_arg],"append-slash")) {
8239 flags |= REDIRECT_FLAG_APPEND_SLASH;
8240 }
8241 else if (strcmp(args[cur_arg], "if") == 0 ||
8242 strcmp(args[cur_arg], "unless") == 0) {
8243 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8244 if (!cond) {
8245 memprintf(errmsg, "error in condition: %s", *errmsg);
8246 return NULL;
8247 }
8248 break;
8249 }
8250 else {
8251 memprintf(errmsg,
8252 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8253 args[cur_arg]);
8254 return NULL;
8255 }
8256 cur_arg++;
8257 }
8258
8259 if (type == REDIRECT_TYPE_NONE) {
8260 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8261 return NULL;
8262 }
8263
8264 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8265 rule->cond = cond;
8266 rule->rdr_str = strdup(destination);
8267 rule->rdr_len = strlen(destination);
8268 if (cookie) {
8269 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8270 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8271 */
8272 rule->cookie_len = strlen(cookie);
8273 if (cookie_set) {
8274 rule->cookie_str = malloc(rule->cookie_len + 10);
8275 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8276 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8277 rule->cookie_len += 9;
8278 } else {
8279 rule->cookie_str = malloc(rule->cookie_len + 21);
8280 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8281 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8282 rule->cookie_len += 20;
8283 }
8284 }
8285 rule->type = type;
8286 rule->code = code;
8287 rule->flags = flags;
8288 LIST_INIT(&rule->list);
8289 return rule;
8290
8291 missing_arg:
8292 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8293 return NULL;
8294}
8295
Willy Tarreau8797c062007-05-07 00:55:35 +02008296/************************************************************************/
8297/* The code below is dedicated to ACL parsing and matching */
8298/************************************************************************/
8299
8300
Willy Tarreau14174bc2012-04-16 14:34:04 +02008301/* This function ensures that the prerequisites for an L7 fetch are ready,
8302 * which means that a request or response is ready. If some data is missing,
8303 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008304 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8305 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008306 *
8307 * The function returns :
8308 * 0 if some data is missing or if the requested data cannot be fetched
8309 * -1 if it is certain that we'll never have any HTTP message there
8310 * 1 if an HTTP message is ready
8311 */
8312static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008313acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008314 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008315{
8316 struct http_txn *txn = l7;
8317 struct http_msg *msg = &txn->req;
8318
8319 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8320 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8321 */
8322
8323 if (unlikely(!s || !txn))
8324 return 0;
8325
8326 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008327 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008328
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008329 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008330 if (unlikely(!s->req))
8331 return 0;
8332
8333 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008334 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008335 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008336 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008337 return -1;
8338 }
8339
8340 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008342 http_msg_analyzer(msg, &txn->hdr_idx);
8343
8344 /* Still no valid request ? */
8345 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008346 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008347 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008348 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008349 return -1;
8350 }
8351 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008352 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008353 return 0;
8354 }
8355
8356 /* OK we just got a valid HTTP request. We have some minor
8357 * preparation to perform so that further checks can rely
8358 * on HTTP tests.
8359 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008360 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008361 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8362 s->flags |= SN_REDIRECTABLE;
8363
8364 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008365 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008366 return -1;
8367 }
8368 }
8369
Willy Tarreau24e32d82012-04-23 23:55:44 +02008370 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008371 return 0; /* data might have moved and indexes changed */
8372
8373 /* otherwise everything's ready for the request */
8374 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008375 else {
8376 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008377 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8378 return 0;
8379 }
8380
8381 /* everything's OK */
8382 return 1;
8383}
Willy Tarreau8797c062007-05-07 00:55:35 +02008384
Willy Tarreauc0239e02012-04-16 14:42:55 +02008385#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008386 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 +02008387
Willy Tarreau24e32d82012-04-23 23:55:44 +02008388#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008389 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 +02008390
Willy Tarreau8797c062007-05-07 00:55:35 +02008391
8392/* 1. Check on METHOD
8393 * We use the pre-parsed method if it is known, and store its number as an
8394 * integer. If it is unknown, we use the pointer and the length.
8395 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008396static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008397{
8398 int len, meth;
8399
Willy Tarreauae8b7962007-06-09 23:10:04 +02008400 len = strlen(*text);
8401 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008402
8403 pattern->val.i = meth;
8404 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008405 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008406 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008407 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008408 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008409 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008410 pattern->len = len;
8411 }
8412 return 1;
8413}
8414
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008415/* This function fetches the method of current HTTP request and stores
8416 * it in the global pattern struct as a chunk. There are two possibilities :
8417 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8418 * in <len> and <ptr> is NULL ;
8419 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8420 * <len> to its length.
8421 * This is intended to be used with acl_match_meth() only.
8422 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008423static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008424acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008425 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008426{
8427 int meth;
8428 struct http_txn *txn = l7;
8429
Willy Tarreau24e32d82012-04-23 23:55:44 +02008430 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008431
Willy Tarreau8797c062007-05-07 00:55:35 +02008432 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008433 smp->type = SMP_T_UINT;
8434 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008435 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008436 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8437 /* ensure the indexes are not affected */
8438 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008439 smp->type = SMP_T_CSTR;
8440 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008441 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008442 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008443 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008444 return 1;
8445}
8446
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008447/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008448static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008449{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008450 int icase;
8451
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008452
Willy Tarreauf853c462012-04-23 18:53:56 +02008453 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008454 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008455 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008456 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008457 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008458 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008459
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008460 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8461 if (pattern->val.i != HTTP_METH_OTHER)
8462 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008463
8464 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008465 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008466 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008467
8468 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008469 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8470 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008471 return ACL_PAT_FAIL;
8472 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008473}
8474
8475/* 2. Check on Request/Status Version
8476 * We simply compare strings here.
8477 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008478static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008479{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008480 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008481 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008482 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008483 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008484 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008485 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008486 return 1;
8487}
8488
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008489static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008490acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008491 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008492{
8493 struct http_txn *txn = l7;
8494 char *ptr;
8495 int len;
8496
Willy Tarreauc0239e02012-04-16 14:42:55 +02008497 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008498
Willy Tarreau8797c062007-05-07 00:55:35 +02008499 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008500 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008501
8502 while ((len-- > 0) && (*ptr++ != '/'));
8503 if (len <= 0)
8504 return 0;
8505
Willy Tarreauf853c462012-04-23 18:53:56 +02008506 smp->type = SMP_T_CSTR;
8507 smp->data.str.str = ptr;
8508 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008509
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008510 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008511 return 1;
8512}
8513
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008514static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008515acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008516 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008517{
8518 struct http_txn *txn = l7;
8519 char *ptr;
8520 int len;
8521
Willy Tarreauc0239e02012-04-16 14:42:55 +02008522 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008523
Willy Tarreauf26b2522012-12-14 08:33:14 +01008524 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8525 return 0;
8526
Willy Tarreau8797c062007-05-07 00:55:35 +02008527 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008528 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008529
8530 while ((len-- > 0) && (*ptr++ != '/'));
8531 if (len <= 0)
8532 return 0;
8533
Willy Tarreauf853c462012-04-23 18:53:56 +02008534 smp->type = SMP_T_CSTR;
8535 smp->data.str.str = ptr;
8536 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008537
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008538 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008539 return 1;
8540}
8541
8542/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008543static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008544acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008545 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008546{
8547 struct http_txn *txn = l7;
8548 char *ptr;
8549 int len;
8550
Willy Tarreauc0239e02012-04-16 14:42:55 +02008551 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008552
Willy Tarreauf26b2522012-12-14 08:33:14 +01008553 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8554 return 0;
8555
Willy Tarreau8797c062007-05-07 00:55:35 +02008556 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008557 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008558
Willy Tarreauf853c462012-04-23 18:53:56 +02008559 smp->type = SMP_T_UINT;
8560 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008561 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008562 return 1;
8563}
8564
8565/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008566static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008567smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008568 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008569{
8570 struct http_txn *txn = l7;
8571
Willy Tarreauc0239e02012-04-16 14:42:55 +02008572 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008573
Willy Tarreauf853c462012-04-23 18:53:56 +02008574 smp->type = SMP_T_CSTR;
8575 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008576 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008577 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008578 return 1;
8579}
8580
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008581static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008582smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008583 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008584{
8585 struct http_txn *txn = l7;
8586
Willy Tarreauc0239e02012-04-16 14:42:55 +02008587 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008588
8589 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008590 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8591 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008592 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008593 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008594 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008595
8596 /*
8597 * If we are parsing url in frontend space, we prepare backend stage
8598 * to not parse again the same url ! optimization lazyness...
8599 */
8600 if (px->options & PR_O_HTTP_PROXY)
8601 l4->flags |= SN_ADDR_SET;
8602
Willy Tarreau37406352012-04-23 16:16:37 +02008603 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008604 return 1;
8605}
8606
8607static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008608smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008609 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008610{
8611 struct http_txn *txn = l7;
8612
Willy Tarreauc0239e02012-04-16 14:42:55 +02008613 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008614
8615 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008616 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02008617 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008618 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008619
8620 if (px->options & PR_O_HTTP_PROXY)
8621 l4->flags |= SN_ADDR_SET;
8622
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008623 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008624 return 1;
8625}
8626
Willy Tarreau185b5c42012-04-26 15:11:51 +02008627/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8628 * Accepts an optional argument of type string containing the header field name,
8629 * and an optional argument of type signed or unsigned integer to request an
8630 * explicit occurrence of the header. Note that in the event of a missing name,
8631 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008632 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008633static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008634smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008635 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008636{
8637 struct http_txn *txn = l7;
8638 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008639 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008640 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008641 int occ = 0;
8642 const char *name_str = NULL;
8643 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008644
Willy Tarreau185b5c42012-04-26 15:11:51 +02008645 if (args) {
8646 if (args[0].type != ARGT_STR)
8647 return 0;
8648 name_str = args[0].data.str.str;
8649 name_len = args[0].data.str.len;
8650
8651 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8652 occ = args[1].data.uint;
8653 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008654
Willy Tarreaue333ec92012-04-16 16:26:40 +02008655 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008656
Willy Tarreau185b5c42012-04-26 15:11:51 +02008657 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008658 /* search for header from the beginning */
8659 ctx->idx = 0;
8660
Willy Tarreau185b5c42012-04-26 15:11:51 +02008661 if (!occ && !(opt & SMP_OPT_ITERATE))
8662 /* no explicit occurrence and single fetch => last header by default */
8663 occ = -1;
8664
8665 if (!occ)
8666 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008667 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008668
Willy Tarreau185b5c42012-04-26 15:11:51 +02008669 smp->type = SMP_T_CSTR;
8670 smp->flags |= SMP_F_VOL_HDR;
8671 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 +02008672 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008673
Willy Tarreau37406352012-04-23 16:16:37 +02008674 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008675 return 0;
8676}
8677
Willy Tarreauc11416f2007-06-17 16:58:38 +02008678/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008679 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008680 */
8681static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008682smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008683 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008684{
8685 struct http_txn *txn = l7;
8686 struct hdr_idx *idx = &txn->hdr_idx;
8687 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008688 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008689 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008690
Willy Tarreau24e32d82012-04-23 23:55:44 +02008691 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008692 return 0;
8693
Willy Tarreaue333ec92012-04-16 16:26:40 +02008694 CHECK_HTTP_MESSAGE_FIRST();
8695
Willy Tarreau33a7e692007-06-10 19:45:56 +02008696 ctx.idx = 0;
8697 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008698 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008699 cnt++;
8700
Willy Tarreauf853c462012-04-23 18:53:56 +02008701 smp->type = SMP_T_UINT;
8702 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008703 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008704 return 1;
8705}
8706
Willy Tarreau185b5c42012-04-26 15:11:51 +02008707/* Fetch an HTTP header's integer value. The integer value is returned. It
8708 * takes a mandatory argument of type string and an optional one of type int
8709 * to designate a specific occurrence. It returns an unsigned integer, which
8710 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008711 */
8712static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008713smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008714 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008715{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008716 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008717
Willy Tarreauf853c462012-04-23 18:53:56 +02008718 if (ret > 0) {
8719 smp->type = SMP_T_UINT;
8720 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8721 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008722
Willy Tarreaud53e2422012-04-16 17:21:11 +02008723 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008724}
8725
Cyril Bonté69fa9922012-10-25 00:01:06 +02008726/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8727 * and an optional one of type int to designate a specific occurrence.
8728 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008729 */
8730static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008731smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008732 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008733{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008734 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008735
Willy Tarreau185b5c42012-04-26 15:11:51 +02008736 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008737 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8738 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008739 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008740 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008741 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008742 if (smp->data.str.len < temp->size - 1) {
8743 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8744 temp->str[smp->data.str.len] = '\0';
8745 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8746 smp->type = SMP_T_IPV6;
8747 break;
8748 }
8749 }
8750 }
8751
Willy Tarreaud53e2422012-04-16 17:21:11 +02008752 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008753 if (!(smp->flags & SMP_F_NOT_LAST))
8754 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008755 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008756 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008757}
8758
Willy Tarreau737b0c12007-06-10 21:28:46 +02008759/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8760 * the first '/' after the possible hostname, and ends before the possible '?'.
8761 */
8762static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008763smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008764 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008765{
8766 struct http_txn *txn = l7;
8767 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008768
Willy Tarreauc0239e02012-04-16 14:42:55 +02008769 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008770
Willy Tarreau9b28e032012-10-12 23:49:43 +02008771 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008772 ptr = http_get_path(txn);
8773 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008774 return 0;
8775
8776 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008777 smp->type = SMP_T_CSTR;
8778 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008779
8780 while (ptr < end && *ptr != '?')
8781 ptr++;
8782
Willy Tarreauf853c462012-04-23 18:53:56 +02008783 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008784 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008785 return 1;
8786}
8787
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008788/* This produces a concatenation of the first occurrence of the Host header
8789 * followed by the path component if it begins with a slash ('/'). This means
8790 * that '*' will not be added, resulting in exactly the first Host entry.
8791 * If no Host header is found, then the path is returned as-is. The returned
8792 * value is stored in the trash so it does not need to be marked constant.
8793 */
8794static int
8795smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8796 const struct arg *args, struct sample *smp)
8797{
8798 struct http_txn *txn = l7;
8799 char *ptr, *end, *beg;
8800 struct hdr_ctx ctx;
8801
8802 CHECK_HTTP_MESSAGE_FIRST();
8803
8804 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008805 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008806 !ctx.vlen)
8807 return smp_fetch_path(px, l4, l7, opt, args, smp);
8808
8809 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008810 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008811 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008812 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008813 smp->data.str.len = ctx.vlen;
8814
8815 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008816 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008817 beg = http_get_path(txn);
8818 if (!beg)
8819 beg = end;
8820
8821 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8822
8823 if (beg < ptr && *beg == '/') {
8824 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8825 smp->data.str.len += ptr - beg;
8826 }
8827
8828 smp->flags = SMP_F_VOL_1ST;
8829 return 1;
8830}
8831
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008832/* This produces a 32-bit hash of the concatenation of the first occurrence of
8833 * the Host header followed by the path component if it begins with a slash ('/').
8834 * This means that '*' will not be added, resulting in exactly the first Host
8835 * entry. If no Host header is found, then the path is used. The resulting value
8836 * is hashed using the url hash followed by a full avalanche hash and provides a
8837 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8838 * high-traffic sites without having to store whole paths.
8839 */
8840static int
8841smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8842 const struct arg *args, struct sample *smp)
8843{
8844 struct http_txn *txn = l7;
8845 struct hdr_ctx ctx;
8846 unsigned int hash = 0;
8847 char *ptr, *beg, *end;
8848 int len;
8849
8850 CHECK_HTTP_MESSAGE_FIRST();
8851
8852 ctx.idx = 0;
8853 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8854 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8855 ptr = ctx.line + ctx.val;
8856 len = ctx.vlen;
8857 while (len--)
8858 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8859 }
8860
8861 /* now retrieve the path */
8862 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8863 beg = http_get_path(txn);
8864 if (!beg)
8865 beg = end;
8866
8867 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8868
8869 if (beg < ptr && *beg == '/') {
8870 while (beg < ptr)
8871 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8872 }
8873 hash = full_hash(hash);
8874
8875 smp->type = SMP_T_UINT;
8876 smp->data.uint = hash;
8877 smp->flags = SMP_F_VOL_1ST;
8878 return 1;
8879}
8880
Willy Tarreau4a550602012-12-09 14:53:32 +01008881/* This concatenates the source address with the 32-bit hash of the Host and
8882 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8883 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8884 * on the source address length. The URL hash is stored before the address so
8885 * that in environments where IPv6 is insignificant, truncating the output to
8886 * 8 bytes would still work.
8887 */
8888static int
8889smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8890 const struct arg *args, struct sample *smp)
8891{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008892 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008893
8894 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8895 return 0;
8896
Willy Tarreau47ca5452012-12-23 20:22:19 +01008897 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008898 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8899 temp->len += sizeof(smp->data.uint);
8900
8901 switch (l4->si[0].conn->addr.from.ss_family) {
8902 case AF_INET:
8903 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
8904 temp->len += 4;
8905 break;
8906 case AF_INET6:
8907 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
8908 temp->len += 16;
8909 break;
8910 default:
8911 return 0;
8912 }
8913
8914 smp->data.str = *temp;
8915 smp->type = SMP_T_BIN;
8916 return 1;
8917}
8918
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008919static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008920acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008921 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008922{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008923 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8924 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8925 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008926
Willy Tarreau24e32d82012-04-23 23:55:44 +02008927 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008928
Willy Tarreauf853c462012-04-23 18:53:56 +02008929 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008930 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008931 return 1;
8932}
8933
Willy Tarreau7f18e522010-10-22 20:04:13 +02008934/* return a valid test if the current request is the first one on the connection */
8935static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008936acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008937 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008938{
8939 if (!s)
8940 return 0;
8941
Willy Tarreauf853c462012-04-23 18:53:56 +02008942 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008943 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008944 return 1;
8945}
8946
Willy Tarreau34db1082012-04-19 17:16:54 +02008947/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008948static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008949acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008950 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008951{
8952
Willy Tarreau24e32d82012-04-23 23:55:44 +02008953 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008954 return 0;
8955
Willy Tarreauc0239e02012-04-16 14:42:55 +02008956 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008957
Willy Tarreauc0239e02012-04-16 14:42:55 +02008958 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008959 return 0;
8960
Willy Tarreauf853c462012-04-23 18:53:56 +02008961 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008962 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 +01008963 return 1;
8964}
Willy Tarreau8797c062007-05-07 00:55:35 +02008965
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008966/* Accepts exactly 1 argument of type userlist */
8967static int
8968acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8969 const struct arg *args, struct sample *smp)
8970{
8971
8972 if (!args || args->type != ARGT_USR)
8973 return 0;
8974
8975 CHECK_HTTP_MESSAGE_FIRST();
8976
8977 if (!get_http_auth(l4))
8978 return 0;
8979
8980 /* acl_match_auth() will need several information at once */
8981 smp->ctx.a[0] = args->data.usr; /* user list */
8982 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8983 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8984
8985 /* if the user does not belong to the userlist or has a wrong password,
8986 * report that it unconditionally does not match. Otherwise we return
8987 * a non-zero integer which will be ignored anyway since all the params
8988 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8989 */
8990 smp->type = SMP_T_BOOL;
8991 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8992 if (smp->data.uint)
8993 smp->type = SMP_T_UINT;
8994
8995 return 1;
8996}
8997
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008998/* Try to find the next occurrence of a cookie name in a cookie header value.
8999 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9000 * the cookie value is returned into *value and *value_l, and the function
9001 * returns a pointer to the next pointer to search from if the value was found.
9002 * Otherwise if the cookie was not found, NULL is returned and neither value
9003 * nor value_l are touched. The input <hdr> string should first point to the
9004 * header's value, and the <hdr_end> pointer must point to the first character
9005 * not part of the value. <list> must be non-zero if value may represent a list
9006 * of values (cookie headers). This makes it faster to abort parsing when no
9007 * list is expected.
9008 */
9009static char *
9010extract_cookie_value(char *hdr, const char *hdr_end,
9011 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009012 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009013{
9014 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9015 char *next;
9016
9017 /* we search at least a cookie name followed by an equal, and more
9018 * generally something like this :
9019 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9020 */
9021 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9022 /* Iterate through all cookies on this line */
9023
9024 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9025 att_beg++;
9026
9027 /* find att_end : this is the first character after the last non
9028 * space before the equal. It may be equal to hdr_end.
9029 */
9030 equal = att_end = att_beg;
9031
9032 while (equal < hdr_end) {
9033 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9034 break;
9035 if (http_is_spht[(unsigned char)*equal++])
9036 continue;
9037 att_end = equal;
9038 }
9039
9040 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9041 * is between <att_beg> and <equal>, both may be identical.
9042 */
9043
9044 /* look for end of cookie if there is an equal sign */
9045 if (equal < hdr_end && *equal == '=') {
9046 /* look for the beginning of the value */
9047 val_beg = equal + 1;
9048 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9049 val_beg++;
9050
9051 /* find the end of the value, respecting quotes */
9052 next = find_cookie_value_end(val_beg, hdr_end);
9053
9054 /* make val_end point to the first white space or delimitor after the value */
9055 val_end = next;
9056 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9057 val_end--;
9058 } else {
9059 val_beg = val_end = next = equal;
9060 }
9061
9062 /* We have nothing to do with attributes beginning with '$'. However,
9063 * they will automatically be removed if a header before them is removed,
9064 * since they're supposed to be linked together.
9065 */
9066 if (*att_beg == '$')
9067 continue;
9068
9069 /* Ignore cookies with no equal sign */
9070 if (equal == next)
9071 continue;
9072
9073 /* Now we have the cookie name between att_beg and att_end, and
9074 * its value between val_beg and val_end.
9075 */
9076
9077 if (att_end - att_beg == cookie_name_l &&
9078 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9079 /* let's return this value and indicate where to go on from */
9080 *value = val_beg;
9081 *value_l = val_end - val_beg;
9082 return next + 1;
9083 }
9084
9085 /* Set-Cookie headers only have the name in the first attr=value part */
9086 if (!list)
9087 break;
9088 }
9089
9090 return NULL;
9091}
9092
Willy Tarreaue333ec92012-04-16 16:26:40 +02009093/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009094 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
9095 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009096 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009097 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009098 * Accepts exactly 1 argument of type string. If the input options indicate
9099 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009100 */
9101static int
Willy Tarreau51539362012-05-08 12:46:28 +02009102smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9103 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009104{
9105 struct http_txn *txn = l7;
9106 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02009107 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009108 const struct http_msg *msg;
9109 const char *hdr_name;
9110 int hdr_name_len;
9111 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009112 int occ = 0;
9113 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009114
Willy Tarreau24e32d82012-04-23 23:55:44 +02009115 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009116 return 0;
9117
Willy Tarreaue333ec92012-04-16 16:26:40 +02009118 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009119
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009120 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009121 msg = &txn->req;
9122 hdr_name = "Cookie";
9123 hdr_name_len = 6;
9124 } else {
9125 msg = &txn->rsp;
9126 hdr_name = "Set-Cookie";
9127 hdr_name_len = 10;
9128 }
9129
Willy Tarreau28376d62012-04-26 21:26:10 +02009130 if (!occ && !(opt & SMP_OPT_ITERATE))
9131 /* no explicit occurrence and single fetch => last cookie by default */
9132 occ = -1;
9133
9134 /* OK so basically here, either we want only one value and it's the
9135 * last one, or we want to iterate over all of them and we fetch the
9136 * next one.
9137 */
9138
Willy Tarreau9b28e032012-10-12 23:49:43 +02009139 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009140 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009141 /* search for the header from the beginning, we must first initialize
9142 * the search parameters.
9143 */
Willy Tarreau37406352012-04-23 16:16:37 +02009144 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009145 ctx->idx = 0;
9146 }
9147
Willy Tarreau28376d62012-04-26 21:26:10 +02009148 smp->flags |= SMP_F_VOL_HDR;
9149
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009150 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009151 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9152 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009153 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9154 goto out;
9155
Willy Tarreau24e32d82012-04-23 23:55:44 +02009156 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009157 continue;
9158
Willy Tarreau37406352012-04-23 16:16:37 +02009159 smp->ctx.a[0] = ctx->line + ctx->val;
9160 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009161 }
9162
Willy Tarreauf853c462012-04-23 18:53:56 +02009163 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009164 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009165 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009166 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009167 &smp->data.str.str,
9168 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009169 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009170 found = 1;
9171 if (occ >= 0) {
9172 /* one value was returned into smp->data.str.{str,len} */
9173 smp->flags |= SMP_F_NOT_LAST;
9174 return 1;
9175 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009176 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009177 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009178 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009179 /* all cookie headers and values were scanned. If we're looking for the
9180 * last occurrence, we may return it now.
9181 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009182 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009183 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009184 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009185}
9186
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009187/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009188 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009189 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009190 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009191 */
9192static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009193acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009194 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009195{
9196 struct http_txn *txn = l7;
9197 struct hdr_idx *idx = &txn->hdr_idx;
9198 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009199 const struct http_msg *msg;
9200 const char *hdr_name;
9201 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009202 int cnt;
9203 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009204 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009205
Willy Tarreau24e32d82012-04-23 23:55:44 +02009206 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009207 return 0;
9208
Willy Tarreaue333ec92012-04-16 16:26:40 +02009209 CHECK_HTTP_MESSAGE_FIRST();
9210
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009211 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009212 msg = &txn->req;
9213 hdr_name = "Cookie";
9214 hdr_name_len = 6;
9215 } else {
9216 msg = &txn->rsp;
9217 hdr_name = "Set-Cookie";
9218 hdr_name_len = 10;
9219 }
9220
Willy Tarreau9b28e032012-10-12 23:49:43 +02009221 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009222 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009223 ctx.idx = 0;
9224 cnt = 0;
9225
9226 while (1) {
9227 /* Note: val_beg == NULL every time we need to fetch a new header */
9228 if (!val_beg) {
9229 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9230 break;
9231
Willy Tarreau24e32d82012-04-23 23:55:44 +02009232 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009233 continue;
9234
9235 val_beg = ctx.line + ctx.val;
9236 val_end = val_beg + ctx.vlen;
9237 }
9238
Willy Tarreauf853c462012-04-23 18:53:56 +02009239 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009240 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009241 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009242 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009243 &smp->data.str.str,
9244 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009245 cnt++;
9246 }
9247 }
9248
Willy Tarreauf853c462012-04-23 18:53:56 +02009249 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009250 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009251 return 1;
9252}
9253
Willy Tarreau51539362012-05-08 12:46:28 +02009254/* Fetch an cookie's integer value. The integer value is returned. It
9255 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9256 */
9257static int
9258smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9259 const struct arg *args, struct sample *smp)
9260{
9261 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9262
9263 if (ret > 0) {
9264 smp->type = SMP_T_UINT;
9265 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9266 }
9267
9268 return ret;
9269}
9270
Willy Tarreau8797c062007-05-07 00:55:35 +02009271/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009272/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009273/************************************************************************/
9274
David Cournapeau16023ee2010-12-23 20:55:41 +09009275/*
9276 * Given a path string and its length, find the position of beginning of the
9277 * query string. Returns NULL if no query string is found in the path.
9278 *
9279 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9280 *
9281 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9282 */
bedis4c75cca2012-10-05 08:38:24 +02009283static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009284{
9285 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009286
bedis4c75cca2012-10-05 08:38:24 +02009287 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009288 return p ? p + 1 : NULL;
9289}
9290
bedis4c75cca2012-10-05 08:38:24 +02009291static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009292{
bedis4c75cca2012-10-05 08:38:24 +02009293 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009294}
9295
9296/*
9297 * Given a url parameter, find the starting position of the first occurence,
9298 * or NULL if the parameter is not found.
9299 *
9300 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9301 * the function will return query_string+8.
9302 */
9303static char*
9304find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009305 char* url_param_name, size_t url_param_name_l,
9306 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009307{
9308 char *pos, *last;
9309
9310 pos = query_string;
9311 last = query_string + query_string_l - url_param_name_l - 1;
9312
9313 while (pos <= last) {
9314 if (pos[url_param_name_l] == '=') {
9315 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9316 return pos;
9317 pos += url_param_name_l + 1;
9318 }
bedis4c75cca2012-10-05 08:38:24 +02009319 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009320 pos++;
9321 pos++;
9322 }
9323 return NULL;
9324}
9325
9326/*
9327 * Given a url parameter name, returns its value and size into *value and
9328 * *value_l respectively, and returns non-zero. If the parameter is not found,
9329 * zero is returned and value/value_l are not touched.
9330 */
9331static int
9332find_url_param_value(char* path, size_t path_l,
9333 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009334 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009335{
9336 char *query_string, *qs_end;
9337 char *arg_start;
9338 char *value_start, *value_end;
9339
bedis4c75cca2012-10-05 08:38:24 +02009340 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009341 if (!query_string)
9342 return 0;
9343
9344 qs_end = path + path_l;
9345 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009346 url_param_name, url_param_name_l,
9347 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009348 if (!arg_start)
9349 return 0;
9350
9351 value_start = arg_start + url_param_name_l + 1;
9352 value_end = value_start;
9353
bedis4c75cca2012-10-05 08:38:24 +02009354 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009355 value_end++;
9356
9357 *value = value_start;
9358 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009359 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009360}
9361
9362static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009363smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9364 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009365{
bedis4c75cca2012-10-05 08:38:24 +02009366 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009367 struct http_txn *txn = l7;
9368 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009369
bedis4c75cca2012-10-05 08:38:24 +02009370 if (!args || args[0].type != ARGT_STR ||
9371 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009372 return 0;
9373
9374 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009375
bedis4c75cca2012-10-05 08:38:24 +02009376 if (args[1].type)
9377 delim = *args[1].data.str.str;
9378
Willy Tarreau9b28e032012-10-12 23:49:43 +02009379 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009380 args->data.str.str, args->data.str.len,
9381 &smp->data.str.str, &smp->data.str.len,
9382 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009383 return 0;
9384
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009385 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009386 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009387 return 1;
9388}
9389
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009390/* Return the signed integer value for the specified url parameter (see url_param
9391 * above).
9392 */
9393static int
9394smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9395 const struct arg *args, struct sample *smp)
9396{
9397 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9398
9399 if (ret > 0) {
9400 smp->type = SMP_T_UINT;
9401 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9402 }
9403
9404 return ret;
9405}
9406
Willy Tarreau185b5c42012-04-26 15:11:51 +02009407/* This function is used to validate the arguments passed to any "hdr" fetch
9408 * keyword. These keywords support an optional positive or negative occurrence
9409 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9410 * is assumed that the types are already the correct ones. Returns 0 on error,
9411 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9412 * error message in case of error, that the caller is responsible for freeing.
9413 * The initial location must either be freeable or NULL.
9414 */
9415static int val_hdr(struct arg *arg, char **err_msg)
9416{
9417 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009418 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009419 return 0;
9420 }
9421 return 1;
9422}
9423
Willy Tarreau4a568972010-05-12 08:08:50 +02009424/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009425/* All supported ACL keywords must be declared here. */
9426/************************************************************************/
9427
9428/* Note: must not be declared <const> as its list will be overwritten.
9429 * Please take care of keeping this list alphabetically sorted.
9430 */
9431static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009432 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9433 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9434 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9435 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9436 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9437 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9438 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9439 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
9440
Willy Tarreau51539362012-05-08 12:46:28 +02009441 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9442 { "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 +02009443 { "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 +02009444 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9445 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9446 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9447 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9448 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9449 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9450 { "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 +02009451
Willy Tarreau185b5c42012-04-26 15:11:51 +02009452 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9453 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9454 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9455 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9456 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9457 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9458 { "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 },
9459 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9460 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9461 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9462 { "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 +02009463
9464 { "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 +02009465 { "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 +02009466 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9467
9468 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9469
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009470 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9471 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9472 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9473 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9474 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9475 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9476 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9477 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009478
9479 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9480 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9481 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
9482
Willy Tarreau51539362012-05-08 12:46:28 +02009483 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9484 { "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 +02009485 { "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 +02009486 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9487 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9488 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9489 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9490 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9491 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9492 { "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 +02009493
Willy Tarreau185b5c42012-04-26 15:11:51 +02009494 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9495 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9496 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9497 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9498 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9499 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9500 { "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 },
9501 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9502 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9503 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9504 { "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 +02009505
9506 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9507
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009508 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9509 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9510 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9511 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9512 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9513 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9514 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9515 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9516 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9517 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009518
9519 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9520 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9521 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9522 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9523 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9524 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9525 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9526 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9527 { "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 +02009528 { "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 +02009529
9530 { NULL, NULL, NULL, NULL },
9531}};
9532
9533/************************************************************************/
9534/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009535/************************************************************************/
9536/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009537static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009538 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9539 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009540 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau4a550602012-12-09 14:53:32 +01009541 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009542 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9543 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9544 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9545 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9546 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9547 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9548 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02009549 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009550}};
9551
Willy Tarreau8797c062007-05-07 00:55:35 +02009552
9553__attribute__((constructor))
9554static void __http_protocol_init(void)
9555{
9556 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009557 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009558}
9559
9560
Willy Tarreau58f10d72006-12-04 02:26:12 +01009561/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009562 * Local variables:
9563 * c-indent-level: 8
9564 * c-basic-offset: 8
9565 * End:
9566 */