blob: 087cd4703404e313bd9ba3be73fff170f41e5a03 [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 Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040094
95/* same as 302 except that the browser MUST retry with the same method */
96const char *HTTP_307 =
97 "HTTP/1.1 307 Temporary Redirect\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Content-length: 0\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
102/* same as 301 except that the browser MUST retry with the same method */
103const char *HTTP_308 =
104 "HTTP/1.1 308 Permanent Redirect\r\n"
105 "Content-length: 0\r\n"
106 "Location: "; /* not terminated since it will be concatenated with the URL */
107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
109const char *HTTP_401_fmt =
110 "HTTP/1.0 401 Unauthorized\r\n"
111 "Cache-Control: no-cache\r\n"
112 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200113 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
115 "\r\n"
116 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
117
Willy Tarreau844a7e72010-01-31 21:46:18 +0100118const char *HTTP_407_fmt =
119 "HTTP/1.0 407 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau0f772532006-12-23 20:51:41 +0100127
128const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200129 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 [HTTP_ERR_400] = 400,
131 [HTTP_ERR_403] = 403,
132 [HTTP_ERR_408] = 408,
133 [HTTP_ERR_500] = 500,
134 [HTTP_ERR_502] = 502,
135 [HTTP_ERR_503] = 503,
136 [HTTP_ERR_504] = 504,
137};
138
Willy Tarreau80587432006-12-24 17:47:20 +0100139static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] =
141 "HTTP/1.0 200 OK\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
147
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100149 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
155
156 [HTTP_ERR_403] =
157 "HTTP/1.0 403 Forbidden\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
163
164 [HTTP_ERR_408] =
165 "HTTP/1.0 408 Request Time-out\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
171
172 [HTTP_ERR_500] =
173 "HTTP/1.0 500 Server Error\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
179
180 [HTTP_ERR_502] =
181 "HTTP/1.0 502 Bad Gateway\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
187
188 [HTTP_ERR_503] =
189 "HTTP/1.0 503 Service Unavailable\r\n"
190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
195
196 [HTTP_ERR_504] =
197 "HTTP/1.0 504 Gateway Time-out\r\n"
198 "Cache-Control: no-cache\r\n"
199 "Connection: close\r\n"
200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
203
204};
205
Cyril Bonté19979e12012-04-04 12:57:21 +0200206/* status codes available for the stats admin page (strictly 4 chars length) */
207const char *stat_status_codes[STAT_STATUS_SIZE] = {
208 [STAT_STATUS_DENY] = "DENY",
209 [STAT_STATUS_DONE] = "DONE",
210 [STAT_STATUS_ERRP] = "ERRP",
211 [STAT_STATUS_EXCD] = "EXCD",
212 [STAT_STATUS_NONE] = "NONE",
213 [STAT_STATUS_PART] = "PART",
214 [STAT_STATUS_UNKN] = "UNKN",
215};
216
217
Willy Tarreau80587432006-12-24 17:47:20 +0100218/* We must put the messages here since GCC cannot initialize consts depending
219 * on strlen().
220 */
221struct chunk http_err_chunks[HTTP_ERR_SIZE];
222
Willy Tarreau42250582007-04-01 01:30:43 +0200223#define FD_SETS_ARE_BITFIELDS
224#ifdef FD_SETS_ARE_BITFIELDS
225/*
226 * This map is used with all the FD_* macros to check whether a particular bit
227 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
228 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
229 * byte should be encoded. Be careful to always pass bytes from 0 to 255
230 * exclusively to the macros.
231 */
232fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
233fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
234
235#else
236#error "Check if your OS uses bitfields for fd_sets"
237#endif
238
Willy Tarreau80587432006-12-24 17:47:20 +0100239void init_proto_http()
240{
Willy Tarreau42250582007-04-01 01:30:43 +0200241 int i;
242 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100243 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200244
Willy Tarreau80587432006-12-24 17:47:20 +0100245 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
246 if (!http_err_msgs[msg]) {
247 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
248 abort();
249 }
250
251 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
252 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
253 }
Willy Tarreau42250582007-04-01 01:30:43 +0200254
255 /* initialize the log header encoding map : '{|}"#' should be encoded with
256 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
257 * URL encoding only requires '"', '#' to be encoded as well as non-
258 * printable characters above.
259 */
260 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
261 memset(url_encode_map, 0, sizeof(url_encode_map));
262 for (i = 0; i < 32; i++) {
263 FD_SET(i, hdr_encode_map);
264 FD_SET(i, url_encode_map);
265 }
266 for (i = 127; i < 256; i++) {
267 FD_SET(i, hdr_encode_map);
268 FD_SET(i, url_encode_map);
269 }
270
271 tmp = "\"#{|}";
272 while (*tmp) {
273 FD_SET(*tmp, hdr_encode_map);
274 tmp++;
275 }
276
277 tmp = "\"#";
278 while (*tmp) {
279 FD_SET(*tmp, url_encode_map);
280 tmp++;
281 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200282
283 /* memory allocations */
284 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100285 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100286}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287
Willy Tarreau53b6c742006-12-17 13:37:46 +0100288/*
289 * We have 26 list of methods (1 per first letter), each of which can have
290 * up to 3 entries (2 valid, 1 null).
291 */
292struct http_method_desc {
293 http_meth_t meth;
294 int len;
295 const char text[8];
296};
297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100299 ['C' - 'A'] = {
300 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
301 },
302 ['D' - 'A'] = {
303 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
304 },
305 ['G' - 'A'] = {
306 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
307 },
308 ['H' - 'A'] = {
309 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
310 },
311 ['P' - 'A'] = {
312 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
313 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
314 },
315 ['T' - 'A'] = {
316 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
317 },
318 /* rest is empty like this :
319 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
320 */
321};
322
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100323/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200324 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100325 * RFC2616 for those chars.
326 */
327
328const char http_is_spht[256] = {
329 [' '] = 1, ['\t'] = 1,
330};
331
332const char http_is_crlf[256] = {
333 ['\r'] = 1, ['\n'] = 1,
334};
335
336const char http_is_lws[256] = {
337 [' '] = 1, ['\t'] = 1,
338 ['\r'] = 1, ['\n'] = 1,
339};
340
341const char http_is_sep[256] = {
342 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
343 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
344 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
345 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
346 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
347};
348
349const char http_is_ctl[256] = {
350 [0 ... 31] = 1,
351 [127] = 1,
352};
353
354/*
355 * A token is any ASCII char that is neither a separator nor a CTL char.
356 * Do not overwrite values in assignment since gcc-2.95 will not handle
357 * them correctly. Instead, define every non-CTL char's status.
358 */
359const char http_is_token[256] = {
360 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
361 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
362 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
363 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
364 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
365 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
366 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
367 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
368 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
369 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
370 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
371 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
372 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
373 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
374 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
375 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
376 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
377 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
378 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
379 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
380 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
381 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
382 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
383 ['|'] = 1, ['}'] = 0, ['~'] = 1,
384};
385
386
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100387/*
388 * An http ver_token is any ASCII which can be found in an HTTP version,
389 * which includes 'H', 'T', 'P', '/', '.' and any digit.
390 */
391const char http_is_ver_token[256] = {
392 ['.'] = 1, ['/'] = 1,
393 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
394 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
395 ['H'] = 1, ['P'] = 1, ['T'] = 1,
396};
397
398
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100399/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100400 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
401 */
402#if defined(DEBUG_FSM)
403static void http_silent_debug(int line, struct session *s)
404{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100405 chunk_printf(&trash,
406 "[%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",
407 line,
408 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
409 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);
410 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100411
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100412 chunk_printf(&trash,
413 " %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",
414 line,
415 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
416 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);
417 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100418}
419#else
420#define http_silent_debug(l,s) do { } while (0)
421#endif
422
423/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100424 * Adds a header and its CRLF at the tail of the message's buffer, just before
425 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 * The header is also automatically added to the index <hdr_idx>, and the end
427 * of headers is automatically adjusted. The number of bytes added is returned
428 * on success, otherwise <0 is returned indicating an error.
429 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100430int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100431{
432 int bytes, len;
433
434 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200435 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100436 if (!bytes)
437 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100438 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
440}
441
442/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100443 * Adds a header and its CRLF at the tail of the message's buffer, just before
444 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100445 * the buffer is only opened and the space reserved, but nothing is copied.
446 * The header is also automatically added to the index <hdr_idx>, and the end
447 * of headers is automatically adjusted. The number of bytes added is returned
448 * on success, otherwise <0 is returned indicating an error.
449 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100450int http_header_add_tail2(struct http_msg *msg,
451 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100452{
453 int bytes;
454
Willy Tarreau9b28e032012-10-12 23:49:43 +0200455 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456 if (!bytes)
457 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100458 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
460}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461
462/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100463 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
464 * If so, returns the position of the first non-space character relative to
465 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
466 * to return a pointer to the place after the first space. Returns 0 if the
467 * header name does not match. Checks are case-insensitive.
468 */
469int http_header_match2(const char *hdr, const char *end,
470 const char *name, int len)
471{
472 const char *val;
473
474 if (hdr + len >= end)
475 return 0;
476 if (hdr[len] != ':')
477 return 0;
478 if (strncasecmp(hdr, name, len) != 0)
479 return 0;
480 val = hdr + len + 1;
481 while (val < end && HTTP_IS_SPHT(*val))
482 val++;
483 if ((val >= end) && (len + 2 <= end - hdr))
484 return len + 2; /* we may replace starting from second space */
485 return val - hdr;
486}
487
Willy Tarreau68085d82010-01-18 14:54:04 +0100488/* Find the end of the header value contained between <s> and <e>. See RFC2616,
489 * par 2.2 for more information. Note that it requires a valid header to return
490 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100492char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200493{
494 int quoted, qdpair;
495
496 quoted = qdpair = 0;
497 for (; s < e; s++) {
498 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200499 else if (quoted) {
500 if (*s == '\\') qdpair = 1;
501 else if (*s == '"') quoted = 0;
502 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200503 else if (*s == '"') quoted = 1;
504 else if (*s == ',') return s;
505 }
506 return s;
507}
508
509/* Find the first or next occurrence of header <name> in message buffer <sol>
510 * using headers index <idx>, and return it in the <ctx> structure. This
511 * structure holds everything necessary to use the header and find next
512 * occurrence. If its <idx> member is 0, the header is searched from the
513 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 * 1 when it finds a value, and 0 when there is no more. It is designed to work
515 * with headers defined as comma-separated lists. As a special case, if ctx->val
516 * is NULL when searching for a new values of a header, the current header is
517 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 */
519int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100520 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 struct hdr_ctx *ctx)
522{
Willy Tarreau68085d82010-01-18 14:54:04 +0100523 char *eol, *sov;
524 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200525
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 cur_idx = ctx->idx;
527 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528 /* We have previously returned a value, let's search
529 * another one on the same line.
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200532 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100533 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 eol = sol + idx->v[cur_idx].len;
535
536 if (sov >= eol)
537 /* no more values in this header */
538 goto next_hdr;
539
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 /* values remaining for this header, skip the comma but save it
541 * for later use (eg: for header deletion).
542 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 sov++;
544 while (sov < eol && http_is_lws[(unsigned char)*sov])
545 sov++;
546
547 goto return_hdr;
548 }
549
550 /* first request for this header */
551 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100552 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200554 while (cur_idx) {
555 eol = sol + idx->v[cur_idx].len;
556
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200557 if (len == 0) {
558 /* No argument was passed, we want any header.
559 * To achieve this, we simply build a fake request. */
560 while (sol + len < eol && sol[len] != ':')
561 len++;
562 name = sol;
563 }
564
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 if ((len < eol - sol) &&
566 (sol[len] == ':') &&
567 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100568 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200569 sov = sol + len + 1;
570 while (sov < eol && http_is_lws[(unsigned char)*sov])
571 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100572
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100574 ctx->prev = old_idx;
575 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 ctx->idx = cur_idx;
577 ctx->val = sov - sol;
578
579 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200580 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200581 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200582 eol--;
583 ctx->tws++;
584 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200585 ctx->vlen = eol - sov;
586 return 1;
587 }
588 next_hdr:
589 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100590 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 cur_idx = idx->v[cur_idx].next;
592 }
593 return 0;
594}
595
596int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100597 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200598 struct hdr_ctx *ctx)
599{
600 return http_find_header2(name, strlen(name), sol, idx, ctx);
601}
602
Willy Tarreau68085d82010-01-18 14:54:04 +0100603/* Remove one value of a header. This only works on a <ctx> returned by one of
604 * the http_find_header functions. The value is removed, as well as surrounding
605 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100606 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100607 * message <msg>. The new index is returned. If it is zero, it means there is
608 * no more header, so any processing may stop. The ctx is always left in a form
609 * that can be handled by http_find_header2() to find next occurrence.
610 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100611int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100612{
613 int cur_idx = ctx->idx;
614 char *sol = ctx->line;
615 struct hdr_idx_elem *hdr;
616 int delta, skip_comma;
617
618 if (!cur_idx)
619 return 0;
620
621 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200624 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 http_msg_move_end(msg, delta);
626 idx->used--;
627 hdr->len = 0; /* unused entry */
628 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100629 if (idx->tail == ctx->idx)
630 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100631 ctx->idx = ctx->prev; /* walk back to the end of previous header */
632 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
633 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 return ctx->idx;
636 }
637
638 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200639 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
640 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 */
642
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200643 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200644 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200645 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 NULL, 0);
647 hdr->len += delta;
648 http_msg_move_end(msg, delta);
649 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200650 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100651 return ctx->idx;
652}
653
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100654/* This function handles a server error at the stream interface level. The
655 * stream interface is assumed to be already in a closed state. An optional
656 * message is copied into the input buffer, and an HTTP status code stored.
657 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100658 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100660static void http_server_error(struct session *t, struct stream_interface *si,
661 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200663 channel_auto_read(si->ob);
664 channel_abort(si->ob);
665 channel_auto_close(si->ob);
666 channel_erase(si->ob);
667 channel_auto_close(si->ib);
668 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100669 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100670 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200671 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 }
673 if (!(t->flags & SN_ERR_MASK))
674 t->flags |= err;
675 if (!(t->flags & SN_FINST_MASK))
676 t->flags |= finst;
677}
678
Willy Tarreau80587432006-12-24 17:47:20 +0100679/* This function returns the appropriate error location for the given session
680 * and message.
681 */
682
Willy Tarreau783f2582012-09-04 12:19:04 +0200683struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100684{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200685 if (s->be->errmsg[msgnum].str)
686 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100687 else if (s->fe->errmsg[msgnum].str)
688 return &s->fe->errmsg[msgnum];
689 else
690 return &http_err_chunks[msgnum];
691}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692
Willy Tarreau53b6c742006-12-17 13:37:46 +0100693/*
694 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
695 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
696 */
697static http_meth_t find_http_meth(const char *str, const int len)
698{
699 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100700 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100701
702 m = ((unsigned)*str - 'A');
703
704 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100705 for (h = http_methods[m]; h->len > 0; h++) {
706 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100707 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100708 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100709 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100710 };
711 return HTTP_METH_OTHER;
712 }
713 return HTTP_METH_NONE;
714
715}
716
Willy Tarreau21d2af32008-02-14 20:25:24 +0100717/* Parse the URI from the given transaction (which is assumed to be in request
718 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
719 * It is returned otherwise.
720 */
721static char *
722http_get_path(struct http_txn *txn)
723{
724 char *ptr, *end;
725
Willy Tarreau9b28e032012-10-12 23:49:43 +0200726 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100727 end = ptr + txn->req.sl.rq.u_l;
728
729 if (ptr >= end)
730 return NULL;
731
732 /* RFC2616, par. 5.1.2 :
733 * Request-URI = "*" | absuri | abspath | authority
734 */
735
736 if (*ptr == '*')
737 return NULL;
738
739 if (isalpha((unsigned char)*ptr)) {
740 /* this is a scheme as described by RFC3986, par. 3.1 */
741 ptr++;
742 while (ptr < end &&
743 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
744 ptr++;
745 /* skip '://' */
746 if (ptr == end || *ptr++ != ':')
747 return NULL;
748 if (ptr == end || *ptr++ != '/')
749 return NULL;
750 if (ptr == end || *ptr++ != '/')
751 return NULL;
752 }
753 /* skip [user[:passwd]@]host[:[port]] */
754
755 while (ptr < end && *ptr != '/')
756 ptr++;
757
758 if (ptr == end)
759 return NULL;
760
761 /* OK, we got the '/' ! */
762 return ptr;
763}
764
Willy Tarreau71241ab2012-12-27 11:30:54 +0100765/* Returns a 302 for a redirectable request that reaches a server working in
766 * in redirect mode. This may only be called just after the stream interface
767 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
768 * follow normal proxy processing. NOTE: this function is designed to support
769 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100771void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772{
773 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200776 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100777
778 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100779 trash.len = strlen(HTTP_302);
780 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100781
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100782 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100783
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100785 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786 return;
787
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100788 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100789 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100790 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
791 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100792 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200794 /* 3: add the request URI. Since it was already forwarded, we need
795 * to temporarily rewind the buffer.
796 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200798 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200799
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200801 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 +0200802
Willy Tarreau9b28e032012-10-12 23:49:43 +0200803 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200804
Willy Tarreauefb453c2008-10-26 20:49:47 +0100805 if (!path)
806 return;
807
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100808 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809 return;
810
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100811 memcpy(trash.str + trash.len, path, len);
812 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100813
814 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100815 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
816 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100817 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
819 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100820 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821
822 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200823 si_shutr(si);
824 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 si->err_type = SI_ET_NONE;
826 si->err_loc = NULL;
827 si->state = SI_ST_CLO;
828
829 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100830 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831
832 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100833 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834}
835
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100836/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 * that the server side is closed. Note that err_type is actually a
838 * bitmask, where almost only aborts may be cumulated with other
839 * values. We consider that aborted operations are more important
840 * than timeouts or errors due to the fact that nobody else in the
841 * logs might explain incomplete retries. All others should avoid
842 * being cumulated. It should normally not be possible to have multiple
843 * aborts at once, but just in case, the first one in sequence is reported.
844 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100845void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100847 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100848
849 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200851 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100859 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200860 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100861 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100862 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200863 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100864 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100865 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200866 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100867 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100868 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200869 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870}
871
Willy Tarreau42250582007-04-01 01:30:43 +0200872extern const char sess_term_cond[8];
873extern const char sess_fin_state[8];
874extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200875struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100876struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100877struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100878
Willy Tarreau117f59e2007-03-04 18:17:17 +0100879/*
880 * Capture headers from message starting at <som> according to header list
881 * <cap_hdr>, and fill the <idx> structure appropriately.
882 */
883void capture_headers(char *som, struct hdr_idx *idx,
884 char **cap, struct cap_hdr *cap_hdr)
885{
886 char *eol, *sol, *col, *sov;
887 int cur_idx;
888 struct cap_hdr *h;
889 int len;
890
891 sol = som + hdr_idx_first_pos(idx);
892 cur_idx = hdr_idx_first_idx(idx);
893
894 while (cur_idx) {
895 eol = sol + idx->v[cur_idx].len;
896
897 col = sol;
898 while (col < eol && *col != ':')
899 col++;
900
901 sov = col + 1;
902 while (sov < eol && http_is_lws[(unsigned char)*sov])
903 sov++;
904
905 for (h = cap_hdr; h; h = h->next) {
906 if ((h->namelen == col - sol) &&
907 (strncasecmp(sol, h->name, h->namelen) == 0)) {
908 if (cap[h->index] == NULL)
909 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200910 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100911
912 if (cap[h->index] == NULL) {
913 Alert("HTTP capture : out of memory.\n");
914 continue;
915 }
916
917 len = eol - sov;
918 if (len > h->len)
919 len = h->len;
920
921 memcpy(cap[h->index], sov, len);
922 cap[h->index][len]=0;
923 }
924 }
925 sol = eol + idx->v[cur_idx].cr + 1;
926 cur_idx = idx->v[cur_idx].next;
927 }
928}
929
930
Willy Tarreau42250582007-04-01 01:30:43 +0200931/* either we find an LF at <ptr> or we jump to <bad>.
932 */
933#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
934
935/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
936 * otherwise to <http_msg_ood> with <state> set to <st>.
937 */
938#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
939 ptr++; \
940 if (likely(ptr < end)) \
941 goto good; \
942 else { \
943 state = (st); \
944 goto http_msg_ood; \
945 } \
946 } while (0)
947
948
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100950 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
952 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
953 * will give undefined results.
954 * Note that it is upon the caller's responsibility to ensure that ptr < end,
955 * and that msg->sol points to the beginning of the response.
956 * If a complete line is found (which implies that at least one CR or LF is
957 * found before <end>, the updated <ptr> is returned, otherwise NULL is
958 * returned indicating an incomplete line (which does not mean that parts have
959 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
960 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
961 * upon next call.
962 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200963 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
965 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200966 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200968const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100969 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100970 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100971{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200972 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100973
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200976 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100977 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
979
980 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100981 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
983 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100984 state = HTTP_MSG_ERROR;
985 break;
986
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200988 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100990 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991 goto http_msg_rpcode;
992 }
993 if (likely(HTTP_IS_SPHT(*ptr)))
994 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
995 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100996 state = HTTP_MSG_ERROR;
997 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100998
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001000 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 if (likely(!HTTP_IS_LWS(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1003
1004 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001005 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001006 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1007 }
1008
1009 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001010 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 http_msg_rsp_reason:
1012 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 msg->sl.st.r_l = 0;
1015 goto http_msg_rpline_eol;
1016
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001018 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001019 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001020 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 goto http_msg_rpreason;
1022 }
1023 if (likely(HTTP_IS_SPHT(*ptr)))
1024 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1025 /* so it's a CR/LF, so there is no reason phrase */
1026 goto http_msg_rsp_reason;
1027
Willy Tarreau8973c702007-01-21 23:58:29 +01001028 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001029 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001030 if (likely(!HTTP_IS_CRLF(*ptr)))
1031 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001032 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 http_msg_rpline_eol:
1034 /* We have seen the end of line. Note that we do not
1035 * necessarily have the \n yet, but at least we know that we
1036 * have EITHER \r OR \n, otherwise the response would not be
1037 * complete. We can then record the response length and return
1038 * to the caller which will be able to register it.
1039 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001040 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001041 return ptr;
1042
1043#ifdef DEBUG_FULL
1044 default:
1045 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1046 exit(1);
1047#endif
1048 }
1049
1050 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001051 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001052 if (ret_state)
1053 *ret_state = state;
1054 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001055 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001056 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001057}
1058
Willy Tarreau8973c702007-01-21 23:58:29 +01001059/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 * This function parses a request line between <ptr> and <end>, starting with
1061 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1062 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1063 * will give undefined results.
1064 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1065 * and that msg->sol points to the beginning of the request.
1066 * If a complete line is found (which implies that at least one CR or LF is
1067 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1068 * returned indicating an incomplete line (which does not mean that parts have
1069 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1070 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1071 * upon next call.
1072 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001073 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1075 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001076 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001078const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001079 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001080 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001082 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001086 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 if (likely(HTTP_IS_TOKEN(*ptr)))
1088 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001089
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1093 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 if (likely(HTTP_IS_CRLF(*ptr))) {
1096 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001097 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001099 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001101 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001103 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 msg->sl.rq.v_l = 0;
1105 goto http_msg_rqline_eol;
1106 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001107 state = HTTP_MSG_ERROR;
1108 break;
1109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001111 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001113 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 goto http_msg_rquri;
1115 }
1116 if (likely(HTTP_IS_SPHT(*ptr)))
1117 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1118 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1119 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001122 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001126 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001127 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1129 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001130
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001131 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001132 /* non-ASCII chars are forbidden unless option
1133 * accept-invalid-http-request is enabled in the frontend.
1134 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001135 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001136 if (msg->err_pos < -1)
1137 goto invalid_char;
1138 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001139 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1141 }
1142
1143 if (likely(HTTP_IS_CRLF(*ptr))) {
1144 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1145 goto http_msg_req09_uri_e;
1146 }
1147
1148 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001149 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001150 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001151 state = HTTP_MSG_ERROR;
1152 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001155 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001157 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 goto http_msg_rqver;
1159 }
1160 if (likely(HTTP_IS_SPHT(*ptr)))
1161 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1162 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1163 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001164
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001166 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001167 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001169
1170 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172 http_msg_rqline_eol:
1173 /* We have seen the end of line. Note that we do not
1174 * necessarily have the \n yet, but at least we know that we
1175 * have EITHER \r OR \n, otherwise the request would not be
1176 * complete. We can then record the request length and return
1177 * to the caller which will be able to register it.
1178 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001179 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001180 return ptr;
1181 }
1182
1183 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001184 state = HTTP_MSG_ERROR;
1185 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187#ifdef DEBUG_FULL
1188 default:
1189 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1190 exit(1);
1191#endif
1192 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001195 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 if (ret_state)
1197 *ret_state = state;
1198 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001199 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001201}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001203/*
1204 * Returns the data from Authorization header. Function may be called more
1205 * than once so data is stored in txn->auth_data. When no header is found
1206 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1207 * searching again for something we are unable to find anyway.
1208 */
1209
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001210char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001211
1212int
1213get_http_auth(struct session *s)
1214{
1215
1216 struct http_txn *txn = &s->txn;
1217 struct chunk auth_method;
1218 struct hdr_ctx ctx;
1219 char *h, *p;
1220 int len;
1221
1222#ifdef DEBUG_AUTH
1223 printf("Auth for session %p: %d\n", s, txn->auth.method);
1224#endif
1225
1226 if (txn->auth.method == HTTP_AUTH_WRONG)
1227 return 0;
1228
1229 if (txn->auth.method)
1230 return 1;
1231
1232 txn->auth.method = HTTP_AUTH_WRONG;
1233
1234 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001235
1236 if (txn->flags & TX_USE_PX_CONN) {
1237 h = "Proxy-Authorization";
1238 len = strlen(h);
1239 } else {
1240 h = "Authorization";
1241 len = strlen(h);
1242 }
1243
Willy Tarreau9b28e032012-10-12 23:49:43 +02001244 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001245 return 0;
1246
1247 h = ctx.line + ctx.val;
1248
1249 p = memchr(h, ' ', ctx.vlen);
1250 if (!p || p == h)
1251 return 0;
1252
1253 chunk_initlen(&auth_method, h, 0, p-h);
1254 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1255
1256 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1257
1258 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001259 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260
1261 if (len < 0)
1262 return 0;
1263
1264
1265 get_http_auth_buff[len] = '\0';
1266
1267 p = strchr(get_http_auth_buff, ':');
1268
1269 if (!p)
1270 return 0;
1271
1272 txn->auth.user = get_http_auth_buff;
1273 *p = '\0';
1274 txn->auth.pass = p+1;
1275
1276 txn->auth.method = HTTP_AUTH_BASIC;
1277 return 1;
1278 }
1279
1280 return 0;
1281}
1282
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8973c702007-01-21 23:58:29 +01001284/*
1285 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001286 * depending on the initial msg->msg_state. The caller is responsible for
1287 * ensuring that the message does not wrap. The function can be preempted
1288 * everywhere when data are missing and recalled at the exact same location
1289 * with no information loss. The message may even be realigned between two
1290 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001291 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001292 * fields. Note that msg->sol will be initialized after completing the first
1293 * state, so that none of the msg pointers has to be initialized prior to the
1294 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001296void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001298 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001300 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001302 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001303 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001304 ptr = buf->p + msg->next;
1305 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 if (unlikely(ptr >= end))
1308 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 /*
1312 * First, states that are specific to the response only.
1313 * We check them first so that request and headers are
1314 * closer to each other (accessed more often).
1315 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001319 /* we have a start of message, but we have to check
1320 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001321 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001322 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001323 if (unlikely(ptr != buf->p)) {
1324 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001325 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001326 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001327 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 }
Willy Tarreau26927362012-05-18 23:22:52 +02001329 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001330 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 hdr_idx_init(idx);
1332 state = HTTP_MSG_RPVER;
1333 goto http_msg_rpver;
1334 }
1335
1336 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1337 goto http_msg_invalid;
1338
1339 if (unlikely(*ptr == '\n'))
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1342 /* stop here */
1343
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001345 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 EXPECT_LF_HERE(ptr, http_msg_invalid);
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1348 /* stop here */
1349
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001351 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPVER_SP:
1353 case HTTP_MSG_RPCODE:
1354 case HTTP_MSG_RPCODE_SP:
1355 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001356 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001357 state, ptr, end,
1358 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 if (unlikely(!ptr))
1360 return;
1361
1362 /* we have a full response and we know that we have either a CR
1363 * or an LF at <ptr>.
1364 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001365 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1366
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001367 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 if (likely(*ptr == '\r'))
1369 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1370 goto http_msg_rpline_end;
1371
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001373 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 /* msg->sol must point to the first of CR or LF. */
1375 EXPECT_LF_HERE(ptr, http_msg_invalid);
1376 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1377 /* stop here */
1378
1379 /*
1380 * Second, states that are specific to the request only
1381 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001383 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001385 /* we have a start of message, but we have to check
1386 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001387 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001388 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001389 if (likely(ptr != buf->p)) {
1390 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001392 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001393 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 }
Willy Tarreau26927362012-05-18 23:22:52 +02001395 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001396 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 state = HTTP_MSG_RQMETH;
1398 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1402 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 if (unlikely(*ptr == '\n'))
1405 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001407 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001410 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 EXPECT_LF_HERE(ptr, http_msg_invalid);
1412 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001416 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 case HTTP_MSG_RQMETH_SP:
1418 case HTTP_MSG_RQURI:
1419 case HTTP_MSG_RQURI_SP:
1420 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001421 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001422 state, ptr, end,
1423 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 if (unlikely(!ptr))
1425 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* we have a full request and we know that we have either a CR
1428 * or an LF at <ptr>.
1429 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001432 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 if (likely(*ptr == '\r'))
1434 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 /* check for HTTP/0.9 request : no version information available.
1440 * msg->sol must point to the first of CR or LF.
1441 */
1442 if (unlikely(msg->sl.rq.v_l == 0))
1443 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 EXPECT_LF_HERE(ptr, http_msg_invalid);
1446 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001448
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 /*
1450 * Common states below
1451 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001453 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001454 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 if (likely(!HTTP_IS_CRLF(*ptr))) {
1456 goto http_msg_hdr_name;
1457 }
1458
1459 if (likely(*ptr == '\r'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1461 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001464 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* assumes msg->sol points to the first char */
1466 if (likely(HTTP_IS_TOKEN(*ptr)))
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001468
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001469 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001472 if (likely(msg->err_pos < -1) || *ptr == '\n')
1473 goto http_msg_invalid;
1474
1475 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001477
1478 /* and we still accept this non-token character */
1479 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001483 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 if (likely(HTTP_IS_SPHT(*ptr)))
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001488 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 if (likely(!HTTP_IS_CRLF(*ptr))) {
1491 goto http_msg_hdr_val;
1492 }
1493
1494 if (likely(*ptr == '\r'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1496 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001499 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 EXPECT_LF_HERE(ptr, http_msg_invalid);
1501 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (likely(HTTP_IS_SPHT(*ptr))) {
1506 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001507 for (; buf->p + msg->sov < ptr; msg->sov++)
1508 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 goto http_msg_hdr_l1_sp;
1510 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001511 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001512 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 goto http_msg_complete_header;
1514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001516 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001517 /* assumes msg->sol points to the first char, and msg->sov
1518 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 */
1520 if (likely(!HTTP_IS_CRLF(*ptr)))
1521 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 /* Note: we could also copy eol into ->eoh so that we have the
1525 * real header end in case it ends with lots of LWS, but is this
1526 * really needed ?
1527 */
1528 if (likely(*ptr == '\r'))
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1530 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001533 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 EXPECT_LF_HERE(ptr, http_msg_invalid);
1535 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1540 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001541 for (; buf->p + msg->eol < ptr; msg->eol++)
1542 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 goto http_msg_hdr_val;
1544 }
1545 http_msg_complete_header:
1546 /*
1547 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001548 * Assumes msg->sol points to the first char, msg->sov points
1549 * to the first character of the value and msg->eol to the
1550 * first CR or LF so we know how the line ends. We insert last
1551 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001553 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 idx, idx->tail) < 0))
1555 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001556
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001557 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 if (likely(!HTTP_IS_CRLF(*ptr))) {
1559 goto http_msg_hdr_name;
1560 }
1561
1562 if (likely(*ptr == '\r'))
1563 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1564 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001567 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 /* Assumes msg->sol points to the first of either CR or LF */
1569 EXPECT_LF_HERE(ptr, http_msg_invalid);
1570 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001571 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001572 msg->eoh = msg->sol;
1573 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001574 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001576
1577 case HTTP_MSG_ERROR:
1578 /* this may only happen if we call http_msg_analyser() twice with an error */
1579 break;
1580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581#ifdef DEBUG_FULL
1582 default:
1583 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1584 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001585#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 }
1587 http_msg_ood:
1588 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001589 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001590 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 http_msg_invalid:
1594 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001595 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001596 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 return;
1598}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001599
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001600/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1601 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1602 * nothing is done and 1 is returned.
1603 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001604static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001605{
1606 int delta;
1607 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001608 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609
1610 if (msg->sl.rq.v_l != 0)
1611 return 1;
1612
Willy Tarreau9b28e032012-10-12 23:49:43 +02001613 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 delta = 0;
1615
1616 if (msg->sl.rq.u_l == 0) {
1617 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001618 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001619 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001620 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001621 }
1622 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001623 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001624 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001625 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001626 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001627 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001628 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001629 NULL, NULL);
1630 if (unlikely(!cur_end))
1631 return 0;
1632
1633 /* we have a full HTTP/1.0 request now and we know that
1634 * we have either a CR or an LF at <ptr>.
1635 */
1636 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1637 return 1;
1638}
1639
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001640/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001641 * and "keep-alive" values. If we already know that some headers may safely
1642 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1644 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001645 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1647 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1648 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001649 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001650 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001651void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001652{
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001654 const char *hdr_val = "Connection";
1655 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001656
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001658 return;
1659
Willy Tarreau88d349d2010-01-25 12:15:43 +01001660 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1661 hdr_val = "Proxy-Connection";
1662 hdr_len = 16;
1663 }
1664
Willy Tarreau5b154472009-12-21 20:11:07 +01001665 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001666 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001667 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001668 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1669 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001670 if (to_del & 2)
1671 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672 else
1673 txn->flags |= TX_CON_KAL_SET;
1674 }
1675 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1676 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001677 if (to_del & 1)
1678 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679 else
1680 txn->flags |= TX_CON_CLO_SET;
1681 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001682 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1683 txn->flags |= TX_HDR_CONN_UPG;
1684 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001685 }
1686
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001687 txn->flags |= TX_HDR_CONN_PRS;
1688 return;
1689}
Willy Tarreau5b154472009-12-21 20:11:07 +01001690
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691/* Apply desired changes on the Connection: header. Values may be removed and/or
1692 * added depending on the <wanted> flags, which are exclusively composed of
1693 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1694 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1695 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001696void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001697{
1698 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001699 const char *hdr_val = "Connection";
1700 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701
1702 ctx.idx = 0;
1703
Willy Tarreau88d349d2010-01-25 12:15:43 +01001704
1705 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1706 hdr_val = "Proxy-Connection";
1707 hdr_len = 16;
1708 }
1709
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001710 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001711 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1713 if (wanted & TX_CON_KAL_SET)
1714 txn->flags |= TX_CON_KAL_SET;
1715 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001716 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001717 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1719 if (wanted & TX_CON_CLO_SET)
1720 txn->flags |= TX_CON_CLO_SET;
1721 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001722 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001723 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001724 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001725
1726 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1727 return;
1728
1729 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1730 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001731 hdr_val = "Connection: close";
1732 hdr_len = 17;
1733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection: close";
1735 hdr_len = 23;
1736 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001737 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 }
1739
1740 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1741 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001742 hdr_val = "Connection: keep-alive";
1743 hdr_len = 22;
1744 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1745 hdr_val = "Proxy-Connection: keep-alive";
1746 hdr_len = 28;
1747 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001748 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001749 }
1750 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001751}
1752
Willy Tarreaua458b672012-03-05 11:17:50 +01001753/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001754 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001755 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001756 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001758 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001759static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001760{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001761 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001763 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001764 const char *end = buf->data + buf->size;
1765 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 unsigned int chunk = 0;
1767
1768 /* The chunk size is in the following form, though we are only
1769 * interested in the size and CRLF :
1770 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1771 */
1772 while (1) {
1773 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001774 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001775 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001776 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001777 if (c < 0) /* not a hex digit anymore */
1778 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001779 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001781 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001782 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 chunk = (chunk << 4) + c;
1784 }
1785
Willy Tarreaud98cf932009-12-27 22:54:55 +01001786 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001787 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001788 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789
1790 while (http_is_spht[(unsigned char)*ptr]) {
1791 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001792 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001793 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001794 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001795 }
1796
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 /* Up to there, we know that at least one byte is present at *ptr. Check
1798 * for the end of chunk size.
1799 */
1800 while (1) {
1801 if (likely(HTTP_IS_CRLF(*ptr))) {
1802 /* we now have a CR or an LF at ptr */
1803 if (likely(*ptr == '\r')) {
1804 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001805 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001806 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001807 return 0;
1808 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001809
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001811 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001813 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 /* done */
1815 break;
1816 }
1817 else if (*ptr == ';') {
1818 /* chunk extension, ends at next CRLF */
1819 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001821 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001823
1824 while (!HTTP_IS_CRLF(*ptr)) {
1825 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001826 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001827 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 return 0;
1829 }
1830 /* we have a CRLF now, loop above */
1831 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001832 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001834 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 }
1836
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001838 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001841 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001842 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001844 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001845 msg->chunk_len = chunk;
1846 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001847 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001848 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001849 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001850 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001851 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001852}
1853
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001854/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001855 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 * the trailers is found, it is automatically scheduled to be forwarded,
1857 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1858 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001859 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001860 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001861 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001862 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1863 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001864 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 * matches the length of trailers already parsed and not forwarded. It is also
1866 * important to note that this function is designed to be able to parse wrapped
1867 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001869static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001870{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001871 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001872
Willy Tarreaua458b672012-03-05 11:17:50 +01001873 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001874 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001875 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001876 const char *ptr = b_ptr(buf, msg->next);
1877 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001878 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879
1880 /* scan current line and stop at LF or CRLF */
1881 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001882 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 return 0;
1884
1885 if (*ptr == '\n') {
1886 if (!p1)
1887 p1 = ptr;
1888 p2 = ptr;
1889 break;
1890 }
1891
1892 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001893 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001894 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001896 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001897 p1 = ptr;
1898 }
1899
1900 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001901 if (ptr >= buf->data + buf->size)
1902 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001903 }
1904
1905 /* after LF; point to beginning of next line */
1906 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001907 if (p2 >= buf->data + buf->size)
1908 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001911 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001913
1914 /* schedule this line for forwarding */
1915 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 if (msg->sov >= buf->size)
1917 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001920 /* LF/CRLF at beginning of line => end of trailers at p2.
1921 * Everything was scheduled for forwarding, there's nothing
1922 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001923 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001924 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 msg->msg_state = HTTP_MSG_DONE;
1926 return 1;
1927 }
1928 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 }
1931}
1932
Willy Tarreau54d23df2012-10-25 19:04:45 +02001933/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001935 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001936 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1938 * not enough data are available, the function does not change anything and
1939 * returns zero. If a parse error is encountered, the function returns < 0 and
1940 * does not change anything. Note: this function is designed to parse wrapped
1941 * CRLF at the end of the buffer.
1942 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001943static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001945 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001946 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 int bytes;
1948
1949 /* NB: we'll check data availabilty at the end. It's not a
1950 * problem because whatever we match first will be checked
1951 * against the correct length.
1952 */
1953 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 if (*ptr == '\r') {
1956 bytes++;
1957 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001958 if (ptr >= buf->data + buf->size)
1959 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 }
1961
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001962 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 return 0;
1964
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001965 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001967 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969
1970 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02001971 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001972 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001973 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1974 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001975 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1977 return 1;
1978}
Willy Tarreau5b154472009-12-21 20:11:07 +01001979
William Lallemand82fe75c2012-10-23 10:25:10 +02001980
1981/*
1982 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001983 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001984int select_compression_request_header(struct session *s, struct buffer *req)
1985{
1986 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001987 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001988 struct hdr_ctx ctx;
1989 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001990 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001991
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001992 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1993 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001994 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1995 */
1996 ctx.idx = 0;
1997 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1998 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001999 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2000 (ctx.vlen < 31 ||
2001 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2002 ctx.line[ctx.val + 30] < '6' ||
2003 (ctx.line[ctx.val + 30] == '6' &&
2004 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2005 s->comp_algo = NULL;
2006 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002007 }
2008
William Lallemand82fe75c2012-10-23 10:25:10 +02002009 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002010 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 +02002011 ctx.idx = 0;
2012 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002013 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002014 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2015 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002016
2017 /* remove all occurrences of the header when "compression offload" is set */
2018
2019 if ((s->be->comp && s->be->comp->offload) ||
2020 (s->fe->comp && s->fe->comp->offload)) {
2021 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2022 ctx.idx = 0;
2023 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2024 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2025 }
2026 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002027 return 1;
2028 }
2029 }
2030 }
2031 }
2032
2033 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002034 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2035 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002036 if (comp_algo->add_data == identity_add_data) {
2037 s->comp_algo = comp_algo;
2038 return 1;
2039 }
2040 }
2041 }
2042
2043 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002044 return 0;
2045}
2046
2047/*
2048 * Selects a comression algorithm depending of the server response.
2049 */
2050int select_compression_response_header(struct session *s, struct buffer *res)
2051{
2052 struct http_txn *txn = &s->txn;
2053 struct http_msg *msg = &txn->rsp;
2054 struct hdr_ctx ctx;
2055 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002056
2057 /* no common compression algorithm was found in request header */
2058 if (s->comp_algo == NULL)
2059 goto fail;
2060
2061 /* HTTP < 1.1 should not be compressed */
2062 if (!(msg->flags & HTTP_MSGF_VER_11))
2063 goto fail;
2064
William Lallemandd3002612012-11-26 14:34:47 +01002065 /* 200 only */
2066 if (txn->status != 200)
2067 goto fail;
2068
William Lallemand82fe75c2012-10-23 10:25:10 +02002069 /* Content-Length is null */
2070 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2071 goto fail;
2072
2073 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002074 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002075 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2076 goto fail;
2077
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002078 /* no compression when Cache-Control: no-transform is present in the message */
2079 ctx.idx = 0;
2080 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2081 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2082 goto fail;
2083 }
2084
William Lallemand82fe75c2012-10-23 10:25:10 +02002085 comp_type = NULL;
2086
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002087 /* we don't want to compress multipart content-types, nor content-types that are
2088 * not listed in the "compression type" directive if any. If no content-type was
2089 * found but configuration requires one, we don't compress either. Backend has
2090 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002091 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002092 ctx.idx = 0;
2093 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2094 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2095 goto fail;
2096
2097 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2098 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002099 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002100 if (ctx.vlen >= comp_type->name_len &&
2101 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002102 /* this Content-Type should be compressed */
2103 break;
2104 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002105 /* this Content-Type should not be compressed */
2106 if (comp_type == NULL)
2107 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002108 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002109 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002110 else { /* no content-type header */
2111 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2112 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002113 }
2114
William Lallemandd85f9172012-11-09 17:05:39 +01002115 /* limit compression rate */
2116 if (global.comp_rate_lim > 0)
2117 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2118 goto fail;
2119
William Lallemand072a2bf2012-11-20 17:01:01 +01002120 /* limit cpu usage */
2121 if (idle_pct < compress_min_idle)
2122 goto fail;
2123
William Lallemand4c49fae2012-11-07 15:00:23 +01002124 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002125 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002126 goto fail;
2127
William Lallemandec3e3892012-11-12 17:02:18 +01002128 s->flags |= SN_COMP_READY;
2129
William Lallemand82fe75c2012-10-23 10:25:10 +02002130 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002131 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002132 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2133 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2134
2135 /* add Transfer-Encoding header */
2136 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2137 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2138
2139 /*
2140 * Add Content-Encoding header when it's not identity encoding.
2141 * RFC 2616 : Identity encoding: This content-coding is used only in the
2142 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2143 * header.
2144 */
2145 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002146 trash.len = 18;
2147 memcpy(trash.str, "Content-Encoding: ", trash.len);
2148 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2149 trash.len += s->comp_algo->name_len;
2150 trash.str[trash.len] = '\0';
2151 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002152 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002153 return 1;
2154
2155fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002156 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002157 return 0;
2158}
2159
2160
Willy Tarreaud787e662009-07-07 10:14:51 +02002161/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2162 * processing can continue on next analysers, or zero if it either needs more
2163 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2164 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2165 * when it has nothing left to do, and may remove any analyser when it wants to
2166 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002167 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002168int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002169{
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 /*
2171 * We will parse the partial (or complete) lines.
2172 * We will check the request syntax, and also join multi-line
2173 * headers. An index of all the lines will be elaborated while
2174 * parsing.
2175 *
2176 * For the parsing, we use a 28 states FSM.
2177 *
2178 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002179 * req->buf->p = beginning of request
2180 * req->buf->p + msg->eoh = end of processed headers / start of current one
2181 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002182 * msg->eol = end of current header or line (LF or CRLF)
2183 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002184 *
2185 * At end of parsing, we may perform a capture of the error (if any), and
2186 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2187 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2188 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002192 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002193 struct http_txn *txn = &s->txn;
2194 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002195 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002196
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002197 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 +01002198 now_ms, __FUNCTION__,
2199 s,
2200 req,
2201 req->rex, req->wex,
2202 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002203 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002204 req->analysers);
2205
Willy Tarreau52a0c602009-08-16 22:45:38 +02002206 /* we're speaking HTTP here, so let's speak HTTP to the client */
2207 s->srv_error = http_return_srv_error;
2208
Willy Tarreau83e3af02009-12-28 17:39:57 +01002209 /* There's a protected area at the end of the buffer for rewriting
2210 * purposes. We don't want to start to parse the request if the
2211 * protected area is affected, because we may have to move processed
2212 * data later, which is much more complicated.
2213 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002214 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002215 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002216 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002217 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2218 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2219 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002220 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002221 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002222 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002223 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002224 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002225 return 0;
2226 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002227 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2228 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2229 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002230 }
2231
Willy Tarreau065e8332010-01-08 00:30:20 +01002232 /* Note that we have the same problem with the response ; we
2233 * may want to send a redirect, error or anything which requires
2234 * some spare space. So we'll ensure that we have at least
2235 * maxrewrite bytes available in the response buffer before
2236 * processing that one. This will only affect pipelined
2237 * keep-alive requests.
2238 */
2239 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002240 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002241 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2242 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2243 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002244 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002245 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002246 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002247 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002248 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002249 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002250 return 0;
2251 }
2252 }
2253
Willy Tarreau9b28e032012-10-12 23:49:43 +02002254 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002255 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002256 }
2257
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 /* 1: we might have to print this header in debug mode */
2259 if (unlikely((global.mode & MODE_DEBUG) &&
2260 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002261 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002263
Willy Tarreau9b28e032012-10-12 23:49:43 +02002264 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002265 /* this is a bit complex : in case of error on the request line,
2266 * we know that rq.l is still zero, so we display only the part
2267 * up to the end of the line (truncated by debug_hdr).
2268 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002269 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 sol += hdr_idx_first_pos(&txn->hdr_idx);
2273 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002274
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 while (cur_idx) {
2276 eol = sol + txn->hdr_idx.v[cur_idx].len;
2277 debug_hdr("clihdr", s, sol, eol);
2278 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2279 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002280 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 }
2282
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Willy Tarreau59234e92008-11-30 23:51:27 +01002284 /*
2285 * Now we quickly check if we have found a full valid request.
2286 * If not so, we check the FD and buffer states before leaving.
2287 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002288 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002289 * requests are checked first. When waiting for a second request
2290 * on a keep-alive session, if we encounter and error, close, t/o,
2291 * we note the error in the session flags but don't set any state.
2292 * Since the error will be noted there, it will not be counted by
2293 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002294 * Last, we may increase some tracked counters' http request errors on
2295 * the cases that are deliberately the client's fault. For instance,
2296 * a timeout or connection reset is not counted as an error. However
2297 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002299
Willy Tarreau655dce92009-11-08 13:10:58 +01002300 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002304 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002305 session_inc_http_req_ctr(s);
2306 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002307 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* 1: Since we are in header mode, if there's no space
2312 * left for headers, we won't be able to free more
2313 * later, so the session will never terminate. We
2314 * must terminate it now.
2315 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002316 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 /* FIXME: check if URI is set and return Status
2318 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002319 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002320 session_inc_http_req_ctr(s);
2321 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002322 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002323 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002324 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 goto return_bad_req;
2326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002327
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002329 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002330 if (!(s->flags & SN_ERR_MASK))
2331 s->flags |= SN_ERR_CLICL;
2332
Willy Tarreaufcffa692010-01-10 14:21:19 +01002333 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002334 goto failed_keep_alive;
2335
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002337 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002338 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002339 session_inc_http_err_ctr(s);
2340 }
2341
Willy Tarreaudc979f22012-12-04 10:39:01 +01002342 txn->status = 400;
2343 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 msg->msg_state = HTTP_MSG_ERROR;
2345 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002346
Willy Tarreauda7ff642010-06-23 11:44:09 +02002347 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002348 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002349 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002350 if (s->listener->counters)
2351 s->listener->counters->failed_req++;
2352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 if (!(s->flags & SN_FINST_MASK))
2354 s->flags |= SN_FINST_R;
2355 return 0;
2356 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002359 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002360 if (!(s->flags & SN_ERR_MASK))
2361 s->flags |= SN_ERR_CLITO;
2362
Willy Tarreaufcffa692010-01-10 14:21:19 +01002363 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002364 goto failed_keep_alive;
2365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002367 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002368 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002369 session_inc_http_err_ctr(s);
2370 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002372 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002373 msg->msg_state = HTTP_MSG_ERROR;
2374 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002375
Willy Tarreauda7ff642010-06-23 11:44:09 +02002376 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002377 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002378 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002379 if (s->listener->counters)
2380 s->listener->counters->failed_req++;
2381
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 if (!(s->flags & SN_FINST_MASK))
2383 s->flags |= SN_FINST_R;
2384 return 0;
2385 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002386
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002388 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002389 if (!(s->flags & SN_ERR_MASK))
2390 s->flags |= SN_ERR_CLICL;
2391
Willy Tarreaufcffa692010-01-10 14:21:19 +01002392 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002393 goto failed_keep_alive;
2394
Willy Tarreau4076a152009-04-02 15:18:36 +02002395 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002396 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002398 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 msg->msg_state = HTTP_MSG_ERROR;
2400 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002401
Willy Tarreauda7ff642010-06-23 11:44:09 +02002402 session_inc_http_err_ctr(s);
2403 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002404 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002405 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002406 if (s->listener->counters)
2407 s->listener->counters->failed_req++;
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 if (!(s->flags & SN_FINST_MASK))
2410 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002411 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002412 }
2413
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002414 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002415 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2416 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002417#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002418 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002419 /* We need more data, we have to re-enable quick-ack in case we
2420 * previously disabled it, otherwise we might cause the client
2421 * to delay next data.
2422 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002423 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002424 }
2425#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002426
Willy Tarreaufcffa692010-01-10 14:21:19 +01002427 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2428 /* If the client starts to talk, let's fall back to
2429 * request timeout processing.
2430 */
2431 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002432 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002433 }
2434
Willy Tarreau59234e92008-11-30 23:51:27 +01002435 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002436 if (!tick_isset(req->analyse_exp)) {
2437 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2438 (txn->flags & TX_WAIT_NEXT_RQ) &&
2439 tick_isset(s->be->timeout.httpka))
2440 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2441 else
2442 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 /* we're not ready yet */
2446 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002447
2448 failed_keep_alive:
2449 /* Here we process low-level errors for keep-alive requests. In
2450 * short, if the request is not the first one and it experiences
2451 * a timeout, read error or shutdown, we just silently close so
2452 * that the client can try again.
2453 */
2454 txn->status = 0;
2455 msg->msg_state = HTTP_MSG_RQBEFORE;
2456 req->analysers = 0;
2457 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002458 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002459 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002460 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002462
Willy Tarreaud787e662009-07-07 10:14:51 +02002463 /* OK now we have a complete HTTP request with indexed headers. Let's
2464 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002465 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002466 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002467 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002468 * byte after the last LF. msg->sov points to the first byte of data.
2469 * msg->eol cannot be trusted because it may have been left uninitialized
2470 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002471 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002472
Willy Tarreauda7ff642010-06-23 11:44:09 +02002473 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002474 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2475
Willy Tarreaub16a5742010-01-10 14:46:16 +01002476 if (txn->flags & TX_WAIT_NEXT_RQ) {
2477 /* kill the pending keep-alive timeout */
2478 txn->flags &= ~TX_WAIT_NEXT_RQ;
2479 req->analyse_exp = TICK_ETERNITY;
2480 }
2481
2482
Willy Tarreaud787e662009-07-07 10:14:51 +02002483 /* Maybe we found in invalid header name while we were configured not
2484 * to block on that, so we have to capture it now.
2485 */
2486 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002487 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002488
Willy Tarreau59234e92008-11-30 23:51:27 +01002489 /*
2490 * 1: identify the method
2491 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002493
2494 /* we can make use of server redirect on GET and HEAD */
2495 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2496 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /*
2499 * 2: check if the URI matches the monitor_uri.
2500 * We have to do this for every request which gets in, because
2501 * the monitor-uri is defined by the frontend.
2502 */
2503 if (unlikely((s->fe->monitor_uri_len != 0) &&
2504 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002505 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 s->fe->monitor_uri,
2507 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002508 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002510 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002514 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002517 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002518 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 ret = acl_pass(ret);
2521 if (cond->pol == ACL_COND_UNLESS)
2522 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 if (ret) {
2525 /* we fail this request, let's return 503 service unavail */
2526 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002527 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002529 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002530 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002531
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 /* nothing to fail, let's reply normaly */
2533 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002534 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 goto return_prx_cond;
2536 }
2537
2538 /*
2539 * 3: Maybe we have to copy the original REQURI for the logs ?
2540 * Note: we cannot log anymore if the request has been
2541 * classified as invalid.
2542 */
2543 if (unlikely(s->logs.logwait & LW_REQ)) {
2544 /* we have a complete HTTP request that we must log */
2545 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2546 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002547
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 if (urilen >= REQURI_LEN)
2549 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002550 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002552
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002553 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 s->do_log(s);
2555 } else {
2556 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002557 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 }
Willy Tarreau06619262006-12-17 08:37:22 +01002559
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002560 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2561 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002564 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002565 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002566
Willy Tarreau5b154472009-12-21 20:11:07 +01002567 /* ... and check if the request is HTTP/1.1 or above */
2568 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002569 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2570 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2571 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002572 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002573
2574 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002575 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 +01002576
Willy Tarreau88d349d2010-01-25 12:15:43 +01002577 /* if the frontend has "option http-use-proxy-header", we'll check if
2578 * we have what looks like a proxied connection instead of a connection,
2579 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2580 * Note that this is *not* RFC-compliant, however browsers and proxies
2581 * happen to do that despite being non-standard :-(
2582 * We consider that a request not beginning with either '/' or '*' is
2583 * a proxied connection, which covers both "scheme://location" and
2584 * CONNECT ip:port.
2585 */
2586 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002587 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002588 txn->flags |= TX_USE_PX_CONN;
2589
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002590 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002591 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002594 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002595 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002597
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002598 /* 6: determine the transfer-length.
2599 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2600 * the presence of a message-body in a REQUEST and its transfer length
2601 * must be determined that way (in order of precedence) :
2602 * 1. The presence of a message-body in a request is signaled by the
2603 * inclusion of a Content-Length or Transfer-Encoding header field
2604 * in the request's header fields. When a request message contains
2605 * both a message-body of non-zero length and a method that does
2606 * not define any semantics for that request message-body, then an
2607 * origin server SHOULD either ignore the message-body or respond
2608 * with an appropriate error message (e.g., 413). A proxy or
2609 * gateway, when presented the same request, SHOULD either forward
2610 * the request inbound with the message- body or ignore the
2611 * message-body when determining a response.
2612 *
2613 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2614 * and the "chunked" transfer-coding (Section 6.2) is used, the
2615 * transfer-length is defined by the use of this transfer-coding.
2616 * If a Transfer-Encoding header field is present and the "chunked"
2617 * transfer-coding is not present, the transfer-length is defined
2618 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002620 * 3. If a Content-Length header field is present, its decimal value in
2621 * OCTETs represents both the entity-length and the transfer-length.
2622 * If a message is received with both a Transfer-Encoding header
2623 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002625 * 4. By the server closing the connection. (Closing the connection
2626 * cannot be used to indicate the end of a request body, since that
2627 * would leave no possibility for the server to send back a response.)
2628 *
2629 * Whenever a transfer-coding is applied to a message-body, the set of
2630 * transfer-codings MUST include "chunked", unless the message indicates
2631 * it is terminated by closing the connection. When the "chunked"
2632 * transfer-coding is used, it MUST be the last transfer-coding applied
2633 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 */
2635
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002636 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002637 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002638 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002639 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002641 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002642 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2643 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002644 /* bad transfer-encoding (chunked followed by something else) */
2645 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002646 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002647 break;
2648 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002649 }
2650
Willy Tarreau32b47f42009-10-18 20:55:02 +02002651 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002652 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002653 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002654 signed long long cl;
2655
Willy Tarreauad14f752011-09-02 20:33:27 +02002656 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002657 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002659 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002660
Willy Tarreauad14f752011-09-02 20:33:27 +02002661 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002663 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002664 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002665
Willy Tarreauad14f752011-09-02 20:33:27 +02002666 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002667 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002668 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002669 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002670
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002671 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002672 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002673 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002674 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002675
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002676 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002677 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002678 }
2679
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002680 /* bodyless requests have a known length */
2681 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002682 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002683
Willy Tarreaud787e662009-07-07 10:14:51 +02002684 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002685 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002686 req->analyse_exp = TICK_ETERNITY;
2687 return 1;
2688
2689 return_bad_req:
2690 /* We centralize bad requests processing here */
2691 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2692 /* we detected a parsing error. We want to archive this request
2693 * in the dedicated proxy area for later troubleshooting.
2694 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002695 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002696 }
2697
2698 txn->req.msg_state = HTTP_MSG_ERROR;
2699 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002700 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002701
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002702 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002703 if (s->listener->counters)
2704 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002705
2706 return_prx_cond:
2707 if (!(s->flags & SN_ERR_MASK))
2708 s->flags |= SN_ERR_PRXCOND;
2709 if (!(s->flags & SN_FINST_MASK))
2710 s->flags |= SN_FINST_R;
2711
2712 req->analysers = 0;
2713 req->analyse_exp = TICK_ETERNITY;
2714 return 0;
2715}
2716
Cyril Bonté70be45d2010-10-12 00:14:35 +02002717/* We reached the stats page through a POST request.
2718 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002719 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002720 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002721int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002722{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002723 struct proxy *px = NULL;
2724 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002725
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002726 char key[LINESIZE];
2727 int action = ST_ADM_ACTION_NONE;
2728 int reprocess = 0;
2729
2730 int total_servers = 0;
2731 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732
2733 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002734 char *st_cur_param = NULL;
2735 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002736
Willy Tarreau9b28e032012-10-12 23:49:43 +02002737 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002738 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
2740 cur_param = next_param = end_params;
2741
Willy Tarreau9b28e032012-10-12 23:49:43 +02002742 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002743 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002744 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002745 return 1;
2746 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002747 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002748 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002749 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002750 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002751 }
2752
2753 *end_params = '\0';
2754
Willy Tarreau295a8372011-03-10 11:25:07 +01002755 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002756
2757 /*
2758 * Parse the parameters in reverse order to only store the last value.
2759 * From the html form, the backend and the action are at the end.
2760 */
2761 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002762 char *value;
2763 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002764
2765 cur_param--;
2766 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002767 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002768 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002769 poffset = (cur_param != first_param ? 1 : 0);
2770 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2771 if ((plen > 0) && (plen <= sizeof(key))) {
2772 strncpy(key, cur_param + poffset, plen);
2773 key[plen - 1] = '\0';
2774 } else {
2775 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2776 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002777 }
2778
2779 /* Parse the value */
2780 value = key;
2781 while (*value != '\0' && *value != '=') {
2782 value++;
2783 }
2784 if (*value == '=') {
2785 /* Ok, a value is found, we can mark the end of the key */
2786 *value++ = '\0';
2787 }
2788
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002789 if (!url_decode(key) || !url_decode(value))
2790 break;
2791
Cyril Bonté70be45d2010-10-12 00:14:35 +02002792 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002793 if (!px && (strcmp(key, "b") == 0)) {
2794 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2795 /* the backend name is unknown or ambiguous (duplicate names) */
2796 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2797 goto out;
2798 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002799 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002800 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002801 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002802 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002803 }
2804 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002805 action = ST_ADM_ACTION_ENABLE;
2806 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002807 else if (strcmp(value, "stop") == 0) {
2808 action = ST_ADM_ACTION_STOP;
2809 }
2810 else if (strcmp(value, "start") == 0) {
2811 action = ST_ADM_ACTION_START;
2812 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002813 else if (strcmp(value, "shutdown") == 0) {
2814 action = ST_ADM_ACTION_SHUTDOWN;
2815 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002816 else {
2817 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2818 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 }
2820 }
2821 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002822 if (!(px && action)) {
2823 /*
2824 * Indicates that we'll need to reprocess the parameters
2825 * as soon as backend and action are known
2826 */
2827 if (!reprocess) {
2828 st_cur_param = cur_param;
2829 st_next_param = next_param;
2830 }
2831 reprocess = 1;
2832 }
2833 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002834 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002835 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002836 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002837 /* Not already in maintenance, we can change the server state */
2838 sv->state |= SRV_MAINTAIN;
2839 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002840 altered_servers++;
2841 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002842 }
2843 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002844 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002845 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002846 /* Already in maintenance, we can change the server state */
2847 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002848 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002849 altered_servers++;
2850 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002851 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002852 break;
2853 case ST_ADM_ACTION_STOP:
2854 case ST_ADM_ACTION_START:
2855 if (action == ST_ADM_ACTION_START)
2856 sv->uweight = sv->iweight;
2857 else
2858 sv->uweight = 0;
2859
2860 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2861 /* we must take care of not pushing the server to full throttle during slow starts */
2862 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2863 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2864 else
2865 sv->eweight = BE_WEIGHT_SCALE;
2866 sv->eweight *= sv->uweight;
2867 } else {
2868 sv->eweight = sv->uweight;
2869 }
2870
2871 /* static LB algorithms are a bit harder to update */
2872 if (px->lbprm.update_server_eweight)
2873 px->lbprm.update_server_eweight(sv);
2874 else if (sv->eweight) {
2875 if (px->lbprm.set_server_status_up)
2876 px->lbprm.set_server_status_up(sv);
2877 }
2878 else {
2879 if (px->lbprm.set_server_status_down)
2880 px->lbprm.set_server_status_down(sv);
2881 }
2882 altered_servers++;
2883 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002884 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002885 case ST_ADM_ACTION_SHUTDOWN:
2886 if (px->state != PR_STSTOPPED) {
2887 struct session *sess, *sess_bck;
2888
2889 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2890 if (sess->srv_conn == sv)
2891 session_shutdown(sess, SN_ERR_KILLED);
2892
2893 altered_servers++;
2894 total_servers++;
2895 }
2896 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002898 } else {
2899 /* the server name is unknown or ambiguous (duplicate names) */
2900 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002901 }
2902 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002903 if (reprocess && px && action) {
2904 /* Now, we know the backend and the action chosen by the user.
2905 * We can safely restart from the first server parameter
2906 * to reprocess them
2907 */
2908 cur_param = st_cur_param;
2909 next_param = st_next_param;
2910 reprocess = 0;
2911 goto reprocess_servers;
2912 }
2913
Cyril Bonté70be45d2010-10-12 00:14:35 +02002914 next_param = cur_param;
2915 }
2916 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002917
2918 if (total_servers == 0) {
2919 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2920 }
2921 else if (altered_servers == 0) {
2922 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2923 }
2924 else if (altered_servers == total_servers) {
2925 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2926 }
2927 else {
2928 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2929 }
2930 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002931 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002932}
2933
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002934/* This function checks whether we need to enable a POST analyser to parse a
2935 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002936 * if it needs to come back again, otherwise non-zero if it finishes. In the
2937 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002938 */
2939int http_handle_stats(struct session *s, struct channel *req)
2940{
2941 struct stats_admin_rule *stats_admin_rule;
2942 struct stream_interface *si = s->rep->prod;
2943 struct http_txn *txn = &s->txn;
2944 struct http_msg *msg = &txn->req;
2945 struct uri_auth *uri = s->be->uri_auth;
2946
2947 /* now check whether we have some admin rules for this request */
2948 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2949 int ret = 1;
2950
2951 if (stats_admin_rule->cond) {
2952 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2953 ret = acl_pass(ret);
2954 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2955 ret = !ret;
2956 }
2957
2958 if (ret) {
2959 /* no rule, or the rule matches */
2960 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2961 break;
2962 }
2963 }
2964
2965 /* Was the status page requested with a POST ? */
2966 if (unlikely(txn->meth == HTTP_METH_POST)) {
2967 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2968 if (msg->msg_state < HTTP_MSG_100_SENT) {
2969 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2970 * send an HTTP/1.1 100 Continue intermediate response.
2971 */
2972 if (msg->flags & HTTP_MSGF_VER_11) {
2973 struct hdr_ctx ctx;
2974 ctx.idx = 0;
2975 /* Expect is allowed in 1.1, look for it */
2976 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2977 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2978 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2979 }
2980 }
2981 msg->msg_state = HTTP_MSG_100_SENT;
2982 s->logs.tv_request = now; /* update the request timer to reflect full request */
2983 }
2984 if (!http_process_req_stat_post(si, txn, req))
2985 return 0; /* we need more data */
2986 }
2987 else
2988 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
2989
2990 /* We don't want to land on the posted stats page because a refresh will
2991 * repost the data. We don't want this to happen on accident so we redirect
2992 * the browse to the stats page with a GET.
2993 */
2994 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04002995 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002996 "Cache-Control: no-cache\r\n"
2997 "Content-Type: text/plain\r\n"
2998 "Connection: close\r\n"
2999 "Location: %s;st=%s\r\n"
3000 "\r\n",
3001 uri->uri_prefix,
3002 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3003 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3004 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3005 stat_status_codes[si->applet.ctx.stats.st_code] :
3006 stat_status_codes[STAT_STATUS_UNKN]);
3007
3008 s->txn.status = 303;
3009 s->logs.tv_request = now;
3010 stream_int_retnclose(req->prod, &trash);
3011 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3012
3013 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3014 s->fe->fe_counters.intercepted_req++;
3015
3016 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3017 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3018 if (!(s->flags & SN_FINST_MASK))
3019 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003020 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003021 return 1;
3022 }
3023
3024 /* OK, let's go on now */
3025
3026 chunk_printf(&trash,
3027 "HTTP/1.0 200 OK\r\n"
3028 "Cache-Control: no-cache\r\n"
3029 "Connection: close\r\n"
3030 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003031 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003032
3033 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3034 chunk_appendf(&trash, "Refresh: %d\r\n",
3035 uri->refresh);
3036
3037 chunk_appendf(&trash, "\r\n");
3038
3039 s->txn.status = 200;
3040 s->logs.tv_request = now;
3041
3042 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3043 s->fe->fe_counters.intercepted_req++;
3044
3045 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3046 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3047 if (!(s->flags & SN_FINST_MASK))
3048 s->flags |= SN_FINST_R;
3049
3050 if (s->txn.meth == HTTP_METH_HEAD) {
3051 /* that's all we return in case of HEAD request, so let's immediately close. */
3052 stream_int_retnclose(req->prod, &trash);
3053 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003054 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003055 return 1;
3056 }
3057
3058 /* OK, push the response and hand over to the stats I/O handler */
3059 bi_putchk(s->rep, &trash);
3060
3061 s->task->nice = -32; /* small boost for HTTP statistics */
3062 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3063 s->target = s->rep->prod->conn->target; // for logging only
3064 s->rep->prod->conn->xprt_ctx = s;
3065 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3066 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003067 return 1;
3068}
3069
Willy Tarreau20b0de52012-12-24 15:45:22 +01003070/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003071 * transaction <txn>. Returns the first rule that prevents further processing
3072 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3073 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3074 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003075 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003076static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003077http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078{
Willy Tarreauff011f22011-01-06 17:51:27 +01003079 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003080 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081
Willy Tarreauff011f22011-01-06 17:51:27 +01003082 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003083 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003084 continue;
3085
Willy Tarreau96257ec2012-12-27 10:46:37 +01003086 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003087 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088 int ret;
3089
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003090 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003091 ret = acl_pass(ret);
3092
Willy Tarreauff011f22011-01-06 17:51:27 +01003093 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003094 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003095
3096 if (!ret) /* condition not matched */
3097 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003098 }
3099
Willy Tarreau20b0de52012-12-24 15:45:22 +01003100
Willy Tarreau96257ec2012-12-27 10:46:37 +01003101 switch (rule->action) {
3102 case HTTP_REQ_ACT_ALLOW:
3103 return NULL; /* "allow" rules are OK */
3104
3105 case HTTP_REQ_ACT_DENY:
3106 txn->flags |= TX_CLDENY;
3107 return rule;
3108
Willy Tarreauccbcc372012-12-27 12:37:57 +01003109 case HTTP_REQ_ACT_TARPIT:
3110 txn->flags |= TX_CLTARPIT;
3111 return rule;
3112
Willy Tarreau96257ec2012-12-27 10:46:37 +01003113 case HTTP_REQ_ACT_AUTH:
3114 return rule;
3115
Willy Tarreau81499eb2012-12-27 12:19:02 +01003116 case HTTP_REQ_ACT_REDIR:
3117 return rule;
3118
Willy Tarreau96257ec2012-12-27 10:46:37 +01003119 case HTTP_REQ_ACT_SET_HDR:
3120 ctx.idx = 0;
3121 /* remove all occurrences of the header */
3122 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3123 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3124 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003125 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003126 /* now fall through to header addition */
3127
3128 case HTTP_REQ_ACT_ADD_HDR:
3129 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3130 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3131 trash.len = rule->arg.hdr_add.name_len;
3132 trash.str[trash.len++] = ':';
3133 trash.str[trash.len++] = ' ';
3134 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3135 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3136 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003137 }
3138 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003139
3140 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003141 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003142}
3143
Willy Tarreau71241ab2012-12-27 11:30:54 +01003144
3145/* Perform an HTTP redirect based on the information in <rule>. The function
3146 * returns non-zero on success, or zero in case of a, irrecoverable error such
3147 * as too large a request to build a valid response.
3148 */
3149static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3150{
3151 struct http_msg *msg = &txn->req;
3152 const char *msg_fmt;
3153
3154 /* build redirect message */
3155 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003156 case 308:
3157 msg_fmt = HTTP_308;
3158 break;
3159 case 307:
3160 msg_fmt = HTTP_307;
3161 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003162 case 303:
3163 msg_fmt = HTTP_303;
3164 break;
3165 case 301:
3166 msg_fmt = HTTP_301;
3167 break;
3168 case 302:
3169 default:
3170 msg_fmt = HTTP_302;
3171 break;
3172 }
3173
3174 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3175 return 0;
3176
3177 switch(rule->type) {
3178 case REDIRECT_TYPE_SCHEME: {
3179 const char *path;
3180 const char *host;
3181 struct hdr_ctx ctx;
3182 int pathlen;
3183 int hostlen;
3184
3185 host = "";
3186 hostlen = 0;
3187 ctx.idx = 0;
3188 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3189 host = ctx.line + ctx.val;
3190 hostlen = ctx.vlen;
3191 }
3192
3193 path = http_get_path(txn);
3194 /* build message using path */
3195 if (path) {
3196 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3197 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3198 int qs = 0;
3199 while (qs < pathlen) {
3200 if (path[qs] == '?') {
3201 pathlen = qs;
3202 break;
3203 }
3204 qs++;
3205 }
3206 }
3207 } else {
3208 path = "/";
3209 pathlen = 1;
3210 }
3211
3212 /* check if we can add scheme + "://" + host + path */
3213 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3214 return 0;
3215
3216 /* add scheme */
3217 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3218 trash.len += rule->rdr_len;
3219
3220 /* add "://" */
3221 memcpy(trash.str + trash.len, "://", 3);
3222 trash.len += 3;
3223
3224 /* add host */
3225 memcpy(trash.str + trash.len, host, hostlen);
3226 trash.len += hostlen;
3227
3228 /* add path */
3229 memcpy(trash.str + trash.len, path, pathlen);
3230 trash.len += pathlen;
3231
3232 /* append a slash at the end of the location is needed and missing */
3233 if (trash.len && trash.str[trash.len - 1] != '/' &&
3234 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3235 if (trash.len > trash.size - 5)
3236 return 0;
3237 trash.str[trash.len] = '/';
3238 trash.len++;
3239 }
3240
3241 break;
3242 }
3243 case REDIRECT_TYPE_PREFIX: {
3244 const char *path;
3245 int pathlen;
3246
3247 path = http_get_path(txn);
3248 /* build message using path */
3249 if (path) {
3250 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3251 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3252 int qs = 0;
3253 while (qs < pathlen) {
3254 if (path[qs] == '?') {
3255 pathlen = qs;
3256 break;
3257 }
3258 qs++;
3259 }
3260 }
3261 } else {
3262 path = "/";
3263 pathlen = 1;
3264 }
3265
3266 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3267 return 0;
3268
3269 /* add prefix. Note that if prefix == "/", we don't want to
3270 * add anything, otherwise it makes it hard for the user to
3271 * configure a self-redirection.
3272 */
3273 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3274 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3275 trash.len += rule->rdr_len;
3276 }
3277
3278 /* add path */
3279 memcpy(trash.str + trash.len, path, pathlen);
3280 trash.len += pathlen;
3281
3282 /* append a slash at the end of the location is needed and missing */
3283 if (trash.len && trash.str[trash.len - 1] != '/' &&
3284 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3285 if (trash.len > trash.size - 5)
3286 return 0;
3287 trash.str[trash.len] = '/';
3288 trash.len++;
3289 }
3290
3291 break;
3292 }
3293 case REDIRECT_TYPE_LOCATION:
3294 default:
3295 if (trash.len + rule->rdr_len > trash.size - 4)
3296 return 0;
3297
3298 /* add location */
3299 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3300 trash.len += rule->rdr_len;
3301 break;
3302 }
3303
3304 if (rule->cookie_len) {
3305 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3306 trash.len += 14;
3307 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3308 trash.len += rule->cookie_len;
3309 memcpy(trash.str + trash.len, "\r\n", 2);
3310 trash.len += 2;
3311 }
3312
3313 /* add end of headers and the keep-alive/close status.
3314 * We may choose to set keep-alive if the Location begins
3315 * with a slash, because the client will come back to the
3316 * same server.
3317 */
3318 txn->status = rule->code;
3319 /* let's log the request time */
3320 s->logs.tv_request = now;
3321
3322 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3323 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3324 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3325 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3326 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3327 /* keep-alive possible */
3328 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3329 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3330 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3331 trash.len += 30;
3332 } else {
3333 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3334 trash.len += 24;
3335 }
3336 }
3337 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3338 trash.len += 4;
3339 bo_inject(txn->rsp.chn, trash.str, trash.len);
3340 /* "eat" the request */
3341 bi_fast_delete(txn->req.chn->buf, msg->sov);
3342 msg->sov = 0;
3343 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3344 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3345 txn->req.msg_state = HTTP_MSG_CLOSED;
3346 txn->rsp.msg_state = HTTP_MSG_DONE;
3347 } else {
3348 /* keep-alive not possible */
3349 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3350 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3351 trash.len += 29;
3352 } else {
3353 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3354 trash.len += 23;
3355 }
3356 stream_int_retnclose(txn->req.chn->prod, &trash);
3357 txn->req.chn->analysers = 0;
3358 }
3359
3360 if (!(s->flags & SN_ERR_MASK))
3361 s->flags |= SN_ERR_PRXCOND;
3362 if (!(s->flags & SN_FINST_MASK))
3363 s->flags |= SN_FINST_R;
3364
3365 return 1;
3366}
3367
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003368/* This stream analyser runs all HTTP request processing which is common to
3369 * frontends and backends, which means blocking ACLs, filters, connection-close,
3370 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003371 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003372 * either needs more data or wants to immediately abort the request (eg: deny,
3373 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003374 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003375int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003376{
Willy Tarreaud787e662009-07-07 10:14:51 +02003377 struct http_txn *txn = &s->txn;
3378 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003379 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003380 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003381 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003382 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003383 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003384
Willy Tarreau655dce92009-11-08 13:10:58 +01003385 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003386 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003387 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003388 return 0;
3389 }
3390
Willy Tarreau3a816292009-07-07 10:55:49 +02003391 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003392 req->analyse_exp = TICK_ETERNITY;
3393
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003394 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 +02003395 now_ms, __FUNCTION__,
3396 s,
3397 req,
3398 req->rex, req->wex,
3399 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003400 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003401 req->analysers);
3402
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003403 /* first check whether we have some ACLs set to block this request */
3404 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003405 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003406
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003407 ret = acl_pass(ret);
3408 if (cond->pol == ACL_COND_UNLESS)
3409 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003410
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003411 if (ret) {
3412 txn->status = 403;
3413 /* let's log the request time */
3414 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003415 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003416 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003417 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003418 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003420
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003421 /* just in case we have some per-backend tracking */
3422 session_inc_be_http_req_ctr(s);
3423
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003424 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003425 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003426
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003427 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003428 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003429 do_stats = stats_check_uri(s->rep->prod, txn, px);
3430 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003431 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 +01003432 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003433 else
3434 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003435
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003436 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003437 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3438 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003439 txn->status = 403;
3440 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003441 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003442 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003443 s->fe->fe_counters.denied_req++;
3444 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3445 s->be->be_counters.denied_req++;
3446 if (s->listener->counters)
3447 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003448 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003449 }
3450 /* When a connection is tarpitted, we use the tarpit timeout,
3451 * which may be the same as the connect timeout if unspecified.
3452 * If unset, then set it to zero because we really want it to
3453 * eventually expire. We build the tarpit as an analyser.
3454 */
3455 if (txn->flags & TX_CLTARPIT) {
3456 channel_erase(s->req);
3457 /* wipe the request out so that we can drop the connection early
3458 * if the client closes first.
3459 */
3460 channel_dont_connect(req);
3461 req->analysers = 0; /* remove switching rules etc... */
3462 req->analysers |= AN_REQ_HTTP_TARPIT;
3463 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3464 if (!req->analyse_exp)
3465 req->analyse_exp = tick_add(now_ms, 0);
3466 session_inc_http_err_ctr(s);
3467 s->fe->fe_counters.denied_req++;
3468 if (s->fe != s->be)
3469 s->be->be_counters.denied_req++;
3470 if (s->listener->counters)
3471 s->listener->counters->denied_req++;
3472 return 1;
3473 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003474 }
3475
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003476 /* try headers filters */
3477 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003478 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003479 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003480
Willy Tarreau59234e92008-11-30 23:51:27 +01003481 /* has the request been denied ? */
3482 if (txn->flags & TX_CLDENY) {
3483 /* no need to go further */
3484 txn->status = 403;
3485 /* let's log the request time */
3486 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003487 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003488 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003489 goto return_prx_cond;
3490 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003491
3492 /* When a connection is tarpitted, we use the tarpit timeout,
3493 * which may be the same as the connect timeout if unspecified.
3494 * If unset, then set it to zero because we really want it to
3495 * eventually expire. We build the tarpit as an analyser.
3496 */
3497 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003498 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003499 /* wipe the request out so that we can drop the connection early
3500 * if the client closes first.
3501 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003502 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003503 req->analysers = 0; /* remove switching rules etc... */
3504 req->analysers |= AN_REQ_HTTP_TARPIT;
3505 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3506 if (!req->analyse_exp)
3507 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003508 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003509 return 1;
3510 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003511 }
Willy Tarreau06619262006-12-17 08:37:22 +01003512
Willy Tarreau5b154472009-12-21 20:11:07 +01003513 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3514 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003515 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3516 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003517 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3518 * avoid to redo the same work if FE and BE have the same settings (common).
3519 * The method consists in checking if options changed between the two calls
3520 * (implying that either one is non-null, or one of them is non-null and we
3521 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003522 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003523
Willy Tarreaudc008c52010-02-01 16:20:08 +01003524 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3525 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3526 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3527 (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 +01003528 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003529
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003530 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3531 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003532 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003533 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3534 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003535 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003536 tmp = TX_CON_WANT_CLO;
3537
Willy Tarreau5b154472009-12-21 20:11:07 +01003538 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3539 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003540
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003541 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3542 /* parse the Connection header and possibly clean it */
3543 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003544 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003545 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3546 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003547 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003548 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003549 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003550 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003551 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003552
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003553 /* check if client or config asks for explicit close in KAL/SCL */
3554 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3555 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3556 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003557 (!(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 +01003558 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003559 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003560 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003561 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3562 }
Willy Tarreau78599912009-10-17 20:12:21 +02003563
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003564 /* we can be blocked here because the request needs to be authenticated,
3565 * either to pass or to access stats.
3566 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003567 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003568 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003569
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003570 if (!realm)
3571 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003573 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003574 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003575 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003576 /* on 401 we still count one error, because normal browsing
3577 * won't significantly increase the counter but brute force
3578 * attempts will.
3579 */
3580 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003581 goto return_prx_cond;
3582 }
3583
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003584 /* add request headers from the rule sets in the same order */
3585 list_for_each_entry(wl, &px->req_add, list) {
3586 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003587 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003588 ret = acl_pass(ret);
3589 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3590 ret = !ret;
3591 if (!ret)
3592 continue;
3593 }
3594
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003595 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003596 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003597 }
3598
3599 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3600 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3601 goto return_bad_req;
3602 req->analyse_exp = TICK_ETERNITY;
3603 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003604 }
3605
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003606 if (unlikely(do_stats)) {
3607 /* process the stats request now */
3608 if (!http_handle_stats(s, req)) {
3609 /* we need more data, let's come back here later */
3610 req->analysers |= an_bit;
3611 channel_dont_connect(req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003612 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003613 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003615
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003616 /* check whether we have some ACLs set to redirect this request */
3617 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003618 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003619 int ret;
3620
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003621 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003622 ret = acl_pass(ret);
3623 if (rule->cond->pol == ACL_COND_UNLESS)
3624 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003625 if (!ret)
3626 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003627 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003628 if (!http_apply_redirect_rule(rule, s, txn))
3629 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003630
Willy Tarreau71241ab2012-12-27 11:30:54 +01003631 req->analyse_exp = TICK_ETERNITY;
3632 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003634
Willy Tarreau2be39392010-01-03 17:24:51 +01003635 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3636 * If this happens, then the data will not come immediately, so we must
3637 * send all what we have without waiting. Note that due to the small gain
3638 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003639 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003640 * itself once used.
3641 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003642 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003643
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003644 /* that's OK for us now, let's move on to next analysers */
3645 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003646
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003647 return_bad_req:
3648 /* We centralize bad requests processing here */
3649 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3650 /* we detected a parsing error. We want to archive this request
3651 * in the dedicated proxy area for later troubleshooting.
3652 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003653 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003654 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003655
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003656 txn->req.msg_state = HTTP_MSG_ERROR;
3657 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003658 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003659
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003660 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003661 if (s->listener->counters)
3662 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003663
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003664 return_prx_cond:
3665 if (!(s->flags & SN_ERR_MASK))
3666 s->flags |= SN_ERR_PRXCOND;
3667 if (!(s->flags & SN_FINST_MASK))
3668 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003669
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 req->analysers = 0;
3671 req->analyse_exp = TICK_ETERNITY;
3672 return 0;
3673}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003674
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003675/* This function performs all the processing enabled for the current request.
3676 * It returns 1 if the processing can continue on next analysers, or zero if it
3677 * needs more data, encounters an error, or wants to immediately abort the
3678 * request. It relies on buffers flags, and updates s->req->analysers.
3679 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003680int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003681{
3682 struct http_txn *txn = &s->txn;
3683 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003684
Willy Tarreau655dce92009-11-08 13:10:58 +01003685 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003686 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003687 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003688 return 0;
3689 }
3690
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003691 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 +02003692 now_ms, __FUNCTION__,
3693 s,
3694 req,
3695 req->rex, req->wex,
3696 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003697 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003698 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003699
William Lallemand82fe75c2012-10-23 10:25:10 +02003700 if (s->fe->comp || s->be->comp)
3701 select_compression_request_header(s, req->buf);
3702
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 /*
3704 * Right now, we know that we have processed the entire headers
3705 * and that unwanted requests have been filtered out. We can do
3706 * whatever we want with the remaining request. Also, now we
3707 * may have separate values for ->fe, ->be.
3708 */
Willy Tarreau06619262006-12-17 08:37:22 +01003709
Willy Tarreau59234e92008-11-30 23:51:27 +01003710 /*
3711 * If HTTP PROXY is set we simply get remote server address
3712 * parsing incoming request.
3713 */
3714 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003715 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 +01003716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003717
Willy Tarreau59234e92008-11-30 23:51:27 +01003718 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003719 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 * Note that doing so might move headers in the request, but
3721 * the fields will stay coherent and the URI will not move.
3722 * This should only be performed in the backend.
3723 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003724 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003725 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3726 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003729 * 8: the appsession cookie was looked up very early in 1.2,
3730 * so let's do the same now.
3731 */
3732
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003733 /* It needs to look into the URI unless persistence must be ignored */
3734 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003735 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 +01003736 }
3737
William Lallemanda73203e2012-03-12 12:48:57 +01003738 /* add unique-id if "header-unique-id" is specified */
3739
3740 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3741 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3742
3743 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003744 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3745 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003746 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003747 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003748 goto return_bad_req;
3749 }
3750
Cyril Bontéb21570a2009-11-29 20:04:48 +01003751 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003752 * 9: add X-Forwarded-For if either the frontend or the backend
3753 * asks for it.
3754 */
3755 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003756 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003757 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003758 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3759 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003760 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003761 /* The header is set to be added only if none is present
3762 * and we found it, so don't do anything.
3763 */
3764 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003765 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003766 /* Add an X-Forwarded-For header unless the source IP is
3767 * in the 'except' network range.
3768 */
3769 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003770 (((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 +01003771 != s->fe->except_net.s_addr) &&
3772 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003773 (((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 +01003774 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003775 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003776 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003777 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003778
3779 /* Note: we rely on the backend to get the header name to be used for
3780 * x-forwarded-for, because the header is really meant for the backends.
3781 * However, if the backend did not specify any option, we have to rely
3782 * on the frontend's header name.
3783 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003784 if (s->be->fwdfor_hdr_len) {
3785 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003786 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003787 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003788 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003789 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003790 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003791 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003792
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003793 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003794 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003795 }
3796 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003797 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003798 /* FIXME: for the sake of completeness, we should also support
3799 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003800 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 int len;
3802 char pn[INET6_ADDRSTRLEN];
3803 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003804 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003806
Willy Tarreau59234e92008-11-30 23:51:27 +01003807 /* Note: we rely on the backend to get the header name to be used for
3808 * x-forwarded-for, because the header is really meant for the backends.
3809 * However, if the backend did not specify any option, we have to rely
3810 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003811 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003812 if (s->be->fwdfor_hdr_len) {
3813 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003814 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 } else {
3816 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003817 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003818 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003819 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003820
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003821 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 goto return_bad_req;
3823 }
3824 }
3825
3826 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003827 * 10: add X-Original-To if either the frontend or the backend
3828 * asks for it.
3829 */
3830 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3831
3832 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003833 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003834 /* Add an X-Original-To header unless the destination IP is
3835 * in the 'except' network range.
3836 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003837 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003838
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003839 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003840 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003841 (((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 +02003842 != s->fe->except_to.s_addr) &&
3843 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003844 (((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 +02003845 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003846 int len;
3847 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003848 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003849
3850 /* Note: we rely on the backend to get the header name to be used for
3851 * x-original-to, because the header is really meant for the backends.
3852 * However, if the backend did not specify any option, we have to rely
3853 * on the frontend's header name.
3854 */
3855 if (s->be->orgto_hdr_len) {
3856 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003857 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003858 } else {
3859 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003860 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003861 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003862 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003864 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003865 goto return_bad_req;
3866 }
3867 }
3868 }
3869
Willy Tarreau50fc7772012-11-11 22:19:57 +01003870 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3871 * If an "Upgrade" token is found, the header is left untouched in order not to have
3872 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3873 * "Upgrade" is present in the Connection header.
3874 */
3875 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3876 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3877 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003878 unsigned int want_flags = 0;
3879
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003880 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003881 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3882 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3883 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003884 want_flags |= TX_CON_CLO_SET;
3885 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003886 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3887 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3888 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003889 want_flags |= TX_CON_KAL_SET;
3890 }
3891
3892 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003893 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003894 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003895
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003896
Willy Tarreau522d6c02009-12-06 18:49:18 +01003897 /* If we have no server assigned yet and we're balancing on url_param
3898 * with a POST request, we may be interested in checking the body for
3899 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 */
3901 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3902 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003904 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003905 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003906 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003907 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003908
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003909 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003910 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003911#ifdef TCP_QUICKACK
3912 /* We expect some data from the client. Unless we know for sure
3913 * we already have a full request, we have to re-enable quick-ack
3914 * in case we previously disabled it, otherwise we might cause
3915 * the client to delay further data.
3916 */
3917 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003918 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003919 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003920 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003921#endif
3922 }
Willy Tarreau03945942009-12-22 16:50:27 +01003923
Willy Tarreau59234e92008-11-30 23:51:27 +01003924 /*************************************************************
3925 * OK, that's finished for the headers. We have done what we *
3926 * could. Let's switch to the DATA state. *
3927 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003928 req->analyse_exp = TICK_ETERNITY;
3929 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003930
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003931 /* if the server closes the connection, we want to immediately react
3932 * and close the socket to save packets and syscalls.
3933 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003934 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3935 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003936
Willy Tarreau59234e92008-11-30 23:51:27 +01003937 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003938 /* OK let's go on with the BODY now */
3939 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003940
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003942 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003943 /* we detected a parsing error. We want to archive this request
3944 * in the dedicated proxy area for later troubleshooting.
3945 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003946 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003947 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003948
Willy Tarreau59234e92008-11-30 23:51:27 +01003949 txn->req.msg_state = HTTP_MSG_ERROR;
3950 txn->status = 400;
3951 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003952 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003953
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003954 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003955 if (s->listener->counters)
3956 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003957
Willy Tarreau59234e92008-11-30 23:51:27 +01003958 if (!(s->flags & SN_ERR_MASK))
3959 s->flags |= SN_ERR_PRXCOND;
3960 if (!(s->flags & SN_FINST_MASK))
3961 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003962 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003963}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003964
Willy Tarreau60b85b02008-11-30 23:28:40 +01003965/* This function is an analyser which processes the HTTP tarpit. It always
3966 * returns zero, at the beginning because it prevents any other processing
3967 * from occurring, and at the end because it terminates the request.
3968 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003969int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003970{
3971 struct http_txn *txn = &s->txn;
3972
3973 /* This connection is being tarpitted. The CLIENT side has
3974 * already set the connect expiration date to the right
3975 * timeout. We just have to check that the client is still
3976 * there and that the timeout has not expired.
3977 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003978 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003979 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003980 !tick_is_expired(req->analyse_exp, now_ms))
3981 return 0;
3982
3983 /* We will set the queue timer to the time spent, just for
3984 * logging purposes. We fake a 500 server error, so that the
3985 * attacker will not suspect his connection has been tarpitted.
3986 * It will not cause trouble to the logs because we can exclude
3987 * the tarpitted connections by filtering on the 'PT' status flags.
3988 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003989 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3990
3991 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003992 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003993 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003994
3995 req->analysers = 0;
3996 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003997
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003998 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003999 if (s->listener->counters)
4000 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004001
Willy Tarreau60b85b02008-11-30 23:28:40 +01004002 if (!(s->flags & SN_ERR_MASK))
4003 s->flags |= SN_ERR_PRXCOND;
4004 if (!(s->flags & SN_FINST_MASK))
4005 s->flags |= SN_FINST_T;
4006 return 0;
4007}
4008
Willy Tarreaud34af782008-11-30 23:36:37 +01004009/* This function is an analyser which processes the HTTP request body. It looks
4010 * for parameters to be used for the load balancing algorithm (url_param). It
4011 * must only be called after the standard HTTP request processing has occurred,
4012 * because it expects the request to be parsed. It returns zero if it needs to
4013 * read more data, or 1 once it has completed its analysis.
4014 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004015int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004016{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004017 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004018 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004019 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004020
4021 /* We have to parse the HTTP request body to find any required data.
4022 * "balance url_param check_post" should have been the only way to get
4023 * into this. We were brought here after HTTP header analysis, so all
4024 * related structures are ready.
4025 */
4026
Willy Tarreau522d6c02009-12-06 18:49:18 +01004027 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4028 goto missing_data;
4029
4030 if (msg->msg_state < HTTP_MSG_100_SENT) {
4031 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4032 * send an HTTP/1.1 100 Continue intermediate response.
4033 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004034 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004035 struct hdr_ctx ctx;
4036 ctx.idx = 0;
4037 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004038 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004040 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004041 }
4042 }
4043 msg->msg_state = HTTP_MSG_100_SENT;
4044 }
4045
4046 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004047 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004048 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004049 * is still null. We must save the body in msg->next because it
4050 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004051 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004052 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004053
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004054 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004055 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4056 else
4057 msg->msg_state = HTTP_MSG_DATA;
4058 }
4059
4060 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004061 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004062 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004063 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004064 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004065 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004066
Willy Tarreau115acb92009-12-26 13:56:06 +01004067 if (!ret)
4068 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004069 else if (ret < 0) {
4070 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004072 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004073 }
4074
Willy Tarreaud98cf932009-12-27 22:54:55 +01004075 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004076 * We have the first data byte is in msg->sov. We're waiting for at
4077 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004078 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004079
Willy Tarreau124d9912011-03-01 20:30:48 +01004080 if (msg->body_len < limit)
4081 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004082
Willy Tarreau9b28e032012-10-12 23:49:43 +02004083 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084 goto http_end;
4085
4086 missing_data:
4087 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004088 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004089 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004090 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004091 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004092
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004093 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004095 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004096
4097 if (!(s->flags & SN_ERR_MASK))
4098 s->flags |= SN_ERR_CLITO;
4099 if (!(s->flags & SN_FINST_MASK))
4100 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004101 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004102 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004103
4104 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004105 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004106 /* Not enough data. We'll re-use the http-request
4107 * timeout here. Ideally, we should set the timeout
4108 * relative to the accept() date. We just set the
4109 * request timeout once at the beginning of the
4110 * request.
4111 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004112 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004113 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004114 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004115 return 0;
4116 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004117
4118 http_end:
4119 /* The situation will not evolve, so let's give up on the analysis. */
4120 s->logs.tv_request = now; /* update the request timer to reflect full request */
4121 req->analysers &= ~an_bit;
4122 req->analyse_exp = TICK_ETERNITY;
4123 return 1;
4124
4125 return_bad_req: /* let's centralize all bad requests */
4126 txn->req.msg_state = HTTP_MSG_ERROR;
4127 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004128 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129
Willy Tarreau79ebac62010-06-07 13:47:49 +02004130 if (!(s->flags & SN_ERR_MASK))
4131 s->flags |= SN_ERR_PRXCOND;
4132 if (!(s->flags & SN_FINST_MASK))
4133 s->flags |= SN_FINST_R;
4134
Willy Tarreau522d6c02009-12-06 18:49:18 +01004135 return_err_msg:
4136 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004137 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004138 if (s->listener->counters)
4139 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004140 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004141}
4142
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004143/* send a server's name with an outgoing request over an established connection.
4144 * Note: this function is designed to be called once the request has been scheduled
4145 * for being forwarded. This is the reason why it rewinds the buffer before
4146 * proceeding.
4147 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004148int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004149
4150 struct hdr_ctx ctx;
4151
Mark Lamourinec2247f02012-01-04 13:02:01 -05004152 char *hdr_name = be->server_id_hdr_name;
4153 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004154 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004155 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004156 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004157
William Lallemandd9e90662012-01-30 17:27:17 +01004158 ctx.idx = 0;
4159
Willy Tarreau9b28e032012-10-12 23:49:43 +02004160 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004161 if (old_o) {
4162 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004163 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004164 }
4165
Willy Tarreau9b28e032012-10-12 23:49:43 +02004166 old_i = chn->buf->i;
4167 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 -05004168 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004169 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170 }
4171
4172 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004173 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004174 memcpy(hdr_val, hdr_name, hdr_name_len);
4175 hdr_val += hdr_name_len;
4176 *hdr_val++ = ':';
4177 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004178 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4179 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004180
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004181 if (old_o) {
4182 /* If this was a forwarded request, we must readjust the amount of
4183 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004184 * variations. Note that if the request was already scheduled for
4185 * forwarding, it had its req->sol pointing to the body, which
4186 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004187 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004188 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004189 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004190 }
4191
Mark Lamourinec2247f02012-01-04 13:02:01 -05004192 return 0;
4193}
4194
Willy Tarreau610ecce2010-01-04 21:15:02 +01004195/* Terminate current transaction and prepare a new one. This is very tricky
4196 * right now but it works.
4197 */
4198void http_end_txn_clean_session(struct session *s)
4199{
4200 /* FIXME: We need a more portable way of releasing a backend's and a
4201 * server's connections. We need a safer way to reinitialize buffer
4202 * flags. We also need a more accurate method for computing per-request
4203 * data.
4204 */
4205 http_silent_debug(__LINE__, s);
4206
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004207 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004208 si_shutr(s->req->cons);
4209 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210
4211 http_silent_debug(__LINE__, s);
4212
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004213 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004214 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004215 if (unlikely(s->srv_conn))
4216 sess_change_server(s, NULL);
4217 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218
4219 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4220 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004221 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222
4223 if (s->txn.status) {
4224 int n;
4225
4226 n = s->txn.status / 100;
4227 if (n < 1 || n > 5)
4228 n = 0;
4229
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004230 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004231 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004232 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004233 s->fe->fe_counters.p.http.comp_rsp++;
4234 }
Willy Tarreau24657792010-02-26 10:30:28 +01004235 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004236 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004237 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004238 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004239 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004240 s->be->be_counters.p.http.comp_rsp++;
4241 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 }
4243
4244 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004245 s->logs.bytes_in -= s->req->buf->i;
4246 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247
4248 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004249 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 !(s->flags & SN_MONITOR) &&
4251 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4252 s->do_log(s);
4253 }
4254
4255 s->logs.accept_date = date; /* user-visible date for logging */
4256 s->logs.tv_accept = now; /* corrected date for internal use */
4257 tv_zero(&s->logs.tv_request);
4258 s->logs.t_queue = -1;
4259 s->logs.t_connect = -1;
4260 s->logs.t_data = -1;
4261 s->logs.t_close = 0;
4262 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4263 s->logs.srv_queue_size = 0; /* we will get this number soon */
4264
Willy Tarreau9b28e032012-10-12 23:49:43 +02004265 s->logs.bytes_in = s->req->total = s->req->buf->i;
4266 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267
4268 if (s->pend_pos)
4269 pendconn_free(s->pend_pos);
4270
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004271 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 if (s->flags & SN_CURR_SESS) {
4273 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004274 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004276 if (may_dequeue_tasks(objt_server(s->target), s->be))
4277 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 }
4279
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004280 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281
4282 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004283 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4284 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004285 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004287 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 s->req->cons->err_loc = NULL;
4289 s->req->cons->exp = TICK_ETERNITY;
4290 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004291 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4292 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 +02004293 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 +01004294 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004295
4296 if (s->flags & SN_COMP_READY)
4297 s->comp_algo->end(&s->comp_ctx);
4298 s->comp_algo = NULL;
4299 s->flags &= ~SN_COMP_READY;
4300
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 s->txn.meth = 0;
4302 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004303 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004304 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305 s->req->cons->flags |= SI_FL_INDEP_STR;
4306
Willy Tarreau96e31212011-05-30 18:10:30 +02004307 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004308 s->req->flags |= CF_NEVER_WAIT;
4309 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004310 }
4311
Willy Tarreau610ecce2010-01-04 21:15:02 +01004312 /* if the request buffer is not empty, it means we're
4313 * about to process another request, so send pending
4314 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004315 * Just don't do this if the buffer is close to be full,
4316 * because the request will wait for it to flush a little
4317 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004318 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004319 if (s->req->buf->i) {
4320 if (s->rep->buf->o &&
4321 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4322 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004323 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004324 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004325
4326 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004327 channel_auto_read(s->req);
4328 channel_auto_close(s->req);
4329 channel_auto_read(s->rep);
4330 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331
Willy Tarreau342b11c2010-11-24 16:22:09 +01004332 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 s->rep->analysers = 0;
4334
4335 http_silent_debug(__LINE__, s);
4336}
4337
4338
4339/* This function updates the request state machine according to the response
4340 * state machine and buffer flags. It returns 1 if it changes anything (flag
4341 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4342 * it is only used to find when a request/response couple is complete. Both
4343 * this function and its equivalent should loop until both return zero. It
4344 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4345 */
4346int http_sync_req_state(struct session *s)
4347{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004348 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004350 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004351 unsigned int old_state = txn->req.msg_state;
4352
4353 http_silent_debug(__LINE__, s);
4354 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4355 return 0;
4356
4357 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004358 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004359 * We can shut the read side unless we want to abort_on_close,
4360 * or we have a POST request. The issue with POST requests is
4361 * that some browsers still send a CRLF after the request, and
4362 * this CRLF must be read so that it does not remain in the kernel
4363 * buffers, otherwise a close could cause an RST on some systems
4364 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004365 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004366 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004367 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004368
Willy Tarreau40f151a2012-12-20 12:10:09 +01004369 /* if the server closes the connection, we want to immediately react
4370 * and close the socket to save packets and syscalls.
4371 */
4372 chn->cons->flags |= SI_FL_NOHALF;
4373
Willy Tarreau610ecce2010-01-04 21:15:02 +01004374 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4375 goto wait_other_side;
4376
4377 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4378 /* The server has not finished to respond, so we
4379 * don't want to move in order not to upset it.
4380 */
4381 goto wait_other_side;
4382 }
4383
4384 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4385 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004386 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004387 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004388 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389 goto wait_other_side;
4390 }
4391
4392 /* When we get here, it means that both the request and the
4393 * response have finished receiving. Depending on the connection
4394 * mode, we'll have to wait for the last bytes to leave in either
4395 * direction, and sometimes for a close to be effective.
4396 */
4397
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004398 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4399 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004400 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4401 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004402 }
4403 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4404 /* Option forceclose is set, or either side wants to close,
4405 * let's enforce it now that we're not expecting any new
4406 * data to come. The caller knows the session is complete
4407 * once both states are CLOSED.
4408 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004409 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4410 channel_shutr_now(chn);
4411 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004413 }
4414 else {
4415 /* The last possible modes are keep-alive and tunnel. Since tunnel
4416 * mode does not set the body analyser, we can't reach this place
4417 * in tunnel mode, so we're left with keep-alive only.
4418 * This mode is currently not implemented, we switch to tunnel mode.
4419 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004420 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004421 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004422 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004423 }
4424
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004425 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004426 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004427 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004428
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004429 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430 txn->req.msg_state = HTTP_MSG_CLOSING;
4431 goto http_msg_closing;
4432 }
4433 else {
4434 txn->req.msg_state = HTTP_MSG_CLOSED;
4435 goto http_msg_closed;
4436 }
4437 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004438 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004439 }
4440
4441 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4442 http_msg_closing:
4443 /* nothing else to forward, just waiting for the output buffer
4444 * to be empty and for the shutw_now to take effect.
4445 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004446 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 txn->req.msg_state = HTTP_MSG_CLOSED;
4448 goto http_msg_closed;
4449 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004450 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 txn->req.msg_state = HTTP_MSG_ERROR;
4452 goto wait_other_side;
4453 }
4454 }
4455
4456 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4457 http_msg_closed:
4458 goto wait_other_side;
4459 }
4460
4461 wait_other_side:
4462 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004463 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464}
4465
4466
4467/* This function updates the response state machine according to the request
4468 * state machine and buffer flags. It returns 1 if it changes anything (flag
4469 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4470 * it is only used to find when a request/response couple is complete. Both
4471 * this function and its equivalent should loop until both return zero. It
4472 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4473 */
4474int http_sync_res_state(struct session *s)
4475{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004476 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004477 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004478 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004479 unsigned int old_state = txn->rsp.msg_state;
4480
4481 http_silent_debug(__LINE__, s);
4482 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4483 return 0;
4484
4485 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4486 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004487 * still monitor the server connection for a possible close
4488 * while the request is being uploaded, so we don't disable
4489 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004491 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492
4493 if (txn->req.msg_state == HTTP_MSG_ERROR)
4494 goto wait_other_side;
4495
4496 if (txn->req.msg_state < HTTP_MSG_DONE) {
4497 /* The client seems to still be sending data, probably
4498 * because we got an error response during an upload.
4499 * We have the choice of either breaking the connection
4500 * or letting it pass through. Let's do the later.
4501 */
4502 goto wait_other_side;
4503 }
4504
4505 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4506 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004509 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510 goto wait_other_side;
4511 }
4512
4513 /* When we get here, it means that both the request and the
4514 * response have finished receiving. Depending on the connection
4515 * mode, we'll have to wait for the last bytes to leave in either
4516 * direction, and sometimes for a close to be effective.
4517 */
4518
4519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4520 /* Server-close mode : shut read and wait for the request
4521 * side to close its output buffer. The caller will detect
4522 * when we're in DONE and the other is in CLOSED and will
4523 * catch that for the final cleanup.
4524 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004525 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4526 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004528 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4529 /* Option forceclose is set, or either side wants to close,
4530 * let's enforce it now that we're not expecting any new
4531 * data to come. The caller knows the session is complete
4532 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004534 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4535 channel_shutr_now(chn);
4536 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004537 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004538 }
4539 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004540 /* The last possible modes are keep-alive and tunnel. Since tunnel
4541 * mode does not set the body analyser, we can't reach this place
4542 * in tunnel mode, so we're left with keep-alive only.
4543 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004545 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004547 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
4549
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004550 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004552 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4554 goto http_msg_closing;
4555 }
4556 else {
4557 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4558 goto http_msg_closed;
4559 }
4560 }
4561 goto wait_other_side;
4562 }
4563
4564 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4565 http_msg_closing:
4566 /* nothing else to forward, just waiting for the output buffer
4567 * to be empty and for the shutw_now to take effect.
4568 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004569 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4571 goto http_msg_closed;
4572 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004573 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004575 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004576 if (objt_server(s->target))
4577 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004578 goto wait_other_side;
4579 }
4580 }
4581
4582 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4583 http_msg_closed:
4584 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004585 bi_erase(chn);
4586 channel_auto_close(chn);
4587 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 goto wait_other_side;
4589 }
4590
4591 wait_other_side:
4592 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004593 /* We force the response to leave immediately if we're waiting for the
4594 * other side, since there is no pending shutdown to push it out.
4595 */
4596 if (!channel_is_empty(chn))
4597 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004598 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599}
4600
4601
4602/* Resync the request and response state machines. Return 1 if either state
4603 * changes.
4604 */
4605int http_resync_states(struct session *s)
4606{
4607 struct http_txn *txn = &s->txn;
4608 int old_req_state = txn->req.msg_state;
4609 int old_res_state = txn->rsp.msg_state;
4610
4611 http_silent_debug(__LINE__, s);
4612 http_sync_req_state(s);
4613 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004614 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 if (!http_sync_res_state(s))
4616 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004617 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 if (!http_sync_req_state(s))
4619 break;
4620 }
4621 http_silent_debug(__LINE__, s);
4622 /* OK, both state machines agree on a compatible state.
4623 * There are a few cases we're interested in :
4624 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4625 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4626 * directions, so let's simply disable both analysers.
4627 * - HTTP_MSG_CLOSED on the response only means we must abort the
4628 * request.
4629 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4630 * with server-close mode means we've completed one request and we
4631 * must re-initialize the server connection.
4632 */
4633
4634 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4635 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4636 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4637 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4638 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004639 channel_auto_close(s->req);
4640 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004642 channel_auto_close(s->rep);
4643 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004645 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4646 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004647 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004648 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004649 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004650 channel_auto_close(s->rep);
4651 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004652 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 channel_abort(s->req);
4654 channel_auto_close(s->req);
4655 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004656 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004657 }
4658 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4659 txn->rsp.msg_state == HTTP_MSG_DONE &&
4660 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4661 /* server-close: terminate this server connection and
4662 * reinitialize a fresh-new transaction.
4663 */
4664 http_end_txn_clean_session(s);
4665 }
4666
4667 http_silent_debug(__LINE__, s);
4668 return txn->req.msg_state != old_req_state ||
4669 txn->rsp.msg_state != old_res_state;
4670}
4671
Willy Tarreaud98cf932009-12-27 22:54:55 +01004672/* This function is an analyser which forwards request body (including chunk
4673 * sizes if any). It is called as soon as we must forward, even if we forward
4674 * zero byte. The only situation where it must not be called is when we're in
4675 * tunnel mode and we want to forward till the close. It's used both to forward
4676 * remaining data and to resync after end of body. It expects the msg_state to
4677 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4678 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004679 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004680 * bytes of pending data + the headers if not already done (between sol and sov).
4681 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004682 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004683int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004684{
4685 struct http_txn *txn = &s->txn;
4686 struct http_msg *msg = &s->txn.req;
4687
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004688 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4689 return 0;
4690
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004691 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004692 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004693 /* Output closed while we were sending data. We must abort and
4694 * wake the other side up.
4695 */
4696 msg->msg_state = HTTP_MSG_ERROR;
4697 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004698 return 1;
4699 }
4700
Willy Tarreau4fe41902010-06-07 22:27:41 +02004701 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004702 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004703
4704 /* Note that we don't have to send 100-continue back because we don't
4705 * need the data to complete our job, and it's up to the server to
4706 * decide whether to return 100, 417 or anything else in return of
4707 * an "Expect: 100-continue" header.
4708 */
4709
4710 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004711 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004712 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004713 * is still null. We must save the body in msg->next because it
4714 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004715 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004716 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004717
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004718 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004719 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004720 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004721 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004722 }
4723
Willy Tarreaud98cf932009-12-27 22:54:55 +01004724 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004725 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004726
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004728 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004729 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004730 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004731 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004732 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004733 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004734 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004735 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004736
Willy Tarreaucaabe412010-01-03 23:08:28 +01004737 if (msg->msg_state == HTTP_MSG_DATA) {
4738 /* must still forward */
4739 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004740 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004741
4742 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004743 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004744 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004746 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004747 }
4748 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004749 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004750 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004751 * TRAILERS state.
4752 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004753 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004754
Willy Tarreau54d23df2012-10-25 19:04:45 +02004755 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004756 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004757 else if (ret < 0) {
4758 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004759 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004760 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004761 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004762 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004763 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004765 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004766 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004767 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004768
4769 if (ret == 0)
4770 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004771 else if (ret < 0) {
4772 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004773 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004774 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004775 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004776 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777 /* we're in MSG_CHUNK_SIZE now */
4778 }
4779 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004780 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781
4782 if (ret == 0)
4783 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004784 else if (ret < 0) {
4785 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004786 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004787 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004788 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004789 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004790 /* we're in HTTP_MSG_DONE now */
4791 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004792 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004793 int old_state = msg->msg_state;
4794
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004796 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004797 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4798 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004799 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800 if (http_resync_states(s)) {
4801 /* some state changes occurred, maybe the analyser
4802 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004803 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004804 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004805 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004806 /* request errors are most likely due to
4807 * the server aborting the transfer.
4808 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004809 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004810 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004811 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004812 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004814 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004815 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004816 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004817
4818 /* If "option abortonclose" is set on the backend, we
4819 * want to monitor the client's connection and forward
4820 * any shutdown notification to the server, which will
4821 * decide whether to close or to go on processing the
4822 * request.
4823 */
4824 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004825 channel_auto_read(req);
4826 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004827 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004828 else if (s->txn.meth == HTTP_METH_POST) {
4829 /* POST requests may require to read extra CRLF
4830 * sent by broken browsers and which could cause
4831 * an RST to be sent upon close on some systems
4832 * (eg: Linux).
4833 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004834 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004835 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004836
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004838 }
4839 }
4840
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004842 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004843 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004844 if (!(s->flags & SN_ERR_MASK))
4845 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004846 if (!(s->flags & SN_FINST_MASK)) {
4847 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4848 s->flags |= SN_FINST_H;
4849 else
4850 s->flags |= SN_FINST_D;
4851 }
4852
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004853 s->fe->fe_counters.cli_aborts++;
4854 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004855 if (objt_server(s->target))
4856 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004857
4858 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004859 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004860
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004861 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004862 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004863 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004865 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004866 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004867 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004868 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004869 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004870
Willy Tarreau5c620922011-05-11 19:56:11 +02004871 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004872 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004873 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004874 * modes are already handled by the stream sock layer. We must not do
4875 * this in content-length mode because it could present the MSG_MORE
4876 * flag with the last block of forwarded data, which would cause an
4877 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004878 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004879 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004880 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004881
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004883 return 0;
4884
Willy Tarreaud98cf932009-12-27 22:54:55 +01004885 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004886 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004887 if (s->listener->counters)
4888 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004889 return_bad_req_stats_ok:
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 = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004896 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
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 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004900
4901 if (!(s->flags & SN_ERR_MASK))
4902 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004903 if (!(s->flags & SN_FINST_MASK)) {
4904 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4905 s->flags |= SN_FINST_H;
4906 else
4907 s->flags |= SN_FINST_D;
4908 }
4909 return 0;
4910
4911 aborted_xfer:
4912 txn->req.msg_state = HTTP_MSG_ERROR;
4913 if (txn->status) {
4914 /* Note: we don't send any error if some data were already sent */
4915 stream_int_retnclose(req->prod, NULL);
4916 } else {
4917 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004918 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004919 }
4920 req->analysers = 0;
4921 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4922
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004923 s->fe->fe_counters.srv_aborts++;
4924 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004925 if (objt_server(s->target))
4926 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004927
4928 if (!(s->flags & SN_ERR_MASK))
4929 s->flags |= SN_ERR_SRVCL;
4930 if (!(s->flags & SN_FINST_MASK)) {
4931 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4932 s->flags |= SN_FINST_H;
4933 else
4934 s->flags |= SN_FINST_D;
4935 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004936 return 0;
4937}
4938
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4940 * processing can continue on next analysers, or zero if it either needs more
4941 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4942 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4943 * when it has nothing left to do, and may remove any analyser when it wants to
4944 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004945 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004946int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004947{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948 struct http_txn *txn = &s->txn;
4949 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004951 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004952 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004953 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004954
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004955 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 +02004956 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004957 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004958 rep,
4959 rep->rex, rep->wex,
4960 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004961 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004962 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004963
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004964 /*
4965 * Now parse the partial (or complete) lines.
4966 * We will check the response syntax, and also join multi-line
4967 * headers. An index of all the lines will be elaborated while
4968 * parsing.
4969 *
4970 * For the parsing, we use a 28 states FSM.
4971 *
4972 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004973 * rep->buf->p = beginning of response
4974 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4975 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004976 * msg->eol = end of current header or line (LF or CRLF)
4977 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004978 */
4979
Willy Tarreau83e3af02009-12-28 17:39:57 +01004980 /* There's a protected area at the end of the buffer for rewriting
4981 * purposes. We don't want to start to parse the request if the
4982 * protected area is affected, because we may have to move processed
4983 * data later, which is much more complicated.
4984 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004985 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004986 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004987 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4988 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4989 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004990 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004991 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004992 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004993 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004994 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004995 return 0;
4996 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004997 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4998 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004999 }
5000
Willy Tarreau9b28e032012-10-12 23:49:43 +02005001 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005002 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005003 }
5004
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005005 /* 1: we might have to print this header in debug mode */
5006 if (unlikely((global.mode & MODE_DEBUG) &&
5007 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005008 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005009 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005010
Willy Tarreau9b28e032012-10-12 23:49:43 +02005011 sol = rep->buf->p;
5012 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005013 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005014
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 sol += hdr_idx_first_pos(&txn->hdr_idx);
5016 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005018 while (cur_idx) {
5019 eol = sol + txn->hdr_idx.v[cur_idx].len;
5020 debug_hdr("srvhdr", s, sol, eol);
5021 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5022 cur_idx = txn->hdr_idx.v[cur_idx].next;
5023 }
5024 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005025
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005026 /*
5027 * Now we quickly check if we have found a full valid response.
5028 * If not so, we check the FD and buffer states before leaving.
5029 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005030 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005031 * responses are checked first.
5032 *
5033 * Depending on whether the client is still there or not, we
5034 * may send an error response back or not. Note that normally
5035 * we should only check for HTTP status there, and check I/O
5036 * errors somewhere else.
5037 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038
Willy Tarreau655dce92009-11-08 13:10:58 +01005039 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005040 /* Invalid response */
5041 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5042 /* we detected a parsing error. We want to archive this response
5043 * in the dedicated proxy area for later troubleshooting.
5044 */
5045 hdr_response_bad:
5046 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005047 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005048
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005049 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005050 if (objt_server(s->target)) {
5051 objt_server(s->target)->counters.failed_resp++;
5052 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005053 }
Willy Tarreau64648412010-03-05 10:41:54 +01005054 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005055 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 rep->analysers = 0;
5057 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005058 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005059 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005060 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061
5062 if (!(s->flags & SN_ERR_MASK))
5063 s->flags |= SN_ERR_PRXCOND;
5064 if (!(s->flags & SN_FINST_MASK))
5065 s->flags |= SN_FINST_H;
5066
5067 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005068 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005069
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005071 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005072 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005073 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005074 goto hdr_response_bad;
5075 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005076
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005078 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005080 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005081
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005082 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005083 if (objt_server(s->target)) {
5084 objt_server(s->target)->counters.failed_resp++;
5085 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005086 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005087
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005088 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 rep->analysers = 0;
5090 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005091 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005092 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005093 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005094
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 if (!(s->flags & SN_ERR_MASK))
5096 s->flags |= SN_ERR_SRVCL;
5097 if (!(s->flags & SN_FINST_MASK))
5098 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005099 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005100 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005101
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005102 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005103 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005104 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005105 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005106
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005107 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005108 if (objt_server(s->target)) {
5109 objt_server(s->target)->counters.failed_resp++;
5110 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005111 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005112
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005113 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 rep->analysers = 0;
5115 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005116 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005117 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005118 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005119
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 if (!(s->flags & SN_ERR_MASK))
5121 s->flags |= SN_ERR_SRVTO;
5122 if (!(s->flags & SN_FINST_MASK))
5123 s->flags |= SN_FINST_H;
5124 return 0;
5125 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005126
Willy Tarreauf003d372012-11-26 13:35:37 +01005127 /* client abort with an abortonclose */
5128 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5129 s->fe->fe_counters.cli_aborts++;
5130 s->be->be_counters.cli_aborts++;
5131 if (objt_server(s->target))
5132 objt_server(s->target)->counters.cli_aborts++;
5133
5134 rep->analysers = 0;
5135 channel_auto_close(rep);
5136
5137 txn->status = 400;
5138 bi_erase(rep);
5139 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5140
5141 if (!(s->flags & SN_ERR_MASK))
5142 s->flags |= SN_ERR_CLICL;
5143 if (!(s->flags & SN_FINST_MASK))
5144 s->flags |= SN_FINST_H;
5145
5146 /* process_session() will take care of the error */
5147 return 0;
5148 }
5149
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005150 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005151 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005152 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005153 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005154
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005155 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005156 if (objt_server(s->target)) {
5157 objt_server(s->target)->counters.failed_resp++;
5158 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005159 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005160
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005161 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162 rep->analysers = 0;
5163 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005164 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005165 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005166 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005167
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168 if (!(s->flags & SN_ERR_MASK))
5169 s->flags |= SN_ERR_SRVCL;
5170 if (!(s->flags & SN_FINST_MASK))
5171 s->flags |= SN_FINST_H;
5172 return 0;
5173 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005174
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005175 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005176 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005177 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005178 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005179
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005180 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005181 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005182 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183
5184 if (!(s->flags & SN_ERR_MASK))
5185 s->flags |= SN_ERR_CLICL;
5186 if (!(s->flags & SN_FINST_MASK))
5187 s->flags |= SN_FINST_H;
5188
5189 /* process_session() will take care of the error */
5190 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005191 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005192
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005193 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 return 0;
5195 }
5196
5197 /* More interesting part now : we know that we have a complete
5198 * response which at least looks like HTTP. We have an indicator
5199 * of each header's length, so we can parse them quickly.
5200 */
5201
5202 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005203 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005205 /*
5206 * 1: get the status code
5207 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005208 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 if (n < 1 || n > 5)
5210 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005211 /* when the client triggers a 4xx from the server, it's most often due
5212 * to a missing object or permission. These events should be tracked
5213 * because if they happen often, it may indicate a brute force or a
5214 * vulnerability scan.
5215 */
5216 if (n == 4)
5217 session_inc_http_err_ctr(s);
5218
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005219 if (objt_server(s->target))
5220 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005221
Willy Tarreau5b154472009-12-21 20:11:07 +01005222 /* check if the response is HTTP/1.1 or above */
5223 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005224 ((rep->buf->p[5] > '1') ||
5225 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005226 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005227
5228 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005229 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 +01005230
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005231 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005232 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005233
Willy Tarreau9b28e032012-10-12 23:49:43 +02005234 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235
Willy Tarreau39650402010-03-15 19:44:39 +01005236 /* Adjust server's health based on status code. Note: status codes 501
5237 * and 505 are triggered on demand by client request, so we must not
5238 * count them as server failures.
5239 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005240 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005241 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005242 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005243 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005244 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005245 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005246
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 /*
5248 * 2: check for cacheability.
5249 */
5250
5251 switch (txn->status) {
5252 case 200:
5253 case 203:
5254 case 206:
5255 case 300:
5256 case 301:
5257 case 410:
5258 /* RFC2616 @13.4:
5259 * "A response received with a status code of
5260 * 200, 203, 206, 300, 301 or 410 MAY be stored
5261 * by a cache (...) unless a cache-control
5262 * directive prohibits caching."
5263 *
5264 * RFC2616 @9.5: POST method :
5265 * "Responses to this method are not cacheable,
5266 * unless the response includes appropriate
5267 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005269 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005270 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5272 break;
5273 default:
5274 break;
5275 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005276
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277 /*
5278 * 3: we may need to capture headers
5279 */
5280 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005281 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005282 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005283 txn->rsp.cap, s->fe->rsp_cap);
5284
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005285 /* 4: determine the transfer-length.
5286 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5287 * the presence of a message-body in a RESPONSE and its transfer length
5288 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005289 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005290 * All responses to the HEAD request method MUST NOT include a
5291 * message-body, even though the presence of entity-header fields
5292 * might lead one to believe they do. All 1xx (informational), 204
5293 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5294 * message-body. All other responses do include a message-body,
5295 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005296 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005297 * 1. Any response which "MUST NOT" include a message-body (such as the
5298 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5299 * always terminated by the first empty line after the header fields,
5300 * regardless of the entity-header fields present in the message.
5301 *
5302 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5303 * the "chunked" transfer-coding (Section 6.2) is used, the
5304 * transfer-length is defined by the use of this transfer-coding.
5305 * If a Transfer-Encoding header field is present and the "chunked"
5306 * transfer-coding is not present, the transfer-length is defined by
5307 * the sender closing the connection.
5308 *
5309 * 3. If a Content-Length header field is present, its decimal value in
5310 * OCTETs represents both the entity-length and the transfer-length.
5311 * If a message is received with both a Transfer-Encoding header
5312 * field and a Content-Length header field, the latter MUST be ignored.
5313 *
5314 * 4. If the message uses the media type "multipart/byteranges", and
5315 * the transfer-length is not otherwise specified, then this self-
5316 * delimiting media type defines the transfer-length. This media
5317 * type MUST NOT be used unless the sender knows that the recipient
5318 * can parse it; the presence in a request of a Range header with
5319 * multiple byte-range specifiers from a 1.1 client implies that the
5320 * client can parse multipart/byteranges responses.
5321 *
5322 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005323 */
5324
5325 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005326 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005327 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005328 * FIXME: should we parse anyway and return an error on chunked encoding ?
5329 */
5330 if (txn->meth == HTTP_METH_HEAD ||
5331 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005332 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005333 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005334 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005335 goto skip_content_length;
5336 }
5337
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005338 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005339 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005340 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005341 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005342 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005343 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5344 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005345 /* bad transfer-encoding (chunked followed by something else) */
5346 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005347 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005348 break;
5349 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005350 }
5351
5352 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5353 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005354 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005355 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005356 signed long long cl;
5357
Willy Tarreauad14f752011-09-02 20:33:27 +02005358 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005359 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005360 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005361 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005362
Willy Tarreauad14f752011-09-02 20:33:27 +02005363 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005364 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005365 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005366 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005367
Willy Tarreauad14f752011-09-02 20:33:27 +02005368 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005369 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005370 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005371 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005372
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005373 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005374 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005375 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005376 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005377
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005378 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005379 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005380 }
5381
William Lallemand82fe75c2012-10-23 10:25:10 +02005382 if (s->fe->comp || s->be->comp)
5383 select_compression_response_header(s, rep->buf);
5384
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005385 /* FIXME: we should also implement the multipart/byterange method.
5386 * For now on, we resort to close mode in this case (unknown length).
5387 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005388skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005389
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005390 /* end of job, return OK */
5391 rep->analysers &= ~an_bit;
5392 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005393 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394 return 1;
5395}
5396
5397/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005398 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5399 * and updates t->rep->analysers. It might make sense to explode it into several
5400 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005402int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403{
5404 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005405 struct http_msg *msg = &txn->rsp;
5406 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005407 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005408
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005409 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 +02005410 now_ms, __FUNCTION__,
5411 t,
5412 rep,
5413 rep->rex, rep->wex,
5414 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005415 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 rep->analysers);
5417
Willy Tarreau655dce92009-11-08 13:10:58 +01005418 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 return 0;
5420
5421 rep->analysers &= ~an_bit;
5422 rep->analyse_exp = TICK_ETERNITY;
5423
Willy Tarreau5b154472009-12-21 20:11:07 +01005424 /* Now we have to check if we need to modify the Connection header.
5425 * This is more difficult on the response than it is on the request,
5426 * because we can have two different HTTP versions and we don't know
5427 * how the client will interprete a response. For instance, let's say
5428 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5429 * HTTP/1.1 response without any header. Maybe it will bound itself to
5430 * HTTP/1.0 because it only knows about it, and will consider the lack
5431 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5432 * the lack of header as a keep-alive. Thus we will use two flags
5433 * indicating how a request MAY be understood by the client. In case
5434 * of multiple possibilities, we'll fix the header to be explicit. If
5435 * ambiguous cases such as both close and keepalive are seen, then we
5436 * will fall back to explicit close. Note that we won't take risks with
5437 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005438 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005439 */
5440
Willy Tarreaudc008c52010-02-01 16:20:08 +01005441 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5442 txn->status == 101)) {
5443 /* Either we've established an explicit tunnel, or we're
5444 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005445 * to understand the next protocols. We have to switch to tunnel
5446 * mode, so that we transfer the request and responses then let
5447 * this protocol pass unmodified. When we later implement specific
5448 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005449 * header which contains information about that protocol for
5450 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005451 */
5452 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5453 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005454 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5455 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5456 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005457 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005458
Willy Tarreau60466522010-01-18 19:08:45 +01005459 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005460 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005461 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5462 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005463
Willy Tarreau60466522010-01-18 19:08:45 +01005464 /* now adjust header transformations depending on current state */
5465 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5466 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5467 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005468 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005469 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005470 }
Willy Tarreau60466522010-01-18 19:08:45 +01005471 else { /* SCL / KAL */
5472 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005473 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005474 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005475 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005476
Willy Tarreau60466522010-01-18 19:08:45 +01005477 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005478 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005479
Willy Tarreau60466522010-01-18 19:08:45 +01005480 /* Some keep-alive responses are converted to Server-close if
5481 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005482 */
Willy Tarreau60466522010-01-18 19:08:45 +01005483 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5484 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005485 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005486 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005487 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005488 }
5489
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005490 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005491 /*
5492 * 3: we will have to evaluate the filters.
5493 * As opposed to version 1.2, now they will be evaluated in the
5494 * filters order and not in the header order. This means that
5495 * each filter has to be validated among all headers.
5496 *
5497 * Filters are tried with ->be first, then with ->fe if it is
5498 * different from ->be.
5499 */
5500
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005501 cur_proxy = t->be;
5502 while (1) {
5503 struct proxy *rule_set = cur_proxy;
5504
5505 /* try headers filters */
5506 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005507 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005508 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005509 if (objt_server(t->target)) {
5510 objt_server(t->target)->counters.failed_resp++;
5511 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005512 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005513 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005514 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005515 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005516 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005517 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005518 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005519 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005520 if (!(t->flags & SN_ERR_MASK))
5521 t->flags |= SN_ERR_PRXCOND;
5522 if (!(t->flags & SN_FINST_MASK))
5523 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005524 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005525 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005526 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005527
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005528 /* has the response been denied ? */
5529 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005530 if (objt_server(t->target))
5531 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005532
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005533 t->be->be_counters.denied_resp++;
5534 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535 if (t->listener->counters)
5536 t->listener->counters->denied_resp++;
5537
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005538 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005539 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005542 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005543 if (txn->status < 200)
5544 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005545 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005546 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005547 ret = acl_pass(ret);
5548 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5549 ret = !ret;
5550 if (!ret)
5551 continue;
5552 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005553 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005554 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005555 }
5556
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005557 /* check whether we're already working on the frontend */
5558 if (cur_proxy == t->fe)
5559 break;
5560 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005562
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005563 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005564 * We may be facing a 100-continue response, in which case this
5565 * is not the right response, and we're waiting for the next one.
5566 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005567 * next one.
5568 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005569 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005570 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005571 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005572 msg->msg_state = HTTP_MSG_RPBEFORE;
5573 txn->status = 0;
5574 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5575 return 1;
5576 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005577 else if (unlikely(txn->status < 200))
5578 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005579
5580 /* we don't have any 1xx status code now */
5581
5582 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005583 * 4: check for server cookie.
5584 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005585 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5586 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005587 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005588
Willy Tarreaubaaee002006-06-26 02:48:02 +02005589
Willy Tarreaua15645d2007-03-18 16:22:39 +01005590 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005591 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005592 */
Willy Tarreau67402132012-05-31 20:40:20 +02005593 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005594 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005596 /*
5597 * 6: add server cookie in the response if needed
5598 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005599 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005600 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005601 (!(t->flags & SN_DIRECT) ||
5602 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5603 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5604 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5605 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005606 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005607 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005608 /* the server is known, it's not the one the client requested, or the
5609 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005610 * insert a set-cookie here, except if we want to insert only on POST
5611 * requests and this one isn't. Note that servers which don't have cookies
5612 * (eg: some backup servers) will return a full cookie removal request.
5613 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005614 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005615 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005616 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5617 t->be->cookie_name);
5618 }
5619 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005620 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005621
5622 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5623 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5625 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5626 trash.len += 5;
5627
Willy Tarreauef4f3912010-10-07 21:00:29 +02005628 if (t->be->cookie_maxlife) {
5629 /* emit first_date, which is either the original one or
5630 * the current date.
5631 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005632 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005633 s30tob64(txn->cookie_first_date ?
5634 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005635 (date.tv_sec+3) >> 2, trash.str + trash.len);
5636 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005637 }
5638 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005639 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005640 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005641
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005642 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005643 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005644
Willy Tarreau4992dd22012-05-31 21:02:17 +02005645 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005646 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005647
5648 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005649 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005650
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005651 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005652 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005653
Willy Tarreauf1348312010-10-07 15:54:11 +02005654 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005655 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005656 /* the server did not change, only the date was updated */
5657 txn->flags |= TX_SCK_UPDATED;
5658 else
5659 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005660
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005661 /* Here, we will tell an eventual cache on the client side that we don't
5662 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5663 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5664 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5665 */
Willy Tarreau67402132012-05-31 20:40:20 +02005666 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005667
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005668 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5669
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005670 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005671 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005672 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005673 }
5674 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005675
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005676 /*
5677 * 7: check if result will be cacheable with a cookie.
5678 * We'll block the response if security checks have caught
5679 * nasty things such as a cacheable cookie.
5680 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005681 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5682 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005683 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005684
5685 /* we're in presence of a cacheable response containing
5686 * a set-cookie header. We'll block it as requested by
5687 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005688 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005689 if (objt_server(t->target))
5690 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005691
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005692 t->be->be_counters.denied_resp++;
5693 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005694 if (t->listener->counters)
5695 t->listener->counters->denied_resp++;
5696
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005697 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005698 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699 send_log(t->be, LOG_ALERT,
5700 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005701 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005702 goto return_srv_prx_502;
5703 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704
5705 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005706 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005707 * If an "Upgrade" token is found, the header is left untouched in order
5708 * not to have to deal with some client bugs : some of them fail an upgrade
5709 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005710 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005711 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5712 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5713 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005714 unsigned int want_flags = 0;
5715
5716 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5717 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5718 /* we want a keep-alive response here. Keep-alive header
5719 * required if either side is not 1.1.
5720 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005721 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005722 want_flags |= TX_CON_KAL_SET;
5723 }
5724 else {
5725 /* we want a close response here. Close header required if
5726 * the server is 1.1, regardless of the client.
5727 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005728 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005729 want_flags |= TX_CON_CLO_SET;
5730 }
5731
5732 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005733 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005734 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005735
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005736 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005737 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005738 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005739 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005740
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005741 /*************************************************************
5742 * OK, that's finished for the headers. We have done what we *
5743 * could. Let's switch to the DATA state. *
5744 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005745
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005747
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 /* if the user wants to log as soon as possible, without counting
5749 * bytes from the server, then this is the right moment. We have
5750 * to temporarily assign bytes_out to log what we currently have.
5751 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005752 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005753 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5754 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005755 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005756 t->logs.bytes_out = 0;
5757 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005758
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005759 /* Note: we must not try to cheat by jumping directly to DATA,
5760 * otherwise we would not let the client side wake up.
5761 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005763 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005764 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005765 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005766}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005767
Willy Tarreaud98cf932009-12-27 22:54:55 +01005768/* This function is an analyser which forwards response body (including chunk
5769 * sizes if any). It is called as soon as we must forward, even if we forward
5770 * zero byte. The only situation where it must not be called is when we're in
5771 * tunnel mode and we want to forward till the close. It's used both to forward
5772 * remaining data and to resync after end of body. It expects the msg_state to
5773 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5774 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005775 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005776 * bytes of pending data + the headers if not already done (between sol and sov).
5777 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005778 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005779int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005780{
5781 struct http_txn *txn = &s->txn;
5782 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005783 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005784 static struct buffer *tmpbuf = NULL;
5785 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005786 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005787 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005788
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005789 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5790 return 0;
5791
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005792 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005793 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005794 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005795 /* Output closed while we were sending data. We must abort and
5796 * wake the other side up.
5797 */
5798 msg->msg_state = HTTP_MSG_ERROR;
5799 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005800 return 1;
5801 }
5802
Willy Tarreau4fe41902010-06-07 22:27:41 +02005803 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005804 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005805
William Lallemand82fe75c2012-10-23 10:25:10 +02005806 /* this is the first time we need the compression buffer */
5807 if (s->comp_algo != NULL && tmpbuf == NULL) {
5808 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5809 goto aborted_xfer; /* no memory */
5810 }
5811
Willy Tarreaud98cf932009-12-27 22:54:55 +01005812 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005813 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005814 * rep->buf.p still points to the beginning of the message and msg->sol
5815 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005816 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005817 channel_forward(res, msg->sov);
5818 msg->next = 0;
5819 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005820
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005821 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005822 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005823 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005824 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005825 }
5826
William Lallemand82fe75c2012-10-23 10:25:10 +02005827 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005828 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005829 if (ret < 0)
5830 goto missing_data; /* not enough spaces in buffers */
5831 compressing = 1;
5832 }
5833
Willy Tarreaud98cf932009-12-27 22:54:55 +01005834 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005835 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005836 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005837 if (s->comp_algo == NULL) {
5838 bytes = msg->sov - msg->sol;
5839 if (msg->chunk_len || bytes) {
5840 msg->sol = msg->sov;
5841 msg->next -= bytes; /* will be forwarded */
5842 msg->chunk_len += bytes;
5843 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5844 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005845 }
5846
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005847 switch (msg->msg_state - HTTP_MSG_DATA) {
5848 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005849 if (compressing) {
5850 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5851 if (ret < 0)
5852 goto aborted_xfer;
5853 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005854
5855 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005856 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005857
5858 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005859 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005860 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005861 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005862 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005863 if (compressing && consumed_data) {
5864 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5865 compressing = 0;
5866 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005867 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005868 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005869 /* fall through for HTTP_MSG_CHUNK_CRLF */
5870
5871 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5872 /* we want the CRLF after the data */
5873
5874 ret = http_skip_chunk_crlf(msg);
5875 if (ret == 0)
5876 goto missing_data;
5877 else if (ret < 0) {
5878 if (msg->err_pos >= 0)
5879 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5880 goto return_bad_res;
5881 }
5882 /* skipping data in buffer for compression */
5883 if (compressing) {
5884 b_adv(res->buf, msg->next);
5885 msg->next = 0;
5886 msg->sov = 0;
5887 msg->sol = 0;
5888 }
5889 /* we're in MSG_CHUNK_SIZE now, fall through */
5890
5891 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005892 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005893 * set ->sov and ->next to point to the body and switch to DATA or
5894 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005895 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005896
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005897 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005898 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005899 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005900 else if (ret < 0) {
5901 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005902 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005903 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005904 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005905 if (compressing) {
5906 if (likely(msg->chunk_len > 0)) {
5907 /* skipping data if we are in compression mode */
5908 b_adv(res->buf, msg->next);
5909 msg->next = 0;
5910 msg->sov = 0;
5911 msg->sol = 0;
5912 } else {
5913 if (consumed_data) {
5914 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5915 compressing = 0;
5916 }
5917 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005918 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005919 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005920 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005921
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005922 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5923 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005924 if (ret == 0)
5925 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005926 else if (ret < 0) {
5927 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005928 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005929 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005930 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005931 if (s->comp_algo != NULL) {
5932 /* forwarding trailers */
5933 channel_forward(res, msg->next);
5934 msg->next = 0;
5935 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005936 /* we're in HTTP_MSG_DONE now, fall through */
5937
5938 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005939 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005940
5941 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005942 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005943 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5944 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005945 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005946 if (http_resync_states(s)) {
5947 http_silent_debug(__LINE__, s);
5948 /* some state changes occurred, maybe the analyser
5949 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005950 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005951 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005952 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005953 /* response errors are most likely due to
5954 * the client aborting the transfer.
5955 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005956 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005957 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005958 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005959 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005960 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005961 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005962 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005963 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005964 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005965 }
5966 }
5967
Willy Tarreaud98cf932009-12-27 22:54:55 +01005968 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005969 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005970 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5971 compressing = 0;
5972 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005973
5974 if (res->flags & CF_SHUTW)
5975 goto aborted_xfer;
5976
5977 /* stop waiting for data if the input is closed before the end. If the
5978 * client side was already closed, it means that the client has aborted,
5979 * so we don't want to count this as a server abort. Otherwise it's a
5980 * server abort.
5981 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005982 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005983 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5984 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005985 if (!(s->flags & SN_ERR_MASK))
5986 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005987 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005988 if (objt_server(s->target))
5989 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005990 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005991 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005992
Willy Tarreau40dba092010-03-04 18:14:51 +01005993 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005994 if (!s->req->analysers)
5995 goto return_bad_res;
5996
Willy Tarreauea953162012-05-18 23:41:28 +02005997 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005998 if (s->comp_algo == NULL) {
5999 bytes = msg->sov - msg->sol;
6000 if (msg->chunk_len || bytes) {
6001 msg->sol = msg->sov;
6002 msg->next -= bytes; /* will be forwarded */
6003 msg->chunk_len += bytes;
6004 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6005 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006006 }
6007
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006008 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006009 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006010 * Similarly, with keep-alive on the client side, we don't want to forward a
6011 * close.
6012 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006013 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006014 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6015 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006016 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006017
Willy Tarreau5c620922011-05-11 19:56:11 +02006018 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006019 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006020 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006021 * modes are already handled by the stream sock layer. We must not do
6022 * this in content-length mode because it could present the MSG_MORE
6023 * flag with the last block of forwarded data, which would cause an
6024 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006025 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006026 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006027 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006028
Willy Tarreaud98cf932009-12-27 22:54:55 +01006029 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006030 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006031 return 0;
6032
Willy Tarreau40dba092010-03-04 18:14:51 +01006033 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006034 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006035 if (objt_server(s->target))
6036 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006037
6038 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006039 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006040 /* don't send any error message as we're in the body */
6041 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006042 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006043 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006044 if (objt_server(s->target))
6045 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006046
6047 if (!(s->flags & SN_ERR_MASK))
6048 s->flags |= SN_ERR_PRXCOND;
6049 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006050 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006051 return 0;
6052
6053 aborted_xfer:
6054 txn->rsp.msg_state = HTTP_MSG_ERROR;
6055 /* don't send any error message as we're in the body */
6056 stream_int_retnclose(res->cons, NULL);
6057 res->analysers = 0;
6058 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006060 s->fe->fe_counters.cli_aborts++;
6061 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006062 if (objt_server(s->target))
6063 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006064
6065 if (!(s->flags & SN_ERR_MASK))
6066 s->flags |= SN_ERR_CLICL;
6067 if (!(s->flags & SN_FINST_MASK))
6068 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006069 return 0;
6070}
6071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072/* Iterate the same filter through all request headers.
6073 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006074 * Since it can manage the switch to another backend, it updates the per-proxy
6075 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006077int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 char term;
6080 char *cur_ptr, *cur_end, *cur_next;
6081 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006082 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006084 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086 last_hdr = 0;
6087
Willy Tarreau9b28e032012-10-12 23:49:43 +02006088 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 old_idx = 0;
6090
6091 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006092 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006093 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006094 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 (exp->action == ACT_ALLOW ||
6096 exp->action == ACT_DENY ||
6097 exp->action == ACT_TARPIT))
6098 return 0;
6099
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006100 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006101 if (!cur_idx)
6102 break;
6103
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006104 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105 cur_ptr = cur_next;
6106 cur_end = cur_ptr + cur_hdr->len;
6107 cur_next = cur_end + cur_hdr->cr + 1;
6108
6109 /* Now we have one header between cur_ptr and cur_end,
6110 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111 */
6112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 /* The annoying part is that pattern matching needs
6114 * that we modify the contents to null-terminate all
6115 * strings before testing them.
6116 */
6117
6118 term = *cur_end;
6119 *cur_end = '\0';
6120
6121 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6122 switch (exp->action) {
6123 case ACT_SETBE:
6124 /* It is not possible to jump a second time.
6125 * FIXME: should we return an HTTP/500 here so that
6126 * the admin knows there's a problem ?
6127 */
6128 if (t->be != t->fe)
6129 break;
6130
6131 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006132 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 last_hdr = 1;
6134 break;
6135
6136 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006137 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 last_hdr = 1;
6139 break;
6140
6141 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006142 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006143 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006144
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006145 t->fe->fe_counters.denied_req++;
6146 if (t->fe != t->be)
6147 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006148 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006149 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 break;
6152
6153 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006154 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006156
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006157 t->fe->fe_counters.denied_req++;
6158 if (t->fe != t->be)
6159 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006160 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006161 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 break;
6164
6165 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006166 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6167 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006168 /* FIXME: if the user adds a newline in the replacement, the
6169 * index will not be recalculated for now, and the new line
6170 * will not be counted as a new header.
6171 */
6172
6173 cur_end += delta;
6174 cur_next += delta;
6175 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006176 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006177 break;
6178
6179 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006180 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006181 cur_next += delta;
6182
Willy Tarreaufa355d42009-11-29 18:12:29 +01006183 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006184 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6185 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006186 cur_hdr->len = 0;
6187 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006188 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006189 break;
6190
6191 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006192 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006193 if (cur_end)
6194 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196 /* keep the link from this header to next one in case of later
6197 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006199 old_idx = cur_idx;
6200 }
6201 return 0;
6202}
6203
6204
6205/* Apply the filter to the request line.
6206 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6207 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006208 * Since it can manage the switch to another backend, it updates the per-proxy
6209 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006210 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006211int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006212{
6213 char term;
6214 char *cur_ptr, *cur_end;
6215 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006216 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006217 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreau3d300592007-03-18 18:34:41 +01006219 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006220 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006221 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006222 (exp->action == ACT_ALLOW ||
6223 exp->action == ACT_DENY ||
6224 exp->action == ACT_TARPIT))
6225 return 0;
6226 else if (exp->action == ACT_REMOVE)
6227 return 0;
6228
6229 done = 0;
6230
Willy Tarreau9b28e032012-10-12 23:49:43 +02006231 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006232 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006233
6234 /* Now we have the request line between cur_ptr and cur_end */
6235
6236 /* The annoying part is that pattern matching needs
6237 * that we modify the contents to null-terminate all
6238 * strings before testing them.
6239 */
6240
6241 term = *cur_end;
6242 *cur_end = '\0';
6243
6244 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6245 switch (exp->action) {
6246 case ACT_SETBE:
6247 /* It is not possible to jump a second time.
6248 * FIXME: should we return an HTTP/500 here so that
6249 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006251 if (t->be != t->fe)
6252 break;
6253
6254 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006255 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006256 done = 1;
6257 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006259 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006260 txn->flags |= TX_CLALLOW;
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_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006265 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006266
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006267 t->fe->fe_counters.denied_req++;
6268 if (t->fe != t->be)
6269 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006270 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006271 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006273 done = 1;
6274 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006276 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006277 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006278
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006279 t->fe->fe_counters.denied_req++;
6280 if (t->fe != t->be)
6281 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006282 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006283 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006285 done = 1;
6286 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006287
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 case ACT_REPLACE:
6289 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006290 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6291 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006292 /* FIXME: if the user adds a newline in the replacement, the
6293 * index will not be recalculated for now, and the new line
6294 * will not be counted as a new header.
6295 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006296
Willy Tarreaufa355d42009-11-29 18:12:29 +01006297 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006298 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006299 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 HTTP_MSG_RQMETH,
6301 cur_ptr, cur_end + 1,
6302 NULL, NULL);
6303 if (unlikely(!cur_end))
6304 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006306 /* we have a full request and we know that we have either a CR
6307 * or an LF at <ptr>.
6308 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006309 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6310 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006311 /* there is no point trying this regex on headers */
6312 return 1;
6313 }
6314 }
6315 *cur_end = term; /* restore the string terminator */
6316 return done;
6317}
Willy Tarreau97de6242006-12-27 17:18:38 +01006318
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006322 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006323 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006324 * unparsable request. Since it can manage the switch to another backend, it
6325 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006327int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006328{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006329 struct http_txn *txn = &s->txn;
6330 struct hdr_exp *exp;
6331
6332 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006333 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006335 /*
6336 * The interleaving of transformations and verdicts
6337 * makes it difficult to decide to continue or stop
6338 * the evaluation.
6339 */
6340
Willy Tarreau6c123b12010-01-28 20:22:06 +01006341 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6342 break;
6343
Willy Tarreau3d300592007-03-18 18:34:41 +01006344 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006346 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006348
6349 /* if this filter had a condition, evaluate it now and skip to
6350 * next filter if the condition does not match.
6351 */
6352 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006353 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006354 ret = acl_pass(ret);
6355 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6356 ret = !ret;
6357
6358 if (!ret)
6359 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 }
6361
6362 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006363 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006364 if (unlikely(ret < 0))
6365 return -1;
6366
6367 if (likely(ret == 0)) {
6368 /* The filter did not match the request, it can be
6369 * iterated through all headers.
6370 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006371 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375}
6376
6377
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006380 * Try to retrieve the server associated to the appsession.
6381 * If the server is found, it's assigned to the session.
6382 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006383void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006384 struct http_txn *txn = &t->txn;
6385 appsess *asession = NULL;
6386 char *sessid_temp = NULL;
6387
Cyril Bontéb21570a2009-11-29 20:04:48 +01006388 if (len > t->be->appsession_len) {
6389 len = t->be->appsession_len;
6390 }
6391
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006392 if (t->be->options2 & PR_O2_AS_REQL) {
6393 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006394 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006395 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006396 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006397 }
6398
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006399 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006400 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6401 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6402 return;
6403 }
6404
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006405 memcpy(txn->sessid, buf, len);
6406 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006407 }
6408
6409 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6410 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6411 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6412 return;
6413 }
6414
Cyril Bontéb21570a2009-11-29 20:04:48 +01006415 memcpy(sessid_temp, buf, len);
6416 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006417
6418 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6419 /* free previously allocated memory */
6420 pool_free2(apools.sessid, sessid_temp);
6421
6422 if (asession != NULL) {
6423 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6424 if (!(t->be->options2 & PR_O2_AS_REQL))
6425 asession->request_count++;
6426
6427 if (asession->serverid != NULL) {
6428 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006429
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006430 while (srv) {
6431 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006432 if ((srv->state & SRV_RUNNING) ||
6433 (t->be->options & PR_O_PERSIST) ||
6434 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006435 /* we found the server and it's usable */
6436 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006437 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006438 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006439 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006440
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006441 break;
6442 } else {
6443 txn->flags &= ~TX_CK_MASK;
6444 txn->flags |= TX_CK_DOWN;
6445 }
6446 }
6447 srv = srv->next;
6448 }
6449 }
6450 }
6451}
6452
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006453/* Find the end of a cookie value contained between <s> and <e>. It works the
6454 * same way as with headers above except that the semi-colon also ends a token.
6455 * See RFC2965 for more information. Note that it requires a valid header to
6456 * return a valid result.
6457 */
6458char *find_cookie_value_end(char *s, const char *e)
6459{
6460 int quoted, qdpair;
6461
6462 quoted = qdpair = 0;
6463 for (; s < e; s++) {
6464 if (qdpair) qdpair = 0;
6465 else if (quoted) {
6466 if (*s == '\\') qdpair = 1;
6467 else if (*s == '"') quoted = 0;
6468 }
6469 else if (*s == '"') quoted = 1;
6470 else if (*s == ',' || *s == ';') return s;
6471 }
6472 return s;
6473}
6474
6475/* Delete a value in a header between delimiters <from> and <next> in buffer
6476 * <buf>. The number of characters displaced is returned, and the pointer to
6477 * the first delimiter is updated if required. The function tries as much as
6478 * possible to respect the following principles :
6479 * - replace <from> delimiter by the <next> one unless <from> points to a
6480 * colon, in which case <next> is simply removed
6481 * - set exactly one space character after the new first delimiter, unless
6482 * there are not enough characters in the block being moved to do so.
6483 * - remove unneeded spaces before the previous delimiter and after the new
6484 * one.
6485 *
6486 * It is the caller's responsibility to ensure that :
6487 * - <from> points to a valid delimiter or the colon ;
6488 * - <next> points to a valid delimiter or the final CR/LF ;
6489 * - there are non-space chars before <from> ;
6490 * - there is a CR/LF at or after <next>.
6491 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006492int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493{
6494 char *prev = *from;
6495
6496 if (*prev == ':') {
6497 /* We're removing the first value, preserve the colon and add a
6498 * space if possible.
6499 */
6500 if (!http_is_crlf[(unsigned char)*next])
6501 next++;
6502 prev++;
6503 if (prev < next)
6504 *prev++ = ' ';
6505
6506 while (http_is_spht[(unsigned char)*next])
6507 next++;
6508 } else {
6509 /* Remove useless spaces before the old delimiter. */
6510 while (http_is_spht[(unsigned char)*(prev-1)])
6511 prev--;
6512 *from = prev;
6513
6514 /* copy the delimiter and if possible a space if we're
6515 * not at the end of the line.
6516 */
6517 if (!http_is_crlf[(unsigned char)*next]) {
6518 *prev++ = *next++;
6519 if (prev + 1 < next)
6520 *prev++ = ' ';
6521 while (http_is_spht[(unsigned char)*next])
6522 next++;
6523 }
6524 }
6525 return buffer_replace2(buf, prev, next, NULL, 0);
6526}
6527
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006528/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006529 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 * desirable to call it only when needed. This code is quite complex because
6531 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6532 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006534void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006536 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6540 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006541
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006542 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006543 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006544 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006546 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006548 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006550 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006551 hdr_beg = hdr_next;
6552 hdr_end = hdr_beg + cur_hdr->len;
6553 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 /* We have one full header between hdr_beg and hdr_end, and the
6556 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557 * "Cookie:" headers.
6558 */
6559
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006561 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006562 old_idx = cur_idx;
6563 continue;
6564 }
6565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 del_from = NULL; /* nothing to be deleted */
6567 preserve_hdr = 0; /* assume we may kill the whole header */
6568
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569 /* Now look for cookies. Conforming to RFC2109, we have to support
6570 * attributes whose name begin with a '$', and associate them with
6571 * the right cookie, if we want to delete this cookie.
6572 * So there are 3 cases for each cookie read :
6573 * 1) it's a special attribute, beginning with a '$' : ignore it.
6574 * 2) it's a server id cookie that we *MAY* want to delete : save
6575 * some pointers on it (last semi-colon, beginning of cookie...)
6576 * 3) it's an application cookie : we *MAY* have to delete a previous
6577 * "special" cookie.
6578 * At the end of loop, if a "special" cookie remains, we may have to
6579 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 * *MUST* delete it.
6581 *
6582 * Note: RFC2965 is unclear about the processing of spaces around
6583 * the equal sign in the ATTR=VALUE form. A careful inspection of
6584 * the RFC explicitly allows spaces before it, and not within the
6585 * tokens (attrs or values). An inspection of RFC2109 allows that
6586 * too but section 10.1.3 lets one think that spaces may be allowed
6587 * after the equal sign too, resulting in some (rare) buggy
6588 * implementations trying to do that. So let's do what servers do.
6589 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6590 * allowed quoted strings in values, with any possible character
6591 * after a backslash, including control chars and delimitors, which
6592 * causes parsing to become ambiguous. Browsers also allow spaces
6593 * within values even without quotes.
6594 *
6595 * We have to keep multiple pointers in order to support cookie
6596 * removal at the beginning, middle or end of header without
6597 * corrupting the header. All of these headers are valid :
6598 *
6599 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6600 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6601 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6602 * | | | | | | | | |
6603 * | | | | | | | | hdr_end <--+
6604 * | | | | | | | +--> next
6605 * | | | | | | +----> val_end
6606 * | | | | | +-----------> val_beg
6607 * | | | | +--------------> equal
6608 * | | | +----------------> att_end
6609 * | | +---------------------> att_beg
6610 * | +--------------------------> prev
6611 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612 */
6613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006614 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6615 /* Iterate through all cookies on this line */
6616
6617 /* find att_beg */
6618 att_beg = prev + 1;
6619 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6620 att_beg++;
6621
6622 /* find att_end : this is the first character after the last non
6623 * space before the equal. It may be equal to hdr_end.
6624 */
6625 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006627 while (equal < hdr_end) {
6628 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630 if (http_is_spht[(unsigned char)*equal++])
6631 continue;
6632 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006633 }
6634
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006635 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6636 * is between <att_beg> and <equal>, both may be identical.
6637 */
6638
6639 /* look for end of cookie if there is an equal sign */
6640 if (equal < hdr_end && *equal == '=') {
6641 /* look for the beginning of the value */
6642 val_beg = equal + 1;
6643 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6644 val_beg++;
6645
6646 /* find the end of the value, respecting quotes */
6647 next = find_cookie_value_end(val_beg, hdr_end);
6648
6649 /* make val_end point to the first white space or delimitor after the value */
6650 val_end = next;
6651 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6652 val_end--;
6653 } else {
6654 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006655 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006657 /* We have nothing to do with attributes beginning with '$'. However,
6658 * they will automatically be removed if a header before them is removed,
6659 * since they're supposed to be linked together.
6660 */
6661 if (*att_beg == '$')
6662 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006663
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006664 /* Ignore cookies with no equal sign */
6665 if (equal == next) {
6666 /* This is not our cookie, so we must preserve it. But if we already
6667 * scheduled another cookie for removal, we cannot remove the
6668 * complete header, but we can remove the previous block itself.
6669 */
6670 preserve_hdr = 1;
6671 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006672 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006673 val_end += delta;
6674 next += delta;
6675 hdr_end += delta;
6676 hdr_next += delta;
6677 cur_hdr->len += delta;
6678 http_msg_move_end(&txn->req, delta);
6679 prev = del_from;
6680 del_from = NULL;
6681 }
6682 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006685 /* if there are spaces around the equal sign, we need to
6686 * strip them otherwise we'll get trouble for cookie captures,
6687 * or even for rewrites. Since this happens extremely rarely,
6688 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006690 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6691 int stripped_before = 0;
6692 int stripped_after = 0;
6693
6694 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006695 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006696 equal += stripped_before;
6697 val_beg += stripped_before;
6698 }
6699
6700 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006701 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 val_beg += stripped_after;
6703 stripped_before += stripped_after;
6704 }
6705
6706 val_end += stripped_before;
6707 next += stripped_before;
6708 hdr_end += stripped_before;
6709 hdr_next += stripped_before;
6710 cur_hdr->len += stripped_before;
6711 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006714
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 /* First, let's see if we want to capture this cookie. We check
6716 * that we don't already have a client side cookie, because we
6717 * can only capture one. Also as an optimisation, we ignore
6718 * cookies shorter than the declared name.
6719 */
6720 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6721 (val_end - att_beg >= t->fe->capture_namelen) &&
6722 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6723 int log_len = val_end - att_beg;
6724
6725 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6726 Alert("HTTP logging : out of memory.\n");
6727 } else {
6728 if (log_len > t->fe->capture_len)
6729 log_len = t->fe->capture_len;
6730 memcpy(txn->cli_cookie, att_beg, log_len);
6731 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006732 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006733 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734
Willy Tarreaubca99692010-10-06 19:25:55 +02006735 /* Persistence cookies in passive, rewrite or insert mode have the
6736 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006738 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006740 * For cookies in prefix mode, the form is :
6741 *
6742 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 */
6744 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6745 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6746 struct server *srv = t->be->srv;
6747 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006748
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6750 * have the server ID between val_beg and delim, and the original cookie between
6751 * delim+1 and val_end. Otherwise, delim==val_end :
6752 *
6753 * Cookie: NAME=SRV; # in all but prefix modes
6754 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6755 * | || || | |+-> next
6756 * | || || | +--> val_end
6757 * | || || +---------> delim
6758 * | || |+------------> val_beg
6759 * | || +-------------> att_end = equal
6760 * | |+-----------------> att_beg
6761 * | +------------------> prev
6762 * +-------------------------> hdr_beg
6763 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006764
Willy Tarreau67402132012-05-31 20:40:20 +02006765 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006766 for (delim = val_beg; delim < val_end; delim++)
6767 if (*delim == COOKIE_DELIM)
6768 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006769 } else {
6770 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006772 /* Now check if the cookie contains a date field, which would
6773 * appear after a vertical bar ('|') just after the server name
6774 * and before the delimiter.
6775 */
6776 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6777 if (vbar1) {
6778 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006779 * right is the last seen date. It is a base64 encoded
6780 * 30-bit value representing the UNIX date since the
6781 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006782 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006783 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006784 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006785 if (val_end - vbar1 >= 5) {
6786 val = b64tos30(vbar1);
6787 if (val > 0)
6788 txn->cookie_last_date = val << 2;
6789 }
6790 /* look for a second vertical bar */
6791 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6792 if (vbar1 && (val_end - vbar1 > 5)) {
6793 val = b64tos30(vbar1 + 1);
6794 if (val > 0)
6795 txn->cookie_first_date = val << 2;
6796 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006797 }
6798 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006799
Willy Tarreauf64d1412010-10-07 20:06:11 +02006800 /* if the cookie has an expiration date and the proxy wants to check
6801 * it, then we do that now. We first check if the cookie is too old,
6802 * then only if it has expired. We detect strict overflow because the
6803 * time resolution here is not great (4 seconds). Cookies with dates
6804 * in the future are ignored if their offset is beyond one day. This
6805 * allows an admin to fix timezone issues without expiring everyone
6806 * and at the same time avoids keeping unwanted side effects for too
6807 * long.
6808 */
6809 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006810 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6811 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006812 txn->flags &= ~TX_CK_MASK;
6813 txn->flags |= TX_CK_OLD;
6814 delim = val_beg; // let's pretend we have not found the cookie
6815 txn->cookie_first_date = 0;
6816 txn->cookie_last_date = 0;
6817 }
6818 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006819 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6820 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006821 txn->flags &= ~TX_CK_MASK;
6822 txn->flags |= TX_CK_EXPIRED;
6823 delim = val_beg; // let's pretend we have not found the cookie
6824 txn->cookie_first_date = 0;
6825 txn->cookie_last_date = 0;
6826 }
6827
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006828 /* Here, we'll look for the first running server which supports the cookie.
6829 * This allows to share a same cookie between several servers, for example
6830 * to dedicate backup servers to specific servers only.
6831 * However, to prevent clients from sticking to cookie-less backup server
6832 * when they have incidentely learned an empty cookie, we simply ignore
6833 * empty cookies and mark them as invalid.
6834 * The same behaviour is applied when persistence must be ignored.
6835 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006836 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006837 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006839 while (srv) {
6840 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6841 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6842 if ((srv->state & SRV_RUNNING) ||
6843 (t->be->options & PR_O_PERSIST) ||
6844 (t->flags & SN_FORCE_PRST)) {
6845 /* we found the server and we can use it */
6846 txn->flags &= ~TX_CK_MASK;
6847 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6848 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006849 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 break;
6851 } else {
6852 /* we found a server, but it's down,
6853 * mark it as such and go on in case
6854 * another one is available.
6855 */
6856 txn->flags &= ~TX_CK_MASK;
6857 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006859 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006860 srv = srv->next;
6861 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006862
Willy Tarreauf64d1412010-10-07 20:06:11 +02006863 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006864 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006866 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6867 txn->flags |= TX_CK_UNUSED;
6868 else
6869 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 /* depending on the cookie mode, we may have to either :
6873 * - delete the complete cookie if we're in insert+indirect mode, so that
6874 * the server never sees it ;
6875 * - remove the server id from the cookie value, and tag the cookie as an
6876 * application cookie so that it does not get accidentely removed later,
6877 * if we're in cookie prefix mode
6878 */
Willy Tarreau67402132012-05-31 20:40:20 +02006879 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006880 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006881
Willy Tarreau9b28e032012-10-12 23:49:43 +02006882 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 val_end += delta;
6884 next += delta;
6885 hdr_end += delta;
6886 hdr_next += delta;
6887 cur_hdr->len += delta;
6888 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 del_from = NULL;
6891 preserve_hdr = 1; /* we want to keep this cookie */
6892 }
6893 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006894 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 del_from = prev;
6896 }
6897 } else {
6898 /* This is not our cookie, so we must preserve it. But if we already
6899 * scheduled another cookie for removal, we cannot remove the
6900 * complete header, but we can remove the previous block itself.
6901 */
6902 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006903
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006905 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006906 if (att_beg >= del_from)
6907 att_beg += delta;
6908 if (att_end >= del_from)
6909 att_end += delta;
6910 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 val_end += delta;
6912 next += delta;
6913 hdr_end += delta;
6914 hdr_next += delta;
6915 cur_hdr->len += delta;
6916 http_msg_move_end(&txn->req, delta);
6917 prev = del_from;
6918 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 /* Look for the appsession cookie unless persistence must be ignored */
6923 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6924 int cmp_len, value_len;
6925 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006926
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006927 if (t->be->options2 & PR_O2_AS_PFX) {
6928 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6929 value_begin = att_beg + t->be->appsession_name_len;
6930 value_len = val_end - att_beg - t->be->appsession_name_len;
6931 } else {
6932 cmp_len = att_end - att_beg;
6933 value_begin = val_beg;
6934 value_len = val_end - val_beg;
6935 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 /* let's see if the cookie is our appcookie */
6938 if (cmp_len == t->be->appsession_name_len &&
6939 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6940 manage_client_side_appsession(t, value_begin, value_len);
6941 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006942 }
6943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 /* continue with next cookie on this header line */
6945 att_beg = next;
6946 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 /* There are no more cookies on this line.
6949 * We may still have one (or several) marked for deletion at the
6950 * end of the line. We must do this now in two ways :
6951 * - if some cookies must be preserved, we only delete from the
6952 * mark to the end of line ;
6953 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006954 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006955 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006956 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006958 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006959 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006960 cur_hdr->len += delta;
6961 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006962 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006963
6964 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006965 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6966 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006967 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006968 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006970 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006971 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972 }
6973
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977}
6978
6979
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980/* Iterate the same filter through all response headers contained in <rtr>.
6981 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6982 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006983int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984{
6985 char term;
6986 char *cur_ptr, *cur_end, *cur_next;
6987 int cur_idx, old_idx, last_hdr;
6988 struct http_txn *txn = &t->txn;
6989 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006990 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991
6992 last_hdr = 0;
6993
Willy Tarreau9b28e032012-10-12 23:49:43 +02006994 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 old_idx = 0;
6996
6997 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006998 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007000 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 (exp->action == ACT_ALLOW ||
7002 exp->action == ACT_DENY))
7003 return 0;
7004
7005 cur_idx = txn->hdr_idx.v[old_idx].next;
7006 if (!cur_idx)
7007 break;
7008
7009 cur_hdr = &txn->hdr_idx.v[cur_idx];
7010 cur_ptr = cur_next;
7011 cur_end = cur_ptr + cur_hdr->len;
7012 cur_next = cur_end + cur_hdr->cr + 1;
7013
7014 /* Now we have one header between cur_ptr and cur_end,
7015 * and the next header starts at cur_next.
7016 */
7017
7018 /* The annoying part is that pattern matching needs
7019 * that we modify the contents to null-terminate all
7020 * strings before testing them.
7021 */
7022
7023 term = *cur_end;
7024 *cur_end = '\0';
7025
7026 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7027 switch (exp->action) {
7028 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007029 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 last_hdr = 1;
7031 break;
7032
7033 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007035 last_hdr = 1;
7036 break;
7037
7038 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007039 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7040 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 /* FIXME: if the user adds a newline in the replacement, the
7042 * index will not be recalculated for now, and the new line
7043 * will not be counted as a new header.
7044 */
7045
7046 cur_end += delta;
7047 cur_next += delta;
7048 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007049 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 break;
7051
7052 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007053 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 cur_next += delta;
7055
Willy Tarreaufa355d42009-11-29 18:12:29 +01007056 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7058 txn->hdr_idx.used--;
7059 cur_hdr->len = 0;
7060 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007061 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007062 break;
7063
7064 }
7065 }
7066 if (cur_end)
7067 *cur_end = term; /* restore the string terminator */
7068
7069 /* keep the link from this header to next one in case of later
7070 * removal of next header.
7071 */
7072 old_idx = cur_idx;
7073 }
7074 return 0;
7075}
7076
7077
7078/* Apply the filter to the status line in the response buffer <rtr>.
7079 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7080 * or -1 if a replacement resulted in an invalid status line.
7081 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007082int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083{
7084 char term;
7085 char *cur_ptr, *cur_end;
7086 int done;
7087 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007088 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007089
7090
Willy Tarreau3d300592007-03-18 18:34:41 +01007091 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007093 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007094 (exp->action == ACT_ALLOW ||
7095 exp->action == ACT_DENY))
7096 return 0;
7097 else if (exp->action == ACT_REMOVE)
7098 return 0;
7099
7100 done = 0;
7101
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007103 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007104
7105 /* Now we have the status line between cur_ptr and cur_end */
7106
7107 /* The annoying part is that pattern matching needs
7108 * that we modify the contents to null-terminate all
7109 * strings before testing them.
7110 */
7111
7112 term = *cur_end;
7113 *cur_end = '\0';
7114
7115 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7116 switch (exp->action) {
7117 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007118 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 done = 1;
7120 break;
7121
7122 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007123 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124 done = 1;
7125 break;
7126
7127 case ACT_REPLACE:
7128 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007129 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7130 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 /* FIXME: if the user adds a newline in the replacement, the
7132 * index will not be recalculated for now, and the new line
7133 * will not be counted as a new header.
7134 */
7135
Willy Tarreaufa355d42009-11-29 18:12:29 +01007136 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007138 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007139 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 cur_ptr, cur_end + 1,
7141 NULL, NULL);
7142 if (unlikely(!cur_end))
7143 return -1;
7144
7145 /* we have a full respnse and we know that we have either a CR
7146 * or an LF at <ptr>.
7147 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007148 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007149 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 /* there is no point trying this regex on headers */
7151 return 1;
7152 }
7153 }
7154 *cur_end = term; /* restore the string terminator */
7155 return done;
7156}
7157
7158
7159
7160/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007161 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7163 * unparsable response.
7164 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007165int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007167 struct http_txn *txn = &s->txn;
7168 struct hdr_exp *exp;
7169
7170 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171 int ret;
7172
7173 /*
7174 * The interleaving of transformations and verdicts
7175 * makes it difficult to decide to continue or stop
7176 * the evaluation.
7177 */
7178
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007179 if (txn->flags & TX_SVDENY)
7180 break;
7181
Willy Tarreau3d300592007-03-18 18:34:41 +01007182 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7184 exp->action == ACT_PASS)) {
7185 exp = exp->next;
7186 continue;
7187 }
7188
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007189 /* if this filter had a condition, evaluate it now and skip to
7190 * next filter if the condition does not match.
7191 */
7192 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007193 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007194 ret = acl_pass(ret);
7195 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7196 ret = !ret;
7197 if (!ret)
7198 continue;
7199 }
7200
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007202 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 if (unlikely(ret < 0))
7204 return -1;
7205
7206 if (likely(ret == 0)) {
7207 /* The filter did not match the response, it can be
7208 * iterated through all headers.
7209 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007210 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 }
7213 return 0;
7214}
7215
7216
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007218 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007219 * desirable to call it only when needed. This function is also used when we
7220 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007222void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223{
7224 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007225 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007226 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007228 char *hdr_beg, *hdr_end, *hdr_next;
7229 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 /* Iterate through the headers.
7232 * we start with the start line.
7233 */
7234 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007235 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236
7237 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7238 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007239 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240
7241 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007242 hdr_beg = hdr_next;
7243 hdr_end = hdr_beg + cur_hdr->len;
7244 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245
Willy Tarreau24581ba2010-08-31 22:39:35 +02007246 /* We have one full header between hdr_beg and hdr_end, and the
7247 * next header starts at hdr_next. We're only interested in
7248 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249 */
7250
Willy Tarreau24581ba2010-08-31 22:39:35 +02007251 is_cookie2 = 0;
7252 prev = hdr_beg + 10;
7253 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007254 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007255 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7256 if (!val) {
7257 old_idx = cur_idx;
7258 continue;
7259 }
7260 is_cookie2 = 1;
7261 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 }
7263
Willy Tarreau24581ba2010-08-31 22:39:35 +02007264 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7265 * <prev> points to the colon.
7266 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007267 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268
Willy Tarreau24581ba2010-08-31 22:39:35 +02007269 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7270 * check-cache is enabled) and we are not interested in checking
7271 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007272 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007273 if (t->be->cookie_name == NULL &&
7274 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007275 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 return;
7277
Willy Tarreau24581ba2010-08-31 22:39:35 +02007278 /* OK so now we know we have to process this response cookie.
7279 * The format of the Set-Cookie header is slightly different
7280 * from the format of the Cookie header in that it does not
7281 * support the comma as a cookie delimiter (thus the header
7282 * cannot be folded) because the Expires attribute described in
7283 * the original Netscape's spec may contain an unquoted date
7284 * with a comma inside. We have to live with this because
7285 * many browsers don't support Max-Age and some browsers don't
7286 * support quoted strings. However the Set-Cookie2 header is
7287 * clean.
7288 *
7289 * We have to keep multiple pointers in order to support cookie
7290 * removal at the beginning, middle or end of header without
7291 * corrupting the header (in case of set-cookie2). A special
7292 * pointer, <scav> points to the beginning of the set-cookie-av
7293 * fields after the first semi-colon. The <next> pointer points
7294 * either to the end of line (set-cookie) or next unquoted comma
7295 * (set-cookie2). All of these headers are valid :
7296 *
7297 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7298 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7299 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7300 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7301 * | | | | | | | | | |
7302 * | | | | | | | | +-> next hdr_end <--+
7303 * | | | | | | | +------------> scav
7304 * | | | | | | +--------------> val_end
7305 * | | | | | +--------------------> val_beg
7306 * | | | | +----------------------> equal
7307 * | | | +------------------------> att_end
7308 * | | +----------------------------> att_beg
7309 * | +------------------------------> prev
7310 * +-----------------------------------------> hdr_beg
7311 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
Willy Tarreau24581ba2010-08-31 22:39:35 +02007313 for (; prev < hdr_end; prev = next) {
7314 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315
Willy Tarreau24581ba2010-08-31 22:39:35 +02007316 /* find att_beg */
7317 att_beg = prev + 1;
7318 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7319 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320
Willy Tarreau24581ba2010-08-31 22:39:35 +02007321 /* find att_end : this is the first character after the last non
7322 * space before the equal. It may be equal to hdr_end.
7323 */
7324 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325
Willy Tarreau24581ba2010-08-31 22:39:35 +02007326 while (equal < hdr_end) {
7327 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7328 break;
7329 if (http_is_spht[(unsigned char)*equal++])
7330 continue;
7331 att_end = equal;
7332 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333
Willy Tarreau24581ba2010-08-31 22:39:35 +02007334 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7335 * is between <att_beg> and <equal>, both may be identical.
7336 */
7337
7338 /* look for end of cookie if there is an equal sign */
7339 if (equal < hdr_end && *equal == '=') {
7340 /* look for the beginning of the value */
7341 val_beg = equal + 1;
7342 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7343 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
Willy Tarreau24581ba2010-08-31 22:39:35 +02007345 /* find the end of the value, respecting quotes */
7346 next = find_cookie_value_end(val_beg, hdr_end);
7347
7348 /* make val_end point to the first white space or delimitor after the value */
7349 val_end = next;
7350 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7351 val_end--;
7352 } else {
7353 /* <equal> points to next comma, semi-colon or EOL */
7354 val_beg = val_end = next = equal;
7355 }
7356
7357 if (next < hdr_end) {
7358 /* Set-Cookie2 supports multiple cookies, and <next> points to
7359 * a colon or semi-colon before the end. So skip all attr-value
7360 * pairs and look for the next comma. For Set-Cookie, since
7361 * commas are permitted in values, skip to the end.
7362 */
7363 if (is_cookie2)
7364 next = find_hdr_value_end(next, hdr_end);
7365 else
7366 next = hdr_end;
7367 }
7368
7369 /* Now everything is as on the diagram above */
7370
7371 /* Ignore cookies with no equal sign */
7372 if (equal == val_end)
7373 continue;
7374
7375 /* If there are spaces around the equal sign, we need to
7376 * strip them otherwise we'll get trouble for cookie captures,
7377 * or even for rewrites. Since this happens extremely rarely,
7378 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007380 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7381 int stripped_before = 0;
7382 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007383
Willy Tarreau24581ba2010-08-31 22:39:35 +02007384 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007385 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 equal += stripped_before;
7387 val_beg += stripped_before;
7388 }
7389
7390 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007391 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007392 val_beg += stripped_after;
7393 stripped_before += stripped_after;
7394 }
7395
7396 val_end += stripped_before;
7397 next += stripped_before;
7398 hdr_end += stripped_before;
7399 hdr_next += stripped_before;
7400 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007401 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 }
7403
7404 /* First, let's see if we want to capture this cookie. We check
7405 * that we don't already have a server side cookie, because we
7406 * can only capture one. Also as an optimisation, we ignore
7407 * cookies shorter than the declared name.
7408 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007409 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007410 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 (val_end - att_beg >= t->fe->capture_namelen) &&
7412 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7413 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007414 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415 Alert("HTTP logging : out of memory.\n");
7416 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007417 else {
7418 if (log_len > t->fe->capture_len)
7419 log_len = t->fe->capture_len;
7420 memcpy(txn->srv_cookie, att_beg, log_len);
7421 txn->srv_cookie[log_len] = 0;
7422 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423 }
7424
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007425 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 if (!(t->flags & SN_IGNORE_PRST) &&
7428 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7429 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007430 /* assume passive cookie by default */
7431 txn->flags &= ~TX_SCK_MASK;
7432 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007433
7434 /* If the cookie is in insert mode on a known server, we'll delete
7435 * this occurrence because we'll insert another one later.
7436 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007437 * a direct access.
7438 */
Willy Tarreau67402132012-05-31 20:40:20 +02007439 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007440 /* The "preserve" flag was set, we don't want to touch the
7441 * server's cookie.
7442 */
7443 }
Willy Tarreau67402132012-05-31 20:40:20 +02007444 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7445 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007446 /* this cookie must be deleted */
7447 if (*prev == ':' && next == hdr_end) {
7448 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007449 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7451 txn->hdr_idx.used--;
7452 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007453 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 hdr_next += delta;
7455 http_msg_move_end(&txn->rsp, delta);
7456 /* note: while both invalid now, <next> and <hdr_end>
7457 * are still equal, so the for() will stop as expected.
7458 */
7459 } else {
7460 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007461 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 next = prev;
7463 hdr_end += delta;
7464 hdr_next += delta;
7465 cur_hdr->len += delta;
7466 http_msg_move_end(&txn->rsp, delta);
7467 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007468 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007469 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007471 }
Willy Tarreau67402132012-05-31 20:40:20 +02007472 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007473 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007474 * with this server since we know it.
7475 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007476 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007477 next += delta;
7478 hdr_end += delta;
7479 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007481 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482
Willy Tarreauf1348312010-10-07 15:54:11 +02007483 txn->flags &= ~TX_SCK_MASK;
7484 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007486 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007490 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 next += delta;
7492 hdr_end += delta;
7493 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007495 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496
Willy Tarreau827aee92011-03-10 16:55:02 +01007497 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007498 txn->flags &= ~TX_SCK_MASK;
7499 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500 }
7501 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007502 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7503 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007504 int cmp_len, value_len;
7505 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506
Cyril Bontéb21570a2009-11-29 20:04:48 +01007507 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007508 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7509 value_begin = att_beg + t->be->appsession_name_len;
7510 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007511 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007512 cmp_len = att_end - att_beg;
7513 value_begin = val_beg;
7514 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007515 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007516
Cyril Bonté17530c32010-04-06 21:11:10 +02007517 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7519 /* free a possibly previously allocated memory */
7520 pool_free2(apools.sessid, txn->sessid);
7521
Cyril Bontéb21570a2009-11-29 20:04:48 +01007522 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007523 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007524 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7525 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7526 return;
7527 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007528 memcpy(txn->sessid, value_begin, value_len);
7529 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 }
7532 /* that's done for this cookie, check the next one on the same
7533 * line when next != hdr_end (only if is_cookie2).
7534 */
7535 }
7536 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007537 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007538 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007539
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007540 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007541 appsess *asession = NULL;
7542 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007543 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007544 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007545 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007546 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7547 Alert("Not enough Memory process_srv():asession:calloc().\n");
7548 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7549 return;
7550 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007551 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7552
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007553 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7554 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7555 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007556 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007557 return;
7558 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007559 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007560 asession->sessid[t->be->appsession_len] = 0;
7561
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007562 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007563 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007564 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007565 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007566 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007567 return;
7568 }
7569 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007570 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007571
7572 asession->request_count = 0;
7573 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7574 }
7575
7576 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7577 asession->request_count++;
7578 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579}
7580
7581
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582/*
7583 * Check if response is cacheable or not. Updates t->flags.
7584 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007585void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586{
7587 struct http_txn *txn = &t->txn;
7588 char *p1, *p2;
7589
7590 char *cur_ptr, *cur_end, *cur_next;
7591 int cur_idx;
7592
Willy Tarreau5df51872007-11-25 16:20:08 +01007593 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594 return;
7595
7596 /* Iterate through the headers.
7597 * we start with the start line.
7598 */
7599 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007600 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601
7602 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7603 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007604 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605
7606 cur_hdr = &txn->hdr_idx.v[cur_idx];
7607 cur_ptr = cur_next;
7608 cur_end = cur_ptr + cur_hdr->len;
7609 cur_next = cur_end + cur_hdr->cr + 1;
7610
7611 /* We have one full header between cur_ptr and cur_end, and the
7612 * next header starts at cur_next. We're only interested in
7613 * "Cookie:" headers.
7614 */
7615
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007616 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7617 if (val) {
7618 if ((cur_end - (cur_ptr + val) >= 8) &&
7619 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7620 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7621 return;
7622 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 }
7624
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007625 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7626 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627 continue;
7628
7629 /* OK, right now we know we have a cache-control header at cur_ptr */
7630
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007631 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632
7633 if (p1 >= cur_end) /* no more info */
7634 continue;
7635
7636 /* p1 is at the beginning of the value */
7637 p2 = p1;
7638
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007639 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 p2++;
7641
7642 /* we have a complete value between p1 and p2 */
7643 if (p2 < cur_end && *p2 == '=') {
7644 /* we have something of the form no-cache="set-cookie" */
7645 if ((cur_end - p1 >= 21) &&
7646 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7647 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007648 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 continue;
7650 }
7651
7652 /* OK, so we know that either p2 points to the end of string or to a comma */
7653 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7654 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7655 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7656 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007657 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658 return;
7659 }
7660
7661 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007662 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 continue;
7664 }
7665 }
7666}
7667
7668
Willy Tarreau58f10d72006-12-04 02:26:12 +01007669/*
7670 * Try to retrieve a known appsession in the URI, then the associated server.
7671 * If the server is found, it's assigned to the session.
7672 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007673void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007675 char *end_params, *first_param, *cur_param, *next_param;
7676 char separator;
7677 int value_len;
7678
7679 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007681 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007682 (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 +01007683 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007684 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Cyril Bontéb21570a2009-11-29 20:04:48 +01007686 first_param = NULL;
7687 switch (mode) {
7688 case PR_O2_AS_M_PP:
7689 first_param = memchr(begin, ';', len);
7690 break;
7691 case PR_O2_AS_M_QS:
7692 first_param = memchr(begin, '?', len);
7693 break;
7694 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695
Cyril Bontéb21570a2009-11-29 20:04:48 +01007696 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007698 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007699
Cyril Bontéb21570a2009-11-29 20:04:48 +01007700 switch (mode) {
7701 case PR_O2_AS_M_PP:
7702 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7703 end_params = (char *) begin + len;
7704 }
7705 separator = ';';
7706 break;
7707 case PR_O2_AS_M_QS:
7708 end_params = (char *) begin + len;
7709 separator = '&';
7710 break;
7711 default:
7712 /* unknown mode, shouldn't happen */
7713 return;
7714 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715
Cyril Bontéb21570a2009-11-29 20:04:48 +01007716 cur_param = next_param = end_params;
7717 while (cur_param > first_param) {
7718 cur_param--;
7719 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7720 /* let's see if this is the appsession parameter */
7721 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7722 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7723 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7724 /* Cool... it's the right one */
7725 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7726 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7727 if (value_len > 0) {
7728 manage_client_side_appsession(t, cur_param, value_len);
7729 }
7730 break;
7731 }
7732 next_param = cur_param;
7733 }
7734 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007736 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007737 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007739}
7740
Willy Tarreaub2513902006-12-17 14:52:38 +01007741/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007742 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007743 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007744 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007745 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007746 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007747 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007748 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007749 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007750int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007751{
7752 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007753 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007754 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007755 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007756
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007757 if (!uri_auth)
7758 return 0;
7759
Cyril Bonté70be45d2010-10-12 00:14:35 +02007760 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007761 return 0;
7762
Willy Tarreau295a8372011-03-10 11:25:07 +01007763 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007764 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007765 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007767 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007768 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007769 return 0;
7770
Willy Tarreau3a215be2012-03-09 21:39:51 +01007771 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007772 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007773 return 0;
7774
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007775 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007776 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007777 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007778 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007779 break;
7780 }
7781 h++;
7782 }
7783
7784 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007785 h = uri + uri_auth->uri_len;
7786 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007787 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007788 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007789 break;
7790 }
7791 h++;
7792 }
7793 }
7794
Willy Tarreau3a215be2012-03-09 21:39:51 +01007795 h = uri + uri_auth->uri_len;
7796 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007797 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007798 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007799 break;
7800 }
7801 h++;
7802 }
7803
Willy Tarreau3a215be2012-03-09 21:39:51 +01007804 h = uri + uri_auth->uri_len;
7805 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007806 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007807 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007808 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007809 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007810 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7811 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7812 si->applet.ctx.stats.st_code = i;
7813 break;
7814 }
7815 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007816 break;
7817 }
7818 h++;
7819 }
Willy Tarreaub2513902006-12-17 14:52:38 +01007820 return 1;
7821}
7822
Willy Tarreau4076a152009-04-02 15:18:36 +02007823/*
7824 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007825 * By default it tries to report the error position as msg->err_pos. However if
7826 * this one is not set, it will then report msg->next, which is the last known
7827 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007828 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007829 */
7830void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007831 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007832 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007833{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007834 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007835 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007836
Willy Tarreau9b28e032012-10-12 23:49:43 +02007837 es->len = MIN(chn->buf->i, sizeof(es->buf));
7838 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007839 len1 = MIN(len1, es->len);
7840 len2 = es->len - len1; /* remaining data if buffer wraps */
7841
Willy Tarreau9b28e032012-10-12 23:49:43 +02007842 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007843 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007844 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007845
Willy Tarreau4076a152009-04-02 15:18:36 +02007846 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007847 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007848 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007849 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007850
Willy Tarreau4076a152009-04-02 15:18:36 +02007851 es->when = date; // user-visible date
7852 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007853 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007854 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007855 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007856 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007857 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007858 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007859 es->s_flags = s->flags;
7860 es->t_flags = s->txn.flags;
7861 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007862 es->b_out = chn->buf->o;
7863 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007864 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007865 es->m_clen = msg->chunk_len;
7866 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007867}
Willy Tarreaub2513902006-12-17 14:52:38 +01007868
Willy Tarreau294c4732011-12-16 21:35:50 +01007869/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7870 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7871 * performed over the whole headers. Otherwise it must contain a valid header
7872 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7873 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7874 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7875 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7876 * -1.
7877 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007878 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007879unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007880 struct hdr_idx *idx, int occ,
7881 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007882{
Willy Tarreau294c4732011-12-16 21:35:50 +01007883 struct hdr_ctx local_ctx;
7884 char *ptr_hist[MAX_HDR_HISTORY];
7885 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007886 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007887 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007888
Willy Tarreau294c4732011-12-16 21:35:50 +01007889 if (!ctx) {
7890 local_ctx.idx = 0;
7891 ctx = &local_ctx;
7892 }
7893
Willy Tarreaubce70882009-09-07 11:51:47 +02007894 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007895 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007896 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007897 occ--;
7898 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007899 *vptr = ctx->line + ctx->val;
7900 *vlen = ctx->vlen;
7901 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007902 }
7903 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007904 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007905 }
7906
7907 /* negative occurrence, we scan all the list then walk back */
7908 if (-occ > MAX_HDR_HISTORY)
7909 return 0;
7910
Willy Tarreau294c4732011-12-16 21:35:50 +01007911 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007912 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007913 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7914 len_hist[hist_ptr] = ctx->vlen;
7915 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007916 hist_ptr = 0;
7917 found++;
7918 }
7919 if (-occ > found)
7920 return 0;
7921 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7922 * find occurrence -occ, so we have to check [hist_ptr+occ].
7923 */
7924 hist_ptr += occ;
7925 if (hist_ptr >= MAX_HDR_HISTORY)
7926 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007927 *vptr = ptr_hist[hist_ptr];
7928 *vlen = len_hist[hist_ptr];
7929 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007930}
7931
Willy Tarreaubaaee002006-06-26 02:48:02 +02007932/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007933 * Print a debug line with a header. Always stop at the first CR or LF char,
7934 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7935 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936 */
7937void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7938{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007939 int max;
7940 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007941 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7942 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007943
7944 for (max = 0; start + max < end; max++)
7945 if (start[max] == '\r' || start[max] == '\n')
7946 break;
7947
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007948 UBOUND(max, trash.size - trash.len - 3);
7949 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7950 trash.str[trash.len++] = '\n';
7951 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952}
7953
Willy Tarreau0937bc42009-12-22 15:03:09 +01007954/*
7955 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7956 * the required fields are properly allocated and that we only need to (re)init
7957 * them. This should be used before processing any new request.
7958 */
7959void http_init_txn(struct session *s)
7960{
7961 struct http_txn *txn = &s->txn;
7962 struct proxy *fe = s->fe;
7963
7964 txn->flags = 0;
7965 txn->status = -1;
7966
William Lallemand5f232402012-04-05 18:02:55 +02007967 global.req_count++;
7968
Willy Tarreauf64d1412010-10-07 20:06:11 +02007969 txn->cookie_first_date = 0;
7970 txn->cookie_last_date = 0;
7971
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007972 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007973 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007974 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007975 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007976 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007977 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007978 txn->req.chunk_len = 0LL;
7979 txn->req.body_len = 0LL;
7980 txn->rsp.chunk_len = 0LL;
7981 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007982 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7983 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007984 txn->req.chn = s->req;
7985 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007986
7987 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007988
7989 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7990 if (fe->options2 & PR_O2_REQBUG_OK)
7991 txn->req.err_pos = -1; /* let buggy requests pass */
7992
Willy Tarreau46023632010-01-07 22:51:47 +01007993 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007994 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7995
Willy Tarreau46023632010-01-07 22:51:47 +01007996 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007997 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7998
7999 if (txn->hdr_idx.v)
8000 hdr_idx_init(&txn->hdr_idx);
8001}
8002
8003/* to be used at the end of a transaction */
8004void http_end_txn(struct session *s)
8005{
8006 struct http_txn *txn = &s->txn;
8007
8008 /* these ones will have been dynamically allocated */
8009 pool_free2(pool2_requri, txn->uri);
8010 pool_free2(pool2_capture, txn->cli_cookie);
8011 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008012 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008013 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008014
William Lallemanda73203e2012-03-12 12:48:57 +01008015 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008016 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008017 txn->uri = NULL;
8018 txn->srv_cookie = NULL;
8019 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008020
8021 if (txn->req.cap) {
8022 struct cap_hdr *h;
8023 for (h = s->fe->req_cap; h; h = h->next)
8024 pool_free2(h->pool, txn->req.cap[h->index]);
8025 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8026 }
8027
8028 if (txn->rsp.cap) {
8029 struct cap_hdr *h;
8030 for (h = s->fe->rsp_cap; h; h = h->next)
8031 pool_free2(h->pool, txn->rsp.cap[h->index]);
8032 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8033 }
8034
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035}
8036
8037/* to be used at the end of a transaction to prepare a new one */
8038void http_reset_txn(struct session *s)
8039{
8040 http_end_txn(s);
8041 http_init_txn(s);
8042
8043 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008044 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008045 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008046 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008047 /* re-init store persistence */
8048 s->store_count = 0;
8049
Willy Tarreau0937bc42009-12-22 15:03:09 +01008050 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008051
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008052 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008053
Willy Tarreau739cfba2010-01-25 23:11:14 +01008054 /* We must trim any excess data from the response buffer, because we
8055 * may have blocked an invalid response from a server that we don't
8056 * want to accidentely forward once we disable the analysers, nor do
8057 * we want those data to come along with next response. A typical
8058 * example of such data would be from a buggy server responding to
8059 * a HEAD with some data, or sending more than the advertised
8060 * content-length.
8061 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008062 if (unlikely(s->rep->buf->i))
8063 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008064
Willy Tarreau0937bc42009-12-22 15:03:09 +01008065 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008066 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008067
Willy Tarreaud04e8582010-05-31 12:31:35 +02008068 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008069 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008070
8071 s->req->rex = TICK_ETERNITY;
8072 s->req->wex = TICK_ETERNITY;
8073 s->req->analyse_exp = TICK_ETERNITY;
8074 s->rep->rex = TICK_ETERNITY;
8075 s->rep->wex = TICK_ETERNITY;
8076 s->rep->analyse_exp = TICK_ETERNITY;
8077}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008078
Willy Tarreauff011f22011-01-06 17:51:27 +01008079void free_http_req_rules(struct list *r) {
8080 struct http_req_rule *tr, *pr;
8081
8082 list_for_each_entry_safe(pr, tr, r, list) {
8083 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008084 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008085 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008086
8087 free(pr);
8088 }
8089}
8090
8091struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8092{
8093 struct http_req_rule *rule;
8094 int cur_arg;
8095
8096 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8097 if (!rule) {
8098 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008099 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008100 }
8101
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008102 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008103 rule->action = HTTP_REQ_ACT_ALLOW;
8104 cur_arg = 1;
8105 } else if (!strcmp(args[0], "deny")) {
8106 rule->action = HTTP_REQ_ACT_DENY;
8107 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008108 } else if (!strcmp(args[0], "tarpit")) {
8109 rule->action = HTTP_REQ_ACT_TARPIT;
8110 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008111 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008112 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008113 cur_arg = 1;
8114
8115 while(*args[cur_arg]) {
8116 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008117 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008118 cur_arg+=2;
8119 continue;
8120 } else
8121 break;
8122 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008123 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8124 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8125 cur_arg = 1;
8126
8127 if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) {
8128 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8129 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008130 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008131 }
8132
8133 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8134 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8135 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreau3bfeadb2013-03-24 07:33:22 +01008136 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008137 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008138 } else if (strcmp(args[0], "redirect") == 0) {
8139 struct redirect_rule *redir;
8140 char *errmsg;
8141
8142 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8143 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8144 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8145 goto out_err;
8146 }
8147
8148 /* this redirect rule might already contain a parsed condition which
8149 * we'll pass to the http-request rule.
8150 */
8151 rule->action = HTTP_REQ_ACT_REDIR;
8152 rule->arg.redir = redir;
8153 rule->cond = redir->cond;
8154 redir->cond = NULL;
8155 cur_arg = 2;
8156 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008157 } else {
Baptiste Assmann116eefe2013-01-05 16:02:07 +01008158 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 +01008159 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008160 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008161 }
8162
8163 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8164 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008165 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008166
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008167 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8168 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8169 file, linenum, args[0], errmsg);
8170 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008171 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008172 }
8173 rule->cond = cond;
8174 }
8175 else if (*args[cur_arg]) {
8176 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8177 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8178 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008179 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008180 }
8181
8182 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008183 out_err:
8184 free(rule);
8185 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008186}
8187
Willy Tarreau4baae242012-12-27 12:00:31 +01008188/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8189 * with <err> filled with the error message.
8190 */
8191struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8192 const char **args, char **errmsg)
8193{
8194 struct redirect_rule *rule;
8195 int cur_arg;
8196 int type = REDIRECT_TYPE_NONE;
8197 int code = 302;
8198 const char *destination = NULL;
8199 const char *cookie = NULL;
8200 int cookie_set = 0;
8201 unsigned int flags = REDIRECT_FLAG_NONE;
8202 struct acl_cond *cond = NULL;
8203
8204 cur_arg = 0;
8205 while (*(args[cur_arg])) {
8206 if (strcmp(args[cur_arg], "location") == 0) {
8207 if (!*args[cur_arg + 1])
8208 goto missing_arg;
8209
8210 type = REDIRECT_TYPE_LOCATION;
8211 cur_arg++;
8212 destination = args[cur_arg];
8213 }
8214 else if (strcmp(args[cur_arg], "prefix") == 0) {
8215 if (!*args[cur_arg + 1])
8216 goto missing_arg;
8217
8218 type = REDIRECT_TYPE_PREFIX;
8219 cur_arg++;
8220 destination = args[cur_arg];
8221 }
8222 else if (strcmp(args[cur_arg], "scheme") == 0) {
8223 if (!*args[cur_arg + 1])
8224 goto missing_arg;
8225
8226 type = REDIRECT_TYPE_SCHEME;
8227 cur_arg++;
8228 destination = args[cur_arg];
8229 }
8230 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8231 if (!*args[cur_arg + 1])
8232 goto missing_arg;
8233
8234 cur_arg++;
8235 cookie = args[cur_arg];
8236 cookie_set = 1;
8237 }
8238 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8239 if (!*args[cur_arg + 1])
8240 goto missing_arg;
8241
8242 cur_arg++;
8243 cookie = args[cur_arg];
8244 cookie_set = 0;
8245 }
8246 else if (strcmp(args[cur_arg], "code") == 0) {
8247 if (!*args[cur_arg + 1])
8248 goto missing_arg;
8249
8250 cur_arg++;
8251 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008252 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008253 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008254 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008255 args[cur_arg - 1], args[cur_arg]);
8256 return NULL;
8257 }
8258 }
8259 else if (!strcmp(args[cur_arg],"drop-query")) {
8260 flags |= REDIRECT_FLAG_DROP_QS;
8261 }
8262 else if (!strcmp(args[cur_arg],"append-slash")) {
8263 flags |= REDIRECT_FLAG_APPEND_SLASH;
8264 }
8265 else if (strcmp(args[cur_arg], "if") == 0 ||
8266 strcmp(args[cur_arg], "unless") == 0) {
8267 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8268 if (!cond) {
8269 memprintf(errmsg, "error in condition: %s", *errmsg);
8270 return NULL;
8271 }
8272 break;
8273 }
8274 else {
8275 memprintf(errmsg,
8276 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8277 args[cur_arg]);
8278 return NULL;
8279 }
8280 cur_arg++;
8281 }
8282
8283 if (type == REDIRECT_TYPE_NONE) {
8284 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8285 return NULL;
8286 }
8287
8288 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8289 rule->cond = cond;
8290 rule->rdr_str = strdup(destination);
8291 rule->rdr_len = strlen(destination);
8292 if (cookie) {
8293 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8294 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8295 */
8296 rule->cookie_len = strlen(cookie);
8297 if (cookie_set) {
8298 rule->cookie_str = malloc(rule->cookie_len + 10);
8299 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8300 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8301 rule->cookie_len += 9;
8302 } else {
8303 rule->cookie_str = malloc(rule->cookie_len + 21);
8304 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8305 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8306 rule->cookie_len += 20;
8307 }
8308 }
8309 rule->type = type;
8310 rule->code = code;
8311 rule->flags = flags;
8312 LIST_INIT(&rule->list);
8313 return rule;
8314
8315 missing_arg:
8316 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8317 return NULL;
8318}
8319
Willy Tarreau8797c062007-05-07 00:55:35 +02008320/************************************************************************/
8321/* The code below is dedicated to ACL parsing and matching */
8322/************************************************************************/
8323
8324
Willy Tarreau14174bc2012-04-16 14:34:04 +02008325/* This function ensures that the prerequisites for an L7 fetch are ready,
8326 * which means that a request or response is ready. If some data is missing,
8327 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008328 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8329 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008330 *
8331 * The function returns :
8332 * 0 if some data is missing or if the requested data cannot be fetched
8333 * -1 if it is certain that we'll never have any HTTP message there
8334 * 1 if an HTTP message is ready
8335 */
8336static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008337acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008338 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008339{
8340 struct http_txn *txn = l7;
8341 struct http_msg *msg = &txn->req;
8342
8343 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8344 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8345 */
8346
8347 if (unlikely(!s || !txn))
8348 return 0;
8349
8350 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008351 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008352
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008353 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008354 if (unlikely(!s->req))
8355 return 0;
8356
8357 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008358 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008359 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008360 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008361 return -1;
8362 }
8363
8364 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008365 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008366 http_msg_analyzer(msg, &txn->hdr_idx);
8367
8368 /* Still no valid request ? */
8369 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008370 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008371 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008372 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008373 return -1;
8374 }
8375 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008376 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008377 return 0;
8378 }
8379
8380 /* OK we just got a valid HTTP request. We have some minor
8381 * preparation to perform so that further checks can rely
8382 * on HTTP tests.
8383 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008384 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008385 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8386 s->flags |= SN_REDIRECTABLE;
8387
8388 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008389 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008390 return -1;
8391 }
8392 }
8393
Willy Tarreau24e32d82012-04-23 23:55:44 +02008394 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008395 return 0; /* data might have moved and indexes changed */
8396
8397 /* otherwise everything's ready for the request */
8398 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008399 else {
8400 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008401 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8402 return 0;
8403 }
8404
8405 /* everything's OK */
8406 return 1;
8407}
Willy Tarreau8797c062007-05-07 00:55:35 +02008408
Willy Tarreauc0239e02012-04-16 14:42:55 +02008409#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008410 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 +02008411
Willy Tarreau24e32d82012-04-23 23:55:44 +02008412#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008413 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 +02008414
Willy Tarreau8797c062007-05-07 00:55:35 +02008415
8416/* 1. Check on METHOD
8417 * We use the pre-parsed method if it is known, and store its number as an
8418 * integer. If it is unknown, we use the pointer and the length.
8419 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008420static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008421{
8422 int len, meth;
8423
Willy Tarreauae8b7962007-06-09 23:10:04 +02008424 len = strlen(*text);
8425 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008426
8427 pattern->val.i = meth;
8428 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008429 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008430 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008431 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008432 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008433 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008434 pattern->len = len;
8435 }
8436 return 1;
8437}
8438
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008439/* This function fetches the method of current HTTP request and stores
8440 * it in the global pattern struct as a chunk. There are two possibilities :
8441 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8442 * in <len> and <ptr> is NULL ;
8443 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8444 * <len> to its length.
8445 * This is intended to be used with acl_match_meth() only.
8446 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008447static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008448acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008449 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008450{
8451 int meth;
8452 struct http_txn *txn = l7;
8453
Willy Tarreau24e32d82012-04-23 23:55:44 +02008454 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008455
Willy Tarreau8797c062007-05-07 00:55:35 +02008456 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008457 smp->type = SMP_T_UINT;
8458 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008459 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008460 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8461 /* ensure the indexes are not affected */
8462 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008463 smp->type = SMP_T_CSTR;
8464 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008465 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008466 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008467 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008468 return 1;
8469}
8470
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008471/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008472static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008473{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008474 int icase;
8475
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008476
Willy Tarreauf853c462012-04-23 18:53:56 +02008477 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008478 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008479 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008480 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008481 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008482 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008483
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008484 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8485 if (pattern->val.i != HTTP_METH_OTHER)
8486 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008487
8488 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008489 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008490 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008491
8492 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008493 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8494 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008495 return ACL_PAT_FAIL;
8496 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008497}
8498
8499/* 2. Check on Request/Status Version
8500 * We simply compare strings here.
8501 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008502static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008503{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008504 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008505 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008506 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008507 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008508 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008509 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008510 return 1;
8511}
8512
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008513static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008514acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008515 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008516{
8517 struct http_txn *txn = l7;
8518 char *ptr;
8519 int len;
8520
Willy Tarreauc0239e02012-04-16 14:42:55 +02008521 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008522
Willy Tarreau8797c062007-05-07 00:55:35 +02008523 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008524 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008525
8526 while ((len-- > 0) && (*ptr++ != '/'));
8527 if (len <= 0)
8528 return 0;
8529
Willy Tarreauf853c462012-04-23 18:53:56 +02008530 smp->type = SMP_T_CSTR;
8531 smp->data.str.str = ptr;
8532 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008533
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008534 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008535 return 1;
8536}
8537
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008538static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008539acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008540 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008541{
8542 struct http_txn *txn = l7;
8543 char *ptr;
8544 int len;
8545
Willy Tarreauc0239e02012-04-16 14:42:55 +02008546 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008547
Willy Tarreauf26b2522012-12-14 08:33:14 +01008548 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8549 return 0;
8550
Willy Tarreau8797c062007-05-07 00:55:35 +02008551 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008552 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008553
8554 while ((len-- > 0) && (*ptr++ != '/'));
8555 if (len <= 0)
8556 return 0;
8557
Willy Tarreauf853c462012-04-23 18:53:56 +02008558 smp->type = SMP_T_CSTR;
8559 smp->data.str.str = ptr;
8560 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008561
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008562 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008563 return 1;
8564}
8565
8566/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008567static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008568acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008569 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008570{
8571 struct http_txn *txn = l7;
8572 char *ptr;
8573 int len;
8574
Willy Tarreauc0239e02012-04-16 14:42:55 +02008575 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008576
Willy Tarreauf26b2522012-12-14 08:33:14 +01008577 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8578 return 0;
8579
Willy Tarreau8797c062007-05-07 00:55:35 +02008580 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008581 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008582
Willy Tarreauf853c462012-04-23 18:53:56 +02008583 smp->type = SMP_T_UINT;
8584 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008585 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008586 return 1;
8587}
8588
8589/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008590static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008591smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008592 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008593{
8594 struct http_txn *txn = l7;
8595
Willy Tarreauc0239e02012-04-16 14:42:55 +02008596 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008597
Willy Tarreauf853c462012-04-23 18:53:56 +02008598 smp->type = SMP_T_CSTR;
8599 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008600 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008601 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008602 return 1;
8603}
8604
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008605static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008606smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008607 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008608{
8609 struct http_txn *txn = l7;
8610
Willy Tarreauc0239e02012-04-16 14:42:55 +02008611 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008612
8613 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008614 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8615 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008616 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008617 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008618 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008619
8620 /*
8621 * If we are parsing url in frontend space, we prepare backend stage
8622 * to not parse again the same url ! optimization lazyness...
8623 */
8624 if (px->options & PR_O_HTTP_PROXY)
8625 l4->flags |= SN_ADDR_SET;
8626
Willy Tarreau37406352012-04-23 16:16:37 +02008627 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008628 return 1;
8629}
8630
8631static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008632smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008633 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008634{
8635 struct http_txn *txn = l7;
8636
Willy Tarreauc0239e02012-04-16 14:42:55 +02008637 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008638
8639 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008640 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 +02008641 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008642 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008643
8644 if (px->options & PR_O_HTTP_PROXY)
8645 l4->flags |= SN_ADDR_SET;
8646
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008647 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008648 return 1;
8649}
8650
Willy Tarreau185b5c42012-04-26 15:11:51 +02008651/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8652 * Accepts an optional argument of type string containing the header field name,
8653 * and an optional argument of type signed or unsigned integer to request an
8654 * explicit occurrence of the header. Note that in the event of a missing name,
8655 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008656 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008657static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008658smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008659 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008660{
8661 struct http_txn *txn = l7;
8662 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008663 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008664 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008665 int occ = 0;
8666 const char *name_str = NULL;
8667 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008668
Willy Tarreau185b5c42012-04-26 15:11:51 +02008669 if (args) {
8670 if (args[0].type != ARGT_STR)
8671 return 0;
8672 name_str = args[0].data.str.str;
8673 name_len = args[0].data.str.len;
8674
8675 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8676 occ = args[1].data.uint;
8677 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008678
Willy Tarreaue333ec92012-04-16 16:26:40 +02008679 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008680
Willy Tarreau185b5c42012-04-26 15:11:51 +02008681 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008682 /* search for header from the beginning */
8683 ctx->idx = 0;
8684
Willy Tarreau185b5c42012-04-26 15:11:51 +02008685 if (!occ && !(opt & SMP_OPT_ITERATE))
8686 /* no explicit occurrence and single fetch => last header by default */
8687 occ = -1;
8688
8689 if (!occ)
8690 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008691 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008692
Willy Tarreau185b5c42012-04-26 15:11:51 +02008693 smp->type = SMP_T_CSTR;
8694 smp->flags |= SMP_F_VOL_HDR;
8695 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 +02008696 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008697
Willy Tarreau37406352012-04-23 16:16:37 +02008698 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008699 return 0;
8700}
8701
Willy Tarreauc11416f2007-06-17 16:58:38 +02008702/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008703 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008704 */
8705static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008706smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008707 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008708{
8709 struct http_txn *txn = l7;
8710 struct hdr_idx *idx = &txn->hdr_idx;
8711 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008712 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008713 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008714
Willy Tarreau24e32d82012-04-23 23:55:44 +02008715 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008716 return 0;
8717
Willy Tarreaue333ec92012-04-16 16:26:40 +02008718 CHECK_HTTP_MESSAGE_FIRST();
8719
Willy Tarreau33a7e692007-06-10 19:45:56 +02008720 ctx.idx = 0;
8721 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008722 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 +02008723 cnt++;
8724
Willy Tarreauf853c462012-04-23 18:53:56 +02008725 smp->type = SMP_T_UINT;
8726 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008727 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008728 return 1;
8729}
8730
Willy Tarreau185b5c42012-04-26 15:11:51 +02008731/* Fetch an HTTP header's integer value. The integer value is returned. It
8732 * takes a mandatory argument of type string and an optional one of type int
8733 * to designate a specific occurrence. It returns an unsigned integer, which
8734 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008735 */
8736static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008737smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008738 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008739{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008740 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008741
Willy Tarreauf853c462012-04-23 18:53:56 +02008742 if (ret > 0) {
8743 smp->type = SMP_T_UINT;
8744 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8745 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008746
Willy Tarreaud53e2422012-04-16 17:21:11 +02008747 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008748}
8749
Cyril Bonté69fa9922012-10-25 00:01:06 +02008750/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8751 * and an optional one of type int to designate a specific occurrence.
8752 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008753 */
8754static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008755smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008756 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008757{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008758 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008759
Willy Tarreau185b5c42012-04-26 15:11:51 +02008760 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008761 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8762 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008763 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008764 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008765 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008766 if (smp->data.str.len < temp->size - 1) {
8767 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8768 temp->str[smp->data.str.len] = '\0';
8769 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8770 smp->type = SMP_T_IPV6;
8771 break;
8772 }
8773 }
8774 }
8775
Willy Tarreaud53e2422012-04-16 17:21:11 +02008776 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008777 if (!(smp->flags & SMP_F_NOT_LAST))
8778 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008779 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008780 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008781}
8782
Willy Tarreau737b0c12007-06-10 21:28:46 +02008783/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8784 * the first '/' after the possible hostname, and ends before the possible '?'.
8785 */
8786static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008787smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008788 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008789{
8790 struct http_txn *txn = l7;
8791 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008792
Willy Tarreauc0239e02012-04-16 14:42:55 +02008793 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008794
Willy Tarreau9b28e032012-10-12 23:49:43 +02008795 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008796 ptr = http_get_path(txn);
8797 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008798 return 0;
8799
8800 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008801 smp->type = SMP_T_CSTR;
8802 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008803
8804 while (ptr < end && *ptr != '?')
8805 ptr++;
8806
Willy Tarreauf853c462012-04-23 18:53:56 +02008807 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008808 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008809 return 1;
8810}
8811
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008812/* This produces a concatenation of the first occurrence of the Host header
8813 * followed by the path component if it begins with a slash ('/'). This means
8814 * that '*' will not be added, resulting in exactly the first Host entry.
8815 * If no Host header is found, then the path is returned as-is. The returned
8816 * value is stored in the trash so it does not need to be marked constant.
8817 */
8818static int
8819smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8820 const struct arg *args, struct sample *smp)
8821{
8822 struct http_txn *txn = l7;
8823 char *ptr, *end, *beg;
8824 struct hdr_ctx ctx;
8825
8826 CHECK_HTTP_MESSAGE_FIRST();
8827
8828 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008829 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 +02008830 !ctx.vlen)
8831 return smp_fetch_path(px, l4, l7, opt, args, smp);
8832
8833 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008834 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008835 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008836 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008837 smp->data.str.len = ctx.vlen;
8838
8839 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008840 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 +02008841 beg = http_get_path(txn);
8842 if (!beg)
8843 beg = end;
8844
8845 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8846
8847 if (beg < ptr && *beg == '/') {
8848 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8849 smp->data.str.len += ptr - beg;
8850 }
8851
8852 smp->flags = SMP_F_VOL_1ST;
8853 return 1;
8854}
8855
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008856/* This produces a 32-bit hash of the concatenation of the first occurrence of
8857 * the Host header followed by the path component if it begins with a slash ('/').
8858 * This means that '*' will not be added, resulting in exactly the first Host
8859 * entry. If no Host header is found, then the path is used. The resulting value
8860 * is hashed using the url hash followed by a full avalanche hash and provides a
8861 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8862 * high-traffic sites without having to store whole paths.
8863 */
8864static int
8865smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8866 const struct arg *args, struct sample *smp)
8867{
8868 struct http_txn *txn = l7;
8869 struct hdr_ctx ctx;
8870 unsigned int hash = 0;
8871 char *ptr, *beg, *end;
8872 int len;
8873
8874 CHECK_HTTP_MESSAGE_FIRST();
8875
8876 ctx.idx = 0;
8877 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8878 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8879 ptr = ctx.line + ctx.val;
8880 len = ctx.vlen;
8881 while (len--)
8882 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8883 }
8884
8885 /* now retrieve the path */
8886 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8887 beg = http_get_path(txn);
8888 if (!beg)
8889 beg = end;
8890
8891 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8892
8893 if (beg < ptr && *beg == '/') {
8894 while (beg < ptr)
8895 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8896 }
8897 hash = full_hash(hash);
8898
8899 smp->type = SMP_T_UINT;
8900 smp->data.uint = hash;
8901 smp->flags = SMP_F_VOL_1ST;
8902 return 1;
8903}
8904
Willy Tarreau4a550602012-12-09 14:53:32 +01008905/* This concatenates the source address with the 32-bit hash of the Host and
8906 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8907 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8908 * on the source address length. The URL hash is stored before the address so
8909 * that in environments where IPv6 is insignificant, truncating the output to
8910 * 8 bytes would still work.
8911 */
8912static int
8913smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8914 const struct arg *args, struct sample *smp)
8915{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008916 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008917
8918 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8919 return 0;
8920
Willy Tarreau47ca5452012-12-23 20:22:19 +01008921 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008922 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8923 temp->len += sizeof(smp->data.uint);
8924
8925 switch (l4->si[0].conn->addr.from.ss_family) {
8926 case AF_INET:
8927 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
8928 temp->len += 4;
8929 break;
8930 case AF_INET6:
8931 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
8932 temp->len += 16;
8933 break;
8934 default:
8935 return 0;
8936 }
8937
8938 smp->data.str = *temp;
8939 smp->type = SMP_T_BIN;
8940 return 1;
8941}
8942
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008943static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008944acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008945 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008946{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008947 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8948 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8949 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008950
Willy Tarreau24e32d82012-04-23 23:55:44 +02008951 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008952
Willy Tarreauf853c462012-04-23 18:53:56 +02008953 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008954 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008955 return 1;
8956}
8957
Willy Tarreau7f18e522010-10-22 20:04:13 +02008958/* return a valid test if the current request is the first one on the connection */
8959static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008960acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008961 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008962{
8963 if (!s)
8964 return 0;
8965
Willy Tarreauf853c462012-04-23 18:53:56 +02008966 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008967 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008968 return 1;
8969}
8970
Willy Tarreau34db1082012-04-19 17:16:54 +02008971/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008972static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008973acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008974 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008975{
8976
Willy Tarreau24e32d82012-04-23 23:55:44 +02008977 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008978 return 0;
8979
Willy Tarreauc0239e02012-04-16 14:42:55 +02008980 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008981
Willy Tarreauc0239e02012-04-16 14:42:55 +02008982 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008983 return 0;
8984
Willy Tarreauf853c462012-04-23 18:53:56 +02008985 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008986 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 +01008987 return 1;
8988}
Willy Tarreau8797c062007-05-07 00:55:35 +02008989
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008990/* Accepts exactly 1 argument of type userlist */
8991static int
8992acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8993 const struct arg *args, struct sample *smp)
8994{
8995
8996 if (!args || args->type != ARGT_USR)
8997 return 0;
8998
8999 CHECK_HTTP_MESSAGE_FIRST();
9000
9001 if (!get_http_auth(l4))
9002 return 0;
9003
9004 /* acl_match_auth() will need several information at once */
9005 smp->ctx.a[0] = args->data.usr; /* user list */
9006 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9007 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9008
9009 /* if the user does not belong to the userlist or has a wrong password,
9010 * report that it unconditionally does not match. Otherwise we return
9011 * a non-zero integer which will be ignored anyway since all the params
9012 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
9013 */
9014 smp->type = SMP_T_BOOL;
9015 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9016 if (smp->data.uint)
9017 smp->type = SMP_T_UINT;
9018
9019 return 1;
9020}
9021
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009022/* Try to find the next occurrence of a cookie name in a cookie header value.
9023 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9024 * the cookie value is returned into *value and *value_l, and the function
9025 * returns a pointer to the next pointer to search from if the value was found.
9026 * Otherwise if the cookie was not found, NULL is returned and neither value
9027 * nor value_l are touched. The input <hdr> string should first point to the
9028 * header's value, and the <hdr_end> pointer must point to the first character
9029 * not part of the value. <list> must be non-zero if value may represent a list
9030 * of values (cookie headers). This makes it faster to abort parsing when no
9031 * list is expected.
9032 */
9033static char *
9034extract_cookie_value(char *hdr, const char *hdr_end,
9035 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009036 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009037{
9038 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9039 char *next;
9040
9041 /* we search at least a cookie name followed by an equal, and more
9042 * generally something like this :
9043 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9044 */
9045 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9046 /* Iterate through all cookies on this line */
9047
9048 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9049 att_beg++;
9050
9051 /* find att_end : this is the first character after the last non
9052 * space before the equal. It may be equal to hdr_end.
9053 */
9054 equal = att_end = att_beg;
9055
9056 while (equal < hdr_end) {
9057 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9058 break;
9059 if (http_is_spht[(unsigned char)*equal++])
9060 continue;
9061 att_end = equal;
9062 }
9063
9064 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9065 * is between <att_beg> and <equal>, both may be identical.
9066 */
9067
9068 /* look for end of cookie if there is an equal sign */
9069 if (equal < hdr_end && *equal == '=') {
9070 /* look for the beginning of the value */
9071 val_beg = equal + 1;
9072 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9073 val_beg++;
9074
9075 /* find the end of the value, respecting quotes */
9076 next = find_cookie_value_end(val_beg, hdr_end);
9077
9078 /* make val_end point to the first white space or delimitor after the value */
9079 val_end = next;
9080 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9081 val_end--;
9082 } else {
9083 val_beg = val_end = next = equal;
9084 }
9085
9086 /* We have nothing to do with attributes beginning with '$'. However,
9087 * they will automatically be removed if a header before them is removed,
9088 * since they're supposed to be linked together.
9089 */
9090 if (*att_beg == '$')
9091 continue;
9092
9093 /* Ignore cookies with no equal sign */
9094 if (equal == next)
9095 continue;
9096
9097 /* Now we have the cookie name between att_beg and att_end, and
9098 * its value between val_beg and val_end.
9099 */
9100
9101 if (att_end - att_beg == cookie_name_l &&
9102 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9103 /* let's return this value and indicate where to go on from */
9104 *value = val_beg;
9105 *value_l = val_end - val_beg;
9106 return next + 1;
9107 }
9108
9109 /* Set-Cookie headers only have the name in the first attr=value part */
9110 if (!list)
9111 break;
9112 }
9113
9114 return NULL;
9115}
9116
Willy Tarreaue333ec92012-04-16 16:26:40 +02009117/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009118 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
9119 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009120 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009121 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009122 * Accepts exactly 1 argument of type string. If the input options indicate
9123 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009124 */
9125static int
Willy Tarreau51539362012-05-08 12:46:28 +02009126smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9127 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009128{
9129 struct http_txn *txn = l7;
9130 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02009131 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009132 const struct http_msg *msg;
9133 const char *hdr_name;
9134 int hdr_name_len;
9135 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009136 int occ = 0;
9137 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009138
Willy Tarreau24e32d82012-04-23 23:55:44 +02009139 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009140 return 0;
9141
Willy Tarreaue333ec92012-04-16 16:26:40 +02009142 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009143
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009144 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009145 msg = &txn->req;
9146 hdr_name = "Cookie";
9147 hdr_name_len = 6;
9148 } else {
9149 msg = &txn->rsp;
9150 hdr_name = "Set-Cookie";
9151 hdr_name_len = 10;
9152 }
9153
Willy Tarreau28376d62012-04-26 21:26:10 +02009154 if (!occ && !(opt & SMP_OPT_ITERATE))
9155 /* no explicit occurrence and single fetch => last cookie by default */
9156 occ = -1;
9157
9158 /* OK so basically here, either we want only one value and it's the
9159 * last one, or we want to iterate over all of them and we fetch the
9160 * next one.
9161 */
9162
Willy Tarreau9b28e032012-10-12 23:49:43 +02009163 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009164 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009165 /* search for the header from the beginning, we must first initialize
9166 * the search parameters.
9167 */
Willy Tarreau37406352012-04-23 16:16:37 +02009168 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009169 ctx->idx = 0;
9170 }
9171
Willy Tarreau28376d62012-04-26 21:26:10 +02009172 smp->flags |= SMP_F_VOL_HDR;
9173
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009174 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009175 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9176 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009177 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9178 goto out;
9179
Willy Tarreau24e32d82012-04-23 23:55:44 +02009180 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009181 continue;
9182
Willy Tarreau37406352012-04-23 16:16:37 +02009183 smp->ctx.a[0] = ctx->line + ctx->val;
9184 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009185 }
9186
Willy Tarreauf853c462012-04-23 18:53:56 +02009187 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009188 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009189 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009190 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009191 &smp->data.str.str,
9192 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009193 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009194 found = 1;
9195 if (occ >= 0) {
9196 /* one value was returned into smp->data.str.{str,len} */
9197 smp->flags |= SMP_F_NOT_LAST;
9198 return 1;
9199 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009200 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009201 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009202 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009203 /* all cookie headers and values were scanned. If we're looking for the
9204 * last occurrence, we may return it now.
9205 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009206 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009207 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009208 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009209}
9210
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009211/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009212 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009213 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009214 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009215 */
9216static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009217acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009218 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009219{
9220 struct http_txn *txn = l7;
9221 struct hdr_idx *idx = &txn->hdr_idx;
9222 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009223 const struct http_msg *msg;
9224 const char *hdr_name;
9225 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009226 int cnt;
9227 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009228 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009229
Willy Tarreau24e32d82012-04-23 23:55:44 +02009230 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009231 return 0;
9232
Willy Tarreaue333ec92012-04-16 16:26:40 +02009233 CHECK_HTTP_MESSAGE_FIRST();
9234
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009235 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009236 msg = &txn->req;
9237 hdr_name = "Cookie";
9238 hdr_name_len = 6;
9239 } else {
9240 msg = &txn->rsp;
9241 hdr_name = "Set-Cookie";
9242 hdr_name_len = 10;
9243 }
9244
Willy Tarreau9b28e032012-10-12 23:49:43 +02009245 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009246 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009247 ctx.idx = 0;
9248 cnt = 0;
9249
9250 while (1) {
9251 /* Note: val_beg == NULL every time we need to fetch a new header */
9252 if (!val_beg) {
9253 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9254 break;
9255
Willy Tarreau24e32d82012-04-23 23:55:44 +02009256 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009257 continue;
9258
9259 val_beg = ctx.line + ctx.val;
9260 val_end = val_beg + ctx.vlen;
9261 }
9262
Willy Tarreauf853c462012-04-23 18:53:56 +02009263 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009264 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009265 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009266 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009267 &smp->data.str.str,
9268 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009269 cnt++;
9270 }
9271 }
9272
Willy Tarreauf853c462012-04-23 18:53:56 +02009273 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009274 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009275 return 1;
9276}
9277
Willy Tarreau51539362012-05-08 12:46:28 +02009278/* Fetch an cookie's integer value. The integer value is returned. It
9279 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9280 */
9281static int
9282smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9283 const struct arg *args, struct sample *smp)
9284{
9285 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9286
9287 if (ret > 0) {
9288 smp->type = SMP_T_UINT;
9289 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9290 }
9291
9292 return ret;
9293}
9294
Willy Tarreau8797c062007-05-07 00:55:35 +02009295/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009296/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009297/************************************************************************/
9298
David Cournapeau16023ee2010-12-23 20:55:41 +09009299/*
9300 * Given a path string and its length, find the position of beginning of the
9301 * query string. Returns NULL if no query string is found in the path.
9302 *
9303 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9304 *
9305 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9306 */
bedis4c75cca2012-10-05 08:38:24 +02009307static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009308{
9309 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009310
bedis4c75cca2012-10-05 08:38:24 +02009311 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009312 return p ? p + 1 : NULL;
9313}
9314
bedis4c75cca2012-10-05 08:38:24 +02009315static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009316{
bedis4c75cca2012-10-05 08:38:24 +02009317 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009318}
9319
9320/*
9321 * Given a url parameter, find the starting position of the first occurence,
9322 * or NULL if the parameter is not found.
9323 *
9324 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9325 * the function will return query_string+8.
9326 */
9327static char*
9328find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009329 char* url_param_name, size_t url_param_name_l,
9330 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009331{
9332 char *pos, *last;
9333
9334 pos = query_string;
9335 last = query_string + query_string_l - url_param_name_l - 1;
9336
9337 while (pos <= last) {
9338 if (pos[url_param_name_l] == '=') {
9339 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9340 return pos;
9341 pos += url_param_name_l + 1;
9342 }
bedis4c75cca2012-10-05 08:38:24 +02009343 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009344 pos++;
9345 pos++;
9346 }
9347 return NULL;
9348}
9349
9350/*
9351 * Given a url parameter name, returns its value and size into *value and
9352 * *value_l respectively, and returns non-zero. If the parameter is not found,
9353 * zero is returned and value/value_l are not touched.
9354 */
9355static int
9356find_url_param_value(char* path, size_t path_l,
9357 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009358 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009359{
9360 char *query_string, *qs_end;
9361 char *arg_start;
9362 char *value_start, *value_end;
9363
bedis4c75cca2012-10-05 08:38:24 +02009364 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009365 if (!query_string)
9366 return 0;
9367
9368 qs_end = path + path_l;
9369 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009370 url_param_name, url_param_name_l,
9371 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009372 if (!arg_start)
9373 return 0;
9374
9375 value_start = arg_start + url_param_name_l + 1;
9376 value_end = value_start;
9377
bedis4c75cca2012-10-05 08:38:24 +02009378 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009379 value_end++;
9380
9381 *value = value_start;
9382 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009383 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009384}
9385
9386static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009387smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9388 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009389{
bedis4c75cca2012-10-05 08:38:24 +02009390 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009391 struct http_txn *txn = l7;
9392 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009393
bedis4c75cca2012-10-05 08:38:24 +02009394 if (!args || args[0].type != ARGT_STR ||
9395 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009396 return 0;
9397
9398 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009399
bedis4c75cca2012-10-05 08:38:24 +02009400 if (args[1].type)
9401 delim = *args[1].data.str.str;
9402
Willy Tarreau9b28e032012-10-12 23:49:43 +02009403 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009404 args->data.str.str, args->data.str.len,
9405 &smp->data.str.str, &smp->data.str.len,
9406 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009407 return 0;
9408
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009409 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009410 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009411 return 1;
9412}
9413
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009414/* Return the signed integer value for the specified url parameter (see url_param
9415 * above).
9416 */
9417static int
9418smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9419 const struct arg *args, struct sample *smp)
9420{
9421 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9422
9423 if (ret > 0) {
9424 smp->type = SMP_T_UINT;
9425 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9426 }
9427
9428 return ret;
9429}
9430
Willy Tarreau185b5c42012-04-26 15:11:51 +02009431/* This function is used to validate the arguments passed to any "hdr" fetch
9432 * keyword. These keywords support an optional positive or negative occurrence
9433 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9434 * is assumed that the types are already the correct ones. Returns 0 on error,
9435 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9436 * error message in case of error, that the caller is responsible for freeing.
9437 * The initial location must either be freeable or NULL.
9438 */
9439static int val_hdr(struct arg *arg, char **err_msg)
9440{
9441 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009442 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009443 return 0;
9444 }
9445 return 1;
9446}
9447
Willy Tarreau4a568972010-05-12 08:08:50 +02009448/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009449/* All supported ACL keywords must be declared here. */
9450/************************************************************************/
9451
9452/* Note: must not be declared <const> as its list will be overwritten.
9453 * Please take care of keeping this list alphabetically sorted.
9454 */
9455static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009456 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9457 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9458 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9459 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9460 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9461 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9462 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9463 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
9464
Willy Tarreau51539362012-05-08 12:46:28 +02009465 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9466 { "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 +02009467 { "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 +02009468 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9469 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9470 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9471 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9472 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9473 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9474 { "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 +02009475
Willy Tarreau185b5c42012-04-26 15:11:51 +02009476 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9477 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9478 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9479 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9480 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9481 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9482 { "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 },
9483 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9484 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9485 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9486 { "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 +02009487
9488 { "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 +02009489 { "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 +02009490 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9491
9492 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9493
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009494 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9495 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9496 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9497 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9498 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9499 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9500 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9501 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009502
9503 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9504 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9505 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
9506
Willy Tarreau51539362012-05-08 12:46:28 +02009507 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9508 { "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 +02009509 { "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 +02009510 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9511 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9512 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9513 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9514 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9515 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9516 { "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 +02009517
Willy Tarreau185b5c42012-04-26 15:11:51 +02009518 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9519 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9520 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9521 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9522 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9523 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9524 { "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 },
9525 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9526 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9527 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9528 { "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 +02009529
9530 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9531
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009532 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9533 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9534 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9535 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9536 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9537 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9538 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9539 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9540 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9541 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009542
9543 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9544 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9545 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9546 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9547 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9548 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9549 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9550 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9551 { "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 +02009552 { "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 +02009553
9554 { NULL, NULL, NULL, NULL },
9555}};
9556
9557/************************************************************************/
9558/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009559/************************************************************************/
9560/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009561static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009562 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9563 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009564 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau4a550602012-12-09 14:53:32 +01009565 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009566 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9567 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9568 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9569 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9570 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9571 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9572 { "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 +02009573 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009574}};
9575
Willy Tarreau8797c062007-05-07 00:55:35 +02009576
9577__attribute__((constructor))
9578static void __http_protocol_init(void)
9579{
9580 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009581 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009582}
9583
9584
Willy Tarreau58f10d72006-12-04 02:26:12 +01009585/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009586 * Local variables:
9587 * c-indent-level: 8
9588 * c-basic-offset: 8
9589 * End:
9590 */