blob: a37e2909fae588d12a146388c8fce76ab983ba9d [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
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020063#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020067#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020069#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010070#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020072#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau522d6c02009-12-06 18:49:18 +010075const char HTTP_100[] =
76 "HTTP/1.1 100 Continue\r\n\r\n";
77
78const struct chunk http_100_chunk = {
79 .str = (char *)&HTTP_100,
80 .len = sizeof(HTTP_100)-1
81};
82
Willy Tarreaua9679ac2010-01-03 17:32:57 +010083/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400102
103/* same as 302 except that the browser MUST retry with the same method */
104const char *HTTP_307 =
105 "HTTP/1.1 307 Temporary Redirect\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
110/* same as 301 except that the browser MUST retry with the same method */
111const char *HTTP_308 =
112 "HTTP/1.1 308 Permanent Redirect\r\n"
113 "Content-length: 0\r\n"
114 "Location: "; /* not terminated since it will be concatenated with the URL */
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
117const char *HTTP_401_fmt =
118 "HTTP/1.0 401 Unauthorized\r\n"
119 "Cache-Control: no-cache\r\n"
120 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200121 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
123 "\r\n"
124 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
125
Willy Tarreau844a7e72010-01-31 21:46:18 +0100126const char *HTTP_407_fmt =
127 "HTTP/1.0 407 Unauthorized\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
132 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800133 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135
136const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200137 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_400] = 400,
139 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400140 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_500] = 500,
144 [HTTP_ERR_502] = 502,
145 [HTTP_ERR_503] = 503,
146 [HTTP_ERR_504] = 504,
147};
148
Willy Tarreau80587432006-12-24 17:47:20 +0100149static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200150 [HTTP_ERR_200] =
151 "HTTP/1.0 200 OK\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
157
Willy Tarreau0f772532006-12-23 20:51:41 +0100158 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100159 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
165
166 [HTTP_ERR_403] =
167 "HTTP/1.0 403 Forbidden\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
173
CJ Ess108b1dd2015-04-07 12:03:37 -0400174 [HTTP_ERR_405] =
175 "HTTP/1.0 405 Method Not Allowed\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
181
Willy Tarreau0f772532006-12-23 20:51:41 +0100182 [HTTP_ERR_408] =
183 "HTTP/1.0 408 Request Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
189
CJ Ess108b1dd2015-04-07 12:03:37 -0400190 [HTTP_ERR_429] =
191 "HTTP/1.0 429 Too Many Requests\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
197
Willy Tarreau0f772532006-12-23 20:51:41 +0100198 [HTTP_ERR_500] =
199 "HTTP/1.0 500 Server Error\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
205
206 [HTTP_ERR_502] =
207 "HTTP/1.0 502 Bad Gateway\r\n"
208 "Cache-Control: no-cache\r\n"
209 "Connection: close\r\n"
210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
213
214 [HTTP_ERR_503] =
215 "HTTP/1.0 503 Service Unavailable\r\n"
216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
221
222 [HTTP_ERR_504] =
223 "HTTP/1.0 504 Gateway Time-out\r\n"
224 "Cache-Control: no-cache\r\n"
225 "Connection: close\r\n"
226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
229
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232/* status codes available for the stats admin page (strictly 4 chars length) */
233const char *stat_status_codes[STAT_STATUS_SIZE] = {
234 [STAT_STATUS_DENY] = "DENY",
235 [STAT_STATUS_DONE] = "DONE",
236 [STAT_STATUS_ERRP] = "ERRP",
237 [STAT_STATUS_EXCD] = "EXCD",
238 [STAT_STATUS_NONE] = "NONE",
239 [STAT_STATUS_PART] = "PART",
240 [STAT_STATUS_UNKN] = "UNKN",
241};
242
243
William Lallemand73025dd2014-04-24 14:38:37 +0200244/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_req_keywords.list)
247};
248
249/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200250struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200251 .list = LIST_HEAD_INIT(http_res_keywords.list)
252};
253
Willy Tarreau80587432006-12-24 17:47:20 +0100254/* We must put the messages here since GCC cannot initialize consts depending
255 * on strlen().
256 */
257struct chunk http_err_chunks[HTTP_ERR_SIZE];
258
Willy Tarreaua890d072013-04-02 12:01:06 +0200259/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
260static struct hdr_ctx static_hdr_ctx;
261
Willy Tarreau42250582007-04-01 01:30:43 +0200262#define FD_SETS_ARE_BITFIELDS
263#ifdef FD_SETS_ARE_BITFIELDS
264/*
265 * This map is used with all the FD_* macros to check whether a particular bit
266 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
267 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
268 * byte should be encoded. Be careful to always pass bytes from 0 to 255
269 * exclusively to the macros.
270 */
271fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
272fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100273fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200274
275#else
276#error "Check if your OS uses bitfields for fd_sets"
277#endif
278
Willy Tarreau87b09662015-04-03 00:22:06 +0200279static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200280
David Carlier7365f7d2016-04-04 11:54:42 +0100281static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
282static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100283
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200284/* This function returns a reason associated with the HTTP status.
285 * This function never fails, a message is always returned.
286 */
287const char *get_reason(unsigned int status)
288{
289 switch (status) {
290 case 100: return "Continue";
291 case 101: return "Switching Protocols";
292 case 102: return "Processing";
293 case 200: return "OK";
294 case 201: return "Created";
295 case 202: return "Accepted";
296 case 203: return "Non-Authoritative Information";
297 case 204: return "No Content";
298 case 205: return "Reset Content";
299 case 206: return "Partial Content";
300 case 207: return "Multi-Status";
301 case 210: return "Content Different";
302 case 226: return "IM Used";
303 case 300: return "Multiple Choices";
304 case 301: return "Moved Permanently";
305 case 302: return "Moved Temporarily";
306 case 303: return "See Other";
307 case 304: return "Not Modified";
308 case 305: return "Use Proxy";
309 case 307: return "Temporary Redirect";
310 case 308: return "Permanent Redirect";
311 case 310: return "Too many Redirects";
312 case 400: return "Bad Request";
313 case 401: return "Unauthorized";
314 case 402: return "Payment Required";
315 case 403: return "Forbidden";
316 case 404: return "Not Found";
317 case 405: return "Method Not Allowed";
318 case 406: return "Not Acceptable";
319 case 407: return "Proxy Authentication Required";
320 case 408: return "Request Time-out";
321 case 409: return "Conflict";
322 case 410: return "Gone";
323 case 411: return "Length Required";
324 case 412: return "Precondition Failed";
325 case 413: return "Request Entity Too Large";
326 case 414: return "Request-URI Too Long";
327 case 415: return "Unsupported Media Type";
328 case 416: return "Requested range unsatisfiable";
329 case 417: return "Expectation failed";
330 case 418: return "I'm a teapot";
331 case 422: return "Unprocessable entity";
332 case 423: return "Locked";
333 case 424: return "Method failure";
334 case 425: return "Unordered Collection";
335 case 426: return "Upgrade Required";
336 case 428: return "Precondition Required";
337 case 429: return "Too Many Requests";
338 case 431: return "Request Header Fields Too Large";
339 case 449: return "Retry With";
340 case 450: return "Blocked by Windows Parental Controls";
341 case 451: return "Unavailable For Legal Reasons";
342 case 456: return "Unrecoverable Error";
343 case 499: return "client has closed connection";
344 case 500: return "Internal Server Error";
345 case 501: return "Not Implemented";
346 case 502: return "Bad Gateway ou Proxy Error";
347 case 503: return "Service Unavailable";
348 case 504: return "Gateway Time-out";
349 case 505: return "HTTP Version not supported";
350 case 506: return "Variant also negociate";
351 case 507: return "Insufficient storage";
352 case 508: return "Loop detected";
353 case 509: return "Bandwidth Limit Exceeded";
354 case 510: return "Not extended";
355 case 511: return "Network authentication required";
356 case 520: return "Web server is returning an unknown error";
357 default:
358 switch (status) {
359 case 100 ... 199: return "Informational";
360 case 200 ... 299: return "Success";
361 case 300 ... 399: return "Redirection";
362 case 400 ... 499: return "Client Error";
363 case 500 ... 599: return "Server Error";
364 default: return "Other";
365 }
366 }
367}
368
Willy Tarreau80587432006-12-24 17:47:20 +0100369void init_proto_http()
370{
Willy Tarreau42250582007-04-01 01:30:43 +0200371 int i;
372 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100373 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200374
Willy Tarreau80587432006-12-24 17:47:20 +0100375 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
376 if (!http_err_msgs[msg]) {
377 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
378 abort();
379 }
380
381 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
382 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
383 }
Willy Tarreau42250582007-04-01 01:30:43 +0200384
385 /* initialize the log header encoding map : '{|}"#' should be encoded with
386 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
387 * URL encoding only requires '"', '#' to be encoded as well as non-
388 * printable characters above.
389 */
390 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
391 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100392 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200393 for (i = 0; i < 32; i++) {
394 FD_SET(i, hdr_encode_map);
395 FD_SET(i, url_encode_map);
396 }
397 for (i = 127; i < 256; i++) {
398 FD_SET(i, hdr_encode_map);
399 FD_SET(i, url_encode_map);
400 }
401
402 tmp = "\"#{|}";
403 while (*tmp) {
404 FD_SET(*tmp, hdr_encode_map);
405 tmp++;
406 }
407
408 tmp = "\"#";
409 while (*tmp) {
410 FD_SET(*tmp, url_encode_map);
411 tmp++;
412 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200413
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100414 /* initialize the http header encoding map. The draft httpbis define the
415 * header content as:
416 *
417 * HTTP-message = start-line
418 * *( header-field CRLF )
419 * CRLF
420 * [ message-body ]
421 * header-field = field-name ":" OWS field-value OWS
422 * field-value = *( field-content / obs-fold )
423 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
424 * obs-fold = CRLF 1*( SP / HTAB )
425 * field-vchar = VCHAR / obs-text
426 * VCHAR = %x21-7E
427 * obs-text = %x80-FF
428 *
429 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
430 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
431 * "obs-fold" is volontary forgotten because haproxy remove this.
432 */
433 memset(http_encode_map, 0, sizeof(http_encode_map));
434 for (i = 0x00; i <= 0x08; i++)
435 FD_SET(i, http_encode_map);
436 for (i = 0x0a; i <= 0x1f; i++)
437 FD_SET(i, http_encode_map);
438 FD_SET(0x7f, http_encode_map);
439
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200440 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200441 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200442 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100443 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100444}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445
Willy Tarreau53b6c742006-12-17 13:37:46 +0100446/*
447 * We have 26 list of methods (1 per first letter), each of which can have
448 * up to 3 entries (2 valid, 1 null).
449 */
450struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100451 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100452 int len;
453 const char text[8];
454};
455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100456const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100457 ['C' - 'A'] = {
458 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
459 },
460 ['D' - 'A'] = {
461 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
462 },
463 ['G' - 'A'] = {
464 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
465 },
466 ['H' - 'A'] = {
467 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
468 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200469 ['O' - 'A'] = {
470 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
471 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 ['P' - 'A'] = {
473 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
474 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
475 },
476 ['T' - 'A'] = {
477 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
478 },
479 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200480 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100481 */
482};
483
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100484const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100485 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
486 [HTTP_METH_GET] = { "GET", 3 },
487 [HTTP_METH_HEAD] = { "HEAD", 4 },
488 [HTTP_METH_POST] = { "POST", 4 },
489 [HTTP_METH_PUT] = { "PUT", 3 },
490 [HTTP_METH_DELETE] = { "DELETE", 6 },
491 [HTTP_METH_TRACE] = { "TRACE", 5 },
492 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
493};
494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100495/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200496 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100497 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
498 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
499 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
500 * digit. Note: please do not overwrite values in assignment since gcc-2.95
501 * will not handle them correctly. It's worth noting that chars 128..255 are
502 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100503 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100504const unsigned char http_char_classes[256] = {
505 [ 0] = HTTP_FLG_CTL,
506 [ 1] = HTTP_FLG_CTL,
507 [ 2] = HTTP_FLG_CTL,
508 [ 3] = HTTP_FLG_CTL,
509 [ 4] = HTTP_FLG_CTL,
510 [ 5] = HTTP_FLG_CTL,
511 [ 6] = HTTP_FLG_CTL,
512 [ 7] = HTTP_FLG_CTL,
513 [ 8] = HTTP_FLG_CTL,
514 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
515 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
516 [ 11] = HTTP_FLG_CTL,
517 [ 12] = HTTP_FLG_CTL,
518 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
519 [ 14] = HTTP_FLG_CTL,
520 [ 15] = HTTP_FLG_CTL,
521 [ 16] = HTTP_FLG_CTL,
522 [ 17] = HTTP_FLG_CTL,
523 [ 18] = HTTP_FLG_CTL,
524 [ 19] = HTTP_FLG_CTL,
525 [ 20] = HTTP_FLG_CTL,
526 [ 21] = HTTP_FLG_CTL,
527 [ 22] = HTTP_FLG_CTL,
528 [ 23] = HTTP_FLG_CTL,
529 [ 24] = HTTP_FLG_CTL,
530 [ 25] = HTTP_FLG_CTL,
531 [ 26] = HTTP_FLG_CTL,
532 [ 27] = HTTP_FLG_CTL,
533 [ 28] = HTTP_FLG_CTL,
534 [ 29] = HTTP_FLG_CTL,
535 [ 30] = HTTP_FLG_CTL,
536 [ 31] = HTTP_FLG_CTL,
537 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
538 ['!'] = HTTP_FLG_TOK,
539 ['"'] = HTTP_FLG_SEP,
540 ['#'] = HTTP_FLG_TOK,
541 ['$'] = HTTP_FLG_TOK,
542 ['%'] = HTTP_FLG_TOK,
543 ['&'] = HTTP_FLG_TOK,
544 [ 39] = HTTP_FLG_TOK,
545 ['('] = HTTP_FLG_SEP,
546 [')'] = HTTP_FLG_SEP,
547 ['*'] = HTTP_FLG_TOK,
548 ['+'] = HTTP_FLG_TOK,
549 [','] = HTTP_FLG_SEP,
550 ['-'] = HTTP_FLG_TOK,
551 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
552 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
553 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
554 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
555 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
556 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
557 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
558 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
559 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
560 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
561 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
562 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
563 [':'] = HTTP_FLG_SEP,
564 [';'] = HTTP_FLG_SEP,
565 ['<'] = HTTP_FLG_SEP,
566 ['='] = HTTP_FLG_SEP,
567 ['>'] = HTTP_FLG_SEP,
568 ['?'] = HTTP_FLG_SEP,
569 ['@'] = HTTP_FLG_SEP,
570 ['A'] = HTTP_FLG_TOK,
571 ['B'] = HTTP_FLG_TOK,
572 ['C'] = HTTP_FLG_TOK,
573 ['D'] = HTTP_FLG_TOK,
574 ['E'] = HTTP_FLG_TOK,
575 ['F'] = HTTP_FLG_TOK,
576 ['G'] = HTTP_FLG_TOK,
577 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['I'] = HTTP_FLG_TOK,
579 ['J'] = HTTP_FLG_TOK,
580 ['K'] = HTTP_FLG_TOK,
581 ['L'] = HTTP_FLG_TOK,
582 ['M'] = HTTP_FLG_TOK,
583 ['N'] = HTTP_FLG_TOK,
584 ['O'] = HTTP_FLG_TOK,
585 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
586 ['Q'] = HTTP_FLG_TOK,
587 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
588 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
589 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
590 ['U'] = HTTP_FLG_TOK,
591 ['V'] = HTTP_FLG_TOK,
592 ['W'] = HTTP_FLG_TOK,
593 ['X'] = HTTP_FLG_TOK,
594 ['Y'] = HTTP_FLG_TOK,
595 ['Z'] = HTTP_FLG_TOK,
596 ['['] = HTTP_FLG_SEP,
597 [ 92] = HTTP_FLG_SEP,
598 [']'] = HTTP_FLG_SEP,
599 ['^'] = HTTP_FLG_TOK,
600 ['_'] = HTTP_FLG_TOK,
601 ['`'] = HTTP_FLG_TOK,
602 ['a'] = HTTP_FLG_TOK,
603 ['b'] = HTTP_FLG_TOK,
604 ['c'] = HTTP_FLG_TOK,
605 ['d'] = HTTP_FLG_TOK,
606 ['e'] = HTTP_FLG_TOK,
607 ['f'] = HTTP_FLG_TOK,
608 ['g'] = HTTP_FLG_TOK,
609 ['h'] = HTTP_FLG_TOK,
610 ['i'] = HTTP_FLG_TOK,
611 ['j'] = HTTP_FLG_TOK,
612 ['k'] = HTTP_FLG_TOK,
613 ['l'] = HTTP_FLG_TOK,
614 ['m'] = HTTP_FLG_TOK,
615 ['n'] = HTTP_FLG_TOK,
616 ['o'] = HTTP_FLG_TOK,
617 ['p'] = HTTP_FLG_TOK,
618 ['q'] = HTTP_FLG_TOK,
619 ['r'] = HTTP_FLG_TOK,
620 ['s'] = HTTP_FLG_TOK,
621 ['t'] = HTTP_FLG_TOK,
622 ['u'] = HTTP_FLG_TOK,
623 ['v'] = HTTP_FLG_TOK,
624 ['w'] = HTTP_FLG_TOK,
625 ['x'] = HTTP_FLG_TOK,
626 ['y'] = HTTP_FLG_TOK,
627 ['z'] = HTTP_FLG_TOK,
628 ['{'] = HTTP_FLG_SEP,
629 ['|'] = HTTP_FLG_TOK,
630 ['}'] = HTTP_FLG_SEP,
631 ['~'] = HTTP_FLG_TOK,
632 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100633};
634
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100635/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100636 * Adds a header and its CRLF at the tail of the message's buffer, just before
637 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100638 * The header is also automatically added to the index <hdr_idx>, and the end
639 * of headers is automatically adjusted. The number of bytes added is returned
640 * on success, otherwise <0 is returned indicating an error.
641 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100642int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100643{
644 int bytes, len;
645
646 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200647 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100648 if (!bytes)
649 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100650 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100651 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
652}
653
654/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100655 * Adds a header and its CRLF at the tail of the message's buffer, just before
656 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100657 * the buffer is only opened and the space reserved, but nothing is copied.
658 * The header is also automatically added to the index <hdr_idx>, and the end
659 * of headers is automatically adjusted. The number of bytes added is returned
660 * on success, otherwise <0 is returned indicating an error.
661 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100662int http_header_add_tail2(struct http_msg *msg,
663 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100664{
665 int bytes;
666
Willy Tarreau9b28e032012-10-12 23:49:43 +0200667 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100668 if (!bytes)
669 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100670 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100671 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
672}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
674/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100675 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
676 * If so, returns the position of the first non-space character relative to
677 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
678 * to return a pointer to the place after the first space. Returns 0 if the
679 * header name does not match. Checks are case-insensitive.
680 */
681int http_header_match2(const char *hdr, const char *end,
682 const char *name, int len)
683{
684 const char *val;
685
686 if (hdr + len >= end)
687 return 0;
688 if (hdr[len] != ':')
689 return 0;
690 if (strncasecmp(hdr, name, len) != 0)
691 return 0;
692 val = hdr + len + 1;
693 while (val < end && HTTP_IS_SPHT(*val))
694 val++;
695 if ((val >= end) && (len + 2 <= end - hdr))
696 return len + 2; /* we may replace starting from second space */
697 return val - hdr;
698}
699
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200700/* Find the first or next occurrence of header <name> in message buffer <sol>
701 * using headers index <idx>, and return it in the <ctx> structure. This
702 * structure holds everything necessary to use the header and find next
703 * occurrence. If its <idx> member is 0, the header is searched from the
704 * beginning. Otherwise, the next occurrence is returned. The function returns
705 * 1 when it finds a value, and 0 when there is no more. It is very similar to
706 * http_find_header2() except that it is designed to work with full-line headers
707 * whose comma is not a delimiter but is part of the syntax. As a special case,
708 * if ctx->val is NULL when searching for a new values of a header, the current
709 * header is rescanned. This allows rescanning after a header deletion.
710 */
711int http_find_full_header2(const char *name, int len,
712 char *sol, struct hdr_idx *idx,
713 struct hdr_ctx *ctx)
714{
715 char *eol, *sov;
716 int cur_idx, old_idx;
717
718 cur_idx = ctx->idx;
719 if (cur_idx) {
720 /* We have previously returned a header, let's search another one */
721 sol = ctx->line;
722 eol = sol + idx->v[cur_idx].len;
723 goto next_hdr;
724 }
725
726 /* first request for this header */
727 sol += hdr_idx_first_pos(idx);
728 old_idx = 0;
729 cur_idx = hdr_idx_first_idx(idx);
730 while (cur_idx) {
731 eol = sol + idx->v[cur_idx].len;
732
733 if (len == 0) {
734 /* No argument was passed, we want any header.
735 * To achieve this, we simply build a fake request. */
736 while (sol + len < eol && sol[len] != ':')
737 len++;
738 name = sol;
739 }
740
741 if ((len < eol - sol) &&
742 (sol[len] == ':') &&
743 (strncasecmp(sol, name, len) == 0)) {
744 ctx->del = len;
745 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100746 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200747 sov++;
748
749 ctx->line = sol;
750 ctx->prev = old_idx;
751 ctx->idx = cur_idx;
752 ctx->val = sov - sol;
753 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100754 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200755 eol--;
756 ctx->tws++;
757 }
758 ctx->vlen = eol - sov;
759 return 1;
760 }
761 next_hdr:
762 sol = eol + idx->v[cur_idx].cr + 1;
763 old_idx = cur_idx;
764 cur_idx = idx->v[cur_idx].next;
765 }
766 return 0;
767}
768
Willy Tarreauc90dc232015-02-20 13:51:36 +0100769/* Find the first or next header field in message buffer <sol> using headers
770 * index <idx>, and return it in the <ctx> structure. This structure holds
771 * everything necessary to use the header and find next occurrence. If its
772 * <idx> member is 0, the first header is retrieved. Otherwise, the next
773 * occurrence is returned. The function returns 1 when it finds a value, and
774 * 0 when there is no more. It is equivalent to http_find_full_header2() with
775 * no header name.
776 */
777int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
778{
779 char *eol, *sov;
780 int cur_idx, old_idx;
781 int len;
782
783 cur_idx = ctx->idx;
784 if (cur_idx) {
785 /* We have previously returned a header, let's search another one */
786 sol = ctx->line;
787 eol = sol + idx->v[cur_idx].len;
788 goto next_hdr;
789 }
790
791 /* first request for this header */
792 sol += hdr_idx_first_pos(idx);
793 old_idx = 0;
794 cur_idx = hdr_idx_first_idx(idx);
795 while (cur_idx) {
796 eol = sol + idx->v[cur_idx].len;
797
798 len = 0;
799 while (1) {
800 if (len >= eol - sol)
801 goto next_hdr;
802 if (sol[len] == ':')
803 break;
804 len++;
805 }
806
807 ctx->del = len;
808 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100809 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100810 sov++;
811
812 ctx->line = sol;
813 ctx->prev = old_idx;
814 ctx->idx = cur_idx;
815 ctx->val = sov - sol;
816 ctx->tws = 0;
817
Willy Tarreau2235b262016-11-05 15:50:20 +0100818 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100819 eol--;
820 ctx->tws++;
821 }
822 ctx->vlen = eol - sov;
823 return 1;
824
825 next_hdr:
826 sol = eol + idx->v[cur_idx].cr + 1;
827 old_idx = cur_idx;
828 cur_idx = idx->v[cur_idx].next;
829 }
830 return 0;
831}
832
Willy Tarreau68085d82010-01-18 14:54:04 +0100833/* Find the end of the header value contained between <s> and <e>. See RFC2616,
834 * par 2.2 for more information. Note that it requires a valid header to return
835 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100837char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838{
839 int quoted, qdpair;
840
841 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100842
843#if defined(__x86_64__) || \
844 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
845 defined(__ARM_ARCH_7A__)
846 /* speedup: skip everything not a comma nor a double quote */
847 for (; s <= e - sizeof(int); s += sizeof(int)) {
848 unsigned int c = *(int *)s; // comma
849 unsigned int q = c; // quote
850
851 c ^= 0x2c2c2c2c; // contains one zero on a comma
852 q ^= 0x22222222; // contains one zero on a quote
853
854 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
855 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
856
857 if ((c | q) & 0x80808080)
858 break; // found a comma or a quote
859 }
860#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200861 for (; s < e; s++) {
862 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200863 else if (quoted) {
864 if (*s == '\\') qdpair = 1;
865 else if (*s == '"') quoted = 0;
866 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200867 else if (*s == '"') quoted = 1;
868 else if (*s == ',') return s;
869 }
870 return s;
871}
872
873/* Find the first or next occurrence of header <name> in message buffer <sol>
874 * using headers index <idx>, and return it in the <ctx> structure. This
875 * structure holds everything necessary to use the header and find next
876 * occurrence. If its <idx> member is 0, the header is searched from the
877 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100878 * 1 when it finds a value, and 0 when there is no more. It is designed to work
879 * with headers defined as comma-separated lists. As a special case, if ctx->val
880 * is NULL when searching for a new values of a header, the current header is
881 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200882 */
883int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100884 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200885 struct hdr_ctx *ctx)
886{
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 char *eol, *sov;
888 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200889
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 cur_idx = ctx->idx;
891 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200892 /* We have previously returned a value, let's search
893 * another one on the same line.
894 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200895 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200896 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200898 eol = sol + idx->v[cur_idx].len;
899
900 if (sov >= eol)
901 /* no more values in this header */
902 goto next_hdr;
903
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 /* values remaining for this header, skip the comma but save it
905 * for later use (eg: for header deletion).
906 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200907 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100908 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909 sov++;
910
911 goto return_hdr;
912 }
913
914 /* first request for this header */
915 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200917 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 while (cur_idx) {
919 eol = sol + idx->v[cur_idx].len;
920
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200921 if (len == 0) {
922 /* No argument was passed, we want any header.
923 * To achieve this, we simply build a fake request. */
924 while (sol + len < eol && sol[len] != ':')
925 len++;
926 name = sol;
927 }
928
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 if ((len < eol - sol) &&
930 (sol[len] == ':') &&
931 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100932 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200933 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100934 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200935 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100936
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100938 ctx->prev = old_idx;
939 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200940 ctx->idx = cur_idx;
941 ctx->val = sov - sol;
942
943 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200944 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100945 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200946 eol--;
947 ctx->tws++;
948 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 ctx->vlen = eol - sov;
950 return 1;
951 }
952 next_hdr:
953 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100954 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 cur_idx = idx->v[cur_idx].next;
956 }
957 return 0;
958}
959
960int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100961 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200962 struct hdr_ctx *ctx)
963{
964 return http_find_header2(name, strlen(name), sol, idx, ctx);
965}
966
Willy Tarreau68085d82010-01-18 14:54:04 +0100967/* Remove one value of a header. This only works on a <ctx> returned by one of
968 * the http_find_header functions. The value is removed, as well as surrounding
969 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100970 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100971 * message <msg>. The new index is returned. If it is zero, it means there is
972 * no more header, so any processing may stop. The ctx is always left in a form
973 * that can be handled by http_find_header2() to find next occurrence.
974 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100975int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100976{
977 int cur_idx = ctx->idx;
978 char *sol = ctx->line;
979 struct hdr_idx_elem *hdr;
980 int delta, skip_comma;
981
982 if (!cur_idx)
983 return 0;
984
985 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200986 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100987 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200988 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100989 http_msg_move_end(msg, delta);
990 idx->used--;
991 hdr->len = 0; /* unused entry */
992 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100993 if (idx->tail == ctx->idx)
994 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100995 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100996 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100997 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200998 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100999 return ctx->idx;
1000 }
1001
1002 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001003 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1004 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001005 */
1006
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001007 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001009 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001010 NULL, 0);
1011 hdr->len += delta;
1012 http_msg_move_end(msg, delta);
1013 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001014 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 return ctx->idx;
1016}
1017
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018/* This function handles a server error at the stream interface level. The
1019 * stream interface is assumed to be already in a closed state. An optional
1020 * message is copied into the input buffer, and an HTTP status code stored.
1021 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001022 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001024static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001025 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026{
Christopher Faulet3e344292015-11-24 16:24:13 +01001027 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001028 channel_auto_read(si_oc(si));
1029 channel_abort(si_oc(si));
1030 channel_auto_close(si_oc(si));
1031 channel_erase(si_oc(si));
1032 channel_auto_close(si_ic(si));
1033 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +01001034 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02001035 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001036 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001038 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001039 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001040 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001041 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042}
1043
Willy Tarreau87b09662015-04-03 00:22:06 +02001044/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001045 * and message.
1046 */
1047
Willy Tarreau87b09662015-04-03 00:22:06 +02001048struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +01001049{
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001050 if (s->be->errmsg[msgnum].str)
1051 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001052 else if (strm_fe(s)->errmsg[msgnum].str)
1053 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001054 else
1055 return &http_err_chunks[msgnum];
1056}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001058void
1059http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1060{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001061 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001062 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001063 stream_int_retnclose(&s->si[0], msg);
1064}
1065
Willy Tarreau53b6c742006-12-17 13:37:46 +01001066/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001067 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1068 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001069 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001070enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001071{
1072 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001074
1075 m = ((unsigned)*str - 'A');
1076
1077 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 for (h = http_methods[m]; h->len > 0; h++) {
1079 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001080 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001082 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001083 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001084 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001085 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001086}
1087
Willy Tarreau21d2af32008-02-14 20:25:24 +01001088/* Parse the URI from the given transaction (which is assumed to be in request
1089 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1090 * It is returned otherwise.
1091 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001092char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001093{
1094 char *ptr, *end;
1095
Willy Tarreau9b28e032012-10-12 23:49:43 +02001096 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001097 end = ptr + txn->req.sl.rq.u_l;
1098
1099 if (ptr >= end)
1100 return NULL;
1101
1102 /* RFC2616, par. 5.1.2 :
1103 * Request-URI = "*" | absuri | abspath | authority
1104 */
1105
1106 if (*ptr == '*')
1107 return NULL;
1108
1109 if (isalpha((unsigned char)*ptr)) {
1110 /* this is a scheme as described by RFC3986, par. 3.1 */
1111 ptr++;
1112 while (ptr < end &&
1113 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1114 ptr++;
1115 /* skip '://' */
1116 if (ptr == end || *ptr++ != ':')
1117 return NULL;
1118 if (ptr == end || *ptr++ != '/')
1119 return NULL;
1120 if (ptr == end || *ptr++ != '/')
1121 return NULL;
1122 }
1123 /* skip [user[:passwd]@]host[:[port]] */
1124
1125 while (ptr < end && *ptr != '/')
1126 ptr++;
1127
1128 if (ptr == end)
1129 return NULL;
1130
1131 /* OK, we got the '/' ! */
1132 return ptr;
1133}
1134
William Lallemand65ad6e12014-01-31 15:08:02 +01001135/* Parse the URI from the given string and look for the "/" beginning the PATH.
1136 * If not found, return NULL. It is returned otherwise.
1137 */
1138static char *
1139http_get_path_from_string(char *str)
1140{
1141 char *ptr = str;
1142
1143 /* RFC2616, par. 5.1.2 :
1144 * Request-URI = "*" | absuri | abspath | authority
1145 */
1146
1147 if (*ptr == '*')
1148 return NULL;
1149
1150 if (isalpha((unsigned char)*ptr)) {
1151 /* this is a scheme as described by RFC3986, par. 3.1 */
1152 ptr++;
1153 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1154 ptr++;
1155 /* skip '://' */
1156 if (*ptr == '\0' || *ptr++ != ':')
1157 return NULL;
1158 if (*ptr == '\0' || *ptr++ != '/')
1159 return NULL;
1160 if (*ptr == '\0' || *ptr++ != '/')
1161 return NULL;
1162 }
1163 /* skip [user[:passwd]@]host[:[port]] */
1164
1165 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1166 ptr++;
1167
1168 if (*ptr == '\0' || *ptr == ' ')
1169 return NULL;
1170
1171 /* OK, we got the '/' ! */
1172 return ptr;
1173}
1174
Willy Tarreau71241ab2012-12-27 11:30:54 +01001175/* Returns a 302 for a redirectable request that reaches a server working in
1176 * in redirect mode. This may only be called just after the stream interface
1177 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1178 * follow normal proxy processing. NOTE: this function is designed to support
1179 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001181void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001182{
1183 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001184 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001186 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187
1188 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001189 trash.len = strlen(HTTP_302);
1190 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001191
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001192 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001193
Willy Tarreauefb453c2008-10-26 20:49:47 +01001194 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001195 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001196 return;
1197
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001198 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001199 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001200 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1201 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001202 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001203
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001204 /* 3: add the request URI. Since it was already forwarded, we need
1205 * to temporarily rewind the buffer.
1206 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001207 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001208 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001209
Willy Tarreauefb453c2008-10-26 20:49:47 +01001210 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001211 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 +02001212
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001213 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001214
Willy Tarreauefb453c2008-10-26 20:49:47 +01001215 if (!path)
1216 return;
1217
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001218 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001219 return;
1220
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001221 memcpy(trash.str + trash.len, path, len);
1222 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001223
1224 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001225 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1226 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001227 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001228 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1229 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001230 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001231
1232 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001233 si_shutr(si);
1234 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001235 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001236 si->state = SI_ST_CLO;
1237
1238 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001239 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001240
1241 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001242 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001243 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001244}
1245
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001246/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001247 * that the server side is closed. Note that err_type is actually a
1248 * bitmask, where almost only aborts may be cumulated with other
1249 * values. We consider that aborted operations are more important
1250 * than timeouts or errors due to the fact that nobody else in the
1251 * logs might explain incomplete retries. All others should avoid
1252 * being cumulated. It should normally not be possible to have multiple
1253 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001254 * Note that connection errors appearing on the second request of a keep-alive
1255 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001256 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001257void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001258{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001259 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001260
1261 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001262 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001263 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001264 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001265 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001266 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001267 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001268 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001269 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001270 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001271 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001272 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001273 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001274 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001275 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001276 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001277 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001279 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1280 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001281 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001282 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001283 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001284 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001285 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001286 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001287 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001288 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001289}
1290
Willy Tarreau42250582007-04-01 01:30:43 +02001291extern const char sess_term_cond[8];
1292extern const char sess_fin_state[8];
1293extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001294struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001295struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001296struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001297struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298
Willy Tarreau117f59e2007-03-04 18:17:17 +01001299/*
1300 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001301 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001302 */
1303void capture_headers(char *som, struct hdr_idx *idx,
1304 char **cap, struct cap_hdr *cap_hdr)
1305{
1306 char *eol, *sol, *col, *sov;
1307 int cur_idx;
1308 struct cap_hdr *h;
1309 int len;
1310
1311 sol = som + hdr_idx_first_pos(idx);
1312 cur_idx = hdr_idx_first_idx(idx);
1313
1314 while (cur_idx) {
1315 eol = sol + idx->v[cur_idx].len;
1316
1317 col = sol;
1318 while (col < eol && *col != ':')
1319 col++;
1320
1321 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001322 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001323 sov++;
1324
1325 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001326 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001327 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1328 if (cap[h->index] == NULL)
1329 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001330 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001331
1332 if (cap[h->index] == NULL) {
1333 Alert("HTTP capture : out of memory.\n");
1334 continue;
1335 }
1336
1337 len = eol - sov;
1338 if (len > h->len)
1339 len = h->len;
1340
1341 memcpy(cap[h->index], sov, len);
1342 cap[h->index][len]=0;
1343 }
1344 }
1345 sol = eol + idx->v[cur_idx].cr + 1;
1346 cur_idx = idx->v[cur_idx].next;
1347 }
1348}
1349
1350
Willy Tarreau42250582007-04-01 01:30:43 +02001351/* either we find an LF at <ptr> or we jump to <bad>.
1352 */
1353#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1354
1355/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1356 * otherwise to <http_msg_ood> with <state> set to <st>.
1357 */
1358#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1359 ptr++; \
1360 if (likely(ptr < end)) \
1361 goto good; \
1362 else { \
1363 state = (st); \
1364 goto http_msg_ood; \
1365 } \
1366 } while (0)
1367
1368
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001370 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1372 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1373 * will give undefined results.
1374 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1375 * and that msg->sol points to the beginning of the response.
1376 * If a complete line is found (which implies that at least one CR or LF is
1377 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1378 * returned indicating an incomplete line (which does not mean that parts have
1379 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1380 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1381 * upon next call.
1382 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001383 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1385 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001386 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001388const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001389 enum ht_state state, const char *ptr, const char *end,
1390 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001391{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001392 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001393
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001396 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001397 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1399
1400 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001401 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001402 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1403 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001404 state = HTTP_MSG_ERROR;
1405 break;
1406
Willy Tarreau8973c702007-01-21 23:58:29 +01001407 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001410 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 goto http_msg_rpcode;
1412 }
1413 if (likely(HTTP_IS_SPHT(*ptr)))
1414 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1415 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001416 state = HTTP_MSG_ERROR;
1417 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001418
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001420 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 if (likely(!HTTP_IS_LWS(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1423
1424 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001425 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001426 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1427 }
1428
1429 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001430 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 http_msg_rsp_reason:
1432 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001433 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 msg->sl.st.r_l = 0;
1435 goto http_msg_rpline_eol;
1436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001440 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 goto http_msg_rpreason;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1445 /* so it's a CR/LF, so there is no reason phrase */
1446 goto http_msg_rsp_reason;
1447
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 if (likely(!HTTP_IS_CRLF(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001452 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 http_msg_rpline_eol:
1454 /* We have seen the end of line. Note that we do not
1455 * necessarily have the \n yet, but at least we know that we
1456 * have EITHER \r OR \n, otherwise the response would not be
1457 * complete. We can then record the response length and return
1458 * to the caller which will be able to register it.
1459 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001460 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 return ptr;
1462
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001464#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1466 exit(1);
1467#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001468 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 }
1470
1471 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001472 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (ret_state)
1474 *ret_state = state;
1475 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001476 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001478}
1479
Willy Tarreau8973c702007-01-21 23:58:29 +01001480/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 * This function parses a request line between <ptr> and <end>, starting with
1482 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1483 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1484 * will give undefined results.
1485 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1486 * and that msg->sol points to the beginning of the request.
1487 * If a complete line is found (which implies that at least one CR or LF is
1488 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1489 * returned indicating an incomplete line (which does not mean that parts have
1490 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1491 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1492 * upon next call.
1493 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001494 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1496 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001497 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001499const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001500 enum ht_state state, const char *ptr, const char *end,
1501 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001502{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001503 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(HTTP_IS_TOKEN(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001512 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (likely(HTTP_IS_CRLF(*ptr))) {
1517 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001518 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001520 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001522 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001524 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 msg->sl.rq.v_l = 0;
1526 goto http_msg_rqline_eol;
1527 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001528 state = HTTP_MSG_ERROR;
1529 break;
1530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001532 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001534 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 goto http_msg_rquri;
1536 }
1537 if (likely(HTTP_IS_SPHT(*ptr)))
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1539 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1540 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001543 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001544#if defined(__x86_64__) || \
1545 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1546 defined(__ARM_ARCH_7A__)
1547 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1548 while (ptr <= end - sizeof(int)) {
1549 int x = *(int *)ptr - 0x21212121;
1550 if (x & 0x80808080)
1551 break;
1552
1553 x -= 0x5e5e5e5e;
1554 if (!(x & 0x80808080))
1555 break;
1556
1557 ptr += sizeof(int);
1558 }
1559#endif
1560 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001561 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001562 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001565 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1567 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001569 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001570 /* non-ASCII chars are forbidden unless option
1571 * accept-invalid-http-request is enabled in the frontend.
1572 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001573 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001574 if (msg->err_pos < -1)
1575 goto invalid_char;
1576 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001577 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001578 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1579 }
1580
1581 if (likely(HTTP_IS_CRLF(*ptr))) {
1582 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1583 goto http_msg_req09_uri_e;
1584 }
1585
1586 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001587 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001588 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001589 state = HTTP_MSG_ERROR;
1590 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001595 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 goto http_msg_rqver;
1597 }
1598 if (likely(HTTP_IS_SPHT(*ptr)))
1599 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1600 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1601 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001605 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001607
1608 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001609 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001610 http_msg_rqline_eol:
1611 /* We have seen the end of line. Note that we do not
1612 * necessarily have the \n yet, but at least we know that we
1613 * have EITHER \r OR \n, otherwise the request would not be
1614 * complete. We can then record the request length and return
1615 * to the caller which will be able to register it.
1616 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001617 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001618 return ptr;
1619 }
1620
1621 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001622 state = HTTP_MSG_ERROR;
1623 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001626#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1628 exit(1);
1629#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001630 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001634 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (ret_state)
1636 *ret_state = state;
1637 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001638 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001642/*
1643 * Returns the data from Authorization header. Function may be called more
1644 * than once so data is stored in txn->auth_data. When no header is found
1645 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001646 * searching again for something we are unable to find anyway. However, if
1647 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001648 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001649 */
1650
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001651/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1652 * set according to global.tune.bufsize.
1653 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001654char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001655
1656int
Willy Tarreau87b09662015-04-03 00:22:06 +02001657get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001658{
1659
Willy Tarreaueee5b512015-04-03 23:46:31 +02001660 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001661 struct chunk auth_method;
1662 struct hdr_ctx ctx;
1663 char *h, *p;
1664 int len;
1665
1666#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001668#endif
1669
1670 if (txn->auth.method == HTTP_AUTH_WRONG)
1671 return 0;
1672
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001673 txn->auth.method = HTTP_AUTH_WRONG;
1674
1675 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001676
1677 if (txn->flags & TX_USE_PX_CONN) {
1678 h = "Proxy-Authorization";
1679 len = strlen(h);
1680 } else {
1681 h = "Authorization";
1682 len = strlen(h);
1683 }
1684
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001685 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001686 return 0;
1687
1688 h = ctx.line + ctx.val;
1689
1690 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001691 len = p - h;
1692 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001693 return 0;
1694
David Carlier7365f7d2016-04-04 11:54:42 +01001695 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1696 return 0;
1697
Willy Tarreau5c557d12016-03-13 08:17:02 +01001698 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001699
1700 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1701
1702 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001703 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704
1705 if (len < 0)
1706 return 0;
1707
1708
1709 get_http_auth_buff[len] = '\0';
1710
1711 p = strchr(get_http_auth_buff, ':');
1712
1713 if (!p)
1714 return 0;
1715
1716 txn->auth.user = get_http_auth_buff;
1717 *p = '\0';
1718 txn->auth.pass = p+1;
1719
1720 txn->auth.method = HTTP_AUTH_BASIC;
1721 return 1;
1722 }
1723
1724 return 0;
1725}
1726
Willy Tarreau58f10d72006-12-04 02:26:12 +01001727
Willy Tarreau8973c702007-01-21 23:58:29 +01001728/*
1729 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001730 * depending on the initial msg->msg_state. The caller is responsible for
1731 * ensuring that the message does not wrap. The function can be preempted
1732 * everywhere when data are missing and recalled at the exact same location
1733 * with no information loss. The message may even be realigned between two
1734 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001735 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001736 * fields. Note that msg->sol will be initialized after completing the first
1737 * state, so that none of the msg pointers has to be initialized prior to the
1738 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001739 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001740void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741{
Willy Tarreau3770f232013-12-07 00:01:53 +01001742 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001744 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001746 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001747 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001748 ptr = buf->p + msg->next;
1749 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 if (unlikely(ptr >= end))
1752 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001755 /*
1756 * First, states that are specific to the response only.
1757 * We check them first so that request and headers are
1758 * closer to each other (accessed more often).
1759 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001760 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001761 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001762 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001763 /* we have a start of message, but we have to check
1764 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001765 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001766 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 if (unlikely(ptr != buf->p)) {
1768 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001769 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001770 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001771 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 }
Willy Tarreau26927362012-05-18 23:22:52 +02001773 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001774 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001775 hdr_idx_init(idx);
1776 state = HTTP_MSG_RPVER;
1777 goto http_msg_rpver;
1778 }
1779
1780 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1781 goto http_msg_invalid;
1782
1783 if (unlikely(*ptr == '\n'))
1784 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1785 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1786 /* stop here */
1787
Willy Tarreau8973c702007-01-21 23:58:29 +01001788 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 EXPECT_LF_HERE(ptr, http_msg_invalid);
1791 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1792 /* stop here */
1793
Willy Tarreau8973c702007-01-21 23:58:29 +01001794 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001795 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 case HTTP_MSG_RPVER_SP:
1797 case HTTP_MSG_RPCODE:
1798 case HTTP_MSG_RPCODE_SP:
1799 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001800 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001801 state, ptr, end,
1802 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001803 if (unlikely(!ptr))
1804 return;
1805
1806 /* we have a full response and we know that we have either a CR
1807 * or an LF at <ptr>.
1808 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001809 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1810
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001811 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001812 if (likely(*ptr == '\r'))
1813 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1814 goto http_msg_rpline_end;
1815
Willy Tarreau8973c702007-01-21 23:58:29 +01001816 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001817 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001818 /* msg->sol must point to the first of CR or LF. */
1819 EXPECT_LF_HERE(ptr, http_msg_invalid);
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1821 /* stop here */
1822
1823 /*
1824 * Second, states that are specific to the request only
1825 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001827 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001829 /* we have a start of message, but we have to check
1830 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001831 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001832 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001833 if (likely(ptr != buf->p)) {
1834 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001835 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001836 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 }
Willy Tarreau26927362012-05-18 23:22:52 +02001839 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001840 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001841 state = HTTP_MSG_RQMETH;
1842 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1846 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001847
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 if (unlikely(*ptr == '\n'))
1849 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1850 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001851 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001853 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001855 EXPECT_LF_HERE(ptr, http_msg_invalid);
1856 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001857 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001860 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 case HTTP_MSG_RQMETH_SP:
1862 case HTTP_MSG_RQURI:
1863 case HTTP_MSG_RQURI_SP:
1864 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001865 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001866 state, ptr, end,
1867 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 if (unlikely(!ptr))
1869 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 /* we have a full request and we know that we have either a CR
1872 * or an LF at <ptr>.
1873 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001875
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001876 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 if (likely(*ptr == '\r'))
1878 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001882 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 /* check for HTTP/0.9 request : no version information available.
1884 * msg->sol must point to the first of CR or LF.
1885 */
1886 if (unlikely(msg->sl.rq.v_l == 0))
1887 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 EXPECT_LF_HERE(ptr, http_msg_invalid);
1890 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001891 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001892
Willy Tarreau8973c702007-01-21 23:58:29 +01001893 /*
1894 * Common states below
1895 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 if (likely(!HTTP_IS_CRLF(*ptr))) {
1900 goto http_msg_hdr_name;
1901 }
1902
1903 if (likely(*ptr == '\r'))
1904 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1905 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001908 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 /* assumes msg->sol points to the first char */
1910 if (likely(HTTP_IS_TOKEN(*ptr)))
1911 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001913 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001915
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001916 if (likely(msg->err_pos < -1) || *ptr == '\n')
1917 goto http_msg_invalid;
1918
1919 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001921
1922 /* and we still accept this non-token character */
1923 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001926 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001927 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 if (likely(HTTP_IS_SPHT(*ptr)))
1929 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001930
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001931 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001933
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001934 if (likely(!HTTP_IS_CRLF(*ptr))) {
1935 goto http_msg_hdr_val;
1936 }
1937
1938 if (likely(*ptr == '\r'))
1939 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1940 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001943 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 EXPECT_LF_HERE(ptr, http_msg_invalid);
1945 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001948 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 if (likely(HTTP_IS_SPHT(*ptr))) {
1950 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001951 for (; buf->p + msg->sov < ptr; msg->sov++)
1952 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 goto http_msg_hdr_l1_sp;
1954 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001955 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001956 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 goto http_msg_complete_header;
1958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001960 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001961 /* assumes msg->sol points to the first char, and msg->sov
1962 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001963 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001964
1965 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1966 * and lower. In fact since most of the time is spent in the loop, we
1967 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1968 * loop, but we don't care since they're very rare in header values.
1969 */
1970#if defined(__x86_64__)
1971 while (ptr <= end - sizeof(long)) {
1972 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1973 goto http_msg_hdr_val2;
1974 ptr += sizeof(long);
1975 }
1976#endif
1977#if defined(__x86_64__) || \
1978 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1979 defined(__ARM_ARCH_7A__)
1980 while (ptr <= end - sizeof(int)) {
1981 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1982 goto http_msg_hdr_val2;
1983 ptr += sizeof(int);
1984 }
1985#endif
1986 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001987 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001988 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001989
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 /* Note: we could also copy eol into ->eoh so that we have the
1992 * real header end in case it ends with lots of LWS, but is this
1993 * really needed ?
1994 */
1995 if (likely(*ptr == '\r'))
1996 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1997 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002000 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 EXPECT_LF_HERE(ptr, http_msg_invalid);
2002 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002005 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2007 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 for (; buf->p + msg->eol < ptr; msg->eol++)
2009 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002010 goto http_msg_hdr_val;
2011 }
2012 http_msg_complete_header:
2013 /*
2014 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002015 * Assumes msg->sol points to the first char, msg->sov points
2016 * to the first character of the value and msg->eol to the
2017 * first CR or LF so we know how the line ends. We insert last
2018 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002019 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002021 idx, idx->tail) < 0))
2022 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002023
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002024 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 if (likely(!HTTP_IS_CRLF(*ptr))) {
2026 goto http_msg_hdr_name;
2027 }
2028
2029 if (likely(*ptr == '\r'))
2030 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2031 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002034 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002035 /* Assumes msg->sol points to the first of either CR or LF.
2036 * Sets ->sov and ->next to the total header length, ->eoh to
2037 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002039 EXPECT_LF_HERE(ptr, http_msg_invalid);
2040 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002042 msg->eoh = msg->sol;
2043 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002044 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002045 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002047
2048 case HTTP_MSG_ERROR:
2049 /* this may only happen if we call http_msg_analyser() twice with an error */
2050 break;
2051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002052 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002053#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2055 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002056#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002057 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002058 }
2059 http_msg_ood:
2060 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002061 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002065 http_msg_invalid:
2066 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002067 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 return;
2070}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002071
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002072/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2073 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2074 * nothing is done and 1 is returned.
2075 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002076static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002077{
2078 int delta;
2079 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002080 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002081
2082 if (msg->sl.rq.v_l != 0)
2083 return 1;
2084
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002085 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2086 if (txn->meth != HTTP_METH_GET)
2087 return 0;
2088
Willy Tarreau9b28e032012-10-12 23:49:43 +02002089 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002090
2091 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002092 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2093 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002094 }
2095 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002096 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002097 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002098 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002099 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002100 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002101 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002102 NULL, NULL);
2103 if (unlikely(!cur_end))
2104 return 0;
2105
2106 /* we have a full HTTP/1.0 request now and we know that
2107 * we have either a CR or an LF at <ptr>.
2108 */
2109 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2110 return 1;
2111}
2112
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002113/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002114 * and "keep-alive" values. If we already know that some headers may safely
2115 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002116 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2117 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002118 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002119 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2120 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2121 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002122 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002123 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002124void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002125{
Willy Tarreau5b154472009-12-21 20:11:07 +01002126 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002127 const char *hdr_val = "Connection";
2128 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002129
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002130 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002131 return;
2132
Willy Tarreau88d349d2010-01-25 12:15:43 +01002133 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2134 hdr_val = "Proxy-Connection";
2135 hdr_len = 16;
2136 }
2137
Willy Tarreau5b154472009-12-21 20:11:07 +01002138 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002139 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002140 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002141 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2142 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002143 if (to_del & 2)
2144 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002145 else
2146 txn->flags |= TX_CON_KAL_SET;
2147 }
2148 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2149 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002150 if (to_del & 1)
2151 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002152 else
2153 txn->flags |= TX_CON_CLO_SET;
2154 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002155 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2156 txn->flags |= TX_HDR_CONN_UPG;
2157 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002158 }
2159
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002160 txn->flags |= TX_HDR_CONN_PRS;
2161 return;
2162}
Willy Tarreau5b154472009-12-21 20:11:07 +01002163
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002164/* Apply desired changes on the Connection: header. Values may be removed and/or
2165 * added depending on the <wanted> flags, which are exclusively composed of
2166 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2167 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2168 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002169void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002170{
2171 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002172 const char *hdr_val = "Connection";
2173 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002174
2175 ctx.idx = 0;
2176
Willy Tarreau88d349d2010-01-25 12:15:43 +01002177
2178 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2179 hdr_val = "Proxy-Connection";
2180 hdr_len = 16;
2181 }
2182
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002183 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002184 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002185 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2186 if (wanted & TX_CON_KAL_SET)
2187 txn->flags |= TX_CON_KAL_SET;
2188 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002189 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002190 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002191 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2192 if (wanted & TX_CON_CLO_SET)
2193 txn->flags |= TX_CON_CLO_SET;
2194 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002195 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002196 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002197 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002198
2199 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2200 return;
2201
2202 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2203 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002204 hdr_val = "Connection: close";
2205 hdr_len = 17;
2206 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2207 hdr_val = "Proxy-Connection: close";
2208 hdr_len = 23;
2209 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002210 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002211 }
2212
2213 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2214 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002215 hdr_val = "Connection: keep-alive";
2216 hdr_len = 22;
2217 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2218 hdr_val = "Proxy-Connection: keep-alive";
2219 hdr_len = 28;
2220 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002221 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002222 }
2223 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002224}
2225
Christopher Faulet113f7de2015-12-14 14:52:13 +01002226/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2227 * point to the first byte of data after the chunk size, so that we know we can
2228 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2229 * forming the chunk size. That way it is always possible to differentiate
2230 * between the start of the body and the start of the data. Return the number
2231 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2232 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002233 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002234static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002235{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002236 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002237 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002238 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 const char *end = buf->data + buf->size;
2240 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002241 unsigned int chunk = 0;
2242
2243 /* The chunk size is in the following form, though we are only
2244 * interested in the size and CRLF :
2245 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2246 */
2247 while (1) {
2248 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002249 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002250 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002251 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002252 if (c < 0) /* not a hex digit anymore */
2253 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002254 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002255 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002256 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002257 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002258 chunk = (chunk << 4) + c;
2259 }
2260
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002262 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002263 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264
Willy Tarreau2235b262016-11-05 15:50:20 +01002265 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002266 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002267 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002268 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002269 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002270 }
2271
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272 /* Up to there, we know that at least one byte is present at *ptr. Check
2273 * for the end of chunk size.
2274 */
2275 while (1) {
2276 if (likely(HTTP_IS_CRLF(*ptr))) {
2277 /* we now have a CR or an LF at ptr */
2278 if (likely(*ptr == '\r')) {
2279 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002280 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002281 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 return 0;
2283 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002284
Willy Tarreaud98cf932009-12-27 22:54:55 +01002285 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002286 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002288 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002289 /* done */
2290 break;
2291 }
2292 else if (*ptr == ';') {
2293 /* chunk extension, ends at next CRLF */
2294 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002295 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002296 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002297 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002298
2299 while (!HTTP_IS_CRLF(*ptr)) {
2300 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002301 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002302 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 return 0;
2304 }
2305 /* we have a CRLF now, loop above */
2306 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002307 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002309 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002310 }
2311
Christopher Faulet113f7de2015-12-14 14:52:13 +01002312 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2313 * or may not be present. We save the number of bytes parsed into
2314 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002315 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002316 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002317 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002318 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002319 msg->chunk_len = chunk;
2320 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002321 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002322 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002323 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002324 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002325}
2326
Christopher Faulet113f7de2015-12-14 14:52:13 +01002327/* This function skips trailers in the buffer associated with HTTP message
2328 * <msg>. The first visited position is msg->next. If the end of the trailers is
2329 * found, the function returns >0. So, the caller can automatically schedul it
2330 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2331 * data are available, the function does not change anything except maybe
2332 * msg->sol if it could parse some lines, and returns zero. If a parse error
2333 * is encountered, the function returns < 0 and does not change anything except
2334 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2335 * state before calling this function, which implies that all non-trailers data
2336 * have already been scheduled for forwarding, and that msg->next exactly
2337 * matches the length of trailers already parsed and not forwarded. It is also
2338 * important to note that this function is designed to be able to parse wrapped
2339 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002340 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002341static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002342{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002344
Christopher Faulet113f7de2015-12-14 14:52:13 +01002345 /* we have msg->next which points to next line. Look for CRLF. But
2346 * first, we reset msg->sol */
2347 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002348 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002349 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002350 const char *start = b_ptr(buf, msg->next + msg->sol);
2351 const char *stop = bi_end(buf);
2352 const char *ptr = start;
2353 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002354
2355 /* scan current line and stop at LF or CRLF */
2356 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002357 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002358 return 0;
2359
2360 if (*ptr == '\n') {
2361 if (!p1)
2362 p1 = ptr;
2363 p2 = ptr;
2364 break;
2365 }
2366
2367 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002368 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002369 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002370 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002371 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 p1 = ptr;
2373 }
2374
2375 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002376 if (ptr >= buf->data + buf->size)
2377 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002378 }
2379
2380 /* after LF; point to beginning of next line */
2381 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002382 if (p2 >= buf->data + buf->size)
2383 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002384
Christopher Faulet2fb28802015-12-01 10:40:57 +01002385 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002386 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002387 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002388 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002389
Christopher Fauletd7c91962015-04-30 11:48:27 +02002390 /* LF/CRLF at beginning of line => end of trailers at p2.
2391 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002392 * from this message. */
2393 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002394 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002395
Willy Tarreaud98cf932009-12-27 22:54:55 +01002396 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002397 }
2398}
2399
Christopher Faulet113f7de2015-12-14 14:52:13 +01002400/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2401 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2402 * in order to include this part into the next forwarding phase. Note that the
2403 * caller must ensure that ->p points to the first byte to parse. It returns
2404 * the number of bytes parsed on success, so the caller can set msg_state to
2405 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2406 * change anything and returns zero. If a parse error is encountered, the
2407 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2408 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002409 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002410static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002411{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002412 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002413 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002414 int bytes;
2415
2416 /* NB: we'll check data availabilty at the end. It's not a
2417 * problem because whatever we match first will be checked
2418 * against the correct length.
2419 */
2420 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002421 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002422 if (*ptr == '\r') {
2423 bytes++;
2424 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002425 if (ptr >= buf->data + buf->size)
2426 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002427 }
2428
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002429 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002430 return 0;
2431
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002432 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002433 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002434 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002435 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002436 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002437}
Willy Tarreau5b154472009-12-21 20:11:07 +01002438
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002439/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2440 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2441 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2442 * Unparsable qvalues return 1000 as "q=1.000".
2443 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002444int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002445{
2446 int q = 1000;
2447
Willy Tarreau506c69a2014-07-08 00:59:48 +02002448 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002449 goto out;
2450 q = (*qvalue++ - '0') * 1000;
2451
2452 if (*qvalue++ != '.')
2453 goto out;
2454
Willy Tarreau506c69a2014-07-08 00:59:48 +02002455 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002456 goto out;
2457 q += (*qvalue++ - '0') * 100;
2458
Willy Tarreau506c69a2014-07-08 00:59:48 +02002459 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002460 goto out;
2461 q += (*qvalue++ - '0') * 10;
2462
Willy Tarreau506c69a2014-07-08 00:59:48 +02002463 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002464 goto out;
2465 q += (*qvalue++ - '0') * 1;
2466 out:
2467 if (q > 1000)
2468 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002469 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002470 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002471 return q;
2472}
William Lallemand82fe75c2012-10-23 10:25:10 +02002473
Willy Tarreau87b09662015-04-03 00:22:06 +02002474void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002475{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002476 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002477 int tmp = TX_CON_WANT_KAL;
2478
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002479 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2480 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2482 tmp = TX_CON_WANT_TUN;
2483
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002484 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2486 tmp = TX_CON_WANT_TUN;
2487 }
2488
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2491 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002492 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002493 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2494 tmp = TX_CON_WANT_CLO;
2495 else
2496 tmp = TX_CON_WANT_SCL;
2497 }
2498
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002499 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002500 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2501 tmp = TX_CON_WANT_CLO;
2502
2503 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2504 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2505
2506 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2507 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2508 /* parse the Connection header and possibly clean it */
2509 int to_del = 0;
2510 if ((msg->flags & HTTP_MSGF_VER_11) ||
2511 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002512 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002513 to_del |= 2; /* remove "keep-alive" */
2514 if (!(msg->flags & HTTP_MSGF_VER_11))
2515 to_del |= 1; /* remove "close" */
2516 http_parse_connection_header(txn, msg, to_del);
2517 }
2518
2519 /* check if client or config asks for explicit close in KAL/SCL */
2520 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2521 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2522 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2523 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2524 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002525 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2527}
William Lallemand82fe75c2012-10-23 10:25:10 +02002528
Willy Tarreaud787e662009-07-07 10:14:51 +02002529/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2530 * processing can continue on next analysers, or zero if it either needs more
2531 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002532 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002533 * when it has nothing left to do, and may remove any analyser when it wants to
2534 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002535 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002536int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002537{
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 /*
2539 * We will parse the partial (or complete) lines.
2540 * We will check the request syntax, and also join multi-line
2541 * headers. An index of all the lines will be elaborated while
2542 * parsing.
2543 *
2544 * For the parsing, we use a 28 states FSM.
2545 *
2546 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002547 * req->buf->p = beginning of request
2548 * req->buf->p + msg->eoh = end of processed headers / start of current one
2549 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002550 * msg->eol = end of current header or line (LF or CRLF)
2551 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002552 *
2553 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002554 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2556 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002560 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002561 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002563 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002564
Willy Tarreau87b09662015-04-03 00:22:06 +02002565 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002566 now_ms, __FUNCTION__,
2567 s,
2568 req,
2569 req->rex, req->wex,
2570 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002571 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002572 req->analysers);
2573
Willy Tarreau52a0c602009-08-16 22:45:38 +02002574 /* we're speaking HTTP here, so let's speak HTTP to the client */
2575 s->srv_error = http_return_srv_error;
2576
Willy Tarreau83e3af02009-12-28 17:39:57 +01002577 /* There's a protected area at the end of the buffer for rewriting
2578 * purposes. We don't want to start to parse the request if the
2579 * protected area is affected, because we may have to move processed
2580 * data later, which is much more complicated.
2581 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002582 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002583
2584 /* This point is executed when some data is avalaible for analysis,
2585 * so we log the end of the idle time. */
2586 if (s->logs.t_idle == -1)
2587 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2588
Willy Tarreau379357a2013-06-08 12:55:46 +02002589 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002590 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002591 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002592 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002593 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002594 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002596 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002597 return 0;
2598 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002599 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2600 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2601 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002602 }
2603
Willy Tarreau065e8332010-01-08 00:30:20 +01002604 /* Note that we have the same problem with the response ; we
2605 * may want to send a redirect, error or anything which requires
2606 * some spare space. So we'll ensure that we have at least
2607 * maxrewrite bytes available in the response buffer before
2608 * processing that one. This will only affect pipelined
2609 * keep-alive requests.
2610 */
2611 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002612 unlikely(!channel_is_rewritable(&s->res) ||
2613 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2614 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2615 if (s->res.buf->o) {
2616 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002617 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002618 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002619 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002620 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2621 s->res.flags |= CF_WAKE_WRITE;
2622 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002623 return 0;
2624 }
2625 }
2626
Willy Tarreau9b28e032012-10-12 23:49:43 +02002627 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002628 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002629 }
2630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 /* 1: we might have to print this header in debug mode */
2632 if (unlikely((global.mode & MODE_DEBUG) &&
2633 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002634 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002636
Willy Tarreau9b28e032012-10-12 23:49:43 +02002637 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002638 /* this is a bit complex : in case of error on the request line,
2639 * we know that rq.l is still zero, so we display only the part
2640 * up to the end of the line (truncated by debug_hdr).
2641 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002642 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 sol += hdr_idx_first_pos(&txn->hdr_idx);
2646 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 while (cur_idx) {
2649 eol = sol + txn->hdr_idx.v[cur_idx].len;
2650 debug_hdr("clihdr", s, sol, eol);
2651 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2652 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002653 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 }
2655
Willy Tarreau58f10d72006-12-04 02:26:12 +01002656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /*
2658 * Now we quickly check if we have found a full valid request.
2659 * If not so, we check the FD and buffer states before leaving.
2660 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002661 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002662 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002663 * on a keep-alive stream, if we encounter and error, close, t/o,
2664 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002665 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002666 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002667 * Last, we may increase some tracked counters' http request errors on
2668 * the cases that are deliberately the client's fault. For instance,
2669 * a timeout or connection reset is not counted as an error. However
2670 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002672
Willy Tarreau655dce92009-11-08 13:10:58 +01002673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002674 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002678 stream_inc_http_req_ctr(s);
2679 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002680 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002682 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 /* 1: Since we are in header mode, if there's no space
2685 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002686 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 * must terminate it now.
2688 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002689 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* FIXME: check if URI is set and return Status
2691 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002692 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002693 stream_inc_http_req_ctr(s);
2694 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002695 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002696 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002697 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 goto return_bad_req;
2699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002702 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002703 if (!(s->flags & SF_ERR_MASK))
2704 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002705
Willy Tarreaufcffa692010-01-10 14:21:19 +01002706 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002707 goto failed_keep_alive;
2708
Willy Tarreau0f228a02015-05-01 15:37:53 +02002709 if (sess->fe->options & PR_O_IGNORE_PRB)
2710 goto failed_keep_alive;
2711
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002713 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002714 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 }
2717
Willy Tarreaudc979f22012-12-04 10:39:01 +01002718 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002720 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002721 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002722 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002723 proxy_inc_fe_req_ctr(sess->fe);
2724 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002725 if (sess->listener->counters)
2726 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002727
Willy Tarreaue7dff022015-04-03 01:14:29 +02002728 if (!(s->flags & SF_FINST_MASK))
2729 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 return 0;
2731 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002734 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002735 if (!(s->flags & SF_ERR_MASK))
2736 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002737
Willy Tarreaufcffa692010-01-10 14:21:19 +01002738 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002739 goto failed_keep_alive;
2740
Willy Tarreau0f228a02015-05-01 15:37:53 +02002741 if (sess->fe->options & PR_O_IGNORE_PRB)
2742 goto failed_keep_alive;
2743
Willy Tarreau59234e92008-11-30 23:51:27 +01002744 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002745 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002746 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002747 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002748 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002751 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002752 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreau87b09662015-04-03 00:22:06 +02002754 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002755 proxy_inc_fe_req_ctr(sess->fe);
2756 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002757 if (sess->listener->counters)
2758 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002759
Willy Tarreaue7dff022015-04-03 01:14:29 +02002760 if (!(s->flags & SF_FINST_MASK))
2761 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 return 0;
2763 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002764
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002766 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002767 if (!(s->flags & SF_ERR_MASK))
2768 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002769
Willy Tarreaufcffa692010-01-10 14:21:19 +01002770 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002771 goto failed_keep_alive;
2772
Willy Tarreau0f228a02015-05-01 15:37:53 +02002773 if (sess->fe->options & PR_O_IGNORE_PRB)
2774 goto failed_keep_alive;
2775
Willy Tarreau4076a152009-04-02 15:18:36 +02002776 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002777 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002780 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002781 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002782 stream_inc_http_err_ctr(s);
2783 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002784 proxy_inc_fe_req_ctr(sess->fe);
2785 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002786 if (sess->listener->counters)
2787 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002788
Willy Tarreaue7dff022015-04-03 01:14:29 +02002789 if (!(s->flags & SF_FINST_MASK))
2790 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002791 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002792 }
2793
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002794 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002795 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002796 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002797#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002798 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2799 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002800 /* We need more data, we have to re-enable quick-ack in case we
2801 * previously disabled it, otherwise we might cause the client
2802 * to delay next data.
2803 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002804 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002805 }
2806#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002807
Willy Tarreaufcffa692010-01-10 14:21:19 +01002808 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2809 /* If the client starts to talk, let's fall back to
2810 * request timeout processing.
2811 */
2812 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002813 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002814 }
2815
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002817 if (!tick_isset(req->analyse_exp)) {
2818 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2819 (txn->flags & TX_WAIT_NEXT_RQ) &&
2820 tick_isset(s->be->timeout.httpka))
2821 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2822 else
2823 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002825
Willy Tarreau59234e92008-11-30 23:51:27 +01002826 /* we're not ready yet */
2827 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002828
2829 failed_keep_alive:
2830 /* Here we process low-level errors for keep-alive requests. In
2831 * short, if the request is not the first one and it experiences
2832 * a timeout, read error or shutdown, we just silently close so
2833 * that the client can try again.
2834 */
2835 txn->status = 0;
2836 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002837 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002838 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002839 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002840 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002841 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002842 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002844
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 /* OK now we have a complete HTTP request with indexed headers. Let's
2846 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002847 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002848 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002849 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002850 * byte after the last LF. msg->sov points to the first byte of data.
2851 * msg->eol cannot be trusted because it may have been left uninitialized
2852 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002854
Willy Tarreau87b09662015-04-03 00:22:06 +02002855 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002856 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002857
Willy Tarreaub16a5742010-01-10 14:46:16 +01002858 if (txn->flags & TX_WAIT_NEXT_RQ) {
2859 /* kill the pending keep-alive timeout */
2860 txn->flags &= ~TX_WAIT_NEXT_RQ;
2861 req->analyse_exp = TICK_ETERNITY;
2862 }
2863
2864
Willy Tarreaud787e662009-07-07 10:14:51 +02002865 /* Maybe we found in invalid header name while we were configured not
2866 * to block on that, so we have to capture it now.
2867 */
2868 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002869 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002870
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 /*
2872 * 1: identify the method
2873 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002874 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002875
2876 /* we can make use of server redirect on GET and HEAD */
2877 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002878 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002879
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 /*
2881 * 2: check if the URI matches the monitor_uri.
2882 * We have to do this for every request which gets in, because
2883 * the monitor-uri is defined by the frontend.
2884 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002885 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2886 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002887 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002888 sess->fe->monitor_uri,
2889 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002890 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002891 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002892 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002893 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002894
Willy Tarreaue7dff022015-04-03 01:14:29 +02002895 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002897
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002899 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002900 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002901
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 ret = acl_pass(ret);
2903 if (cond->pol == ACL_COND_UNLESS)
2904 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002905
Willy Tarreau59234e92008-11-30 23:51:27 +01002906 if (ret) {
2907 /* we fail this request, let's return 503 service unavail */
2908 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002909 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002910 if (!(s->flags & SF_ERR_MASK))
2911 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002913 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002914 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002915
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 /* nothing to fail, let's reply normaly */
2917 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002918 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002919 if (!(s->flags & SF_ERR_MASK))
2920 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 goto return_prx_cond;
2922 }
2923
2924 /*
2925 * 3: Maybe we have to copy the original REQURI for the logs ?
2926 * Note: we cannot log anymore if the request has been
2927 * classified as invalid.
2928 */
2929 if (unlikely(s->logs.logwait & LW_REQ)) {
2930 /* we have a complete HTTP request that we must log */
2931 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2932 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002933
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 if (urilen >= REQURI_LEN)
2935 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002936 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002938
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002939 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 s->do_log(s);
2941 } else {
2942 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002943 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 }
Willy Tarreau06619262006-12-17 08:37:22 +01002945
Willy Tarreau91852eb2015-05-01 13:26:00 +02002946 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2947 * exactly one digit "." one digit. This check may be disabled using
2948 * option accept-invalid-http-request.
2949 */
2950 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2951 if (msg->sl.rq.v_l != 8) {
2952 msg->err_pos = msg->sl.rq.v;
2953 goto return_bad_req;
2954 }
2955
2956 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2957 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2958 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2959 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2960 msg->err_pos = msg->sl.rq.v + 4;
2961 goto return_bad_req;
2962 }
2963 }
Willy Tarreau13317662015-05-01 13:47:08 +02002964 else {
2965 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2966 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2967 goto return_bad_req;
2968 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002969
Willy Tarreau5b154472009-12-21 20:11:07 +01002970 /* ... and check if the request is HTTP/1.1 or above */
2971 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002972 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2973 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2974 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002975 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002976
2977 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002978 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 +01002979
Willy Tarreau88d349d2010-01-25 12:15:43 +01002980 /* if the frontend has "option http-use-proxy-header", we'll check if
2981 * we have what looks like a proxied connection instead of a connection,
2982 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2983 * Note that this is *not* RFC-compliant, however browsers and proxies
2984 * happen to do that despite being non-standard :-(
2985 * We consider that a request not beginning with either '/' or '*' is
2986 * a proxied connection, which covers both "scheme://location" and
2987 * CONNECT ip:port.
2988 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002989 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002990 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002991 txn->flags |= TX_USE_PX_CONN;
2992
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002994 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002997 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002998 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002999 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003000
Willy Tarreau557f1992015-05-01 10:05:17 +02003001 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3002 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003003 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003004 * The length of a message body is determined by one of the following
3005 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003007 * 1. Any response to a HEAD request and any response with a 1xx
3008 * (Informational), 204 (No Content), or 304 (Not Modified) status
3009 * code is always terminated by the first empty line after the
3010 * header fields, regardless of the header fields present in the
3011 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003013 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3014 * the connection will become a tunnel immediately after the empty
3015 * line that concludes the header fields. A client MUST ignore any
3016 * Content-Length or Transfer-Encoding header fields received in
3017 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003018 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003019 * 3. If a Transfer-Encoding header field is present and the chunked
3020 * transfer coding (Section 4.1) is the final encoding, the message
3021 * body length is determined by reading and decoding the chunked
3022 * data until the transfer coding indicates the data is complete.
3023 *
3024 * If a Transfer-Encoding header field is present in a response and
3025 * the chunked transfer coding is not the final encoding, the
3026 * message body length is determined by reading the connection until
3027 * it is closed by the server. If a Transfer-Encoding header field
3028 * is present in a request and the chunked transfer coding is not
3029 * the final encoding, the message body length cannot be determined
3030 * reliably; the server MUST respond with the 400 (Bad Request)
3031 * status code and then close the connection.
3032 *
3033 * If a message is received with both a Transfer-Encoding and a
3034 * Content-Length header field, the Transfer-Encoding overrides the
3035 * Content-Length. Such a message might indicate an attempt to
3036 * perform request smuggling (Section 9.5) or response splitting
3037 * (Section 9.4) and ought to be handled as an error. A sender MUST
3038 * remove the received Content-Length field prior to forwarding such
3039 * a message downstream.
3040 *
3041 * 4. If a message is received without Transfer-Encoding and with
3042 * either multiple Content-Length header fields having differing
3043 * field-values or a single Content-Length header field having an
3044 * invalid value, then the message framing is invalid and the
3045 * recipient MUST treat it as an unrecoverable error. If this is a
3046 * request message, the server MUST respond with a 400 (Bad Request)
3047 * status code and then close the connection. If this is a response
3048 * message received by a proxy, the proxy MUST close the connection
3049 * to the server, discard the received response, and send a 502 (Bad
3050 * Gateway) response to the client. If this is a response message
3051 * received by a user agent, the user agent MUST close the
3052 * connection to the server and discard the received response.
3053 *
3054 * 5. If a valid Content-Length header field is present without
3055 * Transfer-Encoding, its decimal value defines the expected message
3056 * body length in octets. If the sender closes the connection or
3057 * the recipient times out before the indicated number of octets are
3058 * received, the recipient MUST consider the message to be
3059 * incomplete and close the connection.
3060 *
3061 * 6. If this is a request message and none of the above are true, then
3062 * the message body length is zero (no message body is present).
3063 *
3064 * 7. Otherwise, this is a response message without a declared message
3065 * body length, so the message body length is determined by the
3066 * number of octets received prior to the server closing the
3067 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003068 */
3069
Willy Tarreau32b47f42009-10-18 20:55:02 +02003070 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003071 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003072 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003073 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003074 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3075 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003076 /* chunked not last, return badreq */
3077 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003078 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003079 }
3080
Willy Tarreau1c913912015-04-30 10:57:51 +02003081 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003082 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003083 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3084 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3085 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3086 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003087 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003088 signed long long cl;
3089
Willy Tarreauad14f752011-09-02 20:33:27 +02003090 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003091 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003092 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003093 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003094
Willy Tarreauad14f752011-09-02 20:33:27 +02003095 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003096 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099
Willy Tarreauad14f752011-09-02 20:33:27 +02003100 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003101 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003105 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003106 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003108 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003110 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003111 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003112 }
3113
Willy Tarreau34dfc602015-05-01 10:09:49 +02003114 /* even bodyless requests have a known length */
3115 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003116
Willy Tarreau179085c2014-04-28 16:48:56 +02003117 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3118 * only change if both the request and the config reference something else.
3119 * Option httpclose by itself sets tunnel mode where headers are mangled.
3120 * However, if another mode is set, it will affect it (eg: server-close/
3121 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3122 * if FE and BE have the same settings (common). The method consists in
3123 * checking if options changed between the two calls (implying that either
3124 * one is non-null, or one of them is non-null and we are there for the first
3125 * time.
3126 */
3127 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003128 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003129 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003130
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003131 /* we may have to wait for the request's body */
3132 if ((s->be->options & PR_O_WREQ_BODY) &&
3133 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3134 req->analysers |= AN_REQ_HTTP_BODY;
3135
Willy Tarreaud787e662009-07-07 10:14:51 +02003136 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003137 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003138 req->analyse_exp = TICK_ETERNITY;
3139 return 1;
3140
3141 return_bad_req:
3142 /* We centralize bad requests processing here */
3143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3144 /* we detected a parsing error. We want to archive this request
3145 * in the dedicated proxy area for later troubleshooting.
3146 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003147 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003148 }
3149
3150 txn->req.msg_state = HTTP_MSG_ERROR;
3151 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003152 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003154 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003155 if (sess->listener->counters)
3156 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003157
3158 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003159 if (!(s->flags & SF_ERR_MASK))
3160 s->flags |= SF_ERR_PRXCOND;
3161 if (!(s->flags & SF_FINST_MASK))
3162 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003163
Christopher Fauletd7c91962015-04-30 11:48:27 +02003164 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003165 req->analyse_exp = TICK_ETERNITY;
3166 return 0;
3167}
3168
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003169
Willy Tarreau347a35d2013-11-22 17:51:09 +01003170/* This function prepares an applet to handle the stats. It can deal with the
3171 * "100-continue" expectation, check that admin rules are met for POST requests,
3172 * and program a response message if something was unexpected. It cannot fail
3173 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003176 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003177 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003178int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179{
3180 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003181 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003182 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003183 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003185 struct uri_auth *uri_auth = s->be->uri_auth;
3186 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003187 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 appctx = si_appctx(si);
3190 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3191 appctx->st1 = appctx->st2 = 0;
3192 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3193 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003194 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003195 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003196
3197 uri = msg->chn->buf->p + msg->sl.rq.u;
3198 lookup = uri + uri_auth->uri_len;
3199
3200 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3201 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003202 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003203 break;
3204 }
3205 }
3206
3207 if (uri_auth->refresh) {
3208 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3209 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003211 break;
3212 }
3213 }
3214 }
3215
3216 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3217 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003218 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003219 break;
3220 }
3221 }
3222
Willy Tarreau1e62df92016-01-11 18:57:53 +01003223 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3224 if (memcmp(h, ";typed", 6) == 0) {
3225 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3226 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3227 break;
3228 }
3229 }
3230
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003231 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3232 if (memcmp(h, ";st=", 4) == 0) {
3233 int i;
3234 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003235 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003236 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3237 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003239 break;
3240 }
3241 }
3242 break;
3243 }
3244 }
3245
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003246 appctx->ctx.stats.scope_str = 0;
3247 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003248 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3249 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3250 int itx = 0;
3251 const char *h2;
3252 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3253 const char *err;
3254
3255 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3256 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003257 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003258 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3259 itx++;
3260 h++;
3261 }
3262
3263 if (itx > STAT_SCOPE_TXT_MAXLEN)
3264 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003266
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003267 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003268 memcpy(scope_txt, h2, itx);
3269 scope_txt[itx] = '\0';
3270 err = invalid_char(scope_txt);
3271 if (err) {
3272 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003273 appctx->ctx.stats.scope_str = 0;
3274 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275 }
3276 break;
3277 }
3278 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003279
3280 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003281 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003282 int ret = 1;
3283
3284 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003285 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003286 ret = acl_pass(ret);
3287 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3288 ret = !ret;
3289 }
3290
3291 if (ret) {
3292 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003294 break;
3295 }
3296 }
3297
3298 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003299 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003300 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003301 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003302 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3303 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003305 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003306 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003307 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3308 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003309 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003310 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003311 else {
3312 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003313 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003314 }
3315
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003316 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003317 return 1;
3318}
3319
Lukas Tribus67db8df2013-06-23 17:37:13 +02003320/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3321 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3322 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003323void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003324{
3325#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003326 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003327 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3328#endif
3329#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003330 if (from->ss_family == AF_INET6) {
3331 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003332 /* v4-mapped addresses need IP_TOS */
3333 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3334 else
3335 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3336 }
3337#endif
3338}
3339
Willy Tarreau87b09662015-04-03 00:22:06 +02003340int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003341 const char* name, unsigned int name_len,
3342 const char *str, struct my_regex *re,
3343 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003344{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003345 struct hdr_ctx ctx;
3346 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003347 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003348 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3349 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003350 struct chunk *output = get_trash_chunk();
3351
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003352 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003353
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003354 /* Choose the header browsing function. */
3355 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003356 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003357 http_find_hdr_func = http_find_header2;
3358 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003359 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003360 http_find_hdr_func = http_find_full_header2;
3361 break;
3362 default: /* impossible */
3363 return -1;
3364 }
3365
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003366 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3367 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003368 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003369 char *val = ctx.line + ctx.val;
3370 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003371
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003372 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3373 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003374
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003375 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003376 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003377 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003378
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003379 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003380
3381 hdr->len += delta;
3382 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003383
3384 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003385 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003386 }
3387
3388 return 0;
3389}
3390
Willy Tarreau87b09662015-04-03 00:22:06 +02003391static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003392 const char* name, unsigned int name_len,
3393 struct list *fmt, struct my_regex *re,
3394 int action)
3395{
3396 struct chunk *replace = get_trash_chunk();
3397
3398 replace->len = build_logline(s, replace->str, replace->size, fmt);
3399 if (replace->len >= replace->size - 1)
3400 return -1;
3401
3402 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3403}
3404
Willy Tarreau87b09662015-04-03 00:22:06 +02003405/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003406 * transaction <txn>. Returns the verdict of the first rule that prevents
3407 * further processing of the request (auth, deny, ...), and defaults to
3408 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3409 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003410 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3411 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3412 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003413 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003414enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003415http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003416{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003417 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003418 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003419 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003420 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003421 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003422 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003423 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003424 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003425
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003426 /* If "the current_rule_list" match the executed rule list, we are in
3427 * resume condition. If a resume is needed it is always in the action
3428 * and never in the ACL or converters. In this case, we initialise the
3429 * current rule, and go to the action execution point.
3430 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003431 if (s->current_rule) {
3432 rule = s->current_rule;
3433 s->current_rule = NULL;
3434 if (s->current_rule_list == rules)
3435 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003436 }
3437 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003438
Willy Tarreauff011f22011-01-06 17:51:27 +01003439 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003440
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003442 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003443 int ret;
3444
Willy Tarreau192252e2015-04-04 01:47:55 +02003445 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003446 ret = acl_pass(ret);
3447
Willy Tarreauff011f22011-01-06 17:51:27 +01003448 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003449 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003450
3451 if (!ret) /* condition not matched */
3452 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003453 }
3454
Willy Tarreauacc98002015-09-27 23:34:39 +02003455 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003456resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003457 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003458 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003459 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003460
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003461 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003462 if (deny_status)
3463 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003464 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003466 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003467 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003468 if (deny_status)
3469 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003470 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003471
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003472 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003473 /* Auth might be performed on regular http-req rules as well as on stats */
3474 auth_realm = rule->arg.auth.realm;
3475 if (!auth_realm) {
3476 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3477 auth_realm = STATS_DEFAULT_REALM;
3478 else
3479 auth_realm = px->id;
3480 }
3481 /* send 401/407 depending on whether we use a proxy or not. We still
3482 * count one error, because normal browsing won't significantly
3483 * increase the counter but brute force attempts will.
3484 */
3485 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3486 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003487 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003488 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003490
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003491 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003492 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3493 return HTTP_RULE_RES_BADREQ;
3494 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003495
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003496 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003497 s->task->nice = rule->arg.nice;
3498 break;
3499
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003500 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003501 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003502 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003503 break;
3504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003505 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003506#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003507 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003508 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003509#endif
3510 break;
3511
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003512 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003513 s->logs.level = rule->arg.loglevel;
3514 break;
3515
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003516 case ACT_HTTP_REPLACE_HDR:
3517 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003518 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3519 rule->arg.hdr_add.name_len,
3520 &rule->arg.hdr_add.fmt,
3521 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003522 return HTTP_RULE_RES_BADREQ;
3523 break;
3524
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003525 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526 ctx.idx = 0;
3527 /* remove all occurrences of the header */
3528 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3529 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3530 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003531 }
Willy Tarreau85603282015-01-21 20:39:27 +01003532 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_SET_HDR:
3535 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003536 /* The scope of the trash buffer must be limited to this function. The
3537 * build_logline() function can execute a lot of other function which
3538 * can use the trash buffer. So for limiting the scope of this global
3539 * buffer, we build first the header value using build_logline, and
3540 * after we store the header name.
3541 */
3542 len = rule->arg.hdr_add.name_len + 2,
3543 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003544 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003545 trash.str[rule->arg.hdr_add.name_len] = ':';
3546 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3547 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003549 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003550 /* remove all occurrences of the header */
3551 ctx.idx = 0;
3552 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3553 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3554 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3555 }
3556 }
3557
Willy Tarreau96257ec2012-12-27 10:46:37 +01003558 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3559 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003561 case ACT_HTTP_DEL_ACL:
3562 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003563 struct pat_ref *ref;
3564 char *key;
3565 int len;
3566
3567 /* collect reference */
3568 ref = pat_ref_lookup(rule->arg.map.ref);
3569 if (!ref)
3570 continue;
3571
3572 /* collect key */
3573 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3574 key = trash.str;
3575 key[len] = '\0';
3576
3577 /* perform update */
3578 /* returned code: 1=ok, 0=ko */
3579 pat_ref_delete(ref, key);
3580
3581 break;
3582 }
3583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003584 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003585 struct pat_ref *ref;
3586 char *key;
3587 struct chunk *trash_key;
3588 int len;
3589
3590 trash_key = get_trash_chunk();
3591
3592 /* collect reference */
3593 ref = pat_ref_lookup(rule->arg.map.ref);
3594 if (!ref)
3595 continue;
3596
3597 /* collect key */
3598 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3599 key = trash_key->str;
3600 key[len] = '\0';
3601
3602 /* perform update */
3603 /* add entry only if it does not already exist */
3604 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003605 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003606
3607 break;
3608 }
3609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003611 struct pat_ref *ref;
3612 char *key, *value;
3613 struct chunk *trash_key, *trash_value;
3614 int len;
3615
3616 trash_key = get_trash_chunk();
3617 trash_value = get_trash_chunk();
3618
3619 /* collect reference */
3620 ref = pat_ref_lookup(rule->arg.map.ref);
3621 if (!ref)
3622 continue;
3623
3624 /* collect key */
3625 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3626 key = trash_key->str;
3627 key[len] = '\0';
3628
3629 /* collect value */
3630 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3631 value = trash_value->str;
3632 value[len] = '\0';
3633
3634 /* perform update */
3635 if (pat_ref_find_elt(ref, key) != NULL)
3636 /* update entry if it exists */
3637 pat_ref_set(ref, key, value, NULL);
3638 else
3639 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003640 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003641
3642 break;
3643 }
William Lallemand73025dd2014-04-24 14:38:37 +02003644
Thierry FOURNIER42148732015-09-02 17:17:33 +02003645 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003646 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3647 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003648
Willy Tarreauacc98002015-09-27 23:34:39 +02003649 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003650 case ACT_RET_ERR:
3651 case ACT_RET_CONT:
3652 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003653 case ACT_RET_STOP:
3654 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003655 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003656 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003657 return HTTP_RULE_RES_YIELD;
3658 }
William Lallemand73025dd2014-04-24 14:38:37 +02003659 break;
3660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003661 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003662 /* Note: only the first valid tracking parameter of each
3663 * applies.
3664 */
3665
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003666 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003667 struct stktable *t;
3668 struct stksess *ts;
3669 struct stktable_key *key;
3670 void *ptr;
3671
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003672 t = rule->arg.trk_ctr.table.t;
3673 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003674
3675 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003676 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003677
3678 /* let's count a new HTTP request as it's the first time we do it */
3679 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3680 if (ptr)
3681 stktable_data_cast(ptr, http_req_cnt)++;
3682
3683 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3684 if (ptr)
3685 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3686 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3687
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003688 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003689 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003690 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003691 }
3692 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003693 break;
3694
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003695 /* other flags exists, but normaly, they never be matched. */
3696 default:
3697 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003698 }
3699 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003700
3701 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003702 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003703}
3704
Willy Tarreau71241ab2012-12-27 11:30:54 +01003705
Willy Tarreau51d861a2015-05-22 17:30:48 +02003706/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3707 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3708 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3709 * is returned, the process can continue the evaluation of next rule list. If
3710 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3711 * is returned, it means the operation could not be processed and a server error
3712 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3713 * deny rule. If *YIELD is returned, the caller must call again the function
3714 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003715 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003716static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003717http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003718{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003719 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003720 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003721 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003722 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003723 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003724 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003725
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003726 /* If "the current_rule_list" match the executed rule list, we are in
3727 * resume condition. If a resume is needed it is always in the action
3728 * and never in the ACL or converters. In this case, we initialise the
3729 * current rule, and go to the action execution point.
3730 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003731 if (s->current_rule) {
3732 rule = s->current_rule;
3733 s->current_rule = NULL;
3734 if (s->current_rule_list == rules)
3735 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003736 }
3737 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003738
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003739 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003740
3741 /* check optional condition */
3742 if (rule->cond) {
3743 int ret;
3744
Willy Tarreau192252e2015-04-04 01:47:55 +02003745 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003746 ret = acl_pass(ret);
3747
3748 if (rule->cond->pol == ACL_COND_UNLESS)
3749 ret = !ret;
3750
3751 if (!ret) /* condition not matched */
3752 continue;
3753 }
3754
Willy Tarreauacc98002015-09-27 23:34:39 +02003755 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003756resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003757 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003758 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003759 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003761 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003762 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003763 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003765 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003766 s->task->nice = rule->arg.nice;
3767 break;
3768
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003769 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003770 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003771 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003772 break;
3773
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003774 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003775#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003776 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003777 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003778#endif
3779 break;
3780
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003781 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003782 s->logs.level = rule->arg.loglevel;
3783 break;
3784
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003785 case ACT_HTTP_REPLACE_HDR:
3786 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003787 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3788 rule->arg.hdr_add.name_len,
3789 &rule->arg.hdr_add.fmt,
3790 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003791 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003792 break;
3793
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003794 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 ctx.idx = 0;
3796 /* remove all occurrences of the header */
3797 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3798 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3799 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3800 }
Willy Tarreau85603282015-01-21 20:39:27 +01003801 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003803 case ACT_HTTP_SET_HDR:
3804 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3806 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3807 trash.len = rule->arg.hdr_add.name_len;
3808 trash.str[trash.len++] = ':';
3809 trash.str[trash.len++] = ' ';
3810 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01003811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003812 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003813 /* remove all occurrences of the header */
3814 ctx.idx = 0;
3815 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3816 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3817 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3818 }
3819 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3821 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003823 case ACT_HTTP_DEL_ACL:
3824 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003825 struct pat_ref *ref;
3826 char *key;
3827 int len;
3828
3829 /* collect reference */
3830 ref = pat_ref_lookup(rule->arg.map.ref);
3831 if (!ref)
3832 continue;
3833
3834 /* collect key */
3835 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3836 key = trash.str;
3837 key[len] = '\0';
3838
3839 /* perform update */
3840 /* returned code: 1=ok, 0=ko */
3841 pat_ref_delete(ref, key);
3842
3843 break;
3844 }
3845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003847 struct pat_ref *ref;
3848 char *key;
3849 struct chunk *trash_key;
3850 int len;
3851
3852 trash_key = get_trash_chunk();
3853
3854 /* collect reference */
3855 ref = pat_ref_lookup(rule->arg.map.ref);
3856 if (!ref)
3857 continue;
3858
3859 /* collect key */
3860 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3861 key = trash_key->str;
3862 key[len] = '\0';
3863
3864 /* perform update */
3865 /* check if the entry already exists */
3866 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003867 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003868
3869 break;
3870 }
3871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003872 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003873 struct pat_ref *ref;
3874 char *key, *value;
3875 struct chunk *trash_key, *trash_value;
3876 int len;
3877
3878 trash_key = get_trash_chunk();
3879 trash_value = get_trash_chunk();
3880
3881 /* collect reference */
3882 ref = pat_ref_lookup(rule->arg.map.ref);
3883 if (!ref)
3884 continue;
3885
3886 /* collect key */
3887 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3888 key = trash_key->str;
3889 key[len] = '\0';
3890
3891 /* collect value */
3892 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3893 value = trash_value->str;
3894 value[len] = '\0';
3895
3896 /* perform update */
3897 if (pat_ref_find_elt(ref, key) != NULL)
3898 /* update entry if it exists */
3899 pat_ref_set(ref, key, value, NULL);
3900 else
3901 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003902 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
3904 break;
3905 }
William Lallemand73025dd2014-04-24 14:38:37 +02003906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003907 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003908 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3909 return HTTP_RULE_RES_BADREQ;
3910 return HTTP_RULE_RES_DONE;
3911
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003912 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3913 /* Note: only the first valid tracking parameter of each
3914 * applies.
3915 */
3916
3917 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3918 struct stktable *t;
3919 struct stksess *ts;
3920 struct stktable_key *key;
3921 void *ptr;
3922
3923 t = rule->arg.trk_ctr.table.t;
3924 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3925
3926 if (key && (ts = stktable_get_entry(t, key))) {
3927 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3928
3929 /* let's count a new HTTP request as it's the first time we do it */
3930 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3931 if (ptr)
3932 stktable_data_cast(ptr, http_req_cnt)++;
3933
3934 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3935 if (ptr)
3936 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3937 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3938
3939 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3940 if (sess->fe != s->be)
3941 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3942
3943 /* When the client triggers a 4xx from the server, it's most often due
3944 * to a missing object or permission. These events should be tracked
3945 * because if they happen often, it may indicate a brute force or a
3946 * vulnerability scan. Normally this is done when receiving the response
3947 * but here we're tracking after this ought to have been done so we have
3948 * to do it on purpose.
3949 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003950 if ((unsigned)(txn->status - 400) < 100) {
3951 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3952 if (ptr)
3953 stktable_data_cast(ptr, http_err_cnt)++;
3954
3955 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3956 if (ptr)
3957 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3958 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3959 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003960 }
3961 }
3962 break;
3963
Thierry FOURNIER42148732015-09-02 17:17:33 +02003964 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003965 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3966 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003967
Willy Tarreauacc98002015-09-27 23:34:39 +02003968 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003969 case ACT_RET_ERR:
3970 case ACT_RET_CONT:
3971 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003972 case ACT_RET_STOP:
3973 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003974 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003975 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003976 return HTTP_RULE_RES_YIELD;
3977 }
William Lallemand73025dd2014-04-24 14:38:37 +02003978 break;
3979
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003980 /* other flags exists, but normaly, they never be matched. */
3981 default:
3982 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003983 }
3984 }
3985
3986 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003987 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003988}
3989
3990
Willy Tarreau71241ab2012-12-27 11:30:54 +01003991/* Perform an HTTP redirect based on the information in <rule>. The function
3992 * returns non-zero on success, or zero in case of a, irrecoverable error such
3993 * as too large a request to build a valid response.
3994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003995static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996{
Willy Tarreaub329a312015-05-22 16:27:37 +02003997 struct http_msg *req = &txn->req;
3998 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003999 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001
4002 /* build redirect message */
4003 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004004 case 308:
4005 msg_fmt = HTTP_308;
4006 break;
4007 case 307:
4008 msg_fmt = HTTP_307;
4009 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004010 case 303:
4011 msg_fmt = HTTP_303;
4012 break;
4013 case 301:
4014 msg_fmt = HTTP_301;
4015 break;
4016 case 302:
4017 default:
4018 msg_fmt = HTTP_302;
4019 break;
4020 }
4021
4022 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4023 return 0;
4024
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004025 location = trash.str + trash.len;
4026
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027 switch(rule->type) {
4028 case REDIRECT_TYPE_SCHEME: {
4029 const char *path;
4030 const char *host;
4031 struct hdr_ctx ctx;
4032 int pathlen;
4033 int hostlen;
4034
4035 host = "";
4036 hostlen = 0;
4037 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004038 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004039 host = ctx.line + ctx.val;
4040 hostlen = ctx.vlen;
4041 }
4042
4043 path = http_get_path(txn);
4044 /* build message using path */
4045 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004046 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004047 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4048 int qs = 0;
4049 while (qs < pathlen) {
4050 if (path[qs] == '?') {
4051 pathlen = qs;
4052 break;
4053 }
4054 qs++;
4055 }
4056 }
4057 } else {
4058 path = "/";
4059 pathlen = 1;
4060 }
4061
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004062 if (rule->rdr_str) { /* this is an old "redirect" rule */
4063 /* check if we can add scheme + "://" + host + path */
4064 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4065 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067 /* add scheme */
4068 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4069 trash.len += rule->rdr_len;
4070 }
4071 else {
4072 /* add scheme with executing log format */
4073 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004074
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004075 /* check if we can add scheme + "://" + host + path */
4076 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4077 return 0;
4078 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004079 /* add "://" */
4080 memcpy(trash.str + trash.len, "://", 3);
4081 trash.len += 3;
4082
4083 /* add host */
4084 memcpy(trash.str + trash.len, host, hostlen);
4085 trash.len += hostlen;
4086
4087 /* add path */
4088 memcpy(trash.str + trash.len, path, pathlen);
4089 trash.len += pathlen;
4090
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004091 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092 if (trash.len && trash.str[trash.len - 1] != '/' &&
4093 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4094 if (trash.len > trash.size - 5)
4095 return 0;
4096 trash.str[trash.len] = '/';
4097 trash.len++;
4098 }
4099
4100 break;
4101 }
4102 case REDIRECT_TYPE_PREFIX: {
4103 const char *path;
4104 int pathlen;
4105
4106 path = http_get_path(txn);
4107 /* build message using path */
4108 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004109 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4111 int qs = 0;
4112 while (qs < pathlen) {
4113 if (path[qs] == '?') {
4114 pathlen = qs;
4115 break;
4116 }
4117 qs++;
4118 }
4119 }
4120 } else {
4121 path = "/";
4122 pathlen = 1;
4123 }
4124
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004125 if (rule->rdr_str) { /* this is an old "redirect" rule */
4126 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4127 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004129 /* add prefix. Note that if prefix == "/", we don't want to
4130 * add anything, otherwise it makes it hard for the user to
4131 * configure a self-redirection.
4132 */
4133 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4134 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4135 trash.len += rule->rdr_len;
4136 }
4137 }
4138 else {
4139 /* add prefix with executing log format */
4140 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4141
4142 /* Check length */
4143 if (trash.len + pathlen > trash.size - 4)
4144 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004145 }
4146
4147 /* add path */
4148 memcpy(trash.str + trash.len, path, pathlen);
4149 trash.len += pathlen;
4150
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004151 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004152 if (trash.len && trash.str[trash.len - 1] != '/' &&
4153 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4154 if (trash.len > trash.size - 5)
4155 return 0;
4156 trash.str[trash.len] = '/';
4157 trash.len++;
4158 }
4159
4160 break;
4161 }
4162 case REDIRECT_TYPE_LOCATION:
4163 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004164 if (rule->rdr_str) { /* this is an old "redirect" rule */
4165 if (trash.len + rule->rdr_len > trash.size - 4)
4166 return 0;
4167
4168 /* add location */
4169 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4170 trash.len += rule->rdr_len;
4171 }
4172 else {
4173 /* add location with executing log format */
4174 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004176 /* Check left length */
4177 if (trash.len > trash.size - 4)
4178 return 0;
4179 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004180 break;
4181 }
4182
4183 if (rule->cookie_len) {
4184 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4185 trash.len += 14;
4186 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4187 trash.len += rule->cookie_len;
4188 memcpy(trash.str + trash.len, "\r\n", 2);
4189 trash.len += 2;
4190 }
4191
4192 /* add end of headers and the keep-alive/close status.
4193 * We may choose to set keep-alive if the Location begins
4194 * with a slash, because the client will come back to the
4195 * same server.
4196 */
4197 txn->status = rule->code;
4198 /* let's log the request time */
4199 s->logs.tv_request = now;
4200
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004201 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004202 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004203 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004204 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4205 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4206 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004207 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004208 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4209 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4210 trash.len += 30;
4211 } else {
4212 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4213 trash.len += 24;
4214 }
4215 }
4216 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4217 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004218 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004219 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004221 bi_fast_delete(req->chn->buf, req->sov);
4222 req->next -= req->sov;
4223 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004224 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4225 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004226 req->msg_state = HTTP_MSG_CLOSED;
4227 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004228 /* Trim any possible response */
4229 res->chn->buf->i = 0;
4230 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004231 } else {
4232 /* keep-alive not possible */
4233 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4234 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4235 trash.len += 29;
4236 } else {
4237 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4238 trash.len += 23;
4239 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004240 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004241 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004242 }
4243
Willy Tarreaue7dff022015-04-03 01:14:29 +02004244 if (!(s->flags & SF_ERR_MASK))
4245 s->flags |= SF_ERR_LOCAL;
4246 if (!(s->flags & SF_FINST_MASK))
4247 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004248
4249 return 1;
4250}
4251
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004252/* This stream analyser runs all HTTP request processing which is common to
4253 * frontends and backends, which means blocking ACLs, filters, connection-close,
4254 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004255 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004256 * either needs more data or wants to immediately abort the request (eg: deny,
4257 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004259int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004260{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004261 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004262 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004263 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004264 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004265 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004266 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004267 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004268
Willy Tarreau655dce92009-11-08 13:10:58 +01004269 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004270 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004271 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004272 }
4273
Willy Tarreau87b09662015-04-03 00:22:06 +02004274 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02004275 now_ms, __FUNCTION__,
4276 s,
4277 req,
4278 req->rex, req->wex,
4279 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004280 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004281 req->analysers);
4282
Willy Tarreau65410832014-04-28 21:25:43 +02004283 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004284 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004285
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004286 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004287 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004288 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004289
Willy Tarreau0b748332014-04-29 00:13:29 +02004290 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004291 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4292 goto return_prx_yield;
4293
Willy Tarreau0b748332014-04-29 00:13:29 +02004294 case HTTP_RULE_RES_CONT:
4295 case HTTP_RULE_RES_STOP: /* nothing to do */
4296 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004297
Willy Tarreau0b748332014-04-29 00:13:29 +02004298 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4299 if (txn->flags & TX_CLTARPIT)
4300 goto tarpit;
4301 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004302
Willy Tarreau0b748332014-04-29 00:13:29 +02004303 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4304 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004305
Willy Tarreau0b748332014-04-29 00:13:29 +02004306 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004307 goto done;
4308
Willy Tarreau0b748332014-04-29 00:13:29 +02004309 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4310 goto return_bad_req;
4311 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004312 }
4313
Willy Tarreau52542592014-04-28 18:33:26 +02004314 /* OK at this stage, we know that the request was accepted according to
4315 * the http-request rules, we can check for the stats. Note that the
4316 * URI is detected *before* the req* rules in order not to be affected
4317 * by a possible reqrep, while they are processed *after* so that a
4318 * reqdeny can still block them. This clearly needs to change in 1.6!
4319 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004320 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004321 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004322 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004323 txn->status = 500;
4324 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004325 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004326
Willy Tarreaue7dff022015-04-03 01:14:29 +02004327 if (!(s->flags & SF_ERR_MASK))
4328 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004329 goto return_prx_cond;
4330 }
4331
4332 /* parse the whole stats request and extract the relevant information */
4333 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004334 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004336
Willy Tarreau0b748332014-04-29 00:13:29 +02004337 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4338 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4341 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004342 }
4343
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004344 /* evaluate the req* rules except reqadd */
4345 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004346 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004347 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004348
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004349 if (txn->flags & TX_CLDENY)
4350 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004351
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004352 if (txn->flags & TX_CLTARPIT)
4353 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004354 }
Willy Tarreau06619262006-12-17 08:37:22 +01004355
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004356 /* add request headers from the rule sets in the same order */
4357 list_for_each_entry(wl, &px->req_add, list) {
4358 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004359 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004360 ret = acl_pass(ret);
4361 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4362 ret = !ret;
4363 if (!ret)
4364 continue;
4365 }
4366
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004367 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004368 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004369 }
4370
Willy Tarreau52542592014-04-28 18:33:26 +02004371
4372 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004373 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004374 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004375 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4376 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004377
Willy Tarreaue7dff022015-04-03 01:14:29 +02004378 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4379 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4380 if (!(s->flags & SF_FINST_MASK))
4381 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004382
Willy Tarreau70730dd2014-04-24 18:06:27 +02004383 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004384 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004385 req->analysers &= ~AN_FLT_XFER_DATA;
4386 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004387 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004388 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004389
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004390 /* check whether we have some ACLs set to redirect this request */
4391 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004392 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004393 int ret;
4394
Willy Tarreau192252e2015-04-04 01:47:55 +02004395 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004396 ret = acl_pass(ret);
4397 if (rule->cond->pol == ACL_COND_UNLESS)
4398 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004399 if (!ret)
4400 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004401 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004402 if (!http_apply_redirect_rule(rule, s, txn))
4403 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004404 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004405 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004406
Willy Tarreau2be39392010-01-03 17:24:51 +01004407 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4408 * If this happens, then the data will not come immediately, so we must
4409 * send all what we have without waiting. Note that due to the small gain
4410 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004411 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004412 * itself once used.
4413 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004414 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004415
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004416 done: /* done with this analyser, continue with next ones that the calling
4417 * points will have set, if any.
4418 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004419 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004420 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4421 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004422 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004423
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004424 tarpit:
4425 /* When a connection is tarpitted, we use the tarpit timeout,
4426 * which may be the same as the connect timeout if unspecified.
4427 * If unset, then set it to zero because we really want it to
4428 * eventually expire. We build the tarpit as an analyser.
4429 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004430 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004431
4432 /* wipe the request out so that we can drop the connection early
4433 * if the client closes first.
4434 */
4435 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004436
4437 /* Allow cookie logging
4438 */
4439 if (s->be->cookie_name || sess->fe->capture_name)
4440 manage_client_side_cookies(s, req);
4441
Christopher Fauletd7c91962015-04-30 11:48:27 +02004442 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004443 req->analysers |= AN_REQ_HTTP_TARPIT;
4444 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4445 if (!req->analyse_exp)
4446 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004447 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004448 sess->fe->fe_counters.denied_req++;
4449 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004450 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004451 if (sess->listener->counters)
4452 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004453 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004454
4455 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004456
4457 /* Allow cookie logging
4458 */
4459 if (s->be->cookie_name || sess->fe->capture_name)
4460 manage_client_side_cookies(s, req);
4461
Willy Tarreau0b748332014-04-29 00:13:29 +02004462 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004463 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004464 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004465 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004466 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004467 sess->fe->fe_counters.denied_req++;
4468 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004469 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004470 if (sess->listener->counters)
4471 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004472 goto return_prx_cond;
4473
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004474 return_bad_req:
4475 /* We centralize bad requests processing here */
4476 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4477 /* we detected a parsing error. We want to archive this request
4478 * in the dedicated proxy area for later troubleshooting.
4479 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004480 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004481 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004482
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004483 txn->req.msg_state = HTTP_MSG_ERROR;
4484 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004485 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004486
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004487 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004488 if (sess->listener->counters)
4489 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004490
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004491 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004492 if (!(s->flags & SF_ERR_MASK))
4493 s->flags |= SF_ERR_PRXCOND;
4494 if (!(s->flags & SF_FINST_MASK))
4495 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004496
Christopher Fauletd7c91962015-04-30 11:48:27 +02004497 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004498 req->analyse_exp = TICK_ETERNITY;
4499 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004500
4501 return_prx_yield:
4502 channel_dont_connect(req);
4503 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004504}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004505
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004506/* This function performs all the processing enabled for the current request.
4507 * It returns 1 if the processing can continue on next analysers, or zero if it
4508 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004509 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004511int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004512{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004513 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004514 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004515 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004516 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004517
Willy Tarreau655dce92009-11-08 13:10:58 +01004518 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004519 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004520 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004521 return 0;
4522 }
4523
Willy Tarreau87b09662015-04-03 00:22:06 +02004524 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004525 now_ms, __FUNCTION__,
4526 s,
4527 req,
4528 req->rex, req->wex,
4529 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004530 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004531 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004532
Willy Tarreau59234e92008-11-30 23:51:27 +01004533 /*
4534 * Right now, we know that we have processed the entire headers
4535 * and that unwanted requests have been filtered out. We can do
4536 * whatever we want with the remaining request. Also, now we
4537 * may have separate values for ->fe, ->be.
4538 */
Willy Tarreau06619262006-12-17 08:37:22 +01004539
Willy Tarreau59234e92008-11-30 23:51:27 +01004540 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004541 * If HTTP PROXY is set we simply get remote server address parsing
4542 * incoming request. Note that this requires that a connection is
4543 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004544 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004545 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004546 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004547 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004548
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004549 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004550 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004551 txn->req.msg_state = HTTP_MSG_ERROR;
4552 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004553 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004554 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004555
Willy Tarreaue7dff022015-04-03 01:14:29 +02004556 if (!(s->flags & SF_ERR_MASK))
4557 s->flags |= SF_ERR_RESOURCE;
4558 if (!(s->flags & SF_FINST_MASK))
4559 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004560
4561 return 0;
4562 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004563
4564 path = http_get_path(txn);
4565 url2sa(req->buf->p + msg->sl.rq.u,
4566 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004567 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004568 /* if the path was found, we have to remove everything between
4569 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4570 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4571 * u_l characters by a single "/".
4572 */
4573 if (path) {
4574 char *cur_ptr = req->buf->p;
4575 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4576 int delta;
4577
4578 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4579 http_msg_move_end(&txn->req, delta);
4580 cur_end += delta;
4581 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4582 goto return_bad_req;
4583 }
4584 else {
4585 char *cur_ptr = req->buf->p;
4586 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4587 int delta;
4588
4589 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4590 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4591 http_msg_move_end(&txn->req, delta);
4592 cur_end += delta;
4593 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4594 goto return_bad_req;
4595 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004597
Willy Tarreau59234e92008-11-30 23:51:27 +01004598 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004599 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004600 * Note that doing so might move headers in the request, but
4601 * the fields will stay coherent and the URI will not move.
4602 * This should only be performed in the backend.
4603 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004604 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004605 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004606
William Lallemanda73203e2012-03-12 12:48:57 +01004607 /* add unique-id if "header-unique-id" is specified */
4608
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004609 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004610 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4611 goto return_bad_req;
4612 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004613 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004614 }
William Lallemanda73203e2012-03-12 12:48:57 +01004615
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004616 if (sess->fe->header_unique_id && s->unique_id) {
4617 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004618 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004619 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004620 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004621 goto return_bad_req;
4622 }
4623
Cyril Bontéb21570a2009-11-29 20:04:48 +01004624 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 * 9: add X-Forwarded-For if either the frontend or the backend
4626 * asks for it.
4627 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004628 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004629 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004630 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4631 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4632 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004633 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004634 /* The header is set to be added only if none is present
4635 * and we found it, so don't do anything.
4636 */
4637 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004638 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004639 /* Add an X-Forwarded-For header unless the source IP is
4640 * in the 'except' network range.
4641 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004642 if ((!sess->fe->except_mask.s_addr ||
4643 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4644 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004645 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004646 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004647 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004648 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004649 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004650 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004651
4652 /* Note: we rely on the backend to get the header name to be used for
4653 * x-forwarded-for, because the header is really meant for the backends.
4654 * However, if the backend did not specify any option, we have to rely
4655 * on the frontend's header name.
4656 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004657 if (s->be->fwdfor_hdr_len) {
4658 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004659 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004660 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004661 len = sess->fe->fwdfor_hdr_len;
4662 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004663 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004664 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004665
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004666 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004667 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004668 }
4669 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004670 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004671 /* FIXME: for the sake of completeness, we should also support
4672 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004673 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004674 int len;
4675 char pn[INET6_ADDRSTRLEN];
4676 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004679
Willy Tarreau59234e92008-11-30 23:51:27 +01004680 /* Note: we rely on the backend to get the header name to be used for
4681 * x-forwarded-for, because the header is really meant for the backends.
4682 * However, if the backend did not specify any option, we have to rely
4683 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004684 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004685 if (s->be->fwdfor_hdr_len) {
4686 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004687 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004688 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004689 len = sess->fe->fwdfor_hdr_len;
4690 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004691 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004692 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004693
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004694 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004695 goto return_bad_req;
4696 }
4697 }
4698
4699 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004700 * 10: add X-Original-To if either the frontend or the backend
4701 * asks for it.
4702 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004703 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004704
4705 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004706 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004707 /* Add an X-Original-To header unless the destination IP is
4708 * in the 'except' network range.
4709 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004710 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004711
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004712 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004713 ((!sess->fe->except_mask_to.s_addr ||
4714 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4715 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004716 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004717 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004718 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004719 int len;
4720 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004721 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004722
4723 /* Note: we rely on the backend to get the header name to be used for
4724 * x-original-to, because the header is really meant for the backends.
4725 * However, if the backend did not specify any option, we have to rely
4726 * on the frontend's header name.
4727 */
4728 if (s->be->orgto_hdr_len) {
4729 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004730 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004731 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004732 len = sess->fe->orgto_hdr_len;
4733 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004734 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004735 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004736
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004737 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004738 goto return_bad_req;
4739 }
4740 }
4741 }
4742
Willy Tarreau50fc7772012-11-11 22:19:57 +01004743 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4744 * If an "Upgrade" token is found, the header is left untouched in order not to have
4745 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4746 * "Upgrade" is present in the Connection header.
4747 */
4748 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4749 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004751 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004752 unsigned int want_flags = 0;
4753
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004754 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004755 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004756 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004757 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004758 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004759 want_flags |= TX_CON_CLO_SET;
4760 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004761 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004762 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004763 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004764 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004765 want_flags |= TX_CON_KAL_SET;
4766 }
4767
4768 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004769 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004770 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004772
Willy Tarreau522d6c02009-12-06 18:49:18 +01004773 /* If we have no server assigned yet and we're balancing on url_param
4774 * with a POST request, we may be interested in checking the body for
4775 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004776 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004777 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004778 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004779 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004780 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004781 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004782 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004783
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004784 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004785 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004786 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004787#ifdef TCP_QUICKACK
4788 /* We expect some data from the client. Unless we know for sure
4789 * we already have a full request, we have to re-enable quick-ack
4790 * in case we previously disabled it, otherwise we might cause
4791 * the client to delay further data.
4792 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004793 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004794 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004795 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004796 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004797 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004798#endif
4799 }
Willy Tarreau03945942009-12-22 16:50:27 +01004800
Willy Tarreau59234e92008-11-30 23:51:27 +01004801 /*************************************************************
4802 * OK, that's finished for the headers. We have done what we *
4803 * could. Let's switch to the DATA state. *
4804 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004805 req->analyse_exp = TICK_ETERNITY;
4806 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004807
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004808 /* if the server closes the connection, we want to immediately react
4809 * and close the socket to save packets and syscalls.
4810 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004811 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004812 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004813
Willy Tarreau59234e92008-11-30 23:51:27 +01004814 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004815 /* OK let's go on with the BODY now */
4816 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004817
Willy Tarreau59234e92008-11-30 23:51:27 +01004818 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004819 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004820 /* we detected a parsing error. We want to archive this request
4821 * in the dedicated proxy area for later troubleshooting.
4822 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004823 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004824 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004825
Willy Tarreau59234e92008-11-30 23:51:27 +01004826 txn->req.msg_state = HTTP_MSG_ERROR;
4827 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004828 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004829 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004830
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004831 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004832 if (sess->listener->counters)
4833 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004834
Willy Tarreaue7dff022015-04-03 01:14:29 +02004835 if (!(s->flags & SF_ERR_MASK))
4836 s->flags |= SF_ERR_PRXCOND;
4837 if (!(s->flags & SF_FINST_MASK))
4838 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004839 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004840}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004841
Willy Tarreau60b85b02008-11-30 23:28:40 +01004842/* This function is an analyser which processes the HTTP tarpit. It always
4843 * returns zero, at the beginning because it prevents any other processing
4844 * from occurring, and at the end because it terminates the request.
4845 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004846int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004847{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004848 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004849
4850 /* This connection is being tarpitted. The CLIENT side has
4851 * already set the connect expiration date to the right
4852 * timeout. We just have to check that the client is still
4853 * there and that the timeout has not expired.
4854 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004855 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004856 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004857 !tick_is_expired(req->analyse_exp, now_ms))
4858 return 0;
4859
4860 /* We will set the queue timer to the time spent, just for
4861 * logging purposes. We fake a 500 server error, so that the
4862 * attacker will not suspect his connection has been tarpitted.
4863 * It will not cause trouble to the logs because we can exclude
4864 * the tarpitted connections by filtering on the 'PT' status flags.
4865 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004866 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4867
4868 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004869 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004870 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004871
Christopher Fauletd7c91962015-04-30 11:48:27 +02004872 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004873 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004874
Willy Tarreaue7dff022015-04-03 01:14:29 +02004875 if (!(s->flags & SF_ERR_MASK))
4876 s->flags |= SF_ERR_PRXCOND;
4877 if (!(s->flags & SF_FINST_MASK))
4878 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004879 return 0;
4880}
4881
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004882/* This function is an analyser which waits for the HTTP request body. It waits
4883 * for either the buffer to be full, or the full advertised contents to have
4884 * reached the buffer. It must only be called after the standard HTTP request
4885 * processing has occurred, because it expects the request to be parsed and will
4886 * look for the Expect header. It may send a 100-Continue interim response. It
4887 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4888 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4889 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004890 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004891int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004892{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004893 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004894 struct http_txn *txn = s->txn;
4895 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004896
4897 /* We have to parse the HTTP request body to find any required data.
4898 * "balance url_param check_post" should have been the only way to get
4899 * into this. We were brought here after HTTP header analysis, so all
4900 * related structures are ready.
4901 */
4902
Willy Tarreau890988f2014-04-10 11:59:33 +02004903 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4904 /* This is the first call */
4905 if (msg->msg_state < HTTP_MSG_BODY)
4906 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004907
Willy Tarreau890988f2014-04-10 11:59:33 +02004908 if (msg->msg_state < HTTP_MSG_100_SENT) {
4909 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4910 * send an HTTP/1.1 100 Continue intermediate response.
4911 */
4912 if (msg->flags & HTTP_MSGF_VER_11) {
4913 struct hdr_ctx ctx;
4914 ctx.idx = 0;
4915 /* Expect is allowed in 1.1, look for it */
4916 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4917 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004918 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004919 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004920 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004921 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004922 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004923
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004924 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004925 * req->buf->p still points to the beginning of the message. We
4926 * must save the body in msg->next because it survives buffer
4927 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004928 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004929 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004930
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004931 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004932 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4933 else
4934 msg->msg_state = HTTP_MSG_DATA;
4935 }
4936
Willy Tarreau890988f2014-04-10 11:59:33 +02004937 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4938 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004939 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004940 goto missing_data;
4941
4942 /* OK we have everything we need now */
4943 goto http_end;
4944 }
4945
4946 /* OK here we're parsing a chunked-encoded message */
4947
Willy Tarreau522d6c02009-12-06 18:49:18 +01004948 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004949 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004950 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004952 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004953 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004954
Willy Tarreau115acb92009-12-26 13:56:06 +01004955 if (!ret)
4956 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004957 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004958 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004960 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004961 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004962 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004963 }
4964
Willy Tarreaud98cf932009-12-27 22:54:55 +01004965 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004966 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4967 * for at least a whole chunk or the whole content length bytes after
4968 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004969 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004970 if (msg->msg_state == HTTP_MSG_TRAILERS)
4971 goto http_end;
4972
Willy Tarreaue115b492015-05-01 23:05:14 +02004973 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004974 goto http_end;
4975
4976 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004977 /* we get here if we need to wait for more data. If the buffer is full,
4978 * we have the maximum we can expect.
4979 */
4980 if (buffer_full(req->buf, global.tune.maxrewrite))
4981 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004982
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004983 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004984 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004985 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004986
Willy Tarreaue7dff022015-04-03 01:14:29 +02004987 if (!(s->flags & SF_ERR_MASK))
4988 s->flags |= SF_ERR_CLITO;
4989 if (!(s->flags & SF_FINST_MASK))
4990 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004991 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004992 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004993
4994 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004995 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004996 /* Not enough data. We'll re-use the http-request
4997 * timeout here. Ideally, we should set the timeout
4998 * relative to the accept() date. We just set the
4999 * request timeout once at the beginning of the
5000 * request.
5001 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005002 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005003 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005004 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005005 return 0;
5006 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005007
5008 http_end:
5009 /* The situation will not evolve, so let's give up on the analysis. */
5010 s->logs.tv_request = now; /* update the request timer to reflect full request */
5011 req->analysers &= ~an_bit;
5012 req->analyse_exp = TICK_ETERNITY;
5013 return 1;
5014
5015 return_bad_req: /* let's centralize all bad requests */
5016 txn->req.msg_state = HTTP_MSG_ERROR;
5017 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005018 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005019
Willy Tarreaue7dff022015-04-03 01:14:29 +02005020 if (!(s->flags & SF_ERR_MASK))
5021 s->flags |= SF_ERR_PRXCOND;
5022 if (!(s->flags & SF_FINST_MASK))
5023 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005024
Willy Tarreau522d6c02009-12-06 18:49:18 +01005025 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02005026 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005027 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005028 if (sess->listener->counters)
5029 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005030 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005031}
5032
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005033/* send a server's name with an outgoing request over an established connection.
5034 * Note: this function is designed to be called once the request has been scheduled
5035 * for being forwarded. This is the reason why it rewinds the buffer before
5036 * proceeding.
5037 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005038int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005039
5040 struct hdr_ctx ctx;
5041
Mark Lamourinec2247f02012-01-04 13:02:01 -05005042 char *hdr_name = be->server_id_hdr_name;
5043 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005044 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005045 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005046 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005047
William Lallemandd9e90662012-01-30 17:27:17 +01005048 ctx.idx = 0;
5049
Willy Tarreau211cdec2014-04-17 20:18:08 +02005050 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005051 if (old_o) {
5052 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005053 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005054 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005055 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005056 }
5057
Willy Tarreau9b28e032012-10-12 23:49:43 +02005058 old_i = chn->buf->i;
5059 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 -05005060 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005061 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005062 }
5063
5064 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005065 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005066 memcpy(hdr_val, hdr_name, hdr_name_len);
5067 hdr_val += hdr_name_len;
5068 *hdr_val++ = ':';
5069 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005070 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5071 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005072
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005073 if (old_o) {
5074 /* If this was a forwarded request, we must readjust the amount of
5075 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005076 * variations. Note that the current state is >= HTTP_MSG_BODY,
5077 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005078 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005079 old_o += chn->buf->i - old_i;
5080 b_adv(chn->buf, old_o);
5081 txn->req.next -= old_o;
5082 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005083 }
5084
Mark Lamourinec2247f02012-01-04 13:02:01 -05005085 return 0;
5086}
5087
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088/* Terminate current transaction and prepare a new one. This is very tricky
5089 * right now but it works.
5090 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005091void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005092{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005093 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005094 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005095 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005096 struct connection *srv_conn;
5097 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005098 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005099
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100 /* FIXME: We need a more portable way of releasing a backend's and a
5101 * server's connections. We need a safer way to reinitialize buffer
5102 * flags. We also need a more accurate method for computing per-request
5103 * data.
5104 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005105 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106
Willy Tarreau4213a112013-12-15 10:25:42 +01005107 /* unless we're doing keep-alive, we want to quickly close the connection
5108 * to the server.
5109 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005110 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005111 !si_conn_ready(&s->si[1])) {
5112 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5113 si_shutr(&s->si[1]);
5114 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005115 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116
Willy Tarreaue7dff022015-04-03 01:14:29 +02005117 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005118 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005119 if (unlikely(s->srv_conn))
5120 sess_change_server(s, NULL);
5121 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122
5123 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005124 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125
Willy Tarreaueee5b512015-04-03 23:46:31 +02005126 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127 int n;
5128
Willy Tarreaueee5b512015-04-03 23:46:31 +02005129 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130 if (n < 1 || n > 5)
5131 n = 0;
5132
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005133 if (fe->mode == PR_MODE_HTTP) {
5134 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005135 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005136 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005137 (be->mode == PR_MODE_HTTP)) {
5138 be->be_counters.p.http.rsp[n]++;
5139 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005140 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 }
5142
5143 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005144 s->logs.bytes_in -= s->req.buf->i;
5145 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146
5147 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005148 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005149 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005150 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 s->do_log(s);
5152 }
5153
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005154 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005155 stream_stop_content_counters(s);
5156 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005157
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 s->logs.accept_date = date; /* user-visible date for logging */
5159 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005160 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5161 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005162 tv_zero(&s->logs.tv_request);
5163 s->logs.t_queue = -1;
5164 s->logs.t_connect = -1;
5165 s->logs.t_data = -1;
5166 s->logs.t_close = 0;
5167 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5168 s->logs.srv_queue_size = 0; /* we will get this number soon */
5169
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005170 s->logs.bytes_in = s->req.total = s->req.buf->i;
5171 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172
5173 if (s->pend_pos)
5174 pendconn_free(s->pend_pos);
5175
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005176 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005177 if (s->flags & SF_CURR_SESS) {
5178 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005179 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005181 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005182 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 }
5184
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005185 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186
Willy Tarreau4213a112013-12-15 10:25:42 +01005187 /* only release our endpoint if we don't intend to reuse the
5188 * connection.
5189 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005190 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005191 !si_conn_ready(&s->si[1])) {
5192 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005193 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005194 }
5195
Willy Tarreau350f4872014-11-28 14:42:25 +01005196 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5197 s->si[1].err_type = SI_ET_NONE;
5198 s->si[1].conn_retries = 0; /* used for logging too */
5199 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005200 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005201 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
5202 s->res.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|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02005203 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5204 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5205 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005206
Willy Tarreaueee5b512015-04-03 23:46:31 +02005207 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005209 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005210
5211 if (prev_status == 401 || prev_status == 407) {
5212 /* In HTTP keep-alive mode, if we receive a 401, we still have
5213 * a chance of being able to send the visitor again to the same
5214 * server over the same connection. This is required by some
5215 * broken protocols such as NTLM, and anyway whenever there is
5216 * an opportunity for sending the challenge to the proper place,
5217 * it's better to do it (at least it helps with debugging).
5218 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005219 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005220 if (srv_conn)
5221 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005222 }
5223
Willy Tarreau53f96852016-02-02 18:50:47 +01005224 /* Never ever allow to reuse a connection from a non-reuse backend */
5225 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5226 srv_conn->flags |= CO_FL_PRIVATE;
5227
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005228 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005229 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005231 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005232 s->req.flags |= CF_NEVER_WAIT;
5233 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005234 }
5235
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236 /* if the request buffer is not empty, it means we're
5237 * about to process another request, so send pending
5238 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005239 * Just don't do this if the buffer is close to be full,
5240 * because the request will wait for it to flush a little
5241 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005243 if (s->req.buf->i) {
5244 if (s->res.buf->o &&
5245 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5246 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5247 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005248 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005249
Willy Tarreau714ea782015-11-25 20:11:11 +01005250 /* we're removing the analysers, we MUST re-enable events detection.
5251 * We don't enable close on the response channel since it's either
5252 * already closed, or in keep-alive with an idle connection handler.
5253 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005254 channel_auto_read(&s->req);
5255 channel_auto_close(&s->req);
5256 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005257
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005258 /* we're in keep-alive with an idle connection, monitor it if not already done */
5259 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005260 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005261 if (!srv)
5262 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005263 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005264 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005265 else if (prev_flags & TX_NOT_FIRST)
5266 /* note: we check the request, not the connection, but
5267 * this is valid for strategies SAFE and AGGR, and in
5268 * case of ALWS, we don't care anyway.
5269 */
5270 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005271 else
5272 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005273 }
Willy Tarreau27375622013-12-17 00:00:28 +01005274
Christopher Faulet3e344292015-11-24 16:24:13 +01005275 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005276 s->req.analysers &= AN_FLT_END;
5277 s->res.analysers &= AN_FLT_END;
5278 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005279 else {
5280 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5281 s->res.analysers = 0;
5282 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283}
5284
5285
5286/* This function updates the request state machine according to the response
5287 * state machine and buffer flags. It returns 1 if it changes anything (flag
5288 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5289 * it is only used to find when a request/response couple is complete. Both
5290 * this function and its equivalent should loop until both return zero. It
5291 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5292 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005293int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005295 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005296 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005297 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298 unsigned int old_state = txn->req.msg_state;
5299
Willy Tarreau610ecce2010-01-04 21:15:02 +01005300 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5301 return 0;
5302
5303 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005304 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005305 * We can shut the read side unless we want to abort_on_close,
5306 * or we have a POST request. The issue with POST requests is
5307 * that some browsers still send a CRLF after the request, and
5308 * this CRLF must be read so that it does not remain in the kernel
5309 * buffers, otherwise a close could cause an RST on some systems
5310 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005311 * Note that if we're using keep-alive on the client side, we'd
5312 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005313 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005314 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005315 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005316 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5317 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5318 !(s->be->options & PR_O_ABRT_CLOSE) &&
5319 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005320 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321
Willy Tarreau40f151a2012-12-20 12:10:09 +01005322 /* if the server closes the connection, we want to immediately react
5323 * and close the socket to save packets and syscalls.
5324 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005325 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005326
Willy Tarreau7f876a12015-11-18 11:59:55 +01005327 /* In any case we've finished parsing the request so we must
5328 * disable Nagle when sending data because 1) we're not going
5329 * to shut this side, and 2) the server is waiting for us to
5330 * send pending data.
5331 */
5332 chn->flags |= CF_NEVER_WAIT;
5333
Willy Tarreau610ecce2010-01-04 21:15:02 +01005334 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5335 goto wait_other_side;
5336
5337 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5338 /* The server has not finished to respond, so we
5339 * don't want to move in order not to upset it.
5340 */
5341 goto wait_other_side;
5342 }
5343
5344 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5345 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005346 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 txn->req.msg_state = HTTP_MSG_TUNNEL;
5348 goto wait_other_side;
5349 }
5350
5351 /* When we get here, it means that both the request and the
5352 * response have finished receiving. Depending on the connection
5353 * mode, we'll have to wait for the last bytes to leave in either
5354 * direction, and sometimes for a close to be effective.
5355 */
5356
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005357 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5358 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005359 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5360 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005361 }
5362 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5363 /* Option forceclose is set, or either side wants to close,
5364 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005365 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005366 * once both states are CLOSED.
5367 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5369 channel_shutr_now(chn);
5370 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005372 }
5373 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005374 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5375 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005376 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005377 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5378 channel_auto_read(chn);
5379 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005380 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 }
5382
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005383 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005384 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005385 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005386
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005387 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 txn->req.msg_state = HTTP_MSG_CLOSING;
5389 goto http_msg_closing;
5390 }
5391 else {
5392 txn->req.msg_state = HTTP_MSG_CLOSED;
5393 goto http_msg_closed;
5394 }
5395 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 }
5398
5399 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5400 http_msg_closing:
5401 /* nothing else to forward, just waiting for the output buffer
5402 * to be empty and for the shutw_now to take effect.
5403 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005404 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 txn->req.msg_state = HTTP_MSG_CLOSED;
5406 goto http_msg_closed;
5407 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_ERROR;
5410 goto wait_other_side;
5411 }
5412 }
5413
5414 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5415 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005416 /* see above in MSG_DONE why we only do this in these states */
5417 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5418 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5419 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005420 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 goto wait_other_side;
5422 }
5423
5424 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426}
5427
5428
5429/* This function updates the response state machine according to the request
5430 * state machine and buffer flags. It returns 1 if it changes anything (flag
5431 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5432 * it is only used to find when a request/response couple is complete. Both
5433 * this function and its equivalent should loop until both return zero. It
5434 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005436int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005438 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005439 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005440 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 unsigned int old_state = txn->rsp.msg_state;
5442
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5444 return 0;
5445
5446 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5447 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005448 * still monitor the server connection for a possible close
5449 * while the request is being uploaded, so we don't disable
5450 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453
5454 if (txn->req.msg_state == HTTP_MSG_ERROR)
5455 goto wait_other_side;
5456
5457 if (txn->req.msg_state < HTTP_MSG_DONE) {
5458 /* The client seems to still be sending data, probably
5459 * because we got an error response during an upload.
5460 * We have the choice of either breaking the connection
5461 * or letting it pass through. Let's do the later.
5462 */
5463 goto wait_other_side;
5464 }
5465
5466 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5467 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005470 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 goto wait_other_side;
5472 }
5473
5474 /* When we get here, it means that both the request and the
5475 * response have finished receiving. Depending on the connection
5476 * mode, we'll have to wait for the last bytes to leave in either
5477 * direction, and sometimes for a close to be effective.
5478 */
5479
5480 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5481 /* Server-close mode : shut read and wait for the request
5482 * side to close its output buffer. The caller will detect
5483 * when we're in DONE and the other is in CLOSED and will
5484 * catch that for the final cleanup.
5485 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005486 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5487 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005489 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5490 /* Option forceclose is set, or either side wants to close,
5491 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005492 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005493 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005494 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005495 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5496 channel_shutr_now(chn);
5497 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005498 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 }
5500 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005501 /* The last possible modes are keep-alive and tunnel. Tunnel will
5502 * need to forward remaining data. Keep-alive will need to monitor
5503 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005505 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005506 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005507 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 }
5510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005511 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005513 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5515 goto http_msg_closing;
5516 }
5517 else {
5518 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5519 goto http_msg_closed;
5520 }
5521 }
5522 goto wait_other_side;
5523 }
5524
5525 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5526 http_msg_closing:
5527 /* nothing else to forward, just waiting for the output buffer
5528 * to be empty and for the shutw_now to take effect.
5529 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5532 goto http_msg_closed;
5533 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005536 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005537 if (objt_server(s->target))
5538 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 goto wait_other_side;
5540 }
5541 }
5542
5543 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5544 http_msg_closed:
5545 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005546 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005547 channel_auto_close(chn);
5548 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549 goto wait_other_side;
5550 }
5551
5552 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005553 /* We force the response to leave immediately if we're waiting for the
5554 * other side, since there is no pending shutdown to push it out.
5555 */
5556 if (!channel_is_empty(chn))
5557 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005558 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559}
5560
5561
5562/* Resync the request and response state machines. Return 1 if either state
5563 * changes.
5564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005565int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005567 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005568 int old_req_state = txn->req.msg_state;
5569 int old_res_state = txn->rsp.msg_state;
5570
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 http_sync_req_state(s);
5572 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 if (!http_sync_res_state(s))
5574 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 if (!http_sync_req_state(s))
5576 break;
5577 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005578
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 /* OK, both state machines agree on a compatible state.
5580 * There are a few cases we're interested in :
5581 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5582 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5583 * directions, so let's simply disable both analysers.
5584 * - HTTP_MSG_CLOSED on the response only means we must abort the
5585 * request.
5586 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5587 * with server-close mode means we've completed one request and we
5588 * must re-initialize the server connection.
5589 */
5590
5591 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5592 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5593 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5594 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005595 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 channel_auto_close(&s->req);
5597 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005598 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005599 channel_auto_close(&s->res);
5600 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005601 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5602 s->req.analysers |= AN_FLT_XFER_DATA;
5603 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5604 s->res.analysers |= AN_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005606 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005607 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005608 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005609 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005610 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005611 channel_auto_close(&s->res);
5612 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005613 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005614 channel_abort(&s->req);
5615 channel_auto_close(&s->req);
5616 channel_auto_read(&s->req);
5617 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005618 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005619 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5620 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005621 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005622 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5623 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5624 /* server-close/keep-alive: terminate this transaction,
5625 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005626 * a fresh-new transaction, but only once we're sure there's
5627 * enough room in the request and response buffer to process
5628 * another request. The request buffer must not hold any
5629 * pending output data and the request buffer must not have
5630 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005631 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005632 if (s->req.buf->o)
5633 s->req.flags |= CF_WAKE_WRITE;
5634 else if (channel_congested(&s->res))
5635 s->res.flags |= CF_WAKE_WRITE;
5636 else
5637 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005638 }
5639
Willy Tarreau610ecce2010-01-04 21:15:02 +01005640 return txn->req.msg_state != old_req_state ||
5641 txn->rsp.msg_state != old_res_state;
5642}
5643
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644/* This function is an analyser which forwards request body (including chunk
5645 * sizes if any). It is called as soon as we must forward, even if we forward
5646 * zero byte. The only situation where it must not be called is when we're in
5647 * tunnel mode and we want to forward till the close. It's used both to forward
5648 * remaining data and to resync after end of body. It expects the msg_state to
5649 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005650 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005651 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005652 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005654int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005656 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005657 struct http_txn *txn = s->txn;
5658 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005659 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005660
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005661 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5662 return 0;
5663
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005664 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005665 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005666 /* Output closed while we were sending data. We must abort and
5667 * wake the other side up.
5668 */
5669 msg->msg_state = HTTP_MSG_ERROR;
5670 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005671 return 1;
5672 }
5673
Willy Tarreaud98cf932009-12-27 22:54:55 +01005674 /* Note that we don't have to send 100-continue back because we don't
5675 * need the data to complete our job, and it's up to the server to
5676 * decide whether to return 100, 417 or anything else in return of
5677 * an "Expect: 100-continue" header.
5678 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005679 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005680 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5681 ? HTTP_MSG_CHUNK_SIZE
5682 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005683
5684 /* TODO/filters: when http-buffer-request option is set or if a
5685 * rule on url_param exists, the first chunk size could be
5686 * already parsed. In that case, msg->next is after the chunk
5687 * size (including the CRLF after the size). So this case should
5688 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005689 }
5690
Willy Tarreau7ba23542014-04-17 21:50:00 +02005691 /* Some post-connect processing might want us to refrain from starting to
5692 * forward data. Currently, the only reason for this is "balance url_param"
5693 * whichs need to parse/process the request after we've enabled forwarding.
5694 */
5695 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005696 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005697 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005698 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005700 }
5701 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5702 }
5703
Willy Tarreau80a92c02014-03-12 10:41:13 +01005704 /* in most states, we should abort in case of early close */
5705 channel_auto_close(req);
5706
Willy Tarreauefdf0942014-04-24 20:08:57 +02005707 if (req->to_forward) {
5708 /* We can't process the buffer's contents yet */
5709 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005710 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005711 }
5712
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005713 if (msg->msg_state < HTTP_MSG_DONE) {
5714 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5715 ? http_msg_forward_chunked_body(s, msg)
5716 : http_msg_forward_body(s, msg));
5717 if (!ret)
5718 goto missing_data_or_waiting;
5719 if (ret < 0)
5720 goto return_bad_req;
5721 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005722
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 /* other states, DONE...TUNNEL */
5724 /* we don't want to forward closes on DONE except in tunnel mode. */
5725 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5726 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005727
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005728 ret = msg->msg_state;
5729 if (http_resync_states(s)) {
5730 /* some state changes occurred, maybe the analyser
5731 * was disabled too. */
5732 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5733 if (req->flags & CF_SHUTW) {
5734 /* request errors are most likely due to the
5735 * server aborting the transfer. */
5736 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005737 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005738 if (msg->err_pos >= 0)
5739 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5740 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005741 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005742 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 }
5744
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005745 /* If "option abortonclose" is set on the backend, we want to monitor
5746 * the client's connection and forward any shutdown notification to the
5747 * server, which will decide whether to close or to go on processing the
5748 * request. We only do that in tunnel mode, and not in other modes since
5749 * it can be abused to exhaust source ports. */
5750 if (s->be->options & PR_O_ABRT_CLOSE) {
5751 channel_auto_read(req);
5752 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5753 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5754 s->si[1].flags |= SI_FL_NOLINGER;
5755 channel_auto_close(req);
5756 }
5757 else if (s->txn->meth == HTTP_METH_POST) {
5758 /* POST requests may require to read extra CRLF sent by broken
5759 * browsers and which could cause an RST to be sent upon close
5760 * on some systems (eg: Linux). */
5761 channel_auto_read(req);
5762 }
5763 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005764
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005765 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005766 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005767 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005768 if (!(s->flags & SF_ERR_MASK))
5769 s->flags |= SF_ERR_CLICL;
5770 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005771 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005772 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005773 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005774 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005775 }
5776
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005777 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005778 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005779 if (objt_server(s->target))
5780 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005781
5782 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005783 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005784
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005785 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005786 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005787 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005788
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005789 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005790 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005791 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005792 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005793 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005794
Willy Tarreau5c620922011-05-11 19:56:11 +02005795 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005796 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005797 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005798 * modes are already handled by the stream sock layer. We must not do
5799 * this in content-length mode because it could present the MSG_MORE
5800 * flag with the last block of forwarded data, which would cause an
5801 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005802 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005803 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005804 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005805
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005806 return 0;
5807
Willy Tarreaud98cf932009-12-27 22:54:55 +01005808 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005809 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005810 if (sess->listener->counters)
5811 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005812
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005813 return_bad_req_stats_ok:
5814 txn->req.msg_state = HTTP_MSG_ERROR;
5815 if (txn->status) {
5816 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005817 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005818 } else {
5819 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005820 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005821 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005822 req->analysers &= AN_FLT_END;
5823 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005824
Willy Tarreaue7dff022015-04-03 01:14:29 +02005825 if (!(s->flags & SF_ERR_MASK))
5826 s->flags |= SF_ERR_PRXCOND;
5827 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005828 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005829 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005830 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005831 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005832 }
5833 return 0;
5834
5835 aborted_xfer:
5836 txn->req.msg_state = HTTP_MSG_ERROR;
5837 if (txn->status) {
5838 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005839 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005840 } else {
5841 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005842 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005843 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005844 req->analysers &= AN_FLT_END;
5845 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005846
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005847 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005848 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005849 if (objt_server(s->target))
5850 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005851
Willy Tarreaue7dff022015-04-03 01:14:29 +02005852 if (!(s->flags & SF_ERR_MASK))
5853 s->flags |= SF_ERR_SRVCL;
5854 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005857 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005858 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005860 return 0;
5861}
5862
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005863/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5864 * processing can continue on next analysers, or zero if it either needs more
5865 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005866 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005867 * when it has nothing left to do, and may remove any analyser when it wants to
5868 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005869 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005870int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005871{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005872 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005873 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005875 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005876 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005878 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005879
Willy Tarreau87b09662015-04-03 00:22:06 +02005880 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005881 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005882 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005883 rep,
5884 rep->rex, rep->wex,
5885 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005886 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005887 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005888
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005889 /*
5890 * Now parse the partial (or complete) lines.
5891 * We will check the response syntax, and also join multi-line
5892 * headers. An index of all the lines will be elaborated while
5893 * parsing.
5894 *
5895 * For the parsing, we use a 28 states FSM.
5896 *
5897 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005898 * rep->buf->p = beginning of response
5899 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5900 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005901 * msg->eol = end of current header or line (LF or CRLF)
5902 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 */
5904
Willy Tarreau628c40c2014-04-24 19:11:26 +02005905 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005906 /* There's a protected area at the end of the buffer for rewriting
5907 * purposes. We don't want to start to parse the request if the
5908 * protected area is affected, because we may have to move processed
5909 * data later, which is much more complicated.
5910 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005911 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005912 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005913 /* some data has still not left the buffer, wake us once that's done */
5914 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5915 goto abort_response;
5916 channel_dont_close(rep);
5917 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005918 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005919 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005920 }
5921
Willy Tarreau379357a2013-06-08 12:55:46 +02005922 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5923 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5924 buffer_slow_realign(rep->buf);
5925
Willy Tarreau9b28e032012-10-12 23:49:43 +02005926 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005927 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005928 }
5929
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005930 /* 1: we might have to print this header in debug mode */
5931 if (unlikely((global.mode & MODE_DEBUG) &&
5932 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005933 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005934 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935
Willy Tarreau9b28e032012-10-12 23:49:43 +02005936 sol = rep->buf->p;
5937 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 sol += hdr_idx_first_pos(&txn->hdr_idx);
5941 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005942
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005943 while (cur_idx) {
5944 eol = sol + txn->hdr_idx.v[cur_idx].len;
5945 debug_hdr("srvhdr", s, sol, eol);
5946 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5947 cur_idx = txn->hdr_idx.v[cur_idx].next;
5948 }
5949 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005951 /*
5952 * Now we quickly check if we have found a full valid response.
5953 * If not so, we check the FD and buffer states before leaving.
5954 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005955 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 * responses are checked first.
5957 *
5958 * Depending on whether the client is still there or not, we
5959 * may send an error response back or not. Note that normally
5960 * we should only check for HTTP status there, and check I/O
5961 * errors somewhere else.
5962 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005963
Willy Tarreau655dce92009-11-08 13:10:58 +01005964 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965 /* Invalid response */
5966 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5967 /* we detected a parsing error. We want to archive this response
5968 * in the dedicated proxy area for later troubleshooting.
5969 */
5970 hdr_response_bad:
5971 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005972 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005974 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005975 if (objt_server(s->target)) {
5976 objt_server(s->target)->counters.failed_resp++;
5977 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005978 }
Willy Tarreau64648412010-03-05 10:41:54 +01005979 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005980 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005981 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005983 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005984 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005985 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986
Willy Tarreaue7dff022015-04-03 01:14:29 +02005987 if (!(s->flags & SF_ERR_MASK))
5988 s->flags |= SF_ERR_PRXCOND;
5989 if (!(s->flags & SF_FINST_MASK))
5990 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991
5992 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005993 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005996 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005997 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999 goto hdr_response_bad;
6000 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006003 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006004 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006005 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006006 else if (txn->flags & TX_NOT_FIRST)
6007 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006008
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006009 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006010 if (objt_server(s->target)) {
6011 objt_server(s->target)->counters.failed_resp++;
6012 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006013 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006014
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006015 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006016 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006017 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006018 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006019 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006020 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006021
Willy Tarreaue7dff022015-04-03 01:14:29 +02006022 if (!(s->flags & SF_ERR_MASK))
6023 s->flags |= SF_ERR_SRVCL;
6024 if (!(s->flags & SF_FINST_MASK))
6025 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006026 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006027 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006028
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006029 /* read timeout : return a 504 to the client. */
6030 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006031 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006032 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006033
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++;
6037 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006038 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006039
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006040 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006041 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006043 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006044 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006045 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006046
Willy Tarreaue7dff022015-04-03 01:14:29 +02006047 if (!(s->flags & SF_ERR_MASK))
6048 s->flags |= SF_ERR_SRVTO;
6049 if (!(s->flags & SF_FINST_MASK))
6050 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006051 return 0;
6052 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006053
Willy Tarreauf003d372012-11-26 13:35:37 +01006054 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006055 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006056 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006057 s->be->be_counters.cli_aborts++;
6058 if (objt_server(s->target))
6059 objt_server(s->target)->counters.cli_aborts++;
6060
Christopher Fauletd7c91962015-04-30 11:48:27 +02006061 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006062 channel_auto_close(rep);
6063
6064 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006065 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006066 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006067
Willy Tarreaue7dff022015-04-03 01:14:29 +02006068 if (!(s->flags & SF_ERR_MASK))
6069 s->flags |= SF_ERR_CLICL;
6070 if (!(s->flags & SF_FINST_MASK))
6071 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006072
Willy Tarreau87b09662015-04-03 00:22:06 +02006073 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006074 return 0;
6075 }
6076
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006077 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006078 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006079 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006080 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006081 else if (txn->flags & TX_NOT_FIRST)
6082 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006083
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006084 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006085 if (objt_server(s->target)) {
6086 objt_server(s->target)->counters.failed_resp++;
6087 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006088 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006089
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006090 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006091 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006092 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006093 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006094 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006095 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006096
Willy Tarreaue7dff022015-04-03 01:14:29 +02006097 if (!(s->flags & SF_ERR_MASK))
6098 s->flags |= SF_ERR_SRVCL;
6099 if (!(s->flags & SF_FINST_MASK))
6100 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006101 return 0;
6102 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006103
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006105 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006106 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006107 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006108 else if (txn->flags & TX_NOT_FIRST)
6109 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006110
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006111 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006112 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006113 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006114
Willy Tarreaue7dff022015-04-03 01:14:29 +02006115 if (!(s->flags & SF_ERR_MASK))
6116 s->flags |= SF_ERR_CLICL;
6117 if (!(s->flags & SF_FINST_MASK))
6118 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006119
Willy Tarreau87b09662015-04-03 00:22:06 +02006120 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006121 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006122 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006123
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006124 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006125 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006126 return 0;
6127 }
6128
6129 /* More interesting part now : we know that we have a complete
6130 * response which at least looks like HTTP. We have an indicator
6131 * of each header's length, so we can parse them quickly.
6132 */
6133
6134 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006135 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006136
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006137 /*
6138 * 1: get the status code
6139 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006140 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006141 if (n < 1 || n > 5)
6142 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006143 /* when the client triggers a 4xx from the server, it's most often due
6144 * to a missing object or permission. These events should be tracked
6145 * because if they happen often, it may indicate a brute force or a
6146 * vulnerability scan.
6147 */
6148 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006149 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006150
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006151 if (objt_server(s->target))
6152 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006153
Willy Tarreau91852eb2015-05-01 13:26:00 +02006154 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6155 * exactly one digit "." one digit. This check may be disabled using
6156 * option accept-invalid-http-response.
6157 */
6158 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6159 if (msg->sl.st.v_l != 8) {
6160 msg->err_pos = 0;
6161 goto hdr_response_bad;
6162 }
6163
6164 if (rep->buf->p[4] != '/' ||
6165 !isdigit((unsigned char)rep->buf->p[5]) ||
6166 rep->buf->p[6] != '.' ||
6167 !isdigit((unsigned char)rep->buf->p[7])) {
6168 msg->err_pos = 4;
6169 goto hdr_response_bad;
6170 }
6171 }
6172
Willy Tarreau5b154472009-12-21 20:11:07 +01006173 /* check if the response is HTTP/1.1 or above */
6174 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006175 ((rep->buf->p[5] > '1') ||
6176 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006177 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006178
6179 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006180 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 +01006181
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006182 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006183 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006184
Willy Tarreau9b28e032012-10-12 23:49:43 +02006185 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006186
Willy Tarreau39650402010-03-15 19:44:39 +01006187 /* Adjust server's health based on status code. Note: status codes 501
6188 * and 505 are triggered on demand by client request, so we must not
6189 * count them as server failures.
6190 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006191 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006192 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006193 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006194 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006195 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006196 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006197
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006198 /*
6199 * 2: check for cacheability.
6200 */
6201
6202 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006203 case 100:
6204 /*
6205 * We may be facing a 100-continue response, in which case this
6206 * is not the right response, and we're waiting for the next one.
6207 * Let's allow this response to go to the client and wait for the
6208 * next one.
6209 */
6210 hdr_idx_init(&txn->hdr_idx);
6211 msg->next -= channel_forward(rep, msg->next);
6212 msg->msg_state = HTTP_MSG_RPBEFORE;
6213 txn->status = 0;
6214 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006215 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006216 goto next_one;
6217
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006218 case 200:
6219 case 203:
6220 case 206:
6221 case 300:
6222 case 301:
6223 case 410:
6224 /* RFC2616 @13.4:
6225 * "A response received with a status code of
6226 * 200, 203, 206, 300, 301 or 410 MAY be stored
6227 * by a cache (...) unless a cache-control
6228 * directive prohibits caching."
6229 *
6230 * RFC2616 @9.5: POST method :
6231 * "Responses to this method are not cacheable,
6232 * unless the response includes appropriate
6233 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006234 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006235 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006236 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6238 break;
6239 default:
6240 break;
6241 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006242
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006243 /*
6244 * 3: we may need to capture headers
6245 */
6246 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006247 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006248 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006249 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006250
Willy Tarreau557f1992015-05-01 10:05:17 +02006251 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6252 * by RFC7230#3.3.3 :
6253 *
6254 * The length of a message body is determined by one of the following
6255 * (in order of precedence):
6256 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006257 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6258 * the connection will become a tunnel immediately after the empty
6259 * line that concludes the header fields. A client MUST ignore
6260 * any Content-Length or Transfer-Encoding header fields received
6261 * in such a message. Any 101 response (Switching Protocols) is
6262 * managed in the same manner.
6263 *
6264 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006265 * (Informational), 204 (No Content), or 304 (Not Modified) status
6266 * code is always terminated by the first empty line after the
6267 * header fields, regardless of the header fields present in the
6268 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006269 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006270 * 3. If a Transfer-Encoding header field is present and the chunked
6271 * transfer coding (Section 4.1) is the final encoding, the message
6272 * body length is determined by reading and decoding the chunked
6273 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006274 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006275 * If a Transfer-Encoding header field is present in a response and
6276 * the chunked transfer coding is not the final encoding, the
6277 * message body length is determined by reading the connection until
6278 * it is closed by the server. If a Transfer-Encoding header field
6279 * is present in a request and the chunked transfer coding is not
6280 * the final encoding, the message body length cannot be determined
6281 * reliably; the server MUST respond with the 400 (Bad Request)
6282 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006283 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006284 * If a message is received with both a Transfer-Encoding and a
6285 * Content-Length header field, the Transfer-Encoding overrides the
6286 * Content-Length. Such a message might indicate an attempt to
6287 * perform request smuggling (Section 9.5) or response splitting
6288 * (Section 9.4) and ought to be handled as an error. A sender MUST
6289 * remove the received Content-Length field prior to forwarding such
6290 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006291 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006292 * 4. If a message is received without Transfer-Encoding and with
6293 * either multiple Content-Length header fields having differing
6294 * field-values or a single Content-Length header field having an
6295 * invalid value, then the message framing is invalid and the
6296 * recipient MUST treat it as an unrecoverable error. If this is a
6297 * request message, the server MUST respond with a 400 (Bad Request)
6298 * status code and then close the connection. If this is a response
6299 * message received by a proxy, the proxy MUST close the connection
6300 * to the server, discard the received response, and send a 502 (Bad
6301 * Gateway) response to the client. If this is a response message
6302 * received by a user agent, the user agent MUST close the
6303 * connection to the server and discard the received response.
6304 *
6305 * 5. If a valid Content-Length header field is present without
6306 * Transfer-Encoding, its decimal value defines the expected message
6307 * body length in octets. If the sender closes the connection or
6308 * the recipient times out before the indicated number of octets are
6309 * received, the recipient MUST consider the message to be
6310 * incomplete and close the connection.
6311 *
6312 * 6. If this is a request message and none of the above are true, then
6313 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006315 * 7. Otherwise, this is a response message without a declared message
6316 * body length, so the message body length is determined by the
6317 * number of octets received prior to the server closing the
6318 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006319 */
6320
6321 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006322 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006323 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006324 * FIXME: should we parse anyway and return an error on chunked encoding ?
6325 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006326 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6327 txn->status == 101)) {
6328 /* Either we've established an explicit tunnel, or we're
6329 * switching the protocol. In both cases, we're very unlikely
6330 * to understand the next protocols. We have to switch to tunnel
6331 * mode, so that we transfer the request and responses then let
6332 * this protocol pass unmodified. When we later implement specific
6333 * parsers for such protocols, we'll want to check the Upgrade
6334 * header which contains information about that protocol for
6335 * responses with status 101 (eg: see RFC2817 about TLS).
6336 */
6337 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6338 msg->flags |= HTTP_MSGF_XFER_LEN;
6339 goto end;
6340 }
6341
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006342 if (txn->meth == HTTP_METH_HEAD ||
6343 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006344 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006345 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006346 goto skip_content_length;
6347 }
6348
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006349 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006350 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006351 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006352 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006353 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6354 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006355 /* bad transfer-encoding (chunked followed by something else) */
6356 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006357 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006358 break;
6359 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006360 }
6361
Willy Tarreau1c913912015-04-30 10:57:51 +02006362 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006364 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006365 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6366 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6367 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006368 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006369 signed long long cl;
6370
Willy Tarreauad14f752011-09-02 20:33:27 +02006371 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006372 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006373 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006374 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006375
Willy Tarreauad14f752011-09-02 20:33:27 +02006376 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006377 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006378 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006379 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006380
Willy Tarreauad14f752011-09-02 20:33:27 +02006381 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006382 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006383 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006384 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006385
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006386 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006387 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006388 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006389 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006390
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006391 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006392 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006393 }
6394
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006395 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006396 /* Now we have to check if we need to modify the Connection header.
6397 * This is more difficult on the response than it is on the request,
6398 * because we can have two different HTTP versions and we don't know
6399 * how the client will interprete a response. For instance, let's say
6400 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6401 * HTTP/1.1 response without any header. Maybe it will bound itself to
6402 * HTTP/1.0 because it only knows about it, and will consider the lack
6403 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6404 * the lack of header as a keep-alive. Thus we will use two flags
6405 * indicating how a request MAY be understood by the client. In case
6406 * of multiple possibilities, we'll fix the header to be explicit. If
6407 * ambiguous cases such as both close and keepalive are seen, then we
6408 * will fall back to explicit close. Note that we won't take risks with
6409 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006410 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006411 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006412 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6413 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6414 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6415 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006416 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006417
Willy Tarreau70dffda2014-01-30 03:07:23 +01006418 /* this situation happens when combining pretend-keepalive with httpclose. */
6419 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006420 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006421 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006422 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6423
Willy Tarreau60466522010-01-18 19:08:45 +01006424 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006425 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006426 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6427 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006428
Willy Tarreau60466522010-01-18 19:08:45 +01006429 /* now adjust header transformations depending on current state */
6430 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6431 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6432 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006433 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006434 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006435 }
Willy Tarreau60466522010-01-18 19:08:45 +01006436 else { /* SCL / KAL */
6437 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006438 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006439 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006440 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006441
Willy Tarreau60466522010-01-18 19:08:45 +01006442 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006443 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006444
Willy Tarreau60466522010-01-18 19:08:45 +01006445 /* Some keep-alive responses are converted to Server-close if
6446 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006447 */
Willy Tarreau60466522010-01-18 19:08:45 +01006448 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6449 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006450 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006451 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006452 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006453 }
6454
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006455 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006456 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006457 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006458
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006459 /* end of job, return OK */
6460 rep->analysers &= ~an_bit;
6461 rep->analyse_exp = TICK_ETERNITY;
6462 channel_auto_close(rep);
6463 return 1;
6464
6465 abort_keep_alive:
6466 /* A keep-alive request to the server failed on a network error.
6467 * The client is required to retry. We need to close without returning
6468 * any other information so that the client retries.
6469 */
6470 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006471 rep->analysers &= AN_FLT_END;
6472 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006473 channel_auto_close(rep);
6474 s->logs.logwait = 0;
6475 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006476 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006477 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006478 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006479 return 0;
6480}
6481
6482/* This function performs all the processing enabled for the current response.
6483 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006484 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006485 * other functions. It works like process_request (see indications above).
6486 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006487int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006488{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006489 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006490 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006491 struct http_msg *msg = &txn->rsp;
6492 struct proxy *cur_proxy;
6493 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006494 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006495
Willy Tarreau87b09662015-04-03 00:22:06 +02006496 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006497 now_ms, __FUNCTION__,
6498 s,
6499 rep,
6500 rep->rex, rep->wex,
6501 rep->flags,
6502 rep->buf->i,
6503 rep->analysers);
6504
6505 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6506 return 0;
6507
Willy Tarreau70730dd2014-04-24 18:06:27 +02006508 /* The stats applet needs to adjust the Connection header but we don't
6509 * apply any filter there.
6510 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006511 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6512 rep->analysers &= ~an_bit;
6513 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006514 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006515 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 /*
6518 * We will have to evaluate the filters.
6519 * As opposed to version 1.2, now they will be evaluated in the
6520 * filters order and not in the header order. This means that
6521 * each filter has to be validated among all headers.
6522 *
6523 * Filters are tried with ->be first, then with ->fe if it is
6524 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006525 *
6526 * Maybe we are in resume condiion. In this case I choose the
6527 * "struct proxy" which contains the rule list matching the resume
6528 * pointer. If none of theses "struct proxy" match, I initialise
6529 * the process with the first one.
6530 *
6531 * In fact, I check only correspondance betwwen the current list
6532 * pointer and the ->fe rule list. If it doesn't match, I initialize
6533 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006534 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006535 if (s->current_rule_list == &sess->fe->http_res_rules)
6536 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006537 else
6538 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006539 while (1) {
6540 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006541
Willy Tarreau58975672014-04-24 21:13:57 +02006542 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006543 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006544 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006545
Willy Tarreau51d861a2015-05-22 17:30:48 +02006546 if (ret == HTTP_RULE_RES_BADREQ)
6547 goto return_srv_prx_502;
6548
6549 if (ret == HTTP_RULE_RES_DONE) {
6550 rep->analysers &= ~an_bit;
6551 rep->analyse_exp = TICK_ETERNITY;
6552 return 1;
6553 }
6554 }
6555
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006556 /* we need to be called again. */
6557 if (ret == HTTP_RULE_RES_YIELD) {
6558 channel_dont_close(rep);
6559 return 0;
6560 }
6561
Willy Tarreau58975672014-04-24 21:13:57 +02006562 /* try headers filters */
6563 if (rule_set->rsp_exp != NULL) {
6564 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6565 return_bad_resp:
6566 if (objt_server(s->target)) {
6567 objt_server(s->target)->counters.failed_resp++;
6568 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006569 }
Willy Tarreau58975672014-04-24 21:13:57 +02006570 s->be->be_counters.failed_resp++;
6571 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006572 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006573 txn->status = 502;
6574 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006575 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006576 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006577 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006578 if (!(s->flags & SF_ERR_MASK))
6579 s->flags |= SF_ERR_PRXCOND;
6580 if (!(s->flags & SF_FINST_MASK))
6581 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006582 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006583 }
Willy Tarreau58975672014-04-24 21:13:57 +02006584 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006585
Willy Tarreau58975672014-04-24 21:13:57 +02006586 /* has the response been denied ? */
6587 if (txn->flags & TX_SVDENY) {
6588 if (objt_server(s->target))
6589 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006590
Willy Tarreau58975672014-04-24 21:13:57 +02006591 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006592 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006593 if (sess->listener->counters)
6594 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006595
Willy Tarreau58975672014-04-24 21:13:57 +02006596 goto return_srv_prx_502;
6597 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006598
Willy Tarreau58975672014-04-24 21:13:57 +02006599 /* add response headers from the rule sets in the same order */
6600 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006601 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006602 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006603 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006604 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006605 ret = acl_pass(ret);
6606 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6607 ret = !ret;
6608 if (!ret)
6609 continue;
6610 }
6611 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6612 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006614
Willy Tarreau58975672014-04-24 21:13:57 +02006615 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006616 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006617 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006618 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006619 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006620
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006621 /* After this point, this anayzer can't return yield, so we can
6622 * remove the bit corresponding to this analyzer from the list.
6623 *
6624 * Note that the intermediate returns and goto found previously
6625 * reset the analyzers.
6626 */
6627 rep->analysers &= ~an_bit;
6628 rep->analyse_exp = TICK_ETERNITY;
6629
Willy Tarreau58975672014-04-24 21:13:57 +02006630 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006631 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006632 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006633
Willy Tarreau58975672014-04-24 21:13:57 +02006634 /*
6635 * Now check for a server cookie.
6636 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006637 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006638 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 /*
6641 * Check for cache-control or pragma headers if required.
6642 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006643 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02006644 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006645
Willy Tarreau58975672014-04-24 21:13:57 +02006646 /*
6647 * Add server cookie in the response if needed
6648 */
6649 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6650 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006651 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006652 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6653 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6654 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6655 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6656 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006657 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006658 /* the server is known, it's not the one the client requested, or the
6659 * cookie's last seen date needs to be refreshed. We have to
6660 * insert a set-cookie here, except if we want to insert only on POST
6661 * requests and this one isn't. Note that servers which don't have cookies
6662 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663 */
Willy Tarreau58975672014-04-24 21:13:57 +02006664 if (!objt_server(s->target)->cookie) {
6665 chunk_printf(&trash,
6666 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6667 s->be->cookie_name);
6668 }
6669 else {
6670 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671
Willy Tarreau58975672014-04-24 21:13:57 +02006672 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6673 /* emit last_date, which is mandatory */
6674 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6675 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6676 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006677
Willy Tarreau58975672014-04-24 21:13:57 +02006678 if (s->be->cookie_maxlife) {
6679 /* emit first_date, which is either the original one or
6680 * the current date.
6681 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006682 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006683 s30tob64(txn->cookie_first_date ?
6684 txn->cookie_first_date >> 2 :
6685 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006686 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006687 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006688 }
Willy Tarreau58975672014-04-24 21:13:57 +02006689 chunk_appendf(&trash, "; path=/");
6690 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006691
Willy Tarreau58975672014-04-24 21:13:57 +02006692 if (s->be->cookie_domain)
6693 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006694
Willy Tarreau58975672014-04-24 21:13:57 +02006695 if (s->be->ck_opts & PR_CK_HTTPONLY)
6696 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 if (s->be->ck_opts & PR_CK_SECURE)
6699 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006700
Willy Tarreau58975672014-04-24 21:13:57 +02006701 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6702 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006703
Willy Tarreau58975672014-04-24 21:13:57 +02006704 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006705 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006706 /* the server did not change, only the date was updated */
6707 txn->flags |= TX_SCK_UPDATED;
6708 else
6709 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 /* Here, we will tell an eventual cache on the client side that we don't
6712 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6713 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6714 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006715 */
Willy Tarreau58975672014-04-24 21:13:57 +02006716 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006717
Willy Tarreau58975672014-04-24 21:13:57 +02006718 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6721 "Cache-control: private", 22) < 0))
6722 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006723 }
Willy Tarreau58975672014-04-24 21:13:57 +02006724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 /*
6727 * Check if result will be cacheable with a cookie.
6728 * We'll block the response if security checks have caught
6729 * nasty things such as a cacheable cookie.
6730 */
6731 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6732 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6733 (s->be->options & PR_O_CHK_CACHE)) {
6734 /* we're in presence of a cacheable response containing
6735 * a set-cookie header. We'll block it as requested by
6736 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737 */
Willy Tarreau58975672014-04-24 21:13:57 +02006738 if (objt_server(s->target))
6739 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006740
Willy Tarreau58975672014-04-24 21:13:57 +02006741 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006742 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006743 if (sess->listener->counters)
6744 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745
Willy Tarreau58975672014-04-24 21:13:57 +02006746 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6747 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6748 send_log(s->be, LOG_ALERT,
6749 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6750 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6751 goto return_srv_prx_502;
6752 }
Willy Tarreau03945942009-12-22 16:50:27 +01006753
Willy Tarreau70730dd2014-04-24 18:06:27 +02006754 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006755 /*
6756 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6757 * If an "Upgrade" token is found, the header is left untouched in order
6758 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006759 * if anything but "Upgrade" is present in the Connection header. We don't
6760 * want to touch any 101 response either since it's switching to another
6761 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006762 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006763 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006764 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006765 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006766 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6767 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006768
Willy Tarreau58975672014-04-24 21:13:57 +02006769 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6770 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6771 /* we want a keep-alive response here. Keep-alive header
6772 * required if either side is not 1.1.
6773 */
6774 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6775 want_flags |= TX_CON_KAL_SET;
6776 }
6777 else {
6778 /* we want a close response here. Close header required if
6779 * the server is 1.1, regardless of the client.
6780 */
6781 if (msg->flags & HTTP_MSGF_VER_11)
6782 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784
Willy Tarreau58975672014-04-24 21:13:57 +02006785 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6786 http_change_connection_header(txn, msg, want_flags);
6787 }
6788
6789 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006790 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6792 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006793 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006794 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795
Willy Tarreau58975672014-04-24 21:13:57 +02006796 /* if the user wants to log as soon as possible, without counting
6797 * bytes from the server, then this is the right moment. We have
6798 * to temporarily assign bytes_out to log what we currently have.
6799 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006800 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006801 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6802 s->logs.bytes_out = txn->rsp.eoh;
6803 s->do_log(s);
6804 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006805 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006806 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006807}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
Willy Tarreaud98cf932009-12-27 22:54:55 +01006809/* This function is an analyser which forwards response body (including chunk
6810 * sizes if any). It is called as soon as we must forward, even if we forward
6811 * zero byte. The only situation where it must not be called is when we're in
6812 * tunnel mode and we want to forward till the close. It's used both to forward
6813 * remaining data and to resync after end of body. It expects the msg_state to
6814 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006815 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006816 *
6817 * It is capable of compressing response data both in content-length mode and
6818 * in chunked mode. The state machines follows different flows depending on
6819 * whether content-length and chunked modes are used, since there are no
6820 * trailers in content-length :
6821 *
6822 * chk-mode cl-mode
6823 * ,----- BODY -----.
6824 * / \
6825 * V size > 0 V chk-mode
6826 * .--> SIZE -------------> DATA -------------> CRLF
6827 * | | size == 0 | last byte |
6828 * | v final crlf v inspected |
6829 * | TRAILERS -----------> DONE |
6830 * | |
6831 * `----------------------------------------------'
6832 *
6833 * Compression only happens in the DATA state, and must be flushed in final
6834 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6835 * is performed at once on final states for all bytes parsed, or when leaving
6836 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006837 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006838int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006839{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006840 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006841 struct http_txn *txn = s->txn;
6842 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006843 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006844
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006845 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6846 return 0;
6847
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006848 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006849 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006850 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006851 /* Output closed while we were sending data. We must abort and
6852 * wake the other side up.
6853 */
6854 msg->msg_state = HTTP_MSG_ERROR;
6855 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006856 return 1;
6857 }
6858
Willy Tarreau4fe41902010-06-07 22:27:41 +02006859 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006860 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006861
Christopher Fauletd7c91962015-04-30 11:48:27 +02006862 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006863 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6864 ? HTTP_MSG_CHUNK_SIZE
6865 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006866 }
6867
Willy Tarreauefdf0942014-04-24 20:08:57 +02006868 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006869 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006870 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006871 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006872 }
6873
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006874 if (msg->msg_state < HTTP_MSG_DONE) {
6875 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6876 ? http_msg_forward_chunked_body(s, msg)
6877 : http_msg_forward_body(s, msg));
6878 if (!ret)
6879 goto missing_data_or_waiting;
6880 if (ret < 0)
6881 goto return_bad_res;
6882 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006883
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006884 /* other states, DONE...TUNNEL */
6885 /* for keep-alive we don't want to forward closes on DONE */
6886 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6887 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6888 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006889
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006890 ret = msg->msg_state;
6891 if (http_resync_states(s)) {
6892 /* some state changes occurred, maybe the analyser was disabled
6893 * too. */
6894 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6895 if (res->flags & CF_SHUTW) {
6896 /* response errors are most likely due to the
6897 * client aborting the transfer. */
6898 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006899 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006900 if (msg->err_pos >= 0)
6901 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6902 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006904 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006905 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006906 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006907
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006908 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006909 if (res->flags & CF_SHUTW)
6910 goto aborted_xfer;
6911
6912 /* stop waiting for data if the input is closed before the end. If the
6913 * client side was already closed, it means that the client has aborted,
6914 * so we don't want to count this as a server abort. Otherwise it's a
6915 * server abort.
6916 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006917 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006918 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006919 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006920 /* If we have some pending data, we continue the processing */
6921 if (!buffer_pending(res->buf)) {
6922 if (!(s->flags & SF_ERR_MASK))
6923 s->flags |= SF_ERR_SRVCL;
6924 s->be->be_counters.srv_aborts++;
6925 if (objt_server(s->target))
6926 objt_server(s->target)->counters.srv_aborts++;
6927 goto return_bad_res_stats_ok;
6928 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006929 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006930
Willy Tarreau40dba092010-03-04 18:14:51 +01006931 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006932 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006933 goto return_bad_res;
6934
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006935 /* When TE: chunked is used, we need to get there again to parse
6936 * remaining chunks even if the server has closed, so we don't want to
6937 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6938 * keep-alive is set on the client side or if there are filters
6939 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006940 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006941 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006942 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006943 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6944 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006945 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006946
Willy Tarreau5c620922011-05-11 19:56:11 +02006947 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006948 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006949 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006950 * modes are already handled by the stream sock layer. We must not do
6951 * this in content-length mode because it could present the MSG_MORE
6952 * flag with the last block of forwarded data, which would cause an
6953 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006954 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006955 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006956 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006957
Willy Tarreau87b09662015-04-03 00:22:06 +02006958 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006959 return 0;
6960
Willy Tarreau40dba092010-03-04 18:14:51 +01006961 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006962 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006963 if (objt_server(s->target))
6964 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006965
6966 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006967 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006968 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006969 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006970 res->analysers &= AN_FLT_END;
6971 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006972 if (objt_server(s->target))
6973 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006974
Willy Tarreaue7dff022015-04-03 01:14:29 +02006975 if (!(s->flags & SF_ERR_MASK))
6976 s->flags |= SF_ERR_PRXCOND;
6977 if (!(s->flags & SF_FINST_MASK))
6978 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006979 return 0;
6980
6981 aborted_xfer:
6982 txn->rsp.msg_state = HTTP_MSG_ERROR;
6983 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006984 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006985 res->analysers &= AN_FLT_END;
6986 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006987
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006988 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006989 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006990 if (objt_server(s->target))
6991 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006992
Willy Tarreaue7dff022015-04-03 01:14:29 +02006993 if (!(s->flags & SF_ERR_MASK))
6994 s->flags |= SF_ERR_CLICL;
6995 if (!(s->flags & SF_FINST_MASK))
6996 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006997 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006998}
6999
7000
7001static inline int
7002http_msg_forward_body(struct stream *s, struct http_msg *msg)
7003{
7004 struct channel *chn = msg->chn;
7005 int ret;
7006
7007 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7008
7009 if (msg->msg_state == HTTP_MSG_ENDING)
7010 goto ending;
7011
7012 /* Neither content-length, nor transfer-encoding was found, so we must
7013 * read the body until the server connection is closed. In that case, we
7014 * eat data as they come. Of course, this happens for response only. */
7015 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7016 unsigned long long len = (chn->buf->i - msg->next);
7017 msg->chunk_len += len;
7018 msg->body_len += len;
7019 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007020 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7021 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7022 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007023 msg->next += ret;
7024 msg->chunk_len -= ret;
7025 if (msg->chunk_len) {
7026 /* input empty or output full */
7027 if (chn->buf->i > msg->next)
7028 chn->flags |= CF_WAKE_WRITE;
7029 goto missing_data_or_waiting;
7030 }
7031
Christopher Faulet32359572016-11-29 14:49:54 +01007032 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR) &&
7033 HAS_DATA_FILTERS(s, chn)) {
7034 /* The server still sending data that should be filtered */
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007035 goto missing_data_or_waiting;
7036 }
7037 msg->msg_state = HTTP_MSG_ENDING;
7038
7039 ending:
7040 /* we may have some pending data starting at res->buf->p such as a last
7041 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007042 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7043 /* default_ret */ msg->next,
7044 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007045 b_adv(chn->buf, ret);
7046 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007047 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7048 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007049 if (msg->next)
7050 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007051
Christopher Fauletda02e172015-12-04 09:25:05 +01007052 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7053 /* default_ret */ 1,
7054 /* on_error */ goto error,
7055 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007056 msg->msg_state = HTTP_MSG_DONE;
7057 return 1;
7058
7059 missing_data_or_waiting:
7060 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007061 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7062 /* default_ret */ msg->next,
7063 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007064 b_adv(chn->buf, ret);
7065 msg->next -= ret;
7066 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7067 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007068 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007069 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007070 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007071 return 0;
7072 error:
7073 return -1;
7074}
7075
7076static inline int
7077http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7078{
7079 struct channel *chn = msg->chn;
7080 int ret;
7081
7082 /* Here we have the guarantee to be in one of the following state:
7083 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7084 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7085
7086 switch_states:
7087 switch (msg->msg_state) {
7088 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007089 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7090 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7091 /* on_error */ goto error);
7092 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007093 msg->chunk_len -= ret;
7094 if (msg->chunk_len) {
7095 /* input empty or output full */
7096 if (chn->buf->i > msg->next)
7097 chn->flags |= CF_WAKE_WRITE;
7098 goto missing_data_or_waiting;
7099 }
7100
7101 /* nothing left to forward for this chunk*/
7102 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7103 /* fall through for HTTP_MSG_CHUNK_CRLF */
7104
7105 case HTTP_MSG_CHUNK_CRLF:
7106 /* we want the CRLF after the data */
7107 ret = http_skip_chunk_crlf(msg);
7108 if (ret == 0)
7109 goto missing_data_or_waiting;
7110 if (ret < 0)
7111 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007112 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007113 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7114 /* fall through for HTTP_MSG_CHUNK_SIZE */
7115
7116 case HTTP_MSG_CHUNK_SIZE:
7117 /* read the chunk size and assign it to ->chunk_len,
7118 * then set ->next to point to the body and switch to
7119 * DATA or TRAILERS state.
7120 */
7121 ret = http_parse_chunk_size(msg);
7122 if (ret == 0)
7123 goto missing_data_or_waiting;
7124 if (ret < 0)
7125 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007126 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007127 if (msg->chunk_len) {
7128 msg->msg_state = HTTP_MSG_DATA;
7129 goto switch_states;
7130 }
7131 msg->msg_state = HTTP_MSG_TRAILERS;
7132 /* fall through for HTTP_MSG_TRAILERS */
7133
7134 case HTTP_MSG_TRAILERS:
7135 ret = http_forward_trailers(msg);
7136 if (ret < 0)
7137 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007138 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7139 /* default_ret */ 1,
7140 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007141 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007142 if (!ret)
7143 goto missing_data_or_waiting;
7144 break;
7145
7146 case HTTP_MSG_ENDING:
7147 goto ending;
7148
7149 default:
7150 /* This should no happen in this function */
7151 goto error;
7152 }
7153
7154 msg->msg_state = HTTP_MSG_ENDING;
7155 ending:
7156 /* we may have some pending data starting at res->buf->p such as a last
7157 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007158 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007159 /* default_ret */ msg->next,
7160 /* on_error */ goto error);
7161 b_adv(chn->buf, ret);
7162 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007163 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7164 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007165 if (msg->next)
7166 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007167
Christopher Fauletda02e172015-12-04 09:25:05 +01007168 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007169 /* default_ret */ 1,
7170 /* on_error */ goto error,
7171 /* on_wait */ goto waiting);
7172 msg->msg_state = HTTP_MSG_DONE;
7173 return 1;
7174
7175 missing_data_or_waiting:
7176 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007177 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007178 /* default_ret */ msg->next,
7179 /* on_error */ goto error);
7180 b_adv(chn->buf, ret);
7181 msg->next -= ret;
7182 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7183 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007184 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007185 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007186 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007187 return 0;
7188
7189 chunk_parsing_error:
7190 if (msg->err_pos >= 0) {
7191 if (chn->flags & CF_ISRESP)
7192 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7193 msg->msg_state, strm_fe(s));
7194 else
7195 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7196 msg, msg->msg_state, s->be);
7197 }
7198 error:
7199 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007200}
7201
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007202
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203/* Iterate the same filter through all request headers.
7204 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 * Since it can manage the switch to another backend, it updates the per-proxy
7206 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007207 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007208int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007209{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007210 char *cur_ptr, *cur_end, *cur_next;
7211 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007212 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007214 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007215
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007216 last_hdr = 0;
7217
Willy Tarreau9b28e032012-10-12 23:49:43 +02007218 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007219 old_idx = 0;
7220
7221 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007222 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007223 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007224 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007225 (exp->action == ACT_ALLOW ||
7226 exp->action == ACT_DENY ||
7227 exp->action == ACT_TARPIT))
7228 return 0;
7229
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007230 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007231 if (!cur_idx)
7232 break;
7233
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007234 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007235 cur_ptr = cur_next;
7236 cur_end = cur_ptr + cur_hdr->len;
7237 cur_next = cur_end + cur_hdr->cr + 1;
7238
7239 /* Now we have one header between cur_ptr and cur_end,
7240 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007241 */
7242
Willy Tarreau15a53a42015-01-21 13:39:42 +01007243 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007245 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007246 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 last_hdr = 1;
7248 break;
7249
7250 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007251 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 break;
7254
7255 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007256 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 break;
7259
7260 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007261 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7262 if (trash.len < 0)
7263 return -1;
7264
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007265 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266 /* FIXME: if the user adds a newline in the replacement, the
7267 * index will not be recalculated for now, and the new line
7268 * will not be counted as a new header.
7269 */
7270
7271 cur_end += delta;
7272 cur_next += delta;
7273 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007274 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 break;
7276
7277 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007278 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 cur_next += delta;
7280
Willy Tarreaufa355d42009-11-29 18:12:29 +01007281 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007282 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7283 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 cur_hdr->len = 0;
7285 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007286 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 break;
7288
7289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290 }
7291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 /* keep the link from this header to next one in case of later
7293 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 old_idx = cur_idx;
7296 }
7297 return 0;
7298}
7299
7300
7301/* Apply the filter to the request line.
7302 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7303 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 * Since it can manage the switch to another backend, it updates the per-proxy
7305 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007307int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 char *cur_ptr, *cur_end;
7310 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007311 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007312 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313
Willy Tarreau3d300592007-03-18 18:34:41 +01007314 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007316 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007317 (exp->action == ACT_ALLOW ||
7318 exp->action == ACT_DENY ||
7319 exp->action == ACT_TARPIT))
7320 return 0;
7321 else if (exp->action == ACT_REMOVE)
7322 return 0;
7323
7324 done = 0;
7325
Willy Tarreau9b28e032012-10-12 23:49:43 +02007326 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007327 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328
7329 /* Now we have the request line between cur_ptr and cur_end */
7330
Willy Tarreau15a53a42015-01-21 13:39:42 +01007331 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007334 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 done = 1;
7336 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007339 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 done = 1;
7341 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007344 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 done = 1;
7346 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007349 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7350 if (trash.len < 0)
7351 return -1;
7352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007353 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354 /* FIXME: if the user adds a newline in the replacement, the
7355 * index will not be recalculated for now, and the new line
7356 * will not be counted as a new header.
7357 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007358
Willy Tarreaufa355d42009-11-29 18:12:29 +01007359 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007361 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 HTTP_MSG_RQMETH,
7363 cur_ptr, cur_end + 1,
7364 NULL, NULL);
7365 if (unlikely(!cur_end))
7366 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007368 /* we have a full request and we know that we have either a CR
7369 * or an LF at <ptr>.
7370 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007371 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7372 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 /* there is no point trying this regex on headers */
7374 return 1;
7375 }
7376 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 return done;
7378}
Willy Tarreau97de6242006-12-27 17:18:38 +01007379
Willy Tarreau58f10d72006-12-04 02:26:12 +01007380
Willy Tarreau58f10d72006-12-04 02:26:12 +01007381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007383 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 * unparsable request. Since it can manage the switch to another backend, it
7386 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007388int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389{
Willy Tarreau192252e2015-04-04 01:47:55 +02007390 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007391 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007392 struct hdr_exp *exp;
7393
7394 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007397 /*
7398 * The interleaving of transformations and verdicts
7399 * makes it difficult to decide to continue or stop
7400 * the evaluation.
7401 */
7402
Willy Tarreau6c123b12010-01-28 20:22:06 +01007403 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7404 break;
7405
Willy Tarreau3d300592007-03-18 18:34:41 +01007406 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007408 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007410
7411 /* if this filter had a condition, evaluate it now and skip to
7412 * next filter if the condition does not match.
7413 */
7414 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007415 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007416 ret = acl_pass(ret);
7417 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7418 ret = !ret;
7419
7420 if (!ret)
7421 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 }
7423
7424 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007425 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426 if (unlikely(ret < 0))
7427 return -1;
7428
7429 if (likely(ret == 0)) {
7430 /* The filter did not match the request, it can be
7431 * iterated through all headers.
7432 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007433 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7434 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007437 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438}
7439
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007440
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441/* Find the end of a cookie value contained between <s> and <e>. It works the
7442 * same way as with headers above except that the semi-colon also ends a token.
7443 * See RFC2965 for more information. Note that it requires a valid header to
7444 * return a valid result.
7445 */
7446char *find_cookie_value_end(char *s, const char *e)
7447{
7448 int quoted, qdpair;
7449
7450 quoted = qdpair = 0;
7451 for (; s < e; s++) {
7452 if (qdpair) qdpair = 0;
7453 else if (quoted) {
7454 if (*s == '\\') qdpair = 1;
7455 else if (*s == '"') quoted = 0;
7456 }
7457 else if (*s == '"') quoted = 1;
7458 else if (*s == ',' || *s == ';') return s;
7459 }
7460 return s;
7461}
7462
7463/* Delete a value in a header between delimiters <from> and <next> in buffer
7464 * <buf>. The number of characters displaced is returned, and the pointer to
7465 * the first delimiter is updated if required. The function tries as much as
7466 * possible to respect the following principles :
7467 * - replace <from> delimiter by the <next> one unless <from> points to a
7468 * colon, in which case <next> is simply removed
7469 * - set exactly one space character after the new first delimiter, unless
7470 * there are not enough characters in the block being moved to do so.
7471 * - remove unneeded spaces before the previous delimiter and after the new
7472 * one.
7473 *
7474 * It is the caller's responsibility to ensure that :
7475 * - <from> points to a valid delimiter or the colon ;
7476 * - <next> points to a valid delimiter or the final CR/LF ;
7477 * - there are non-space chars before <from> ;
7478 * - there is a CR/LF at or after <next>.
7479 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007480int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481{
7482 char *prev = *from;
7483
7484 if (*prev == ':') {
7485 /* We're removing the first value, preserve the colon and add a
7486 * space if possible.
7487 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007488 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 next++;
7490 prev++;
7491 if (prev < next)
7492 *prev++ = ' ';
7493
Willy Tarreau2235b262016-11-05 15:50:20 +01007494 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 next++;
7496 } else {
7497 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007498 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499 prev--;
7500 *from = prev;
7501
7502 /* copy the delimiter and if possible a space if we're
7503 * not at the end of the line.
7504 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007505 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007506 *prev++ = *next++;
7507 if (prev + 1 < next)
7508 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007509 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007510 next++;
7511 }
7512 }
7513 return buffer_replace2(buf, prev, next, NULL, 0);
7514}
7515
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007516/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007517 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 * desirable to call it only when needed. This code is quite complex because
7519 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7520 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007522void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007524 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007525 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007527 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7529 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007530
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007531 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007532 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007533 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007534
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007535 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007537 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007538
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007539 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007540 hdr_beg = hdr_next;
7541 hdr_end = hdr_beg + cur_hdr->len;
7542 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007543
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 /* We have one full header between hdr_beg and hdr_end, and the
7545 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007546 * "Cookie:" headers.
7547 */
7548
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007550 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007551 old_idx = cur_idx;
7552 continue;
7553 }
7554
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 del_from = NULL; /* nothing to be deleted */
7556 preserve_hdr = 0; /* assume we may kill the whole header */
7557
Willy Tarreau58f10d72006-12-04 02:26:12 +01007558 /* Now look for cookies. Conforming to RFC2109, we have to support
7559 * attributes whose name begin with a '$', and associate them with
7560 * the right cookie, if we want to delete this cookie.
7561 * So there are 3 cases for each cookie read :
7562 * 1) it's a special attribute, beginning with a '$' : ignore it.
7563 * 2) it's a server id cookie that we *MAY* want to delete : save
7564 * some pointers on it (last semi-colon, beginning of cookie...)
7565 * 3) it's an application cookie : we *MAY* have to delete a previous
7566 * "special" cookie.
7567 * At the end of loop, if a "special" cookie remains, we may have to
7568 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 * *MUST* delete it.
7570 *
7571 * Note: RFC2965 is unclear about the processing of spaces around
7572 * the equal sign in the ATTR=VALUE form. A careful inspection of
7573 * the RFC explicitly allows spaces before it, and not within the
7574 * tokens (attrs or values). An inspection of RFC2109 allows that
7575 * too but section 10.1.3 lets one think that spaces may be allowed
7576 * after the equal sign too, resulting in some (rare) buggy
7577 * implementations trying to do that. So let's do what servers do.
7578 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7579 * allowed quoted strings in values, with any possible character
7580 * after a backslash, including control chars and delimitors, which
7581 * causes parsing to become ambiguous. Browsers also allow spaces
7582 * within values even without quotes.
7583 *
7584 * We have to keep multiple pointers in order to support cookie
7585 * removal at the beginning, middle or end of header without
7586 * corrupting the header. All of these headers are valid :
7587 *
7588 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7589 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7590 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7591 * | | | | | | | | |
7592 * | | | | | | | | hdr_end <--+
7593 * | | | | | | | +--> next
7594 * | | | | | | +----> val_end
7595 * | | | | | +-----------> val_beg
7596 * | | | | +--------------> equal
7597 * | | | +----------------> att_end
7598 * | | +---------------------> att_beg
7599 * | +--------------------------> prev
7600 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601 */
7602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7604 /* Iterate through all cookies on this line */
7605
7606 /* find att_beg */
7607 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007608 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007609 att_beg++;
7610
7611 /* find att_end : this is the first character after the last non
7612 * space before the equal. It may be equal to hdr_end.
7613 */
7614 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 while (equal < hdr_end) {
7617 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007618 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007619 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 continue;
7621 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007622 }
7623
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7625 * is between <att_beg> and <equal>, both may be identical.
7626 */
7627
7628 /* look for end of cookie if there is an equal sign */
7629 if (equal < hdr_end && *equal == '=') {
7630 /* look for the beginning of the value */
7631 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007632 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007633 val_beg++;
7634
7635 /* find the end of the value, respecting quotes */
7636 next = find_cookie_value_end(val_beg, hdr_end);
7637
7638 /* make val_end point to the first white space or delimitor after the value */
7639 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007640 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 val_end--;
7642 } else {
7643 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007644 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 /* We have nothing to do with attributes beginning with '$'. However,
7647 * they will automatically be removed if a header before them is removed,
7648 * since they're supposed to be linked together.
7649 */
7650 if (*att_beg == '$')
7651 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 /* Ignore cookies with no equal sign */
7654 if (equal == next) {
7655 /* This is not our cookie, so we must preserve it. But if we already
7656 * scheduled another cookie for removal, we cannot remove the
7657 * complete header, but we can remove the previous block itself.
7658 */
7659 preserve_hdr = 1;
7660 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007661 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007662 val_end += delta;
7663 next += delta;
7664 hdr_end += delta;
7665 hdr_next += delta;
7666 cur_hdr->len += delta;
7667 http_msg_move_end(&txn->req, delta);
7668 prev = del_from;
7669 del_from = NULL;
7670 }
7671 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007672 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 /* if there are spaces around the equal sign, we need to
7675 * strip them otherwise we'll get trouble for cookie captures,
7676 * or even for rewrites. Since this happens extremely rarely,
7677 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007678 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7680 int stripped_before = 0;
7681 int stripped_after = 0;
7682
7683 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007684 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 equal += stripped_before;
7686 val_beg += stripped_before;
7687 }
7688
7689 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007690 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 val_beg += stripped_after;
7692 stripped_before += stripped_after;
7693 }
7694
7695 val_end += stripped_before;
7696 next += stripped_before;
7697 hdr_end += stripped_before;
7698 hdr_next += stripped_before;
7699 cur_hdr->len += stripped_before;
7700 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 /* First, let's see if we want to capture this cookie. We check
7705 * that we don't already have a client side cookie, because we
7706 * can only capture one. Also as an optimisation, we ignore
7707 * cookies shorter than the declared name.
7708 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007709 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7710 (val_end - att_beg >= sess->fe->capture_namelen) &&
7711 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 int log_len = val_end - att_beg;
7713
7714 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7715 Alert("HTTP logging : out of memory.\n");
7716 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007717 if (log_len > sess->fe->capture_len)
7718 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 memcpy(txn->cli_cookie, att_beg, log_len);
7720 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723
Willy Tarreaubca99692010-10-06 19:25:55 +02007724 /* Persistence cookies in passive, rewrite or insert mode have the
7725 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007727 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007729 * For cookies in prefix mode, the form is :
7730 *
7731 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007733 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7734 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7735 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007737
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007738 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7739 * have the server ID between val_beg and delim, and the original cookie between
7740 * delim+1 and val_end. Otherwise, delim==val_end :
7741 *
7742 * Cookie: NAME=SRV; # in all but prefix modes
7743 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7744 * | || || | |+-> next
7745 * | || || | +--> val_end
7746 * | || || +---------> delim
7747 * | || |+------------> val_beg
7748 * | || +-------------> att_end = equal
7749 * | |+-----------------> att_beg
7750 * | +------------------> prev
7751 * +-------------------------> hdr_beg
7752 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007753
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007754 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 for (delim = val_beg; delim < val_end; delim++)
7756 if (*delim == COOKIE_DELIM)
7757 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007758 } else {
7759 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007760 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007761 /* Now check if the cookie contains a date field, which would
7762 * appear after a vertical bar ('|') just after the server name
7763 * and before the delimiter.
7764 */
7765 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7766 if (vbar1) {
7767 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007768 * right is the last seen date. It is a base64 encoded
7769 * 30-bit value representing the UNIX date since the
7770 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007771 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007772 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007773 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007774 if (val_end - vbar1 >= 5) {
7775 val = b64tos30(vbar1);
7776 if (val > 0)
7777 txn->cookie_last_date = val << 2;
7778 }
7779 /* look for a second vertical bar */
7780 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7781 if (vbar1 && (val_end - vbar1 > 5)) {
7782 val = b64tos30(vbar1 + 1);
7783 if (val > 0)
7784 txn->cookie_first_date = val << 2;
7785 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007786 }
7787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
Willy Tarreauf64d1412010-10-07 20:06:11 +02007789 /* if the cookie has an expiration date and the proxy wants to check
7790 * it, then we do that now. We first check if the cookie is too old,
7791 * then only if it has expired. We detect strict overflow because the
7792 * time resolution here is not great (4 seconds). Cookies with dates
7793 * in the future are ignored if their offset is beyond one day. This
7794 * allows an admin to fix timezone issues without expiring everyone
7795 * and at the same time avoids keeping unwanted side effects for too
7796 * long.
7797 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007798 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7799 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007800 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007801 txn->flags &= ~TX_CK_MASK;
7802 txn->flags |= TX_CK_OLD;
7803 delim = val_beg; // let's pretend we have not found the cookie
7804 txn->cookie_first_date = 0;
7805 txn->cookie_last_date = 0;
7806 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007807 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7808 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007809 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007810 txn->flags &= ~TX_CK_MASK;
7811 txn->flags |= TX_CK_EXPIRED;
7812 delim = val_beg; // let's pretend we have not found the cookie
7813 txn->cookie_first_date = 0;
7814 txn->cookie_last_date = 0;
7815 }
7816
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007817 /* Here, we'll look for the first running server which supports the cookie.
7818 * This allows to share a same cookie between several servers, for example
7819 * to dedicate backup servers to specific servers only.
7820 * However, to prevent clients from sticking to cookie-less backup server
7821 * when they have incidentely learned an empty cookie, we simply ignore
7822 * empty cookies and mark them as invalid.
7823 * The same behaviour is applied when persistence must be ignored.
7824 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007825 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007826 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007828 while (srv) {
7829 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7830 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007831 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007832 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007833 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007834 /* we found the server and we can use it */
7835 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007836 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007837 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007838 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007839 break;
7840 } else {
7841 /* we found a server, but it's down,
7842 * mark it as such and go on in case
7843 * another one is available.
7844 */
7845 txn->flags &= ~TX_CK_MASK;
7846 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007848 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007849 srv = srv->next;
7850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007851
Willy Tarreauf64d1412010-10-07 20:06:11 +02007852 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007853 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007854 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007855 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007856 txn->flags |= TX_CK_UNUSED;
7857 else
7858 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007859 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 /* depending on the cookie mode, we may have to either :
7862 * - delete the complete cookie if we're in insert+indirect mode, so that
7863 * the server never sees it ;
7864 * - remove the server id from the cookie value, and tag the cookie as an
7865 * application cookie so that it does not get accidentely removed later,
7866 * if we're in cookie prefix mode
7867 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007868 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007869 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007870
Willy Tarreau9b28e032012-10-12 23:49:43 +02007871 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 val_end += delta;
7873 next += delta;
7874 hdr_end += delta;
7875 hdr_next += delta;
7876 cur_hdr->len += delta;
7877 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007879 del_from = NULL;
7880 preserve_hdr = 1; /* we want to keep this cookie */
7881 }
7882 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007883 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007884 del_from = prev;
7885 }
7886 } else {
7887 /* This is not our cookie, so we must preserve it. But if we already
7888 * scheduled another cookie for removal, we cannot remove the
7889 * complete header, but we can remove the previous block itself.
7890 */
7891 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007893 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007894 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007895 if (att_beg >= del_from)
7896 att_beg += delta;
7897 if (att_end >= del_from)
7898 att_end += delta;
7899 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 val_end += delta;
7901 next += delta;
7902 hdr_end += delta;
7903 hdr_next += delta;
7904 cur_hdr->len += delta;
7905 http_msg_move_end(&txn->req, delta);
7906 prev = del_from;
7907 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007908 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007910
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 /* continue with next cookie on this header line */
7912 att_beg = next;
7913 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 /* There are no more cookies on this line.
7916 * We may still have one (or several) marked for deletion at the
7917 * end of the line. We must do this now in two ways :
7918 * - if some cookies must be preserved, we only delete from the
7919 * mark to the end of line ;
7920 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007922 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007923 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007925 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007926 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007927 cur_hdr->len += delta;
7928 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007929 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930
7931 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007932 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7933 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007934 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007935 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007938 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007939 }
7940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007941 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007943 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944}
7945
7946
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947/* Iterate the same filter through all response headers contained in <rtr>.
7948 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7949 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007950int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 char *cur_ptr, *cur_end, *cur_next;
7953 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007954 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007956 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957
7958 last_hdr = 0;
7959
Willy Tarreau9b28e032012-10-12 23:49:43 +02007960 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 old_idx = 0;
7962
7963 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007964 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007966 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 (exp->action == ACT_ALLOW ||
7968 exp->action == ACT_DENY))
7969 return 0;
7970
7971 cur_idx = txn->hdr_idx.v[old_idx].next;
7972 if (!cur_idx)
7973 break;
7974
7975 cur_hdr = &txn->hdr_idx.v[cur_idx];
7976 cur_ptr = cur_next;
7977 cur_end = cur_ptr + cur_hdr->len;
7978 cur_next = cur_end + cur_hdr->cr + 1;
7979
7980 /* Now we have one header between cur_ptr and cur_end,
7981 * and the next header starts at cur_next.
7982 */
7983
Willy Tarreau15a53a42015-01-21 13:39:42 +01007984 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985 switch (exp->action) {
7986 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007987 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 last_hdr = 1;
7989 break;
7990
7991 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007992 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 last_hdr = 1;
7994 break;
7995
7996 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007997 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7998 if (trash.len < 0)
7999 return -1;
8000
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008001 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002 /* FIXME: if the user adds a newline in the replacement, the
8003 * index will not be recalculated for now, and the new line
8004 * will not be counted as a new header.
8005 */
8006
8007 cur_end += delta;
8008 cur_next += delta;
8009 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008010 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 break;
8012
8013 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008014 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 cur_next += delta;
8016
Willy Tarreaufa355d42009-11-29 18:12:29 +01008017 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8019 txn->hdr_idx.used--;
8020 cur_hdr->len = 0;
8021 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008022 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 break;
8024
8025 }
8026 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027
8028 /* keep the link from this header to next one in case of later
8029 * removal of next header.
8030 */
8031 old_idx = cur_idx;
8032 }
8033 return 0;
8034}
8035
8036
8037/* Apply the filter to the status line in the response buffer <rtr>.
8038 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8039 * or -1 if a replacement resulted in an invalid status line.
8040 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008041int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 char *cur_ptr, *cur_end;
8044 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008045 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008046 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008047
8048
Willy Tarreau3d300592007-03-18 18:34:41 +01008049 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008051 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 (exp->action == ACT_ALLOW ||
8053 exp->action == ACT_DENY))
8054 return 0;
8055 else if (exp->action == ACT_REMOVE)
8056 return 0;
8057
8058 done = 0;
8059
Willy Tarreau9b28e032012-10-12 23:49:43 +02008060 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008061 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062
8063 /* Now we have the status line between cur_ptr and cur_end */
8064
Willy Tarreau15a53a42015-01-21 13:39:42 +01008065 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 switch (exp->action) {
8067 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008068 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 done = 1;
8070 break;
8071
8072 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008073 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074 done = 1;
8075 break;
8076
8077 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008078 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8079 if (trash.len < 0)
8080 return -1;
8081
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008082 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083 /* FIXME: if the user adds a newline in the replacement, the
8084 * index will not be recalculated for now, and the new line
8085 * will not be counted as a new header.
8086 */
8087
Willy Tarreaufa355d42009-11-29 18:12:29 +01008088 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008090 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008091 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 cur_ptr, cur_end + 1,
8093 NULL, NULL);
8094 if (unlikely(!cur_end))
8095 return -1;
8096
8097 /* we have a full respnse and we know that we have either a CR
8098 * or an LF at <ptr>.
8099 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008100 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008101 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 /* there is no point trying this regex on headers */
8103 return 1;
8104 }
8105 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106 return done;
8107}
8108
8109
8110
8111/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008112 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8114 * unparsable response.
8115 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008116int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117{
Willy Tarreau192252e2015-04-04 01:47:55 +02008118 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008119 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008120 struct hdr_exp *exp;
8121
8122 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 int ret;
8124
8125 /*
8126 * The interleaving of transformations and verdicts
8127 * makes it difficult to decide to continue or stop
8128 * the evaluation.
8129 */
8130
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008131 if (txn->flags & TX_SVDENY)
8132 break;
8133
Willy Tarreau3d300592007-03-18 18:34:41 +01008134 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8136 exp->action == ACT_PASS)) {
8137 exp = exp->next;
8138 continue;
8139 }
8140
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008141 /* if this filter had a condition, evaluate it now and skip to
8142 * next filter if the condition does not match.
8143 */
8144 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008145 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008146 ret = acl_pass(ret);
8147 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8148 ret = !ret;
8149 if (!ret)
8150 continue;
8151 }
8152
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008154 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008155 if (unlikely(ret < 0))
8156 return -1;
8157
8158 if (likely(ret == 0)) {
8159 /* The filter did not match the response, it can be
8160 * iterated through all headers.
8161 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008162 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8163 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008165 }
8166 return 0;
8167}
8168
8169
Willy Tarreaua15645d2007-03-18 16:22:39 +01008170/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008171 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 * desirable to call it only when needed. This function is also used when we
8173 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008174 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008175void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008177 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008178 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008179 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008182 char *hdr_beg, *hdr_end, *hdr_next;
8183 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184
Willy Tarreaua15645d2007-03-18 16:22:39 +01008185 /* Iterate through the headers.
8186 * we start with the start line.
8187 */
8188 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008189 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190
8191 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8192 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008193 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008194
8195 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 hdr_beg = hdr_next;
8197 hdr_end = hdr_beg + cur_hdr->len;
8198 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 /* We have one full header between hdr_beg and hdr_end, and the
8201 * next header starts at hdr_next. We're only interested in
8202 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203 */
8204
Willy Tarreau24581ba2010-08-31 22:39:35 +02008205 is_cookie2 = 0;
8206 prev = hdr_beg + 10;
8207 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008208 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8210 if (!val) {
8211 old_idx = cur_idx;
8212 continue;
8213 }
8214 is_cookie2 = 1;
8215 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 }
8217
Willy Tarreau24581ba2010-08-31 22:39:35 +02008218 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8219 * <prev> points to the colon.
8220 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008221 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8224 * check-cache is enabled) and we are not interested in checking
8225 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008226 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008227 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 return;
8229
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 /* OK so now we know we have to process this response cookie.
8231 * The format of the Set-Cookie header is slightly different
8232 * from the format of the Cookie header in that it does not
8233 * support the comma as a cookie delimiter (thus the header
8234 * cannot be folded) because the Expires attribute described in
8235 * the original Netscape's spec may contain an unquoted date
8236 * with a comma inside. We have to live with this because
8237 * many browsers don't support Max-Age and some browsers don't
8238 * support quoted strings. However the Set-Cookie2 header is
8239 * clean.
8240 *
8241 * We have to keep multiple pointers in order to support cookie
8242 * removal at the beginning, middle or end of header without
8243 * corrupting the header (in case of set-cookie2). A special
8244 * pointer, <scav> points to the beginning of the set-cookie-av
8245 * fields after the first semi-colon. The <next> pointer points
8246 * either to the end of line (set-cookie) or next unquoted comma
8247 * (set-cookie2). All of these headers are valid :
8248 *
8249 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8250 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8251 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8252 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8253 * | | | | | | | | | |
8254 * | | | | | | | | +-> next hdr_end <--+
8255 * | | | | | | | +------------> scav
8256 * | | | | | | +--------------> val_end
8257 * | | | | | +--------------------> val_beg
8258 * | | | | +----------------------> equal
8259 * | | | +------------------------> att_end
8260 * | | +----------------------------> att_beg
8261 * | +------------------------------> prev
8262 * +-----------------------------------------> hdr_beg
8263 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 for (; prev < hdr_end; prev = next) {
8266 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267
Willy Tarreau24581ba2010-08-31 22:39:35 +02008268 /* find att_beg */
8269 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008270 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008271 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 /* find att_end : this is the first character after the last non
8274 * space before the equal. It may be equal to hdr_end.
8275 */
8276 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277
Willy Tarreau24581ba2010-08-31 22:39:35 +02008278 while (equal < hdr_end) {
8279 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8280 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008281 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008282 continue;
8283 att_end = equal;
8284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008285
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8287 * is between <att_beg> and <equal>, both may be identical.
8288 */
8289
8290 /* look for end of cookie if there is an equal sign */
8291 if (equal < hdr_end && *equal == '=') {
8292 /* look for the beginning of the value */
8293 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008294 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296
Willy Tarreau24581ba2010-08-31 22:39:35 +02008297 /* find the end of the value, respecting quotes */
8298 next = find_cookie_value_end(val_beg, hdr_end);
8299
8300 /* make val_end point to the first white space or delimitor after the value */
8301 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008302 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008303 val_end--;
8304 } else {
8305 /* <equal> points to next comma, semi-colon or EOL */
8306 val_beg = val_end = next = equal;
8307 }
8308
8309 if (next < hdr_end) {
8310 /* Set-Cookie2 supports multiple cookies, and <next> points to
8311 * a colon or semi-colon before the end. So skip all attr-value
8312 * pairs and look for the next comma. For Set-Cookie, since
8313 * commas are permitted in values, skip to the end.
8314 */
8315 if (is_cookie2)
8316 next = find_hdr_value_end(next, hdr_end);
8317 else
8318 next = hdr_end;
8319 }
8320
8321 /* Now everything is as on the diagram above */
8322
8323 /* Ignore cookies with no equal sign */
8324 if (equal == val_end)
8325 continue;
8326
8327 /* If there are spaces around the equal sign, we need to
8328 * strip them otherwise we'll get trouble for cookie captures,
8329 * or even for rewrites. Since this happens extremely rarely,
8330 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008332 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8333 int stripped_before = 0;
8334 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008337 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008338 equal += stripped_before;
8339 val_beg += stripped_before;
8340 }
8341
8342 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008343 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 val_beg += stripped_after;
8345 stripped_before += stripped_after;
8346 }
8347
8348 val_end += stripped_before;
8349 next += stripped_before;
8350 hdr_end += stripped_before;
8351 hdr_next += stripped_before;
8352 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008353 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008354 }
8355
8356 /* First, let's see if we want to capture this cookie. We check
8357 * that we don't already have a server side cookie, because we
8358 * can only capture one. Also as an optimisation, we ignore
8359 * cookies shorter than the declared name.
8360 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008361 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008362 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008363 (val_end - att_beg >= sess->fe->capture_namelen) &&
8364 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008366 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008367 Alert("HTTP logging : out of memory.\n");
8368 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008369 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008370 if (log_len > sess->fe->capture_len)
8371 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008372 memcpy(txn->srv_cookie, att_beg, log_len);
8373 txn->srv_cookie[log_len] = 0;
8374 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375 }
8376
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008377 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008378 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008379 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008380 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8381 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008382 /* assume passive cookie by default */
8383 txn->flags &= ~TX_SCK_MASK;
8384 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008385
8386 /* If the cookie is in insert mode on a known server, we'll delete
8387 * this occurrence because we'll insert another one later.
8388 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008389 * a direct access.
8390 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008391 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008392 /* The "preserve" flag was set, we don't want to touch the
8393 * server's cookie.
8394 */
8395 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008396 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008397 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008398 /* this cookie must be deleted */
8399 if (*prev == ':' && next == hdr_end) {
8400 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008401 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8403 txn->hdr_idx.used--;
8404 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008405 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008406 hdr_next += delta;
8407 http_msg_move_end(&txn->rsp, delta);
8408 /* note: while both invalid now, <next> and <hdr_end>
8409 * are still equal, so the for() will stop as expected.
8410 */
8411 } else {
8412 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008414 next = prev;
8415 hdr_end += delta;
8416 hdr_next += delta;
8417 cur_hdr->len += delta;
8418 http_msg_move_end(&txn->rsp, delta);
8419 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008420 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008421 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008422 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008426 * with this server since we know it.
8427 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008428 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 next += delta;
8430 hdr_end += delta;
8431 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008432 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008433 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008434
Willy Tarreauf1348312010-10-07 15:54:11 +02008435 txn->flags &= ~TX_SCK_MASK;
8436 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008438 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008440 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008442 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008443 next += delta;
8444 hdr_end += delta;
8445 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008447 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008448
Willy Tarreau827aee92011-03-10 16:55:02 +01008449 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008450 txn->flags &= ~TX_SCK_MASK;
8451 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008452 }
8453 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008454 /* that's done for this cookie, check the next one on the same
8455 * line when next != hdr_end (only if is_cookie2).
8456 */
8457 }
8458 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008460 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008461}
8462
8463
Willy Tarreaua15645d2007-03-18 16:22:39 +01008464/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008467void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008469 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 char *p1, *p2;
8471
8472 char *cur_ptr, *cur_end, *cur_next;
8473 int cur_idx;
8474
Willy Tarreau5df51872007-11-25 16:20:08 +01008475 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 return;
8477
8478 /* Iterate through the headers.
8479 * we start with the start line.
8480 */
8481 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008482 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483
8484 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8485 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008486 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487
8488 cur_hdr = &txn->hdr_idx.v[cur_idx];
8489 cur_ptr = cur_next;
8490 cur_end = cur_ptr + cur_hdr->len;
8491 cur_next = cur_end + cur_hdr->cr + 1;
8492
8493 /* We have one full header between cur_ptr and cur_end, and the
8494 * next header starts at cur_next. We're only interested in
8495 * "Cookie:" headers.
8496 */
8497
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008498 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8499 if (val) {
8500 if ((cur_end - (cur_ptr + val) >= 8) &&
8501 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8502 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8503 return;
8504 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505 }
8506
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008507 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8508 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509 continue;
8510
8511 /* OK, right now we know we have a cache-control header at cur_ptr */
8512
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008513 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514
8515 if (p1 >= cur_end) /* no more info */
8516 continue;
8517
8518 /* p1 is at the beginning of the value */
8519 p2 = p1;
8520
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008521 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522 p2++;
8523
8524 /* we have a complete value between p1 and p2 */
8525 if (p2 < cur_end && *p2 == '=') {
8526 /* we have something of the form no-cache="set-cookie" */
8527 if ((cur_end - p1 >= 21) &&
8528 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8529 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008530 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531 continue;
8532 }
8533
8534 /* OK, so we know that either p2 points to the end of string or to a comma */
8535 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008536 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008537 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8538 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8539 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008540 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541 return;
8542 }
8543
8544 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008545 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008546 continue;
8547 }
8548 }
8549}
8550
Willy Tarreau58f10d72006-12-04 02:26:12 +01008551
Willy Tarreaub2513902006-12-17 14:52:38 +01008552/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008553 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008554 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008555 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008556 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008557 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008558 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008559 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008560 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008561int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008562{
8563 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008564 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008565 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008566
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008567 if (!uri_auth)
8568 return 0;
8569
Cyril Bonté70be45d2010-10-12 00:14:35 +02008570 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008571 return 0;
8572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008573 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008574 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008575 return 0;
8576
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008577 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008578 return 0;
8579
Willy Tarreaub2513902006-12-17 14:52:38 +01008580 return 1;
8581}
8582
Willy Tarreau4076a152009-04-02 15:18:36 +02008583/*
8584 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008585 * By default it tries to report the error position as msg->err_pos. However if
8586 * this one is not set, it will then report msg->next, which is the last known
8587 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008588 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008589 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008590void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008591 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008592 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008593{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008594 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008595 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008596 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008597
Willy Tarreauf3764b72016-03-31 13:45:10 +02008598 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008599 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008600 len1 = MIN(len1, es->len);
8601 len2 = es->len - len1; /* remaining data if buffer wraps */
8602
Willy Tarreauf3764b72016-03-31 13:45:10 +02008603 if (!es->buf)
8604 es->buf = malloc(global.tune.bufsize);
8605
8606 if (es->buf) {
8607 memcpy(es->buf, chn->buf->p, len1);
8608 if (len2)
8609 memcpy(es->buf + len1, chn->buf->data, len2);
8610 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008611
Willy Tarreau4076a152009-04-02 15:18:36 +02008612 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008613 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008614 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008615 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008616
Willy Tarreau4076a152009-04-02 15:18:36 +02008617 es->when = date; // user-visible date
8618 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008619 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008620 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008621 if (objt_conn(sess->origin))
8622 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008623 else
8624 memset(&es->src, 0, sizeof(es->src));
8625
Willy Tarreau078272e2010-12-12 12:46:33 +01008626 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008627 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008628 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008629 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008630 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008631 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008632 es->b_out = chn->buf->o;
8633 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008634 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008635 es->m_clen = msg->chunk_len;
8636 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008637}
Willy Tarreaub2513902006-12-17 14:52:38 +01008638
Willy Tarreau294c4732011-12-16 21:35:50 +01008639/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8640 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8641 * performed over the whole headers. Otherwise it must contain a valid header
8642 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8643 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8644 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8645 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008646 * -1. The value fetch stops at commas, so this function is suited for use with
8647 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008648 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008649 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008650unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008651 struct hdr_idx *idx, int occ,
8652 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008653{
Willy Tarreau294c4732011-12-16 21:35:50 +01008654 struct hdr_ctx local_ctx;
8655 char *ptr_hist[MAX_HDR_HISTORY];
8656 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008657 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008658 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008659
Willy Tarreau294c4732011-12-16 21:35:50 +01008660 if (!ctx) {
8661 local_ctx.idx = 0;
8662 ctx = &local_ctx;
8663 }
8664
Willy Tarreaubce70882009-09-07 11:51:47 +02008665 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008666 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008667 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008668 occ--;
8669 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008670 *vptr = ctx->line + ctx->val;
8671 *vlen = ctx->vlen;
8672 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008673 }
8674 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008675 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008676 }
8677
8678 /* negative occurrence, we scan all the list then walk back */
8679 if (-occ > MAX_HDR_HISTORY)
8680 return 0;
8681
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008683 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008684 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8685 len_hist[hist_ptr] = ctx->vlen;
8686 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008687 hist_ptr = 0;
8688 found++;
8689 }
8690 if (-occ > found)
8691 return 0;
8692 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008693 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8694 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8695 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008697 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008698 if (hist_ptr >= MAX_HDR_HISTORY)
8699 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008700 *vptr = ptr_hist[hist_ptr];
8701 *vlen = len_hist[hist_ptr];
8702 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008703}
8704
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008705/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8706 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8707 * performed over the whole headers. Otherwise it must contain a valid header
8708 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8709 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8710 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8711 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8712 * -1. This function differs from http_get_hdr() in that it only returns full
8713 * line header values and does not stop at commas.
8714 * The return value is 0 if nothing was found, or non-zero otherwise.
8715 */
8716unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8717 struct hdr_idx *idx, int occ,
8718 struct hdr_ctx *ctx, char **vptr, int *vlen)
8719{
8720 struct hdr_ctx local_ctx;
8721 char *ptr_hist[MAX_HDR_HISTORY];
8722 int len_hist[MAX_HDR_HISTORY];
8723 unsigned int hist_ptr;
8724 int found;
8725
8726 if (!ctx) {
8727 local_ctx.idx = 0;
8728 ctx = &local_ctx;
8729 }
8730
8731 if (occ >= 0) {
8732 /* search from the beginning */
8733 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8734 occ--;
8735 if (occ <= 0) {
8736 *vptr = ctx->line + ctx->val;
8737 *vlen = ctx->vlen;
8738 return 1;
8739 }
8740 }
8741 return 0;
8742 }
8743
8744 /* negative occurrence, we scan all the list then walk back */
8745 if (-occ > MAX_HDR_HISTORY)
8746 return 0;
8747
8748 found = hist_ptr = 0;
8749 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8750 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8751 len_hist[hist_ptr] = ctx->vlen;
8752 if (++hist_ptr >= MAX_HDR_HISTORY)
8753 hist_ptr = 0;
8754 found++;
8755 }
8756 if (-occ > found)
8757 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008758
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008759 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008760 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8761 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8762 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008763 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008764 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008765 if (hist_ptr >= MAX_HDR_HISTORY)
8766 hist_ptr -= MAX_HDR_HISTORY;
8767 *vptr = ptr_hist[hist_ptr];
8768 *vlen = len_hist[hist_ptr];
8769 return 1;
8770}
8771
Willy Tarreaubaaee002006-06-26 02:48:02 +02008772/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008773 * Print a debug line with a header. Always stop at the first CR or LF char,
8774 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8775 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008777void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008778{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008779 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008780 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008781
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008782 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008783 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008784 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008785 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008786
8787 for (max = 0; start + max < end; max++)
8788 if (start[max] == '\r' || start[max] == '\n')
8789 break;
8790
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008791 UBOUND(max, trash.size - trash.len - 3);
8792 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8793 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008794 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008795}
8796
Willy Tarreaueee5b512015-04-03 23:46:31 +02008797
8798/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8799 * The hdr_idx is allocated as well. In case of allocation failure, everything
8800 * allocated is freed and NULL is returned. Otherwise the new transaction is
8801 * assigned to the stream and returned.
8802 */
8803struct http_txn *http_alloc_txn(struct stream *s)
8804{
8805 struct http_txn *txn = s->txn;
8806
8807 if (txn)
8808 return txn;
8809
8810 txn = pool_alloc2(pool2_http_txn);
8811 if (!txn)
8812 return txn;
8813
8814 txn->hdr_idx.size = global.tune.max_http_hdr;
8815 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8816 if (!txn->hdr_idx.v) {
8817 pool_free2(pool2_http_txn, txn);
8818 return NULL;
8819 }
8820
8821 s->txn = txn;
8822 return txn;
8823}
8824
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008825void http_txn_reset_req(struct http_txn *txn)
8826{
8827 txn->req.flags = 0;
8828 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8829 txn->req.next = 0;
8830 txn->req.chunk_len = 0LL;
8831 txn->req.body_len = 0LL;
8832 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8833}
8834
8835void http_txn_reset_res(struct http_txn *txn)
8836{
8837 txn->rsp.flags = 0;
8838 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8839 txn->rsp.next = 0;
8840 txn->rsp.chunk_len = 0LL;
8841 txn->rsp.body_len = 0LL;
8842 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8843}
8844
Willy Tarreau0937bc42009-12-22 15:03:09 +01008845/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008846 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008847 * the required fields are properly allocated and that we only need to (re)init
8848 * them. This should be used before processing any new request.
8849 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008850void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008851{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008852 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008853 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008854
8855 txn->flags = 0;
8856 txn->status = -1;
8857
Willy Tarreauf64d1412010-10-07 20:06:11 +02008858 txn->cookie_first_date = 0;
8859 txn->cookie_last_date = 0;
8860
Willy Tarreaueee5b512015-04-03 23:46:31 +02008861 txn->srv_cookie = NULL;
8862 txn->cli_cookie = NULL;
8863 txn->uri = NULL;
8864
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008865 http_txn_reset_req(txn);
8866 http_txn_reset_res(txn);
8867
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008868 txn->req.chn = &s->req;
8869 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008870
8871 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008872
8873 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8874 if (fe->options2 & PR_O2_REQBUG_OK)
8875 txn->req.err_pos = -1; /* let buggy requests pass */
8876
Willy Tarreau0937bc42009-12-22 15:03:09 +01008877 if (txn->hdr_idx.v)
8878 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008879
8880 vars_init(&s->vars_txn, SCOPE_TXN);
8881 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008882}
8883
8884/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008885void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008886{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008887 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008888 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008889
8890 /* these ones will have been dynamically allocated */
8891 pool_free2(pool2_requri, txn->uri);
8892 pool_free2(pool2_capture, txn->cli_cookie);
8893 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008894 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008895
William Lallemanda73203e2012-03-12 12:48:57 +01008896 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897 txn->uri = NULL;
8898 txn->srv_cookie = NULL;
8899 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008900
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008901 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008902 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008903 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008904 pool_free2(h->pool, s->req_cap[h->index]);
8905 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008906 }
8907
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008908 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008909 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008910 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008911 pool_free2(h->pool, s->res_cap[h->index]);
8912 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008913 }
8914
Willy Tarreau6204cd92016-03-10 16:33:04 +01008915 vars_prune(&s->vars_txn, s->sess, s);
8916 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008917}
8918
8919/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008920void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008921{
8922 http_end_txn(s);
8923 http_init_txn(s);
8924
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008925 /* reinitialise the current rule list pointer to NULL. We are sure that
8926 * any rulelist match the NULL pointer.
8927 */
8928 s->current_rule_list = NULL;
8929
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008930 s->be = strm_fe(s);
8931 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008932 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008933 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008934 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008935 /* re-init store persistence */
8936 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008937 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008938
Willy Tarreau0937bc42009-12-22 15:03:09 +01008939 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008940
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008941 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008942
Willy Tarreau739cfba2010-01-25 23:11:14 +01008943 /* We must trim any excess data from the response buffer, because we
8944 * may have blocked an invalid response from a server that we don't
8945 * want to accidentely forward once we disable the analysers, nor do
8946 * we want those data to come along with next response. A typical
8947 * example of such data would be from a buggy server responding to
8948 * a HEAD with some data, or sending more than the advertised
8949 * content-length.
8950 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008951 if (unlikely(s->res.buf->i))
8952 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008953
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008954 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008955 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008957 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008958 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008960 s->req.rex = TICK_ETERNITY;
8961 s->req.wex = TICK_ETERNITY;
8962 s->req.analyse_exp = TICK_ETERNITY;
8963 s->res.rex = TICK_ETERNITY;
8964 s->res.wex = TICK_ETERNITY;
8965 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008966}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008967
Sasha Pachev218f0642014-06-16 12:05:59 -06008968void free_http_res_rules(struct list *r)
8969{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008970 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008971
8972 list_for_each_entry_safe(pr, tr, r, list) {
8973 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008974 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008975 free(pr);
8976 }
8977}
8978
8979void free_http_req_rules(struct list *r)
8980{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008981 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008982
8983 list_for_each_entry_safe(pr, tr, r, list) {
8984 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008985 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008986 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008987
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008988 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008989 free(pr);
8990 }
8991}
8992
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008993/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008994struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008995{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008996 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008997 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008998 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008999 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009000
Vincent Bernat02779b62016-04-03 13:48:43 +02009001 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009002 if (!rule) {
9003 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009004 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009005 }
9006
CJ Ess108b1dd2015-04-07 12:03:37 -04009007 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009008 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009009 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009010 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009011 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009012 int code;
9013 int hc;
9014
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009015 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009016 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009017 if (strcmp(args[cur_arg], "deny_status") == 0) {
9018 cur_arg++;
9019 if (!args[cur_arg]) {
9020 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9021 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9022 goto out_err;
9023 }
9024
9025 code = atol(args[cur_arg]);
9026 cur_arg++;
9027 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9028 if (http_err_codes[hc] == code) {
9029 rule->deny_status = hc;
9030 break;
9031 }
9032 }
9033
9034 if (hc >= HTTP_ERR_SIZE) {
9035 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9036 file, linenum, code);
9037 }
9038 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009039 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009040 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009041 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009043 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009044 cur_arg = 1;
9045
9046 while(*args[cur_arg]) {
9047 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009048 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009049 cur_arg+=2;
9050 continue;
9051 } else
9052 break;
9053 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009054 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009055 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009056 cur_arg = 1;
9057
9058 if (!*args[cur_arg] ||
9059 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9060 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9061 file, linenum, args[0]);
9062 goto out_err;
9063 }
9064 rule->arg.nice = atoi(args[cur_arg]);
9065 if (rule->arg.nice < -1024)
9066 rule->arg.nice = -1024;
9067 else if (rule->arg.nice > 1024)
9068 rule->arg.nice = 1024;
9069 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009070 } else if (!strcmp(args[0], "set-tos")) {
9071#ifdef IP_TOS
9072 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009073 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009074 cur_arg = 1;
9075
9076 if (!*args[cur_arg] ||
9077 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9078 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9079 file, linenum, args[0]);
9080 goto out_err;
9081 }
9082
9083 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9084 if (err && *err != '\0') {
9085 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9086 file, linenum, err, args[0]);
9087 goto out_err;
9088 }
9089 cur_arg++;
9090#else
9091 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9092 goto out_err;
9093#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009094 } else if (!strcmp(args[0], "set-mark")) {
9095#ifdef SO_MARK
9096 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009097 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009098 cur_arg = 1;
9099
9100 if (!*args[cur_arg] ||
9101 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9102 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9103 file, linenum, args[0]);
9104 goto out_err;
9105 }
9106
9107 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9108 if (err && *err != '\0') {
9109 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9110 file, linenum, err, args[0]);
9111 goto out_err;
9112 }
9113 cur_arg++;
9114 global.last_checks |= LSTCHK_NETADM;
9115#else
9116 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9117 goto out_err;
9118#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009119 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009120 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009121 cur_arg = 1;
9122
9123 if (!*args[cur_arg] ||
9124 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9125 bad_log_level:
9126 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9127 file, linenum, args[0]);
9128 goto out_err;
9129 }
9130 if (strcmp(args[cur_arg], "silent") == 0)
9131 rule->arg.loglevel = -1;
9132 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9133 goto bad_log_level;
9134 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009135 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009136 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009137 cur_arg = 1;
9138
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009139 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9140 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009141 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9142 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009143 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009144 }
9145
9146 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9147 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9148 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009149
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009150 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009151 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009152 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009153 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9154 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9155 file, linenum, args[0], error);
9156 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009157 goto out_err;
9158 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009159 free(proxy->conf.lfs_file);
9160 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9161 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009162 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009163 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009164 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009165 cur_arg = 1;
9166
9167 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009168 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06009169 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9170 file, linenum, args[0]);
9171 goto out_err;
9172 }
9173
9174 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9175 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9176 LIST_INIT(&rule->arg.hdr_add.fmt);
9177
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009178 error = NULL;
9179 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9180 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9181 args[cur_arg + 1], error);
9182 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009183 goto out_err;
9184 }
9185
9186 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009187 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009188 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009189 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9190 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9191 file, linenum, args[0], error);
9192 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009193 goto out_err;
9194 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009195
9196 free(proxy->conf.lfs_file);
9197 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9198 proxy->conf.lfs_line = proxy->conf.args.line;
9199 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009200 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009201 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009202 cur_arg = 1;
9203
9204 if (!*args[cur_arg] ||
9205 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9206 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9207 file, linenum, args[0]);
9208 goto out_err;
9209 }
9210
9211 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9212 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9213
9214 proxy->conf.args.ctx = ARGC_HRQ;
9215 free(proxy->conf.lfs_file);
9216 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9217 proxy->conf.lfs_line = proxy->conf.args.line;
9218 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009219 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9220 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009221 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009222 struct sample_expr *expr;
9223 unsigned int where;
9224 char *err = NULL;
9225
9226 cur_arg = 1;
9227 proxy->conf.args.ctx = ARGC_TRK;
9228
9229 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9230 if (!expr) {
9231 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9232 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9233 free(err);
9234 goto out_err;
9235 }
9236
9237 where = 0;
9238 if (proxy->cap & PR_CAP_FE)
9239 where |= SMP_VAL_FE_HRQ_HDR;
9240 if (proxy->cap & PR_CAP_BE)
9241 where |= SMP_VAL_BE_HRQ_HDR;
9242
9243 if (!(expr->fetch->val & where)) {
9244 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9245 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9246 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9247 args[cur_arg-1], sample_src_names(expr->fetch->use));
9248 free(expr);
9249 goto out_err;
9250 }
9251
9252 if (strcmp(args[cur_arg], "table") == 0) {
9253 cur_arg++;
9254 if (!args[cur_arg]) {
9255 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9256 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9257 free(expr);
9258 goto out_err;
9259 }
9260 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009261 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009262 cur_arg++;
9263 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009264 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009265 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009266 } else if (strcmp(args[0], "redirect") == 0) {
9267 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009268 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009269
Willy Tarreaube4653b2015-05-28 15:26:58 +02009270 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009271 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9272 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9273 goto out_err;
9274 }
9275
9276 /* this redirect rule might already contain a parsed condition which
9277 * we'll pass to the http-request rule.
9278 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009279 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009280 rule->arg.redir = redir;
9281 rule->cond = redir->cond;
9282 redir->cond = NULL;
9283 cur_arg = 2;
9284 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009285 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9286 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009287 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009288 /*
9289 * '+ 8' for 'add-acl('
9290 * '- 9' for 'add-acl(' + trailing ')'
9291 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009292 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009293
9294 cur_arg = 1;
9295
9296 if (!*args[cur_arg] ||
9297 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9298 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9299 file, linenum, args[0]);
9300 goto out_err;
9301 }
9302
9303 LIST_INIT(&rule->arg.map.key);
9304 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009305 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009306 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009307 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9308 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9309 file, linenum, args[0], error);
9310 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009311 goto out_err;
9312 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009313 free(proxy->conf.lfs_file);
9314 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9315 proxy->conf.lfs_line = proxy->conf.args.line;
9316 cur_arg += 1;
9317 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9318 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009319 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009320 /*
9321 * '+ 8' for 'del-acl('
9322 * '- 9' for 'del-acl(' + trailing ')'
9323 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009324 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009325
9326 cur_arg = 1;
9327
9328 if (!*args[cur_arg] ||
9329 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9330 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9331 file, linenum, args[0]);
9332 goto out_err;
9333 }
9334
9335 LIST_INIT(&rule->arg.map.key);
9336 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009337 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009338 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009339 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9340 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9341 file, linenum, args[0], error);
9342 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009343 goto out_err;
9344 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009345 free(proxy->conf.lfs_file);
9346 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9347 proxy->conf.lfs_line = proxy->conf.args.line;
9348 cur_arg += 1;
9349 } else if (strncmp(args[0], "del-map", 7) == 0) {
9350 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009351 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009352 /*
9353 * '+ 8' for 'del-map('
9354 * '- 9' for 'del-map(' + trailing ')'
9355 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009356 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009357
9358 cur_arg = 1;
9359
9360 if (!*args[cur_arg] ||
9361 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9362 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9363 file, linenum, args[0]);
9364 goto out_err;
9365 }
9366
9367 LIST_INIT(&rule->arg.map.key);
9368 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009369 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009370 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009371 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9372 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9373 file, linenum, args[0], error);
9374 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009375 goto out_err;
9376 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009377 free(proxy->conf.lfs_file);
9378 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9379 proxy->conf.lfs_line = proxy->conf.args.line;
9380 cur_arg += 1;
9381 } else if (strncmp(args[0], "set-map", 7) == 0) {
9382 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009383 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009384 /*
9385 * '+ 8' for 'set-map('
9386 * '- 9' for 'set-map(' + trailing ')'
9387 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009388 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009389
9390 cur_arg = 1;
9391
9392 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9393 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9394 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9395 file, linenum, args[0]);
9396 goto out_err;
9397 }
9398
9399 LIST_INIT(&rule->arg.map.key);
9400 LIST_INIT(&rule->arg.map.value);
9401 proxy->conf.args.ctx = ARGC_HRQ;
9402
9403 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009404 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009405 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009406 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9407 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9408 file, linenum, args[0], error);
9409 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009410 goto out_err;
9411 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009412
9413 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009414 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009415 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009416 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9417 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9418 file, linenum, args[0], error);
9419 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009420 goto out_err;
9421 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009422 free(proxy->conf.lfs_file);
9423 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9424 proxy->conf.lfs_line = proxy->conf.args.line;
9425
9426 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009427 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9428 char *errmsg = NULL;
9429 cur_arg = 1;
9430 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009431 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009432 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009433 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009434 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9435 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9436 free(errmsg);
9437 goto out_err;
9438 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009439 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009440 action_build_list(&http_req_keywords.list, &trash);
9441 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9442 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009443 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009444 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009445 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009446 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009447 }
9448
9449 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9450 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009451 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009452
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009453 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9454 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9455 file, linenum, args[0], errmsg);
9456 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009457 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009458 }
9459 rule->cond = cond;
9460 }
9461 else if (*args[cur_arg]) {
9462 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9463 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9464 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009465 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009466 }
9467
9468 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009469 out_err:
9470 free(rule);
9471 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009472}
9473
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009474/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009475struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009476{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009477 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009478 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009479 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009480 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009481
9482 rule = calloc(1, sizeof(*rule));
9483 if (!rule) {
9484 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9485 goto out_err;
9486 }
9487
9488 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009489 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009490 cur_arg = 1;
9491 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009492 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009493 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009494 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009495 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009496 cur_arg = 1;
9497
9498 if (!*args[cur_arg] ||
9499 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9500 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9501 file, linenum, args[0]);
9502 goto out_err;
9503 }
9504 rule->arg.nice = atoi(args[cur_arg]);
9505 if (rule->arg.nice < -1024)
9506 rule->arg.nice = -1024;
9507 else if (rule->arg.nice > 1024)
9508 rule->arg.nice = 1024;
9509 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009510 } else if (!strcmp(args[0], "set-tos")) {
9511#ifdef IP_TOS
9512 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009513 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009514 cur_arg = 1;
9515
9516 if (!*args[cur_arg] ||
9517 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9518 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9519 file, linenum, args[0]);
9520 goto out_err;
9521 }
9522
9523 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9524 if (err && *err != '\0') {
9525 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9526 file, linenum, err, args[0]);
9527 goto out_err;
9528 }
9529 cur_arg++;
9530#else
9531 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9532 goto out_err;
9533#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009534 } else if (!strcmp(args[0], "set-mark")) {
9535#ifdef SO_MARK
9536 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009537 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009538 cur_arg = 1;
9539
9540 if (!*args[cur_arg] ||
9541 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9542 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9543 file, linenum, args[0]);
9544 goto out_err;
9545 }
9546
9547 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9548 if (err && *err != '\0') {
9549 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9550 file, linenum, err, args[0]);
9551 goto out_err;
9552 }
9553 cur_arg++;
9554 global.last_checks |= LSTCHK_NETADM;
9555#else
9556 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9557 goto out_err;
9558#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009559 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009560 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009561 cur_arg = 1;
9562
9563 if (!*args[cur_arg] ||
9564 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9565 bad_log_level:
9566 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9567 file, linenum, args[0]);
9568 goto out_err;
9569 }
9570 if (strcmp(args[cur_arg], "silent") == 0)
9571 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009572 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009573 goto bad_log_level;
9574 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009575 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009576 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009577 cur_arg = 1;
9578
9579 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9580 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9581 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9582 file, linenum, args[0]);
9583 goto out_err;
9584 }
9585
9586 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9587 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9588 LIST_INIT(&rule->arg.hdr_add.fmt);
9589
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009590 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009591 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009592 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009593 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9594 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9595 file, linenum, args[0], error);
9596 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009597 goto out_err;
9598 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009599 free(proxy->conf.lfs_file);
9600 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9601 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009602 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009603 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009604 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009605 cur_arg = 1;
9606
9607 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009608 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9609 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009610 file, linenum, args[0]);
9611 goto out_err;
9612 }
9613
9614 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9615 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9616 LIST_INIT(&rule->arg.hdr_add.fmt);
9617
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009618 error = NULL;
9619 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9620 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9621 args[cur_arg + 1], error);
9622 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009623 goto out_err;
9624 }
9625
9626 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009627 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009628 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009629 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9630 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9631 file, linenum, args[0], error);
9632 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009633 goto out_err;
9634 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009635
9636 free(proxy->conf.lfs_file);
9637 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9638 proxy->conf.lfs_line = proxy->conf.args.line;
9639 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009640 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009641 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009642 cur_arg = 1;
9643
9644 if (!*args[cur_arg] ||
9645 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9646 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9647 file, linenum, args[0]);
9648 goto out_err;
9649 }
9650
9651 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9652 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9653
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009654 proxy->conf.args.ctx = ARGC_HRS;
9655 free(proxy->conf.lfs_file);
9656 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9657 proxy->conf.lfs_line = proxy->conf.args.line;
9658 cur_arg += 1;
9659 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9660 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009661 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009662 /*
9663 * '+ 8' for 'add-acl('
9664 * '- 9' for 'add-acl(' + trailing ')'
9665 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009666 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009667
9668 cur_arg = 1;
9669
9670 if (!*args[cur_arg] ||
9671 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9672 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9673 file, linenum, args[0]);
9674 goto out_err;
9675 }
9676
9677 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009678 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009679 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009680 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009681 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9682 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9683 file, linenum, args[0], error);
9684 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009685 goto out_err;
9686 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009687 free(proxy->conf.lfs_file);
9688 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9689 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009690
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009691 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009692 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9693 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009694 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009695 /*
9696 * '+ 8' for 'del-acl('
9697 * '- 9' for 'del-acl(' + trailing ')'
9698 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009699 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009700
9701 cur_arg = 1;
9702
9703 if (!*args[cur_arg] ||
9704 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9705 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9706 file, linenum, args[0]);
9707 goto out_err;
9708 }
9709
9710 LIST_INIT(&rule->arg.map.key);
9711 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009712 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009713 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009714 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9715 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9716 file, linenum, args[0], error);
9717 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009718 goto out_err;
9719 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009720 free(proxy->conf.lfs_file);
9721 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9722 proxy->conf.lfs_line = proxy->conf.args.line;
9723 cur_arg += 1;
9724 } else if (strncmp(args[0], "del-map", 7) == 0) {
9725 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009726 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009727 /*
9728 * '+ 8' for 'del-map('
9729 * '- 9' for 'del-map(' + trailing ')'
9730 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009731 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009732
9733 cur_arg = 1;
9734
9735 if (!*args[cur_arg] ||
9736 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9737 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9738 file, linenum, args[0]);
9739 goto out_err;
9740 }
9741
9742 LIST_INIT(&rule->arg.map.key);
9743 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009744 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009745 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009746 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9747 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9748 file, linenum, args[0], error);
9749 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009750 goto out_err;
9751 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009752 free(proxy->conf.lfs_file);
9753 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9754 proxy->conf.lfs_line = proxy->conf.args.line;
9755 cur_arg += 1;
9756 } else if (strncmp(args[0], "set-map", 7) == 0) {
9757 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009758 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009759 /*
9760 * '+ 8' for 'set-map('
9761 * '- 9' for 'set-map(' + trailing ')'
9762 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009763 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009764
9765 cur_arg = 1;
9766
9767 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9768 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9769 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9770 file, linenum, args[0]);
9771 goto out_err;
9772 }
9773
9774 LIST_INIT(&rule->arg.map.key);
9775 LIST_INIT(&rule->arg.map.value);
9776
9777 proxy->conf.args.ctx = ARGC_HRS;
9778
9779 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009780 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009781 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009782 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9783 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9784 file, linenum, args[0], error);
9785 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009786 goto out_err;
9787 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009788
9789 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009790 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009791 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009792 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9793 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9794 file, linenum, args[0], error);
9795 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009796 goto out_err;
9797 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009798
9799 free(proxy->conf.lfs_file);
9800 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9801 proxy->conf.lfs_line = proxy->conf.args.line;
9802
9803 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009804 } else if (strcmp(args[0], "redirect") == 0) {
9805 struct redirect_rule *redir;
9806 char *errmsg = NULL;
9807
9808 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9809 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9810 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9811 goto out_err;
9812 }
9813
9814 /* this redirect rule might already contain a parsed condition which
9815 * we'll pass to the http-request rule.
9816 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009817 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009818 rule->arg.redir = redir;
9819 rule->cond = redir->cond;
9820 redir->cond = NULL;
9821 cur_arg = 2;
9822 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009823 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9824 args[0][9] == '\0' && args[0][8] >= '0' &&
9825 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9826 struct sample_expr *expr;
9827 unsigned int where;
9828 char *err = NULL;
9829
9830 cur_arg = 1;
9831 proxy->conf.args.ctx = ARGC_TRK;
9832
9833 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9834 if (!expr) {
9835 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9836 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9837 free(err);
9838 goto out_err;
9839 }
9840
9841 where = 0;
9842 if (proxy->cap & PR_CAP_FE)
9843 where |= SMP_VAL_FE_HRS_HDR;
9844 if (proxy->cap & PR_CAP_BE)
9845 where |= SMP_VAL_BE_HRS_HDR;
9846
9847 if (!(expr->fetch->val & where)) {
9848 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9849 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9850 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9851 args[cur_arg-1], sample_src_names(expr->fetch->use));
9852 free(expr);
9853 goto out_err;
9854 }
9855
9856 if (strcmp(args[cur_arg], "table") == 0) {
9857 cur_arg++;
9858 if (!args[cur_arg]) {
9859 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9860 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9861 free(expr);
9862 goto out_err;
9863 }
9864 /* we copy the table name for now, it will be resolved later */
9865 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9866 cur_arg++;
9867 }
9868 rule->arg.trk_ctr.expr = expr;
9869 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009870 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9871 char *errmsg = NULL;
9872 cur_arg = 1;
9873 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009874 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009875 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009876 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009877 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9878 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9879 free(errmsg);
9880 goto out_err;
9881 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009882 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009883 action_build_list(&http_res_keywords.list, &trash);
9884 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9885 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009886 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009887 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009888 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009889 goto out_err;
9890 }
9891
9892 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9893 struct acl_cond *cond;
9894 char *errmsg = NULL;
9895
9896 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9897 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9898 file, linenum, args[0], errmsg);
9899 free(errmsg);
9900 goto out_err;
9901 }
9902 rule->cond = cond;
9903 }
9904 else if (*args[cur_arg]) {
9905 Alert("parsing [%s:%d]: 'http-response %s' expects"
9906 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9907 file, linenum, args[0], args[cur_arg]);
9908 goto out_err;
9909 }
9910
9911 return rule;
9912 out_err:
9913 free(rule);
9914 return NULL;
9915}
9916
Willy Tarreau4baae242012-12-27 12:00:31 +01009917/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009918 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009919 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9920 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009921 */
9922struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009923 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009924{
9925 struct redirect_rule *rule;
9926 int cur_arg;
9927 int type = REDIRECT_TYPE_NONE;
9928 int code = 302;
9929 const char *destination = NULL;
9930 const char *cookie = NULL;
9931 int cookie_set = 0;
9932 unsigned int flags = REDIRECT_FLAG_NONE;
9933 struct acl_cond *cond = NULL;
9934
9935 cur_arg = 0;
9936 while (*(args[cur_arg])) {
9937 if (strcmp(args[cur_arg], "location") == 0) {
9938 if (!*args[cur_arg + 1])
9939 goto missing_arg;
9940
9941 type = REDIRECT_TYPE_LOCATION;
9942 cur_arg++;
9943 destination = args[cur_arg];
9944 }
9945 else if (strcmp(args[cur_arg], "prefix") == 0) {
9946 if (!*args[cur_arg + 1])
9947 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009948 type = REDIRECT_TYPE_PREFIX;
9949 cur_arg++;
9950 destination = args[cur_arg];
9951 }
9952 else if (strcmp(args[cur_arg], "scheme") == 0) {
9953 if (!*args[cur_arg + 1])
9954 goto missing_arg;
9955
9956 type = REDIRECT_TYPE_SCHEME;
9957 cur_arg++;
9958 destination = args[cur_arg];
9959 }
9960 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9961 if (!*args[cur_arg + 1])
9962 goto missing_arg;
9963
9964 cur_arg++;
9965 cookie = args[cur_arg];
9966 cookie_set = 1;
9967 }
9968 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9969 if (!*args[cur_arg + 1])
9970 goto missing_arg;
9971
9972 cur_arg++;
9973 cookie = args[cur_arg];
9974 cookie_set = 0;
9975 }
9976 else if (strcmp(args[cur_arg], "code") == 0) {
9977 if (!*args[cur_arg + 1])
9978 goto missing_arg;
9979
9980 cur_arg++;
9981 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009982 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009983 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009984 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009985 args[cur_arg - 1], args[cur_arg]);
9986 return NULL;
9987 }
9988 }
9989 else if (!strcmp(args[cur_arg],"drop-query")) {
9990 flags |= REDIRECT_FLAG_DROP_QS;
9991 }
9992 else if (!strcmp(args[cur_arg],"append-slash")) {
9993 flags |= REDIRECT_FLAG_APPEND_SLASH;
9994 }
9995 else if (strcmp(args[cur_arg], "if") == 0 ||
9996 strcmp(args[cur_arg], "unless") == 0) {
9997 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9998 if (!cond) {
9999 memprintf(errmsg, "error in condition: %s", *errmsg);
10000 return NULL;
10001 }
10002 break;
10003 }
10004 else {
10005 memprintf(errmsg,
10006 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10007 args[cur_arg]);
10008 return NULL;
10009 }
10010 cur_arg++;
10011 }
10012
10013 if (type == REDIRECT_TYPE_NONE) {
10014 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10015 return NULL;
10016 }
10017
Willy Tarreaube4653b2015-05-28 15:26:58 +020010018 if (dir && type != REDIRECT_TYPE_LOCATION) {
10019 memprintf(errmsg, "response only supports redirect type 'location'");
10020 return NULL;
10021 }
10022
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010023 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010024 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010025 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010026
10027 if (!use_fmt) {
10028 /* old-style static redirect rule */
10029 rule->rdr_str = strdup(destination);
10030 rule->rdr_len = strlen(destination);
10031 }
10032 else {
10033 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010034
10035 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10036 * if prefix == "/", we don't want to add anything, otherwise it
10037 * makes it hard for the user to configure a self-redirection.
10038 */
Godbachd9722032014-12-18 15:44:58 +080010039 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010040 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010041 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10042 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010010043 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10044 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010045 return NULL;
10046 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010047 free(curproxy->conf.lfs_file);
10048 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10049 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010050 }
10051 }
10052
Willy Tarreau4baae242012-12-27 12:00:31 +010010053 if (cookie) {
10054 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10055 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10056 */
10057 rule->cookie_len = strlen(cookie);
10058 if (cookie_set) {
10059 rule->cookie_str = malloc(rule->cookie_len + 10);
10060 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10061 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10062 rule->cookie_len += 9;
10063 } else {
10064 rule->cookie_str = malloc(rule->cookie_len + 21);
10065 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10066 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10067 rule->cookie_len += 20;
10068 }
10069 }
10070 rule->type = type;
10071 rule->code = code;
10072 rule->flags = flags;
10073 LIST_INIT(&rule->list);
10074 return rule;
10075
10076 missing_arg:
10077 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10078 return NULL;
10079}
10080
Willy Tarreau8797c062007-05-07 00:55:35 +020010081/************************************************************************/
10082/* The code below is dedicated to ACL parsing and matching */
10083/************************************************************************/
10084
10085
Willy Tarreau14174bc2012-04-16 14:34:04 +020010086/* This function ensures that the prerequisites for an L7 fetch are ready,
10087 * which means that a request or response is ready. If some data is missing,
10088 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010089 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10090 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010091 *
10092 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010093 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10094 * decide whether or not an HTTP message is present ;
10095 * 0 if the requested data cannot be fetched or if it is certain that
10096 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010097 * 1 if an HTTP message is ready
10098 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010099int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010100 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010101{
Willy Tarreau192252e2015-04-04 01:47:55 +020010102 struct http_txn *txn;
10103 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010104
Willy Tarreaube508f12016-03-10 11:47:01 +010010105 /* Note: it is possible that <s> is NULL when called before stream
10106 * initialization (eg: tcp-request connection), so this function is the
10107 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010108 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010109 if (!s)
10110 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010111
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010112 if (!s->txn) {
10113 if (unlikely(!http_alloc_txn(s)))
10114 return 0; /* not enough memory */
10115 http_init_txn(s);
10116 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010117 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010118 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010119
10120 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010121 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010122
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010123 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010124 /* If the buffer does not leave enough free space at the end,
10125 * we must first realign it.
10126 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010127 if (s->req.buf->p > s->req.buf->data &&
10128 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10129 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010130
Willy Tarreau14174bc2012-04-16 14:34:04 +020010131 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010132 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010133 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010134
10135 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010136 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010137 http_msg_analyzer(msg, &txn->hdr_idx);
10138
10139 /* Still no valid request ? */
10140 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010141 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010142 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010143 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144 }
10145 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010146 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010147 return 0;
10148 }
10149
10150 /* OK we just got a valid HTTP request. We have some minor
10151 * preparation to perform so that further checks can rely
10152 * on HTTP tests.
10153 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010154
10155 /* If the request was parsed but was too large, we must absolutely
10156 * return an error so that it is not processed. At the moment this
10157 * cannot happen, but if the parsers are to change in the future,
10158 * we want this check to be maintained.
10159 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010160 if (unlikely(s->req.buf->i + s->req.buf->p >
10161 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010162 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010163 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010164 return 1;
10165 }
10166
Willy Tarreau9b28e032012-10-12 23:49:43 +020010167 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010168 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010169 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010170
Willy Tarreau506d0502013-07-06 13:29:24 +020010171 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10172 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010173 }
10174
Willy Tarreau506d0502013-07-06 13:29:24 +020010175 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010176 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010177 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010178
10179 /* otherwise everything's ready for the request */
10180 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010181 else {
10182 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010183 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10184 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010185 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010186 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010187 }
10188
10189 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010190 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010191 return 1;
10192}
Willy Tarreau8797c062007-05-07 00:55:35 +020010193
Willy Tarreau8797c062007-05-07 00:55:35 +020010194/* 1. Check on METHOD
10195 * We use the pre-parsed method if it is known, and store its number as an
10196 * integer. If it is unknown, we use the pointer and the length.
10197 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010198static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010199{
10200 int len, meth;
10201
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010202 len = strlen(text);
10203 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010204
10205 pattern->val.i = meth;
10206 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010207 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010208 pattern->len = len;
10209 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010210 else {
10211 pattern->ptr.str = NULL;
10212 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010213 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010214 return 1;
10215}
10216
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010217/* This function fetches the method of current HTTP request and stores
10218 * it in the global pattern struct as a chunk. There are two possibilities :
10219 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10220 * in <len> and <ptr> is NULL ;
10221 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10222 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010223 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010224 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010225static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010226smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010227{
10228 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010229 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010230
Willy Tarreau24e32d82012-04-23 23:55:44 +020010231 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010232
Willy Tarreaube508f12016-03-10 11:47:01 +010010233 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010234 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010235 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010236 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010237 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010238 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10239 /* ensure the indexes are not affected */
10240 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010241 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010242 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10243 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010244 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010245 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010246 return 1;
10247}
10248
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010249/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010250static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010251{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010252 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010253 struct pattern_list *lst;
10254 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010255
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010256 list_for_each_entry(lst, &expr->patterns, list) {
10257 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010258
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010259 /* well-known method */
10260 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010261 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010262 return pattern;
10263 else
10264 continue;
10265 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010266
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010267 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010268 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010269 continue;
10270
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010271 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010272 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10273 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010274 return pattern;
10275 }
10276 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010277}
10278
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010279static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010280smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010281{
Willy Tarreaube508f12016-03-10 11:47:01 +010010282 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010283 char *ptr;
10284 int len;
10285
Willy Tarreauc0239e02012-04-16 14:42:55 +020010286 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010287
Willy Tarreaube508f12016-03-10 11:47:01 +010010288 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010289 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010290 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010291
10292 while ((len-- > 0) && (*ptr++ != '/'));
10293 if (len <= 0)
10294 return 0;
10295
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010296 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010297 smp->data.u.str.str = ptr;
10298 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010300 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010301 return 1;
10302}
10303
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010304static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010305smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010306{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010307 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 char *ptr;
10309 int len;
10310
Willy Tarreauc0239e02012-04-16 14:42:55 +020010311 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010312
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010313 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010314 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10315 return 0;
10316
Willy Tarreau8797c062007-05-07 00:55:35 +020010317 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010318 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010319
10320 while ((len-- > 0) && (*ptr++ != '/'));
10321 if (len <= 0)
10322 return 0;
10323
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010324 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 smp->data.u.str.str = ptr;
10326 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010327
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010328 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010329 return 1;
10330}
10331
10332/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010333static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010334smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010335{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010336 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010337 char *ptr;
10338 int len;
10339
Willy Tarreauc0239e02012-04-16 14:42:55 +020010340 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010341
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010342 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010343 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10344 return 0;
10345
Willy Tarreau8797c062007-05-07 00:55:35 +020010346 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010347 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010349 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010351 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010352 return 1;
10353}
10354
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010355static int
10356smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10357{
10358 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10359 return 0;
10360
10361 if (!smp->strm->unique_id) {
10362 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10363 return 0;
10364 smp->strm->unique_id[0] = '\0';
10365 }
10366 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10367 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10368
10369 smp->data.type = SMP_T_STR;
10370 smp->data.u.str.str = smp->strm->unique_id;
10371 smp->flags = SMP_F_CONST;
10372 return 1;
10373}
10374
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010375/* returns the longest available part of the body. This requires that the body
10376 * has been waited for using http-buffer-request.
10377 */
10378static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010379smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010380{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010381 struct http_msg *msg;
10382 unsigned long len;
10383 unsigned long block1;
10384 char *body;
10385 struct chunk *temp;
10386
10387 CHECK_HTTP_MESSAGE_FIRST();
10388
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010389 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010390 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010391 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010392 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010393
10394 len = http_body_bytes(msg);
10395 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10396
10397 block1 = len;
10398 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10399 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10400
10401 if (block1 == len) {
10402 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010403 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010404 smp->data.u.str.str = body;
10405 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010406 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10407 }
10408 else {
10409 /* buffer is wrapped, we need to defragment it */
10410 temp = get_trash_chunk();
10411 memcpy(temp->str, body, block1);
10412 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010413 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010414 smp->data.u.str.str = temp->str;
10415 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010416 smp->flags = SMP_F_VOL_TEST;
10417 }
10418 return 1;
10419}
10420
10421
10422/* returns the available length of the body. This requires that the body
10423 * has been waited for using http-buffer-request.
10424 */
10425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010426smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010427{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010428 struct http_msg *msg;
10429
10430 CHECK_HTTP_MESSAGE_FIRST();
10431
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010432 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010433 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010434 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010435 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010436
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010437 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010438 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010439
10440 smp->flags = SMP_F_VOL_TEST;
10441 return 1;
10442}
10443
10444
10445/* returns the advertised length of the body, or the advertised size of the
10446 * chunks available in the buffer. This requires that the body has been waited
10447 * for using http-buffer-request.
10448 */
10449static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010450smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010451{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010452 struct http_msg *msg;
10453
10454 CHECK_HTTP_MESSAGE_FIRST();
10455
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010456 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010457 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010458 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010459 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010460
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010461 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010462 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010463
10464 smp->flags = SMP_F_VOL_TEST;
10465 return 1;
10466}
10467
10468
Willy Tarreau8797c062007-05-07 00:55:35 +020010469/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010470static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010471smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010472{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010473 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010474
Willy Tarreauc0239e02012-04-16 14:42:55 +020010475 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010476
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010477 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010478 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010479 smp->data.u.str.len = txn->req.sl.rq.u_l;
10480 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010481 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010482 return 1;
10483}
10484
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010485static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010486smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010487{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010488 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010489 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010490
Willy Tarreauc0239e02012-04-16 14:42:55 +020010491 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010492
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010493 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010494 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010495 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010496 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010497
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010498 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010499 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010500 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010501 return 1;
10502}
10503
10504static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010505smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010506{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010507 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010508 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010509
Willy Tarreauc0239e02012-04-16 14:42:55 +020010510 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010511
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010512 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010513 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010514 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10515 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010516
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010517 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010518 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010519 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010520 return 1;
10521}
10522
Willy Tarreau185b5c42012-04-26 15:11:51 +020010523/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10524 * Accepts an optional argument of type string containing the header field name,
10525 * and an optional argument of type signed or unsigned integer to request an
10526 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010527 * headers are considered from the first one. It does not stop on commas and
10528 * returns full lines instead (useful for User-Agent or Date for example).
10529 */
10530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010531smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010532{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010533 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010534 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010535 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010536 int occ = 0;
10537 const char *name_str = NULL;
10538 int name_len = 0;
10539
10540 if (!ctx) {
10541 /* first call */
10542 ctx = &static_hdr_ctx;
10543 ctx->idx = 0;
10544 smp->ctx.a[0] = ctx;
10545 }
10546
10547 if (args) {
10548 if (args[0].type != ARGT_STR)
10549 return 0;
10550 name_str = args[0].data.str.str;
10551 name_len = args[0].data.str.len;
10552
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010553 if (args[1].type == ARGT_SINT)
10554 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010555 }
10556
10557 CHECK_HTTP_MESSAGE_FIRST();
10558
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010559 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010561
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010562 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10563 /* search for header from the beginning */
10564 ctx->idx = 0;
10565
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010566 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010567 /* no explicit occurrence and single fetch => last header by default */
10568 occ = -1;
10569
10570 if (!occ)
10571 /* prepare to report multiple occurrences for ACL fetches */
10572 smp->flags |= SMP_F_NOT_LAST;
10573
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010574 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010575 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010576 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010577 return 1;
10578
10579 smp->flags &= ~SMP_F_NOT_LAST;
10580 return 0;
10581}
10582
10583/* 6. Check on HTTP header count. The number of occurrences is returned.
10584 * Accepts exactly 1 argument of type string. It does not stop on commas and
10585 * returns full lines instead (useful for User-Agent or Date for example).
10586 */
10587static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010588smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010589{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010590 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010591 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010592 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010593 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010594 const char *name = NULL;
10595 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010596
Willy Tarreau601a4d12015-04-01 19:16:09 +020010597 if (args && args->type == ARGT_STR) {
10598 name = args->data.str.str;
10599 len = args->data.str.len;
10600 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010601
10602 CHECK_HTTP_MESSAGE_FIRST();
10603
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010604 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010605 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010606
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010607 ctx.idx = 0;
10608 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010609 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010610 cnt++;
10611
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010612 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010613 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010614 smp->flags = SMP_F_VOL_HDR;
10615 return 1;
10616}
10617
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010618static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010619smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010620{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010621 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010622 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010623 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010624 struct chunk *temp;
10625 char del = ',';
10626
10627 if (args && args->type == ARGT_STR)
10628 del = *args[0].data.str.str;
10629
10630 CHECK_HTTP_MESSAGE_FIRST();
10631
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010632 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010634
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010635 temp = get_trash_chunk();
10636
10637 ctx.idx = 0;
10638 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10639 if (temp->len)
10640 temp->str[temp->len++] = del;
10641 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10642 temp->len += ctx.del;
10643 }
10644
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010645 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010646 smp->data.u.str.str = temp->str;
10647 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010648 smp->flags = SMP_F_VOL_HDR;
10649 return 1;
10650}
10651
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010652/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10653 * Accepts an optional argument of type string containing the header field name,
10654 * and an optional argument of type signed or unsigned integer to request an
10655 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010656 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010657 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010659smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010661 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010662 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010663 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010664 int occ = 0;
10665 const char *name_str = NULL;
10666 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010667
Willy Tarreaua890d072013-04-02 12:01:06 +020010668 if (!ctx) {
10669 /* first call */
10670 ctx = &static_hdr_ctx;
10671 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010672 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010673 }
10674
Willy Tarreau185b5c42012-04-26 15:11:51 +020010675 if (args) {
10676 if (args[0].type != ARGT_STR)
10677 return 0;
10678 name_str = args[0].data.str.str;
10679 name_len = args[0].data.str.len;
10680
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010681 if (args[1].type == ARGT_SINT)
10682 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010683 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010684
Willy Tarreaue333ec92012-04-16 16:26:40 +020010685 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010686
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010687 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010688 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010689
Willy Tarreau185b5c42012-04-26 15:11:51 +020010690 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010691 /* search for header from the beginning */
10692 ctx->idx = 0;
10693
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010694 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010695 /* no explicit occurrence and single fetch => last header by default */
10696 occ = -1;
10697
10698 if (!occ)
10699 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010700 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010701
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010702 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010703 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010704 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010705 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010706
Willy Tarreau37406352012-04-23 16:16:37 +020010707 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010708 return 0;
10709}
10710
Willy Tarreauc11416f2007-06-17 16:58:38 +020010711/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010712 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010713 */
10714static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010715smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010716{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010717 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010718 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010719 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010720 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010721 const char *name = NULL;
10722 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010723
Willy Tarreau601a4d12015-04-01 19:16:09 +020010724 if (args && args->type == ARGT_STR) {
10725 name = args->data.str.str;
10726 len = args->data.str.len;
10727 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010728
Willy Tarreaue333ec92012-04-16 16:26:40 +020010729 CHECK_HTTP_MESSAGE_FIRST();
10730
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010731 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010732 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010733
Willy Tarreau33a7e692007-06-10 19:45:56 +020010734 ctx.idx = 0;
10735 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010736 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010737 cnt++;
10738
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010739 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010740 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010741 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010742 return 1;
10743}
10744
Willy Tarreau185b5c42012-04-26 15:11:51 +020010745/* Fetch an HTTP header's integer value. The integer value is returned. It
10746 * takes a mandatory argument of type string and an optional one of type int
10747 * to designate a specific occurrence. It returns an unsigned integer, which
10748 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010749 */
10750static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010751smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010752{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010753 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010754
Willy Tarreauf853c462012-04-23 18:53:56 +020010755 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010756 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010757 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010758 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010759
Willy Tarreaud53e2422012-04-16 17:21:11 +020010760 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010761}
10762
Cyril Bonté69fa9922012-10-25 00:01:06 +020010763/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10764 * and an optional one of type int to designate a specific occurrence.
10765 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010766 */
10767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010768smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010769{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010770 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010771
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010773 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010774 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010775 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010776 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010777 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010778 if (smp->data.u.str.len < temp->size - 1) {
10779 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10780 temp->str[smp->data.u.str.len] = '\0';
10781 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010782 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010783 break;
10784 }
10785 }
10786 }
10787
Willy Tarreaud53e2422012-04-16 17:21:11 +020010788 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010789 if (!(smp->flags & SMP_F_NOT_LAST))
10790 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010791 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010792 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010793}
10794
Willy Tarreau737b0c12007-06-10 21:28:46 +020010795/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10796 * the first '/' after the possible hostname, and ends before the possible '?'.
10797 */
10798static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010799smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010800{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010801 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010802 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010803
Willy Tarreauc0239e02012-04-16 14:42:55 +020010804 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010805
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010806 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010807 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010808 ptr = http_get_path(txn);
10809 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010810 return 0;
10811
10812 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010813 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010814 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010815
10816 while (ptr < end && *ptr != '?')
10817 ptr++;
10818
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010819 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010820 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010821 return 1;
10822}
10823
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010824/* This produces a concatenation of the first occurrence of the Host header
10825 * followed by the path component if it begins with a slash ('/'). This means
10826 * that '*' will not be added, resulting in exactly the first Host entry.
10827 * If no Host header is found, then the path is returned as-is. The returned
10828 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010829 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010830 */
10831static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010833{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010834 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010835 char *ptr, *end, *beg;
10836 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010837 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010838
10839 CHECK_HTTP_MESSAGE_FIRST();
10840
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010841 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010842 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010843 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010844 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010845
10846 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010847 temp = get_trash_chunk();
10848 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010849 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010850 smp->data.u.str.str = temp->str;
10851 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010852
10853 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010854 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010855 beg = http_get_path(txn);
10856 if (!beg)
10857 beg = end;
10858
10859 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10860
10861 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010862 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10863 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010864 }
10865
10866 smp->flags = SMP_F_VOL_1ST;
10867 return 1;
10868}
10869
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010870/* This produces a 32-bit hash of the concatenation of the first occurrence of
10871 * the Host header followed by the path component if it begins with a slash ('/').
10872 * This means that '*' will not be added, resulting in exactly the first Host
10873 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010874 * is hashed using the path hash followed by a full avalanche hash and provides a
10875 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010876 * high-traffic sites without having to store whole paths.
10877 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010878int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010879smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010880{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010881 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010882 struct hdr_ctx ctx;
10883 unsigned int hash = 0;
10884 char *ptr, *beg, *end;
10885 int len;
10886
10887 CHECK_HTTP_MESSAGE_FIRST();
10888
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010889 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010890 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010891 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010892 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10893 ptr = ctx.line + ctx.val;
10894 len = ctx.vlen;
10895 while (len--)
10896 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10897 }
10898
10899 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010900 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010901 beg = http_get_path(txn);
10902 if (!beg)
10903 beg = end;
10904
10905 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10906
10907 if (beg < ptr && *beg == '/') {
10908 while (beg < ptr)
10909 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10910 }
10911 hash = full_hash(hash);
10912
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010913 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010914 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010915 smp->flags = SMP_F_VOL_1ST;
10916 return 1;
10917}
10918
Willy Tarreau4a550602012-12-09 14:53:32 +010010919/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010920 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10921 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10922 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010923 * that in environments where IPv6 is insignificant, truncating the output to
10924 * 8 bytes would still work.
10925 */
10926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010927smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010928{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010929 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010930 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010931
10932 if (!cli_conn)
10933 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010934
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010935 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010936 return 0;
10937
Willy Tarreau47ca5452012-12-23 20:22:19 +010010938 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010939 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010940 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010941
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010942 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010943 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010944 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010945 temp->len += 4;
10946 break;
10947 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010948 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010949 temp->len += 16;
10950 break;
10951 default:
10952 return 0;
10953 }
10954
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010955 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010956 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010957 return 1;
10958}
10959
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010960/* Extracts the query string, which comes after the question mark '?'. If no
10961 * question mark is found, nothing is returned. Otherwise it returns a sample
10962 * of type string carrying the whole query string.
10963 */
10964static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010965smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010966{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010967 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010968 char *ptr, *end;
10969
10970 CHECK_HTTP_MESSAGE_FIRST();
10971
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010972 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010973 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10974 end = ptr + txn->req.sl.rq.u_l;
10975
10976 /* look up the '?' */
10977 do {
10978 if (ptr == end)
10979 return 0;
10980 } while (*ptr++ != '?');
10981
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010982 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010983 smp->data.u.str.str = ptr;
10984 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010985 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10986 return 1;
10987}
10988
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010989static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010990smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010991{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010992 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10993 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10994 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010995
Willy Tarreau24e32d82012-04-23 23:55:44 +020010996 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010997
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010998 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010999 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011000 return 1;
11001}
11002
Willy Tarreau7f18e522010-10-22 20:04:13 +020011003/* return a valid test if the current request is the first one on the connection */
11004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011005smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011006{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011007 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011008 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011009 return 1;
11010}
11011
Willy Tarreau34db1082012-04-19 17:16:54 +020011012/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011013static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011014smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011015{
11016
Willy Tarreau24e32d82012-04-23 23:55:44 +020011017 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011018 return 0;
11019
Willy Tarreauc0239e02012-04-16 14:42:55 +020011020 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011021
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011022 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011023 return 0;
11024
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011025 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011026 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011027 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011028 return 1;
11029}
Willy Tarreau8797c062007-05-07 00:55:35 +020011030
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011031/* Accepts exactly 1 argument of type userlist */
11032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011033smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011034{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011035 if (!args || args->type != ARGT_USR)
11036 return 0;
11037
11038 CHECK_HTTP_MESSAGE_FIRST();
11039
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011040 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011041 return 0;
11042
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011043 /* if the user does not belong to the userlist or has a wrong password,
11044 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011045 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011046 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011047 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11048 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011049 return 0;
11050
11051 /* pat_match_auth() will need the user list */
11052 smp->ctx.a[0] = args->data.usr;
11053
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011054 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011055 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011056 smp->data.u.str.str = smp->strm->txn->auth.user;
11057 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011058
11059 return 1;
11060}
11061
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011062/* Try to find the next occurrence of a cookie name in a cookie header value.
11063 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11064 * the cookie value is returned into *value and *value_l, and the function
11065 * returns a pointer to the next pointer to search from if the value was found.
11066 * Otherwise if the cookie was not found, NULL is returned and neither value
11067 * nor value_l are touched. The input <hdr> string should first point to the
11068 * header's value, and the <hdr_end> pointer must point to the first character
11069 * not part of the value. <list> must be non-zero if value may represent a list
11070 * of values (cookie headers). This makes it faster to abort parsing when no
11071 * list is expected.
11072 */
David Carlier4686f792015-09-25 14:10:50 +010011073char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011074extract_cookie_value(char *hdr, const char *hdr_end,
11075 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011076 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011077{
11078 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11079 char *next;
11080
11081 /* we search at least a cookie name followed by an equal, and more
11082 * generally something like this :
11083 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11084 */
11085 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11086 /* Iterate through all cookies on this line */
11087
Willy Tarreau2235b262016-11-05 15:50:20 +010011088 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011089 att_beg++;
11090
11091 /* find att_end : this is the first character after the last non
11092 * space before the equal. It may be equal to hdr_end.
11093 */
11094 equal = att_end = att_beg;
11095
11096 while (equal < hdr_end) {
11097 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11098 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011099 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011100 continue;
11101 att_end = equal;
11102 }
11103
11104 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11105 * is between <att_beg> and <equal>, both may be identical.
11106 */
11107
11108 /* look for end of cookie if there is an equal sign */
11109 if (equal < hdr_end && *equal == '=') {
11110 /* look for the beginning of the value */
11111 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011112 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011113 val_beg++;
11114
11115 /* find the end of the value, respecting quotes */
11116 next = find_cookie_value_end(val_beg, hdr_end);
11117
11118 /* make val_end point to the first white space or delimitor after the value */
11119 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011120 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011121 val_end--;
11122 } else {
11123 val_beg = val_end = next = equal;
11124 }
11125
11126 /* We have nothing to do with attributes beginning with '$'. However,
11127 * they will automatically be removed if a header before them is removed,
11128 * since they're supposed to be linked together.
11129 */
11130 if (*att_beg == '$')
11131 continue;
11132
11133 /* Ignore cookies with no equal sign */
11134 if (equal == next)
11135 continue;
11136
11137 /* Now we have the cookie name between att_beg and att_end, and
11138 * its value between val_beg and val_end.
11139 */
11140
11141 if (att_end - att_beg == cookie_name_l &&
11142 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11143 /* let's return this value and indicate where to go on from */
11144 *value = val_beg;
11145 *value_l = val_end - val_beg;
11146 return next + 1;
11147 }
11148
11149 /* Set-Cookie headers only have the name in the first attr=value part */
11150 if (!list)
11151 break;
11152 }
11153
11154 return NULL;
11155}
11156
William Lallemanda43ba4e2014-01-28 18:14:25 +010011157/* Fetch a captured HTTP request header. The index is the position of
11158 * the "capture" option in the configuration file
11159 */
11160static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011161smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011162{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011163 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011164 int idx;
11165
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011166 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011167 return 0;
11168
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011169 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011170
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011171 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011172 return 0;
11173
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011174 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011175 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011176 smp->data.u.str.str = smp->strm->req_cap[idx];
11177 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011178
11179 return 1;
11180}
11181
11182/* Fetch a captured HTTP response header. The index is the position of
11183 * the "capture" option in the configuration file
11184 */
11185static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011186smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011187{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011188 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011189 int idx;
11190
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011191 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011192 return 0;
11193
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011194 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011195
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011196 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011197 return 0;
11198
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011199 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011200 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011201 smp->data.u.str.str = smp->strm->res_cap[idx];
11202 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011203
11204 return 1;
11205}
11206
William Lallemand65ad6e12014-01-31 15:08:02 +010011207/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11208static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011209smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011210{
11211 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011212 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011213 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011214
Willy Tarreau15e91e12015-04-04 00:52:09 +020011215 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011216 return 0;
11217
William Lallemand96a77852014-02-05 00:30:02 +010011218 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011219
William Lallemand96a77852014-02-05 00:30:02 +010011220 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11221 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011222
William Lallemand96a77852014-02-05 00:30:02 +010011223 temp = get_trash_chunk();
11224 temp->str = txn->uri;
11225 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011226 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011227 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011228 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011229
11230 return 1;
11231
11232}
11233
11234/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011236smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011237{
11238 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011239 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011240 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011241
Willy Tarreau15e91e12015-04-04 00:52:09 +020011242 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011243 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011244
William Lallemand65ad6e12014-01-31 15:08:02 +010011245 ptr = txn->uri;
11246
11247 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11248 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011249
William Lallemand65ad6e12014-01-31 15:08:02 +010011250 if (!*ptr)
11251 return 0;
11252
11253 ptr++; /* skip the space */
11254
11255 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011256 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011257 if (!ptr)
11258 return 0;
11259 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11260 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011261
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011262 smp->data.u.str = *temp;
11263 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011264 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011265 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011266
11267 return 1;
11268}
11269
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011270/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11271 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11272 */
11273static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011274smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011275{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011276 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011277
Willy Tarreau15e91e12015-04-04 00:52:09 +020011278 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011279 return 0;
11280
11281 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011282 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011283 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011284 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011285
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011286 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011287 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011288 smp->flags = SMP_F_CONST;
11289 return 1;
11290
11291}
11292
11293/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11294 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11295 */
11296static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011297smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011298{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011299 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011300
Willy Tarreau15e91e12015-04-04 00:52:09 +020011301 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011302 return 0;
11303
11304 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011305 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011306 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011307 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011308
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011309 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011310 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011311 smp->flags = SMP_F_CONST;
11312 return 1;
11313
11314}
11315
William Lallemand65ad6e12014-01-31 15:08:02 +010011316
Willy Tarreaue333ec92012-04-16 16:26:40 +020011317/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011318 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011319 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011320 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011321 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011322 * Accepts exactly 1 argument of type string. If the input options indicate
11323 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011324 * The returned sample is of type CSTR. Can be used to parse cookies in other
11325 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011327int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011328{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011329 struct http_txn *txn;
11330 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011331 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011332 const struct http_msg *msg;
11333 const char *hdr_name;
11334 int hdr_name_len;
11335 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011336 int occ = 0;
11337 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011338
Willy Tarreau24e32d82012-04-23 23:55:44 +020011339 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011340 return 0;
11341
Willy Tarreaua890d072013-04-02 12:01:06 +020011342 if (!ctx) {
11343 /* first call */
11344 ctx = &static_hdr_ctx;
11345 ctx->idx = 0;
11346 smp->ctx.a[2] = ctx;
11347 }
11348
Willy Tarreaue333ec92012-04-16 16:26:40 +020011349 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011350
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011351 txn = smp->strm->txn;
11352 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011353
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011354 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011355 msg = &txn->req;
11356 hdr_name = "Cookie";
11357 hdr_name_len = 6;
11358 } else {
11359 msg = &txn->rsp;
11360 hdr_name = "Set-Cookie";
11361 hdr_name_len = 10;
11362 }
11363
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011364 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011365 /* no explicit occurrence and single fetch => last cookie by default */
11366 occ = -1;
11367
11368 /* OK so basically here, either we want only one value and it's the
11369 * last one, or we want to iterate over all of them and we fetch the
11370 * next one.
11371 */
11372
Willy Tarreau9b28e032012-10-12 23:49:43 +020011373 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011374 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011375 /* search for the header from the beginning, we must first initialize
11376 * the search parameters.
11377 */
Willy Tarreau37406352012-04-23 16:16:37 +020011378 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379 ctx->idx = 0;
11380 }
11381
Willy Tarreau28376d62012-04-26 21:26:10 +020011382 smp->flags |= SMP_F_VOL_HDR;
11383
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011384 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011385 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11386 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011387 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11388 goto out;
11389
Willy Tarreau24e32d82012-04-23 23:55:44 +020011390 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391 continue;
11392
Willy Tarreau37406352012-04-23 16:16:37 +020011393 smp->ctx.a[0] = ctx->line + ctx->val;
11394 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011395 }
11396
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011397 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011398 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011399 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011400 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011401 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 &smp->data.u.str.str,
11403 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011404 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011405 found = 1;
11406 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011407 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011408 smp->flags |= SMP_F_NOT_LAST;
11409 return 1;
11410 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011411 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011412 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011413 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011414 /* all cookie headers and values were scanned. If we're looking for the
11415 * last occurrence, we may return it now.
11416 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011417 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011418 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011419 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011420}
11421
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011422/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011423 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011424 * multiple cookies may be parsed on the same line. The returned sample is of
11425 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011426 */
11427static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011428smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011429{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011430 struct http_txn *txn;
11431 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011432 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011433 const struct http_msg *msg;
11434 const char *hdr_name;
11435 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011436 int cnt;
11437 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011438 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011439
Willy Tarreau24e32d82012-04-23 23:55:44 +020011440 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011441 return 0;
11442
Willy Tarreaue333ec92012-04-16 16:26:40 +020011443 CHECK_HTTP_MESSAGE_FIRST();
11444
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011445 txn = smp->strm->txn;
11446 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011447
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011448 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011449 msg = &txn->req;
11450 hdr_name = "Cookie";
11451 hdr_name_len = 6;
11452 } else {
11453 msg = &txn->rsp;
11454 hdr_name = "Set-Cookie";
11455 hdr_name_len = 10;
11456 }
11457
Willy Tarreau9b28e032012-10-12 23:49:43 +020011458 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011459 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011460 ctx.idx = 0;
11461 cnt = 0;
11462
11463 while (1) {
11464 /* Note: val_beg == NULL every time we need to fetch a new header */
11465 if (!val_beg) {
11466 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11467 break;
11468
Willy Tarreau24e32d82012-04-23 23:55:44 +020011469 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011470 continue;
11471
11472 val_beg = ctx.line + ctx.val;
11473 val_end = val_beg + ctx.vlen;
11474 }
11475
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011476 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011477 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011478 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011479 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011480 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011481 &smp->data.u.str.str,
11482 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011483 cnt++;
11484 }
11485 }
11486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011487 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011488 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011489 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011490 return 1;
11491}
11492
Willy Tarreau51539362012-05-08 12:46:28 +020011493/* Fetch an cookie's integer value. The integer value is returned. It
11494 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11495 */
11496static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011497smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011498{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011499 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011500
11501 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011502 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011503 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011504 }
11505
11506 return ret;
11507}
11508
Willy Tarreau8797c062007-05-07 00:55:35 +020011509/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011510/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011511/************************************************************************/
11512
David Cournapeau16023ee2010-12-23 20:55:41 +090011513/*
11514 * Given a path string and its length, find the position of beginning of the
11515 * query string. Returns NULL if no query string is found in the path.
11516 *
11517 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11518 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011519 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011520 */
bedis4c75cca2012-10-05 08:38:24 +020011521static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011522{
11523 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011524
bedis4c75cca2012-10-05 08:38:24 +020011525 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011526 return p ? p + 1 : NULL;
11527}
11528
bedis4c75cca2012-10-05 08:38:24 +020011529static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011530{
bedis4c75cca2012-10-05 08:38:24 +020011531 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011532}
11533
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011534/* after increasing a pointer value, it can exceed the first buffer
11535 * size. This function transform the value of <ptr> according with
11536 * the expected position. <chunks> is an array of the one or two
11537 * avalaible chunks. The first value is the start of the first chunk,
11538 * the second value if the end+1 of the first chunks. The third value
11539 * is NULL or the start of the second chunk and the fourth value is
11540 * the end+1 of the second chunk. The function returns 1 if does a
11541 * wrap, else returns 0.
11542 */
11543static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11544{
11545 if (*ptr < chunks[1])
11546 return 0;
11547 if (!chunks[2])
11548 return 0;
11549 *ptr = chunks[2] + ( *ptr - chunks[1] );
11550 return 1;
11551}
11552
David Cournapeau16023ee2010-12-23 20:55:41 +090011553/*
11554 * Given a url parameter, find the starting position of the first occurence,
11555 * or NULL if the parameter is not found.
11556 *
11557 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11558 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011559 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011560 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011561 * or the second chunk. The caller must be check the position before using the
11562 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011563 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011564static const char *
11565find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011566 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011567 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011568{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011569 const char *pos, *last, *equal;
11570 const char **bufs = chunks;
11571 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011572
David Cournapeau16023ee2010-12-23 20:55:41 +090011573
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011574 pos = bufs[0];
11575 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011576 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011577 /* Check the equal. */
11578 equal = pos + url_param_name_l;
11579 if (fix_pointer_if_wrap(chunks, &equal)) {
11580 if (equal >= chunks[3])
11581 return NULL;
11582 } else {
11583 if (equal >= chunks[1])
11584 return NULL;
11585 }
11586 if (*equal == '=') {
11587 if (pos + url_param_name_l > last) {
11588 /* process wrap case, we detect a wrap. In this case, the
11589 * comparison is performed in two parts.
11590 */
11591
11592 /* This is the end, we dont have any other chunk. */
11593 if (bufs != chunks || !bufs[2])
11594 return NULL;
11595
11596 /* Compute the length of each part of the comparison. */
11597 l1 = last - pos;
11598 l2 = url_param_name_l - l1;
11599
11600 /* The second buffer is too short to contain the compared string. */
11601 if (bufs[2] + l2 > bufs[3])
11602 return NULL;
11603
11604 if (memcmp(pos, url_param_name, l1) == 0 &&
11605 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11606 return pos;
11607
11608 /* Perform wrapping and jump the string who fail the comparison. */
11609 bufs += 2;
11610 pos = bufs[0] + l2;
11611 last = bufs[1];
11612
11613 } else {
11614 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011615 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11616 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011617 pos += url_param_name_l + 1;
11618 if (fix_pointer_if_wrap(chunks, &pos))
11619 last = bufs[2];
11620 }
11621 }
11622
11623 while (1) {
11624 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011625 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011626 pos++;
11627 if (pos < last)
11628 break;
11629 /* process buffer wrapping. */
11630 if (bufs != chunks || !bufs[2])
11631 return NULL;
11632 bufs += 2;
11633 pos = bufs[0];
11634 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011635 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011636 pos++;
11637 }
11638 return NULL;
11639}
11640
11641/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011642 * Given a url parameter name and a query string, find the next value.
11643 * An empty url_param_name matches the first available parameter.
11644 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11645 * respectively provide a pointer to the value and its end.
11646 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011647 */
11648static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011649find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011650 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011651 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011652{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011653 const char *arg_start, *qs_end;
11654 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011655
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011656 arg_start = chunks[0];
11657 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011658 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011659 /* Looks for an argument name. */
11660 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011661 url_param_name, url_param_name_l,
11662 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011663 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011664 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011665 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011666 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011667 if (!arg_start)
11668 return 0;
11669
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011670 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011671 while (1) {
11672 /* looks for the first argument. */
11673 value_start = memchr(arg_start, '=', qs_end - arg_start);
11674 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 /* Check for wrapping. */
11676 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011677 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011678 chunks[2]) {
11679 arg_start = chunks[2];
11680 qs_end = chunks[3];
11681 continue;
11682 }
11683 return 0;
11684 }
11685 break;
11686 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011687 value_start++;
11688 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 else {
11690 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011691 value_start = arg_start + url_param_name_l + 1;
11692
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 /* Check for pointer wrapping. */
11694 if (fix_pointer_if_wrap(chunks, &value_start)) {
11695 /* Update the end pointer. */
11696 qs_end = chunks[3];
11697
11698 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011699 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011700 return 0;
11701 }
11702 }
11703
David Cournapeau16023ee2010-12-23 20:55:41 +090011704 value_end = value_start;
11705
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011706 while (1) {
11707 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11708 value_end++;
11709 if (value_end < qs_end)
11710 break;
11711 /* process buffer wrapping. */
11712 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011713 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011714 chunks[2]) {
11715 value_end = chunks[2];
11716 qs_end = chunks[3];
11717 continue;
11718 }
11719 break;
11720 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011721
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011722 *vstart = value_start;
11723 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011724 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011725}
11726
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011727/* This scans a URL-encoded query string. It takes an optionally wrapping
11728 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11729 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11730 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011731 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011732static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011733smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011734{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011735 const char *vstart, *vend;
11736 struct chunk *temp;
11737 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011738
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011739 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011740 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011741 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011742 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011743 return 0;
11744
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 /* Create sample. If the value is contiguous, return the pointer as CONST,
11746 * if the value is wrapped, copy-it in a buffer.
11747 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011748 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011749 if (chunks[2] &&
11750 vstart >= chunks[0] && vstart <= chunks[1] &&
11751 vend >= chunks[2] && vend <= chunks[3]) {
11752 /* Wrapped case. */
11753 temp = get_trash_chunk();
11754 memcpy(temp->str, vstart, chunks[1] - vstart);
11755 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011756 smp->data.u.str.str = temp->str;
11757 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011758 } else {
11759 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011760 smp->data.u.str.str = (char *)vstart;
11761 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011762 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11763 }
11764
11765 /* Update context, check wrapping. */
11766 chunks[0] = vend;
11767 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11768 chunks[1] = chunks[3];
11769 chunks[2] = NULL;
11770 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011771
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011772 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011773 smp->flags |= SMP_F_NOT_LAST;
11774
David Cournapeau16023ee2010-12-23 20:55:41 +090011775 return 1;
11776}
11777
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011778/* This function iterates over each parameter of the query string. It uses
11779 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011780 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11781 * An optional parameter name is passed in args[0], otherwise any parameter is
11782 * considered. It supports an optional delimiter argument for the beginning of
11783 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011784 */
11785static int
11786smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11787{
11788 struct http_msg *msg;
11789 char delim = '?';
11790 const char *name;
11791 int name_len;
11792
Dragan Dosen26f77e52015-05-25 10:02:11 +020011793 if (!args ||
11794 (args[0].type && args[0].type != ARGT_STR) ||
11795 (args[1].type && args[1].type != ARGT_STR))
11796 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011797
Dragan Dosen26f77e52015-05-25 10:02:11 +020011798 name = "";
11799 name_len = 0;
11800 if (args->type == ARGT_STR) {
11801 name = args->data.str.str;
11802 name_len = args->data.str.len;
11803 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011804
Dragan Dosen26f77e52015-05-25 10:02:11 +020011805 if (args[1].type)
11806 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011807
Dragan Dosen26f77e52015-05-25 10:02:11 +020011808 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011809 CHECK_HTTP_MESSAGE_FIRST();
11810
11811 msg = &smp->strm->txn->req;
11812
11813 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11814 msg->sl.rq.u_l, delim);
11815 if (!smp->ctx.a[0])
11816 return 0;
11817
11818 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011819
11820 /* Assume that the context is filled with NULL pointer
11821 * before the first call.
11822 * smp->ctx.a[2] = NULL;
11823 * smp->ctx.a[3] = NULL;
11824 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011825 }
11826
11827 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11828}
11829
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011830/* This function iterates over each parameter of the body. This requires
11831 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011832 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11833 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11834 * optional second part if the body wraps at the end of the buffer. An optional
11835 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011836 */
11837static int
11838smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11839{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011840 struct http_msg *msg;
11841 unsigned long len;
11842 unsigned long block1;
11843 char *body;
11844 const char *name;
11845 int name_len;
11846
11847 if (!args || (args[0].type && args[0].type != ARGT_STR))
11848 return 0;
11849
11850 name = "";
11851 name_len = 0;
11852 if (args[0].type == ARGT_STR) {
11853 name = args[0].data.str.str;
11854 name_len = args[0].data.str.len;
11855 }
11856
11857 if (!smp->ctx.a[0]) { // first call, find the query string
11858 CHECK_HTTP_MESSAGE_FIRST();
11859
11860 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011861 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011862 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011863 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011864
11865 len = http_body_bytes(msg);
11866 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11867
11868 block1 = len;
11869 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11870 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11871
11872 if (block1 == len) {
11873 /* buffer is not wrapped (or empty) */
11874 smp->ctx.a[0] = body;
11875 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011876
11877 /* Assume that the context is filled with NULL pointer
11878 * before the first call.
11879 * smp->ctx.a[2] = NULL;
11880 * smp->ctx.a[3] = NULL;
11881 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011882 }
11883 else {
11884 /* buffer is wrapped, we need to defragment it */
11885 smp->ctx.a[0] = body;
11886 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011887 smp->ctx.a[2] = msg->chn->buf->data;
11888 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011889 }
11890 }
11891 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11892}
11893
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011894/* Return the signed integer value for the specified url parameter (see url_param
11895 * above).
11896 */
11897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011898smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011899{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011900 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011901
11902 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011903 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011904 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011905 }
11906
11907 return ret;
11908}
11909
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011910/* This produces a 32-bit hash of the concatenation of the first occurrence of
11911 * the Host header followed by the path component if it begins with a slash ('/').
11912 * This means that '*' will not be added, resulting in exactly the first Host
11913 * entry. If no Host header is found, then the path is used. The resulting value
11914 * is hashed using the url hash followed by a full avalanche hash and provides a
11915 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11916 * high-traffic sites without having to store whole paths.
11917 * this differs from the base32 functions in that it includes the url parameters
11918 * as well as the path
11919 */
11920static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011921smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011922{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011923 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011924 struct hdr_ctx ctx;
11925 unsigned int hash = 0;
11926 char *ptr, *beg, *end;
11927 int len;
11928
11929 CHECK_HTTP_MESSAGE_FIRST();
11930
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011931 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011932 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011933 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011934 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11935 ptr = ctx.line + ctx.val;
11936 len = ctx.vlen;
11937 while (len--)
11938 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11939 }
11940
11941 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011942 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011943 beg = http_get_path(txn);
11944 if (!beg)
11945 beg = end;
11946
11947 for (ptr = beg; ptr < end ; ptr++);
11948
11949 if (beg < ptr && *beg == '/') {
11950 while (beg < ptr)
11951 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11952 }
11953 hash = full_hash(hash);
11954
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011955 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011956 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011957 smp->flags = SMP_F_VOL_1ST;
11958 return 1;
11959}
11960
11961/* This concatenates the source address with the 32-bit hash of the Host and
11962 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11963 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11964 * on the source address length. The URL hash is stored before the address so
11965 * that in environments where IPv6 is insignificant, truncating the output to
11966 * 8 bytes would still work.
11967 */
11968static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011969smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011970{
11971 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011972 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011973
Dragan Dosendb5af612016-06-16 11:23:01 +020011974 if (!cli_conn)
11975 return 0;
11976
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011977 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011978 return 0;
11979
11980 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011981 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11982 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011983
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011984 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011985 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011986 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011987 temp->len += 4;
11988 break;
11989 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011990 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011991 temp->len += 16;
11992 break;
11993 default:
11994 return 0;
11995 }
11996
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011997 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011998 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011999 return 1;
12000}
12001
Willy Tarreau185b5c42012-04-26 15:11:51 +020012002/* This function is used to validate the arguments passed to any "hdr" fetch
12003 * keyword. These keywords support an optional positive or negative occurrence
12004 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12005 * is assumed that the types are already the correct ones. Returns 0 on error,
12006 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12007 * error message in case of error, that the caller is responsible for freeing.
12008 * The initial location must either be freeable or NULL.
12009 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012010int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012011{
12012 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012013 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012014 return 0;
12015 }
12016 return 1;
12017}
12018
Willy Tarreau276fae92013-07-25 14:36:01 +020012019/* takes an UINT value on input supposed to represent the time since EPOCH,
12020 * adds an optional offset found in args[0] and emits a string representing
12021 * the date in RFC-1123/5322 format.
12022 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012023static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012024{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012025 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012026 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12027 struct chunk *temp;
12028 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012029 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012030 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012031
12032 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012033 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012034 curr_date += args[0].data.sint;
12035
12036 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012037 if (!tm)
12038 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012039
12040 temp = get_trash_chunk();
12041 temp->len = snprintf(temp->str, temp->size - temp->len,
12042 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12043 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12044 tm->tm_hour, tm->tm_min, tm->tm_sec);
12045
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012046 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012047 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012048 return 1;
12049}
12050
Thierry FOURNIERad903512014-04-11 17:51:01 +020012051/* Match language range with language tag. RFC2616 14.4:
12052 *
12053 * A language-range matches a language-tag if it exactly equals
12054 * the tag, or if it exactly equals a prefix of the tag such
12055 * that the first tag character following the prefix is "-".
12056 *
12057 * Return 1 if the strings match, else return 0.
12058 */
12059static inline int language_range_match(const char *range, int range_len,
12060 const char *tag, int tag_len)
12061{
12062 const char *end = range + range_len;
12063 const char *tend = tag + tag_len;
12064 while (range < end) {
12065 if (*range == '-' && tag == tend)
12066 return 1;
12067 if (*range != *tag || tag == tend)
12068 return 0;
12069 range++;
12070 tag++;
12071 }
12072 /* Return true only if the last char of the tag is matched. */
12073 return tag == tend;
12074}
12075
12076/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012077static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012078{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012079 const char *al = smp->data.u.str.str;
12080 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012081 const char *token;
12082 int toklen;
12083 int qvalue;
12084 const char *str;
12085 const char *w;
12086 int best_q = 0;
12087
12088 /* Set the constant to the sample, because the output of the
12089 * function will be peek in the constant configuration string.
12090 */
12091 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012092 smp->data.u.str.size = 0;
12093 smp->data.u.str.str = "";
12094 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012095
12096 /* Parse the accept language */
12097 while (1) {
12098
12099 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012100 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012101 al++;
12102 if (al >= end)
12103 break;
12104
12105 /* Start of the fisrt word. */
12106 token = al;
12107
12108 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012109 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012110 al++;
12111 if (al == token)
12112 goto expect_comma;
12113
12114 /* Length of the token. */
12115 toklen = al - token;
12116 qvalue = 1000;
12117
12118 /* Check if the token exists in the list. If the token not exists,
12119 * jump to the next token.
12120 */
12121 str = args[0].data.str.str;
12122 w = str;
12123 while (1) {
12124 if (*str == ';' || *str == '\0') {
12125 if (language_range_match(token, toklen, w, str-w))
12126 goto look_for_q;
12127 if (*str == '\0')
12128 goto expect_comma;
12129 w = str + 1;
12130 }
12131 str++;
12132 }
12133 goto expect_comma;
12134
12135look_for_q:
12136
12137 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012138 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012139 al++;
12140 if (al >= end)
12141 goto process_value;
12142
12143 /* If ',' is found, process the result */
12144 if (*al == ',')
12145 goto process_value;
12146
12147 /* If the character is different from ';', look
12148 * for the end of the header part in best effort.
12149 */
12150 if (*al != ';')
12151 goto expect_comma;
12152
12153 /* Assumes that the char is ';', now expect "q=". */
12154 al++;
12155
12156 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012157 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012158 al++;
12159 if (al >= end)
12160 goto process_value;
12161
12162 /* Expect 'q'. If no 'q', continue in best effort */
12163 if (*al != 'q')
12164 goto process_value;
12165 al++;
12166
12167 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012168 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012169 al++;
12170 if (al >= end)
12171 goto process_value;
12172
12173 /* Expect '='. If no '=', continue in best effort */
12174 if (*al != '=')
12175 goto process_value;
12176 al++;
12177
12178 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012179 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012180 al++;
12181 if (al >= end)
12182 goto process_value;
12183
12184 /* Parse the q value. */
12185 qvalue = parse_qvalue(al, &al);
12186
12187process_value:
12188
12189 /* If the new q value is the best q value, then store the associated
12190 * language in the response. If qvalue is the biggest value (1000),
12191 * break the process.
12192 */
12193 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012194 smp->data.u.str.str = (char *)w;
12195 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012196 if (qvalue >= 1000)
12197 break;
12198 best_q = qvalue;
12199 }
12200
12201expect_comma:
12202
12203 /* Expect comma or end. If the end is detected, quit the loop. */
12204 while (al < end && *al != ',')
12205 al++;
12206 if (al >= end)
12207 break;
12208
12209 /* Comma is found, jump it and restart the analyzer. */
12210 al++;
12211 }
12212
12213 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012214 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12215 smp->data.u.str.str = args[1].data.str.str;
12216 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012217 }
12218
12219 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012220 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012221}
12222
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012223/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012224static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012225{
12226 /* If the constant flag is set or if not size is avalaible at
12227 * the end of the buffer, copy the string in other buffer
12228 * before decoding.
12229 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012230 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012231 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012232 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12233 smp->data.u.str.str = str->str;
12234 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012235 smp->flags &= ~SMP_F_CONST;
12236 }
12237
12238 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012239 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12240 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012241 return 1;
12242}
12243
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012244static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12245{
12246 struct proxy *fe = strm_fe(smp->strm);
12247 int idx, i;
12248 struct cap_hdr *hdr;
12249 int len;
12250
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012251 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012252 return 0;
12253
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012254 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012255
12256 /* Check the availibity of the capture id. */
12257 if (idx > fe->nb_req_cap - 1)
12258 return 0;
12259
12260 /* Look for the original configuration. */
12261 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12262 hdr != NULL && i != idx ;
12263 i--, hdr = hdr->next);
12264 if (!hdr)
12265 return 0;
12266
12267 /* check for the memory allocation */
12268 if (smp->strm->req_cap[hdr->index] == NULL)
12269 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12270 if (smp->strm->req_cap[hdr->index] == NULL)
12271 return 0;
12272
12273 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012274 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012275 if (len > hdr->len)
12276 len = hdr->len;
12277
12278 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012279 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012280 smp->strm->req_cap[idx][len] = '\0';
12281
12282 return 1;
12283}
12284
12285static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12286{
12287 struct proxy *fe = strm_fe(smp->strm);
12288 int idx, i;
12289 struct cap_hdr *hdr;
12290 int len;
12291
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012292 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012293 return 0;
12294
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012295 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012296
12297 /* Check the availibity of the capture id. */
12298 if (idx > fe->nb_rsp_cap - 1)
12299 return 0;
12300
12301 /* Look for the original configuration. */
12302 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12303 hdr != NULL && i != idx ;
12304 i--, hdr = hdr->next);
12305 if (!hdr)
12306 return 0;
12307
12308 /* check for the memory allocation */
12309 if (smp->strm->res_cap[hdr->index] == NULL)
12310 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12311 if (smp->strm->res_cap[hdr->index] == NULL)
12312 return 0;
12313
12314 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012315 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012316 if (len > hdr->len)
12317 len = hdr->len;
12318
12319 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012320 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012321 smp->strm->res_cap[idx][len] = '\0';
12322
12323 return 1;
12324}
12325
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012326/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012327 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012328 * the relevant part of the request line accordingly. Then it updates various
12329 * pointers to the next elements which were moved, and the total buffer length.
12330 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012331 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12332 * error, though this can be revisited when this code is finally exploited.
12333 *
12334 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12335 * query string and 3 to replace uri.
12336 *
12337 * In query string case, the mark question '?' must be set at the start of the
12338 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012339 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012340int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012341 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012342{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012343 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012345 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012346 int delta;
12347
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012348 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012349 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012350 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012351 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12352
12353 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012354 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012355 txn->req.sl.rq.m_l += delta;
12356 txn->req.sl.rq.u += delta;
12357 txn->req.sl.rq.v += delta;
12358 break;
12359
12360 case 1: // path
12361 cur_ptr = http_get_path(txn);
12362 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012363 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012364
12365 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012366 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012367 cur_end++;
12368
12369 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012370 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012371 txn->req.sl.rq.u_l += delta;
12372 txn->req.sl.rq.v += delta;
12373 break;
12374
12375 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012376 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012377 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012378 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12379 while (cur_ptr < cur_end && *cur_ptr != '?')
12380 cur_ptr++;
12381
12382 /* skip the question mark or indicate that we must insert it
12383 * (but only if the format string is not empty then).
12384 */
12385 if (cur_ptr < cur_end)
12386 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012387 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012388 offset = 0;
12389
12390 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012391 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012392 txn->req.sl.rq.u_l += delta;
12393 txn->req.sl.rq.v += delta;
12394 break;
12395
12396 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012397 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012398 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12399
12400 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012402 txn->req.sl.rq.u_l += delta;
12403 txn->req.sl.rq.v += delta;
12404 break;
12405
12406 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012407 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012408 }
12409
12410 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012411 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012412 txn->req.sl.rq.l += delta;
12413 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012414 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012415 return 0;
12416}
12417
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012418/* This function replace the HTTP status code and the associated message. The
12419 * variable <status> contains the new status code. This function never fails.
12420 */
12421void http_set_status(unsigned int status, struct stream *s)
12422{
12423 struct http_txn *txn = s->txn;
12424 char *cur_ptr, *cur_end;
12425 int delta;
12426 char *res;
12427 int c_l;
12428 const char *msg;
12429 int msg_len;
12430
12431 chunk_reset(&trash);
12432
12433 res = ultoa_o(status, trash.str, trash.size);
12434 c_l = res - trash.str;
12435
12436 trash.str[c_l] = ' ';
12437 trash.len = c_l + 1;
12438
12439 msg = get_reason(status);
12440 msg_len = strlen(msg);
12441
12442 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12443 trash.len += msg_len;
12444
12445 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12446 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12447
12448 /* commit changes and adjust message */
12449 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12450
12451 /* adjust res line offsets and lengths */
12452 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12453 txn->rsp.sl.st.c_l = c_l;
12454 txn->rsp.sl.st.r_l = msg_len;
12455
12456 delta = trash.len - (cur_end - cur_ptr);
12457 txn->rsp.sl.st.l += delta;
12458 txn->hdr_idx.v[0].len += delta;
12459 http_msg_move_end(&txn->rsp, delta);
12460}
12461
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012462/* This function executes one of the set-{method,path,query,uri} actions. It
12463 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012464 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012465 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012466 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12467 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012468 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012469enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012470 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012471{
12472 chunk_reset(&trash);
12473
12474 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012475 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012476 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012477 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012478
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012479 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012480 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012481}
12482
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012483/* This function is just a compliant action wrapper for "set-status". */
12484enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012485 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012486{
12487 http_set_status(rule->arg.status.code, s);
12488 return ACT_RET_CONT;
12489}
12490
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012491/* parse an http-request action among :
12492 * set-method
12493 * set-path
12494 * set-query
12495 * set-uri
12496 *
12497 * All of them accept a single argument of type string representing a log-format.
12498 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12499 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012500 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012501 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012502enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12503 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012504{
12505 int cur_arg = *orig_arg;
12506
Thierry FOURNIER42148732015-09-02 17:17:33 +020012507 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012508
12509 switch (args[0][4]) {
12510 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012511 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012512 rule->action_ptr = http_action_set_req_line;
12513 break;
12514 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012515 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012516 rule->action_ptr = http_action_set_req_line;
12517 break;
12518 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012519 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012520 rule->action_ptr = http_action_set_req_line;
12521 break;
12522 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012523 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012524 rule->action_ptr = http_action_set_req_line;
12525 break;
12526 default:
12527 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012528 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012529 }
12530
12531 if (!*args[cur_arg] ||
12532 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12533 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012534 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012535 }
12536
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012537 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012538 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012539 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012540 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012541 return ACT_RET_PRS_ERR;
12542 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012543
12544 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012545 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012546}
12547
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012548/* parse set-status action:
12549 * This action accepts a single argument of type int representing
12550 * an http status code. It returns ACT_RET_PRS_OK on success,
12551 * ACT_RET_PRS_ERR on error.
12552 */
12553enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12554 struct act_rule *rule, char **err)
12555{
12556 char *error;
12557
Thierry FOURNIER42148732015-09-02 17:17:33 +020012558 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012559 rule->action_ptr = action_http_set_status;
12560
12561 /* Check if an argument is available */
12562 if (!*args[*orig_arg]) {
12563 memprintf(err, "expects exactly 1 argument <status>");
12564 return ACT_RET_PRS_ERR;
12565 }
12566
12567 /* convert status code as integer */
12568 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12569 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12570 memprintf(err, "expects an integer status code between 100 and 999");
12571 return ACT_RET_PRS_ERR;
12572 }
12573
12574 (*orig_arg)++;
12575 return ACT_RET_PRS_OK;
12576}
12577
Willy Tarreaua9083d02015-05-08 15:27:59 +020012578/* This function executes the "capture" action. It executes a fetch expression,
12579 * turns the result into a string and puts it in a capture slot. It always
12580 * returns 1. If an error occurs the action is cancelled, but the rule
12581 * processing continues.
12582 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012583enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012584 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012585{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012586 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012587 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012588 char **cap = s->req_cap;
12589 int len;
12590
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012591 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012593 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012594
12595 if (cap[h->index] == NULL)
12596 cap[h->index] = pool_alloc2(h->pool);
12597
12598 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012599 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012600
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012601 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012602 if (len > h->len)
12603 len = h->len;
12604
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012605 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012606 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012607 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012608}
12609
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012610/* This function executes the "capture" action and store the result in a
12611 * capture slot if exists. It executes a fetch expression, turns the result
12612 * into a string and puts it in a capture slot. It always returns 1. If an
12613 * error occurs the action is cancelled, but the rule processing continues.
12614 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012615enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012616 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012617{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012618 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012619 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012620 char **cap = s->req_cap;
12621 struct proxy *fe = strm_fe(s);
12622 int len;
12623 int i;
12624
12625 /* Look for the original configuration. */
12626 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012627 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012628 i--, h = h->next);
12629 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012630 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012631
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012632 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012633 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012634 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012635
12636 if (cap[h->index] == NULL)
12637 cap[h->index] = pool_alloc2(h->pool);
12638
12639 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012640 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012641
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012642 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012643 if (len > h->len)
12644 len = h->len;
12645
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012646 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012647 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012648 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012649}
12650
Willy Tarreaua9083d02015-05-08 15:27:59 +020012651/* parse an "http-request capture" action. It takes a single argument which is
12652 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012653 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012654 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012655 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012656enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12657 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012658{
12659 struct sample_expr *expr;
12660 struct cap_hdr *hdr;
12661 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012662 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012663
12664 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12665 if (strcmp(args[cur_arg], "if") == 0 ||
12666 strcmp(args[cur_arg], "unless") == 0)
12667 break;
12668
12669 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012670 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012671 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012672 }
12673
Willy Tarreaua9083d02015-05-08 15:27:59 +020012674 cur_arg = *orig_arg;
12675 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12676 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012677 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012678
12679 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12680 memprintf(err,
12681 "fetch method '%s' extracts information from '%s', none of which is available here",
12682 args[cur_arg-1], sample_src_names(expr->fetch->use));
12683 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012684 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012685 }
12686
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687 if (!args[cur_arg] || !*args[cur_arg]) {
12688 memprintf(err, "expects 'len or 'id'");
12689 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012690 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012691 }
12692
Willy Tarreaua9083d02015-05-08 15:27:59 +020012693 if (strcmp(args[cur_arg], "len") == 0) {
12694 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012695
12696 if (!(px->cap & PR_CAP_FE)) {
12697 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012698 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012699 }
12700
12701 proxy->conf.args.ctx = ARGC_CAP;
12702
Willy Tarreaua9083d02015-05-08 15:27:59 +020012703 if (!args[cur_arg]) {
12704 memprintf(err, "missing length value");
12705 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012706 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012707 }
12708 /* we copy the table name for now, it will be resolved later */
12709 len = atoi(args[cur_arg]);
12710 if (len <= 0) {
12711 memprintf(err, "length must be > 0");
12712 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012713 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714 }
12715 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012716
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012717 if (!len) {
12718 memprintf(err, "a positive 'len' argument is mandatory");
12719 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012720 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012721 }
12722
Vincent Bernat02779b62016-04-03 13:48:43 +020012723 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012724 hdr->next = px->req_cap;
12725 hdr->name = NULL; /* not a header capture */
12726 hdr->namelen = 0;
12727 hdr->len = len;
12728 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12729 hdr->index = px->nb_req_cap++;
12730
12731 px->req_cap = hdr;
12732 px->to_log |= LW_REQHDR;
12733
Thierry FOURNIER42148732015-09-02 17:17:33 +020012734 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012735 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012736 rule->arg.cap.expr = expr;
12737 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012738 }
12739
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012740 else if (strcmp(args[cur_arg], "id") == 0) {
12741 int id;
12742 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012743
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012744 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012745
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012746 if (!args[cur_arg]) {
12747 memprintf(err, "missing id value");
12748 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012749 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012750 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012751
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012752 id = strtol(args[cur_arg], &error, 10);
12753 if (*error != '\0') {
12754 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12755 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012756 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012757 }
12758 cur_arg++;
12759
12760 proxy->conf.args.ctx = ARGC_CAP;
12761
Thierry FOURNIER42148732015-09-02 17:17:33 +020012762 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012763 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012764 rule->arg.capid.expr = expr;
12765 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012766 }
12767
12768 else {
12769 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12770 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012771 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012772 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012773
12774 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012776}
12777
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012778/* This function executes the "capture" action and store the result in a
12779 * capture slot if exists. It executes a fetch expression, turns the result
12780 * into a string and puts it in a capture slot. It always returns 1. If an
12781 * error occurs the action is cancelled, but the rule processing continues.
12782 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012783enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012784 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012785{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012786 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012788 char **cap = s->res_cap;
12789 struct proxy *fe = strm_fe(s);
12790 int len;
12791 int i;
12792
12793 /* Look for the original configuration. */
12794 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012795 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012796 i--, h = h->next);
12797 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012798 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012799
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012800 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012801 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012802 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012803
12804 if (cap[h->index] == NULL)
12805 cap[h->index] = pool_alloc2(h->pool);
12806
12807 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012808 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012809
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012810 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012811 if (len > h->len)
12812 len = h->len;
12813
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012814 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012815 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012816 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012817}
12818
12819/* parse an "http-response capture" action. It takes a single argument which is
12820 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12821 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012822 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012823 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012824enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12825 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012826{
12827 struct sample_expr *expr;
12828 int cur_arg;
12829 int id;
12830 char *error;
12831
12832 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12833 if (strcmp(args[cur_arg], "if") == 0 ||
12834 strcmp(args[cur_arg], "unless") == 0)
12835 break;
12836
12837 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012838 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012839 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012840 }
12841
12842 cur_arg = *orig_arg;
12843 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12844 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012845 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012846
12847 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12848 memprintf(err,
12849 "fetch method '%s' extracts information from '%s', none of which is available here",
12850 args[cur_arg-1], sample_src_names(expr->fetch->use));
12851 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012852 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012853 }
12854
12855 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012856 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012857 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012858 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012859 }
12860
12861 if (strcmp(args[cur_arg], "id") != 0) {
12862 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12863 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012864 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012865 }
12866
12867 cur_arg++;
12868
12869 if (!args[cur_arg]) {
12870 memprintf(err, "missing id value");
12871 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012872 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012873 }
12874
12875 id = strtol(args[cur_arg], &error, 10);
12876 if (*error != '\0') {
12877 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12878 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012879 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012880 }
12881 cur_arg++;
12882
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012883 proxy->conf.args.ctx = ARGC_CAP;
12884
Thierry FOURNIER42148732015-09-02 17:17:33 +020012885 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012886 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012887 rule->arg.capid.expr = expr;
12888 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012889
12890 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012891 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012892}
12893
William Lallemand73025dd2014-04-24 14:38:37 +020012894/*
12895 * Return the struct http_req_action_kw associated to a keyword.
12896 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012897struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012898{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012899 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012900}
12901
12902/*
12903 * Return the struct http_res_action_kw associated to a keyword.
12904 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012905struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012906{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012907 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012908}
12909
Willy Tarreau12207b32016-11-22 19:48:51 +010012910
12911/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12912 * now.
12913 */
12914static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12915{
12916 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12917 return 1;
12918
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012919 if (*args[2]) {
12920 struct proxy *px;
12921
12922 px = proxy_find_by_name(args[2], 0, 0);
12923 if (px)
12924 appctx->ctx.errors.iid = px->uuid;
12925 else
12926 appctx->ctx.errors.iid = atoi(args[2]);
12927
12928 if (!appctx->ctx.errors.iid) {
12929 appctx->ctx.cli.msg = "No such proxy.\n";
12930 appctx->st0 = CLI_ST_PRINT;
12931 return 1;
12932 }
12933 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012934 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012935 appctx->ctx.errors.iid = -1; // dump all proxies
12936
Willy Tarreau35069f82016-11-25 09:16:37 +010012937 appctx->ctx.errors.flag = 0;
12938 if (strcmp(args[3], "request") == 0)
12939 appctx->ctx.errors.flag |= 4; // ignore response
12940 else if (strcmp(args[3], "response") == 0)
12941 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012942 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012943 return 0;
12944}
12945
12946/* This function dumps all captured errors onto the stream interface's
12947 * read buffer. It returns 0 if the output buffer is full and it needs
12948 * to be called again, otherwise non-zero.
12949 */
12950static int cli_io_handler_show_errors(struct appctx *appctx)
12951{
12952 struct stream_interface *si = appctx->owner;
12953 extern const char *monthname[12];
12954
12955 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12956 return 1;
12957
12958 chunk_reset(&trash);
12959
12960 if (!appctx->ctx.errors.px) {
12961 /* the function had not been called yet, let's prepare the
12962 * buffer for a response.
12963 */
12964 struct tm tm;
12965
12966 get_localtime(date.tv_sec, &tm);
12967 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12968 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12969 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12970 error_snapshot_id);
12971
12972 if (bi_putchk(si_ic(si), &trash) == -1) {
12973 /* Socket buffer full. Let's try again later from the same point */
12974 si_applet_cant_put(si);
12975 return 0;
12976 }
12977
12978 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012979 appctx->ctx.errors.bol = 0;
12980 appctx->ctx.errors.ptr = -1;
12981 }
12982
12983 /* we have two inner loops here, one for the proxy, the other one for
12984 * the buffer.
12985 */
12986 while (appctx->ctx.errors.px) {
12987 struct error_snapshot *es;
12988
Willy Tarreau35069f82016-11-25 09:16:37 +010012989 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012990 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012991 if (appctx->ctx.errors.flag & 2) // skip req
12992 goto next;
12993 }
12994 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012995 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012996 if (appctx->ctx.errors.flag & 4) // skip resp
12997 goto next;
12998 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012999
13000 if (!es->when.tv_sec)
13001 goto next;
13002
13003 if (appctx->ctx.errors.iid >= 0 &&
13004 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13005 es->oe->uuid != appctx->ctx.errors.iid)
13006 goto next;
13007
13008 if (appctx->ctx.errors.ptr < 0) {
13009 /* just print headers now */
13010
13011 char pn[INET6_ADDRSTRLEN];
13012 struct tm tm;
13013 int port;
13014
13015 get_localtime(es->when.tv_sec, &tm);
13016 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13017 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13018 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13019
13020 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13021 case AF_INET:
13022 case AF_INET6:
13023 port = get_host_port(&es->src);
13024 break;
13025 default:
13026 port = 0;
13027 }
13028
Willy Tarreau35069f82016-11-25 09:16:37 +010013029 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013030 case 0:
13031 chunk_appendf(&trash,
13032 " frontend %s (#%d): invalid request\n"
13033 " backend %s (#%d)",
13034 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13035 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13036 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13037 break;
13038 case 1:
13039 chunk_appendf(&trash,
13040 " backend %s (#%d): invalid response\n"
13041 " frontend %s (#%d)",
13042 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13043 es->oe->id, es->oe->uuid);
13044 break;
13045 }
13046
13047 chunk_appendf(&trash,
13048 ", server %s (#%d), event #%u\n"
13049 " src %s:%d, session #%d, session flags 0x%08x\n"
13050 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
13051 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13052 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13053 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13054 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13055 es->ev_id,
13056 pn, port, es->sid, es->s_flags,
13057 es->state, es->m_flags, es->t_flags,
13058 es->m_clen, es->m_blen,
13059 es->b_flags, es->b_out, es->b_tot,
13060 es->len, es->b_wrap, es->pos);
13061
13062 if (bi_putchk(si_ic(si), &trash) == -1) {
13063 /* Socket buffer full. Let's try again later from the same point */
13064 si_applet_cant_put(si);
13065 return 0;
13066 }
13067 appctx->ctx.errors.ptr = 0;
13068 appctx->ctx.errors.sid = es->sid;
13069 }
13070
13071 if (appctx->ctx.errors.sid != es->sid) {
13072 /* the snapshot changed while we were dumping it */
13073 chunk_appendf(&trash,
13074 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13075 if (bi_putchk(si_ic(si), &trash) == -1) {
13076 si_applet_cant_put(si);
13077 return 0;
13078 }
13079 goto next;
13080 }
13081
13082 /* OK, ptr >= 0, so we have to dump the current line */
13083 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13084 int newptr;
13085 int newline;
13086
13087 newline = appctx->ctx.errors.bol;
13088 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13089 if (newptr == appctx->ctx.errors.ptr)
13090 return 0;
13091
13092 if (bi_putchk(si_ic(si), &trash) == -1) {
13093 /* Socket buffer full. Let's try again later from the same point */
13094 si_applet_cant_put(si);
13095 return 0;
13096 }
13097 appctx->ctx.errors.ptr = newptr;
13098 appctx->ctx.errors.bol = newline;
13099 };
13100 next:
13101 appctx->ctx.errors.bol = 0;
13102 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013103 appctx->ctx.errors.flag ^= 1;
13104 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013105 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013106 }
13107
13108 /* dump complete */
13109 return 1;
13110}
13111
13112/* register cli keywords */
13113static struct cli_kw_list cli_kws = {{ },{
13114 { { "show", "errors", NULL },
13115 "show errors : report last request and response errors for each proxy",
13116 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13117 },
13118 {{},}
13119}};
13120
Willy Tarreau4a568972010-05-12 08:08:50 +020013121/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013122/* All supported ACL keywords must be declared here. */
13123/************************************************************************/
13124
13125/* Note: must not be declared <const> as its list will be overwritten.
13126 * Please take care of keeping this list alphabetically sorted.
13127 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013128static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013129 { "base", "base", PAT_MATCH_STR },
13130 { "base_beg", "base", PAT_MATCH_BEG },
13131 { "base_dir", "base", PAT_MATCH_DIR },
13132 { "base_dom", "base", PAT_MATCH_DOM },
13133 { "base_end", "base", PAT_MATCH_END },
13134 { "base_len", "base", PAT_MATCH_LEN },
13135 { "base_reg", "base", PAT_MATCH_REG },
13136 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013137
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013138 { "cook", "req.cook", PAT_MATCH_STR },
13139 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13140 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13141 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13142 { "cook_end", "req.cook", PAT_MATCH_END },
13143 { "cook_len", "req.cook", PAT_MATCH_LEN },
13144 { "cook_reg", "req.cook", PAT_MATCH_REG },
13145 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013146
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013147 { "hdr", "req.hdr", PAT_MATCH_STR },
13148 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13149 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13150 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13151 { "hdr_end", "req.hdr", PAT_MATCH_END },
13152 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13153 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13154 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013155
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013156 /* these two declarations uses strings with list storage (in place
13157 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13158 * and delete functions are relative to the list management. The parse
13159 * and match method are related to the corresponding fetch methods. This
13160 * is very particular ACL declaration mode.
13161 */
13162 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13163 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013164
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013165 { "path", "path", PAT_MATCH_STR },
13166 { "path_beg", "path", PAT_MATCH_BEG },
13167 { "path_dir", "path", PAT_MATCH_DIR },
13168 { "path_dom", "path", PAT_MATCH_DOM },
13169 { "path_end", "path", PAT_MATCH_END },
13170 { "path_len", "path", PAT_MATCH_LEN },
13171 { "path_reg", "path", PAT_MATCH_REG },
13172 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013173
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013174 { "req_ver", "req.ver", PAT_MATCH_STR },
13175 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013176
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013177 { "scook", "res.cook", PAT_MATCH_STR },
13178 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13179 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13180 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13181 { "scook_end", "res.cook", PAT_MATCH_END },
13182 { "scook_len", "res.cook", PAT_MATCH_LEN },
13183 { "scook_reg", "res.cook", PAT_MATCH_REG },
13184 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013185
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013186 { "shdr", "res.hdr", PAT_MATCH_STR },
13187 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13188 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13189 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13190 { "shdr_end", "res.hdr", PAT_MATCH_END },
13191 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13192 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13193 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013194
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013195 { "url", "url", PAT_MATCH_STR },
13196 { "url_beg", "url", PAT_MATCH_BEG },
13197 { "url_dir", "url", PAT_MATCH_DIR },
13198 { "url_dom", "url", PAT_MATCH_DOM },
13199 { "url_end", "url", PAT_MATCH_END },
13200 { "url_len", "url", PAT_MATCH_LEN },
13201 { "url_reg", "url", PAT_MATCH_REG },
13202 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013203
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013204 { "urlp", "urlp", PAT_MATCH_STR },
13205 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13206 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13207 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13208 { "urlp_end", "urlp", PAT_MATCH_END },
13209 { "urlp_len", "urlp", PAT_MATCH_LEN },
13210 { "urlp_reg", "urlp", PAT_MATCH_REG },
13211 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013212
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013213 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013214}};
13215
13216/************************************************************************/
13217/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013218/************************************************************************/
13219/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013220static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013221 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013222 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013223 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13224
Willy Tarreau87b09662015-04-03 00:22:06 +020013225 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013226 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013227
13228 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013229 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13230 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13231 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013232
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013233 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13234 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013235
Willy Tarreau409bcde2013-01-08 00:31:00 +010013236 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13237 * are only here to match the ACL's name, are request-only and are used
13238 * for ACL compatibility only.
13239 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013240 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13241 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013242 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13243 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013244
13245 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13246 * only here to match the ACL's name, are request-only and are used for
13247 * ACL compatibility only.
13248 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013249 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013250 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013251 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013252 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013253
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013254 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013255 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013256 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013257 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013258 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013259 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013260
13261 /* HTTP protocol on the request path */
13262 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013263 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013264
13265 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013266 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13267 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013268
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013269 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013270 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13271 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013272 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013273
Willy Tarreau18ed2562013-01-14 15:56:36 +010013274 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013275 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13276 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013277
Willy Tarreau18ed2562013-01-14 15:56:36 +010013278 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013279 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013280 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13281 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013282
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013283 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013284 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013285 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013286 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013287 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010013288 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013289 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013290
13291 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013292 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013293 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13294 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013295
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013296 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013297 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013298 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013299 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013300 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010013301 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013302 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013303
Willy Tarreau409bcde2013-01-08 00:31:00 +010013304 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013305 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013306 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13307 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013308 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013309
13310 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013311 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013312 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013313 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013314 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013315
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013316 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013317 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013318 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013319 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013320 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013321 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013322 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013323 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13324 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013325 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013326 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013327}};
13328
Willy Tarreau8797c062007-05-07 00:55:35 +020013329
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013330/************************************************************************/
13331/* All supported converter keywords must be declared here. */
13332/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013333/* Note: must not be declared <const> as its list will be overwritten */
13334static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013335 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013336 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013337 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13338 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013339 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013340 { NULL, NULL, 0, 0, 0 },
13341}};
13342
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013343
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013344/************************************************************************/
13345/* All supported http-request action keywords must be declared here. */
13346/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013347struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013348 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013349 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013350 { "set-method", parse_set_req_line },
13351 { "set-path", parse_set_req_line },
13352 { "set-query", parse_set_req_line },
13353 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013354 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013355 }
13356};
13357
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013358struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013359 .kw = {
13360 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013361 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013362 { NULL, NULL }
13363 }
13364};
13365
Willy Tarreau8797c062007-05-07 00:55:35 +020013366__attribute__((constructor))
13367static void __http_protocol_init(void)
13368{
13369 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013370 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013371 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013372 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013373 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013374 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013375}
13376
13377
Willy Tarreau58f10d72006-12-04 02:26:12 +010013378/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013379 * Local variables:
13380 * c-indent-level: 8
13381 * c-basic-offset: 8
13382 * End:
13383 */