blob: 7fed50e6515c8fb0abcb9108d77a5ec0ee5e0d98 [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] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200199 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200204 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100205
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";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200346 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 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
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200369/* This function returns HTTP_ERR_<num> (enum) matching http status code.
370 * Returned value should match codes from http_err_codes.
371 */
372static const int http_get_status_idx(unsigned int status)
373{
374 switch (status) {
375 case 200: return HTTP_ERR_200;
376 case 400: return HTTP_ERR_400;
377 case 403: return HTTP_ERR_403;
378 case 405: return HTTP_ERR_405;
379 case 408: return HTTP_ERR_408;
380 case 429: return HTTP_ERR_429;
381 case 500: return HTTP_ERR_500;
382 case 502: return HTTP_ERR_502;
383 case 503: return HTTP_ERR_503;
384 case 504: return HTTP_ERR_504;
385 default: return HTTP_ERR_500;
386 }
387}
388
Willy Tarreau80587432006-12-24 17:47:20 +0100389void init_proto_http()
390{
Willy Tarreau42250582007-04-01 01:30:43 +0200391 int i;
392 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100393 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200394
Willy Tarreau80587432006-12-24 17:47:20 +0100395 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
396 if (!http_err_msgs[msg]) {
397 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
398 abort();
399 }
400
401 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
402 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
403 }
Willy Tarreau42250582007-04-01 01:30:43 +0200404
405 /* initialize the log header encoding map : '{|}"#' should be encoded with
406 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
407 * URL encoding only requires '"', '#' to be encoded as well as non-
408 * printable characters above.
409 */
410 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
411 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100412 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200413 for (i = 0; i < 32; i++) {
414 FD_SET(i, hdr_encode_map);
415 FD_SET(i, url_encode_map);
416 }
417 for (i = 127; i < 256; i++) {
418 FD_SET(i, hdr_encode_map);
419 FD_SET(i, url_encode_map);
420 }
421
422 tmp = "\"#{|}";
423 while (*tmp) {
424 FD_SET(*tmp, hdr_encode_map);
425 tmp++;
426 }
427
428 tmp = "\"#";
429 while (*tmp) {
430 FD_SET(*tmp, url_encode_map);
431 tmp++;
432 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200433
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100434 /* initialize the http header encoding map. The draft httpbis define the
435 * header content as:
436 *
437 * HTTP-message = start-line
438 * *( header-field CRLF )
439 * CRLF
440 * [ message-body ]
441 * header-field = field-name ":" OWS field-value OWS
442 * field-value = *( field-content / obs-fold )
443 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
444 * obs-fold = CRLF 1*( SP / HTAB )
445 * field-vchar = VCHAR / obs-text
446 * VCHAR = %x21-7E
447 * obs-text = %x80-FF
448 *
449 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
450 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
451 * "obs-fold" is volontary forgotten because haproxy remove this.
452 */
453 memset(http_encode_map, 0, sizeof(http_encode_map));
454 for (i = 0x00; i <= 0x08; i++)
455 FD_SET(i, http_encode_map);
456 for (i = 0x0a; i <= 0x1f; i++)
457 FD_SET(i, http_encode_map);
458 FD_SET(0x7f, http_encode_map);
459
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200460 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200461 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200462 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100463 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
Willy Tarreau53b6c742006-12-17 13:37:46 +0100466/*
467 * We have 26 list of methods (1 per first letter), each of which can have
468 * up to 3 entries (2 valid, 1 null).
469 */
470struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100471 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 int len;
473 const char text[8];
474};
475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100476const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477 ['C' - 'A'] = {
478 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
479 },
480 ['D' - 'A'] = {
481 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
482 },
483 ['G' - 'A'] = {
484 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
485 },
486 ['H' - 'A'] = {
487 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
488 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200489 ['O' - 'A'] = {
490 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
491 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100492 ['P' - 'A'] = {
493 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
494 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
495 },
496 ['T' - 'A'] = {
497 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
498 },
499 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200500 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 */
502};
503
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100505 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
506 [HTTP_METH_GET] = { "GET", 3 },
507 [HTTP_METH_HEAD] = { "HEAD", 4 },
508 [HTTP_METH_POST] = { "POST", 4 },
509 [HTTP_METH_PUT] = { "PUT", 3 },
510 [HTTP_METH_DELETE] = { "DELETE", 6 },
511 [HTTP_METH_TRACE] = { "TRACE", 5 },
512 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
513};
514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100515/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200516 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100517 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
518 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
519 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
520 * digit. Note: please do not overwrite values in assignment since gcc-2.95
521 * will not handle them correctly. It's worth noting that chars 128..255 are
522 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100523 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100524const unsigned char http_char_classes[256] = {
525 [ 0] = HTTP_FLG_CTL,
526 [ 1] = HTTP_FLG_CTL,
527 [ 2] = HTTP_FLG_CTL,
528 [ 3] = HTTP_FLG_CTL,
529 [ 4] = HTTP_FLG_CTL,
530 [ 5] = HTTP_FLG_CTL,
531 [ 6] = HTTP_FLG_CTL,
532 [ 7] = HTTP_FLG_CTL,
533 [ 8] = HTTP_FLG_CTL,
534 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
535 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
536 [ 11] = HTTP_FLG_CTL,
537 [ 12] = HTTP_FLG_CTL,
538 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
539 [ 14] = HTTP_FLG_CTL,
540 [ 15] = HTTP_FLG_CTL,
541 [ 16] = HTTP_FLG_CTL,
542 [ 17] = HTTP_FLG_CTL,
543 [ 18] = HTTP_FLG_CTL,
544 [ 19] = HTTP_FLG_CTL,
545 [ 20] = HTTP_FLG_CTL,
546 [ 21] = HTTP_FLG_CTL,
547 [ 22] = HTTP_FLG_CTL,
548 [ 23] = HTTP_FLG_CTL,
549 [ 24] = HTTP_FLG_CTL,
550 [ 25] = HTTP_FLG_CTL,
551 [ 26] = HTTP_FLG_CTL,
552 [ 27] = HTTP_FLG_CTL,
553 [ 28] = HTTP_FLG_CTL,
554 [ 29] = HTTP_FLG_CTL,
555 [ 30] = HTTP_FLG_CTL,
556 [ 31] = HTTP_FLG_CTL,
557 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
558 ['!'] = HTTP_FLG_TOK,
559 ['"'] = HTTP_FLG_SEP,
560 ['#'] = HTTP_FLG_TOK,
561 ['$'] = HTTP_FLG_TOK,
562 ['%'] = HTTP_FLG_TOK,
563 ['&'] = HTTP_FLG_TOK,
564 [ 39] = HTTP_FLG_TOK,
565 ['('] = HTTP_FLG_SEP,
566 [')'] = HTTP_FLG_SEP,
567 ['*'] = HTTP_FLG_TOK,
568 ['+'] = HTTP_FLG_TOK,
569 [','] = HTTP_FLG_SEP,
570 ['-'] = HTTP_FLG_TOK,
571 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
572 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
573 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
583 [':'] = HTTP_FLG_SEP,
584 [';'] = HTTP_FLG_SEP,
585 ['<'] = HTTP_FLG_SEP,
586 ['='] = HTTP_FLG_SEP,
587 ['>'] = HTTP_FLG_SEP,
588 ['?'] = HTTP_FLG_SEP,
589 ['@'] = HTTP_FLG_SEP,
590 ['A'] = HTTP_FLG_TOK,
591 ['B'] = HTTP_FLG_TOK,
592 ['C'] = HTTP_FLG_TOK,
593 ['D'] = HTTP_FLG_TOK,
594 ['E'] = HTTP_FLG_TOK,
595 ['F'] = HTTP_FLG_TOK,
596 ['G'] = HTTP_FLG_TOK,
597 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
598 ['I'] = HTTP_FLG_TOK,
599 ['J'] = HTTP_FLG_TOK,
600 ['K'] = HTTP_FLG_TOK,
601 ['L'] = HTTP_FLG_TOK,
602 ['M'] = HTTP_FLG_TOK,
603 ['N'] = HTTP_FLG_TOK,
604 ['O'] = HTTP_FLG_TOK,
605 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
606 ['Q'] = HTTP_FLG_TOK,
607 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
610 ['U'] = HTTP_FLG_TOK,
611 ['V'] = HTTP_FLG_TOK,
612 ['W'] = HTTP_FLG_TOK,
613 ['X'] = HTTP_FLG_TOK,
614 ['Y'] = HTTP_FLG_TOK,
615 ['Z'] = HTTP_FLG_TOK,
616 ['['] = HTTP_FLG_SEP,
617 [ 92] = HTTP_FLG_SEP,
618 [']'] = HTTP_FLG_SEP,
619 ['^'] = HTTP_FLG_TOK,
620 ['_'] = HTTP_FLG_TOK,
621 ['`'] = HTTP_FLG_TOK,
622 ['a'] = HTTP_FLG_TOK,
623 ['b'] = HTTP_FLG_TOK,
624 ['c'] = HTTP_FLG_TOK,
625 ['d'] = HTTP_FLG_TOK,
626 ['e'] = HTTP_FLG_TOK,
627 ['f'] = HTTP_FLG_TOK,
628 ['g'] = HTTP_FLG_TOK,
629 ['h'] = HTTP_FLG_TOK,
630 ['i'] = HTTP_FLG_TOK,
631 ['j'] = HTTP_FLG_TOK,
632 ['k'] = HTTP_FLG_TOK,
633 ['l'] = HTTP_FLG_TOK,
634 ['m'] = HTTP_FLG_TOK,
635 ['n'] = HTTP_FLG_TOK,
636 ['o'] = HTTP_FLG_TOK,
637 ['p'] = HTTP_FLG_TOK,
638 ['q'] = HTTP_FLG_TOK,
639 ['r'] = HTTP_FLG_TOK,
640 ['s'] = HTTP_FLG_TOK,
641 ['t'] = HTTP_FLG_TOK,
642 ['u'] = HTTP_FLG_TOK,
643 ['v'] = HTTP_FLG_TOK,
644 ['w'] = HTTP_FLG_TOK,
645 ['x'] = HTTP_FLG_TOK,
646 ['y'] = HTTP_FLG_TOK,
647 ['z'] = HTTP_FLG_TOK,
648 ['{'] = HTTP_FLG_SEP,
649 ['|'] = HTTP_FLG_TOK,
650 ['}'] = HTTP_FLG_SEP,
651 ['~'] = HTTP_FLG_TOK,
652 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653};
654
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100655/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100656 * Adds a header and its CRLF at the tail of the message's buffer, just before
657 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100658 * 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_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100663{
664 int bytes, len;
665
666 len = strlen(text);
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}
673
674/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100675 * Adds a header and its CRLF at the tail of the message's buffer, just before
676 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100677 * the buffer is only opened and the space reserved, but nothing is copied.
678 * The header is also automatically added to the index <hdr_idx>, and the end
679 * of headers is automatically adjusted. The number of bytes added is returned
680 * on success, otherwise <0 is returned indicating an error.
681 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682int http_header_add_tail2(struct http_msg *msg,
683 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100684{
685 int bytes;
686
Willy Tarreau9b28e032012-10-12 23:49:43 +0200687 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100688 if (!bytes)
689 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100690 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100691 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
692}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693
694/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100695 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
696 * If so, returns the position of the first non-space character relative to
697 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
698 * to return a pointer to the place after the first space. Returns 0 if the
699 * header name does not match. Checks are case-insensitive.
700 */
701int http_header_match2(const char *hdr, const char *end,
702 const char *name, int len)
703{
704 const char *val;
705
706 if (hdr + len >= end)
707 return 0;
708 if (hdr[len] != ':')
709 return 0;
710 if (strncasecmp(hdr, name, len) != 0)
711 return 0;
712 val = hdr + len + 1;
713 while (val < end && HTTP_IS_SPHT(*val))
714 val++;
715 if ((val >= end) && (len + 2 <= end - hdr))
716 return len + 2; /* we may replace starting from second space */
717 return val - hdr;
718}
719
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200720/* Find the first or next occurrence of header <name> in message buffer <sol>
721 * using headers index <idx>, and return it in the <ctx> structure. This
722 * structure holds everything necessary to use the header and find next
723 * occurrence. If its <idx> member is 0, the header is searched from the
724 * beginning. Otherwise, the next occurrence is returned. The function returns
725 * 1 when it finds a value, and 0 when there is no more. It is very similar to
726 * http_find_header2() except that it is designed to work with full-line headers
727 * whose comma is not a delimiter but is part of the syntax. As a special case,
728 * if ctx->val is NULL when searching for a new values of a header, the current
729 * header is rescanned. This allows rescanning after a header deletion.
730 */
731int http_find_full_header2(const char *name, int len,
732 char *sol, struct hdr_idx *idx,
733 struct hdr_ctx *ctx)
734{
735 char *eol, *sov;
736 int cur_idx, old_idx;
737
738 cur_idx = ctx->idx;
739 if (cur_idx) {
740 /* We have previously returned a header, let's search another one */
741 sol = ctx->line;
742 eol = sol + idx->v[cur_idx].len;
743 goto next_hdr;
744 }
745
746 /* first request for this header */
747 sol += hdr_idx_first_pos(idx);
748 old_idx = 0;
749 cur_idx = hdr_idx_first_idx(idx);
750 while (cur_idx) {
751 eol = sol + idx->v[cur_idx].len;
752
753 if (len == 0) {
754 /* No argument was passed, we want any header.
755 * To achieve this, we simply build a fake request. */
756 while (sol + len < eol && sol[len] != ':')
757 len++;
758 name = sol;
759 }
760
761 if ((len < eol - sol) &&
762 (sol[len] == ':') &&
763 (strncasecmp(sol, name, len) == 0)) {
764 ctx->del = len;
765 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100766 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200767 sov++;
768
769 ctx->line = sol;
770 ctx->prev = old_idx;
771 ctx->idx = cur_idx;
772 ctx->val = sov - sol;
773 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100774 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200775 eol--;
776 ctx->tws++;
777 }
778 ctx->vlen = eol - sov;
779 return 1;
780 }
781 next_hdr:
782 sol = eol + idx->v[cur_idx].cr + 1;
783 old_idx = cur_idx;
784 cur_idx = idx->v[cur_idx].next;
785 }
786 return 0;
787}
788
Willy Tarreauc90dc232015-02-20 13:51:36 +0100789/* Find the first or next header field in message buffer <sol> using headers
790 * index <idx>, and return it in the <ctx> structure. This structure holds
791 * everything necessary to use the header and find next occurrence. If its
792 * <idx> member is 0, the first header is retrieved. Otherwise, the next
793 * occurrence is returned. The function returns 1 when it finds a value, and
794 * 0 when there is no more. It is equivalent to http_find_full_header2() with
795 * no header name.
796 */
797int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
798{
799 char *eol, *sov;
800 int cur_idx, old_idx;
801 int len;
802
803 cur_idx = ctx->idx;
804 if (cur_idx) {
805 /* We have previously returned a header, let's search another one */
806 sol = ctx->line;
807 eol = sol + idx->v[cur_idx].len;
808 goto next_hdr;
809 }
810
811 /* first request for this header */
812 sol += hdr_idx_first_pos(idx);
813 old_idx = 0;
814 cur_idx = hdr_idx_first_idx(idx);
815 while (cur_idx) {
816 eol = sol + idx->v[cur_idx].len;
817
818 len = 0;
819 while (1) {
820 if (len >= eol - sol)
821 goto next_hdr;
822 if (sol[len] == ':')
823 break;
824 len++;
825 }
826
827 ctx->del = len;
828 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100829 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100830 sov++;
831
832 ctx->line = sol;
833 ctx->prev = old_idx;
834 ctx->idx = cur_idx;
835 ctx->val = sov - sol;
836 ctx->tws = 0;
837
Willy Tarreau2235b262016-11-05 15:50:20 +0100838 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100839 eol--;
840 ctx->tws++;
841 }
842 ctx->vlen = eol - sov;
843 return 1;
844
845 next_hdr:
846 sol = eol + idx->v[cur_idx].cr + 1;
847 old_idx = cur_idx;
848 cur_idx = idx->v[cur_idx].next;
849 }
850 return 0;
851}
852
Willy Tarreau68085d82010-01-18 14:54:04 +0100853/* Find the end of the header value contained between <s> and <e>. See RFC2616,
854 * par 2.2 for more information. Note that it requires a valid header to return
855 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100857char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858{
859 int quoted, qdpair;
860
861 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100862
863#if defined(__x86_64__) || \
864 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
865 defined(__ARM_ARCH_7A__)
866 /* speedup: skip everything not a comma nor a double quote */
867 for (; s <= e - sizeof(int); s += sizeof(int)) {
868 unsigned int c = *(int *)s; // comma
869 unsigned int q = c; // quote
870
871 c ^= 0x2c2c2c2c; // contains one zero on a comma
872 q ^= 0x22222222; // contains one zero on a quote
873
874 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
875 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
876
877 if ((c | q) & 0x80808080)
878 break; // found a comma or a quote
879 }
880#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200881 for (; s < e; s++) {
882 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200883 else if (quoted) {
884 if (*s == '\\') qdpair = 1;
885 else if (*s == '"') quoted = 0;
886 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200887 else if (*s == '"') quoted = 1;
888 else if (*s == ',') return s;
889 }
890 return s;
891}
892
893/* Find the first or next occurrence of header <name> in message buffer <sol>
894 * using headers index <idx>, and return it in the <ctx> structure. This
895 * structure holds everything necessary to use the header and find next
896 * occurrence. If its <idx> member is 0, the header is searched from the
897 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 * 1 when it finds a value, and 0 when there is no more. It is designed to work
899 * with headers defined as comma-separated lists. As a special case, if ctx->val
900 * is NULL when searching for a new values of a header, the current header is
901 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200902 */
903int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200905 struct hdr_ctx *ctx)
906{
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 char *eol, *sov;
908 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 cur_idx = ctx->idx;
911 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200912 /* We have previously returned a value, let's search
913 * another one on the same line.
914 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200915 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 eol = sol + idx->v[cur_idx].len;
919
920 if (sov >= eol)
921 /* no more values in this header */
922 goto next_hdr;
923
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 /* values remaining for this header, skip the comma but save it
925 * for later use (eg: for header deletion).
926 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200927 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100928 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 sov++;
930
931 goto return_hdr;
932 }
933
934 /* first request for this header */
935 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100936 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200938 while (cur_idx) {
939 eol = sol + idx->v[cur_idx].len;
940
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200941 if (len == 0) {
942 /* No argument was passed, we want any header.
943 * To achieve this, we simply build a fake request. */
944 while (sol + len < eol && sol[len] != ':')
945 len++;
946 name = sol;
947 }
948
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 if ((len < eol - sol) &&
950 (sol[len] == ':') &&
951 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100952 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200953 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100954 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100956
Willy Tarreau33a7e692007-06-10 19:45:56 +0200957 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100958 ctx->prev = old_idx;
959 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200960 ctx->idx = cur_idx;
961 ctx->val = sov - sol;
962
963 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200964 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100965 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200966 eol--;
967 ctx->tws++;
968 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200969 ctx->vlen = eol - sov;
970 return 1;
971 }
972 next_hdr:
973 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100974 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200975 cur_idx = idx->v[cur_idx].next;
976 }
977 return 0;
978}
979
980int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100981 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200982 struct hdr_ctx *ctx)
983{
984 return http_find_header2(name, strlen(name), sol, idx, ctx);
985}
986
Willy Tarreau68085d82010-01-18 14:54:04 +0100987/* Remove one value of a header. This only works on a <ctx> returned by one of
988 * the http_find_header functions. The value is removed, as well as surrounding
989 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100990 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100991 * message <msg>. The new index is returned. If it is zero, it means there is
992 * no more header, so any processing may stop. The ctx is always left in a form
993 * that can be handled by http_find_header2() to find next occurrence.
994 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100995int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100996{
997 int cur_idx = ctx->idx;
998 char *sol = ctx->line;
999 struct hdr_idx_elem *hdr;
1000 int delta, skip_comma;
1001
1002 if (!cur_idx)
1003 return 0;
1004
1005 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001006 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001007 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001009 http_msg_move_end(msg, delta);
1010 idx->used--;
1011 hdr->len = 0; /* unused entry */
1012 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001013 if (idx->tail == ctx->idx)
1014 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001016 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001017 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001018 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001019 return ctx->idx;
1020 }
1021
1022 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001023 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1024 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001025 */
1026
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001027 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001028 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001029 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001030 NULL, 0);
1031 hdr->len += delta;
1032 http_msg_move_end(msg, delta);
1033 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001034 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001035 return ctx->idx;
1036}
1037
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001038/* This function handles a server error at the stream interface level. The
1039 * stream interface is assumed to be already in a closed state. An optional
1040 * message is copied into the input buffer, and an HTTP status code stored.
1041 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001042 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001044static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001045 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046{
Christopher Faulet3e344292015-11-24 16:24:13 +01001047 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001048 channel_auto_read(si_oc(si));
1049 channel_abort(si_oc(si));
1050 channel_auto_close(si_oc(si));
1051 channel_erase(si_oc(si));
1052 channel_auto_close(si_ic(si));
1053 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001054 if (status > 0)
Willy Tarreaueee5b512015-04-03 23:46:31 +02001055 s->txn->status = status;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001056 if (msg)
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001057 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001059 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001060 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001061 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062}
1063
Willy Tarreau87b09662015-04-03 00:22:06 +02001064/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001065 * and message.
1066 */
1067
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001068struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001069{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001070 const int msgnum = http_get_status_idx(s->txn->status);
1071
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001072 if (s->be->errmsg[msgnum].str)
1073 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001074 else if (strm_fe(s)->errmsg[msgnum].str)
1075 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001076 else
1077 return &http_err_chunks[msgnum];
1078}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001080void
1081http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1082{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001083 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001084 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001085 stream_int_retnclose(&s->si[0], msg);
1086}
1087
Willy Tarreau53b6c742006-12-17 13:37:46 +01001088/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001089 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1090 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001091 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001092enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001093{
1094 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001096
1097 m = ((unsigned)*str - 'A');
1098
1099 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 for (h = http_methods[m]; h->len > 0; h++) {
1101 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001104 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001105 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001106 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001107 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001108}
1109
Willy Tarreau21d2af32008-02-14 20:25:24 +01001110/* Parse the URI from the given transaction (which is assumed to be in request
1111 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1112 * It is returned otherwise.
1113 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001114char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001115{
1116 char *ptr, *end;
1117
Willy Tarreau9b28e032012-10-12 23:49:43 +02001118 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001119 end = ptr + txn->req.sl.rq.u_l;
1120
1121 if (ptr >= end)
1122 return NULL;
1123
1124 /* RFC2616, par. 5.1.2 :
1125 * Request-URI = "*" | absuri | abspath | authority
1126 */
1127
1128 if (*ptr == '*')
1129 return NULL;
1130
1131 if (isalpha((unsigned char)*ptr)) {
1132 /* this is a scheme as described by RFC3986, par. 3.1 */
1133 ptr++;
1134 while (ptr < end &&
1135 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1136 ptr++;
1137 /* skip '://' */
1138 if (ptr == end || *ptr++ != ':')
1139 return NULL;
1140 if (ptr == end || *ptr++ != '/')
1141 return NULL;
1142 if (ptr == end || *ptr++ != '/')
1143 return NULL;
1144 }
1145 /* skip [user[:passwd]@]host[:[port]] */
1146
1147 while (ptr < end && *ptr != '/')
1148 ptr++;
1149
1150 if (ptr == end)
1151 return NULL;
1152
1153 /* OK, we got the '/' ! */
1154 return ptr;
1155}
1156
William Lallemand65ad6e12014-01-31 15:08:02 +01001157/* Parse the URI from the given string and look for the "/" beginning the PATH.
1158 * If not found, return NULL. It is returned otherwise.
1159 */
1160static char *
1161http_get_path_from_string(char *str)
1162{
1163 char *ptr = str;
1164
1165 /* RFC2616, par. 5.1.2 :
1166 * Request-URI = "*" | absuri | abspath | authority
1167 */
1168
1169 if (*ptr == '*')
1170 return NULL;
1171
1172 if (isalpha((unsigned char)*ptr)) {
1173 /* this is a scheme as described by RFC3986, par. 3.1 */
1174 ptr++;
1175 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1176 ptr++;
1177 /* skip '://' */
1178 if (*ptr == '\0' || *ptr++ != ':')
1179 return NULL;
1180 if (*ptr == '\0' || *ptr++ != '/')
1181 return NULL;
1182 if (*ptr == '\0' || *ptr++ != '/')
1183 return NULL;
1184 }
1185 /* skip [user[:passwd]@]host[:[port]] */
1186
1187 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1188 ptr++;
1189
1190 if (*ptr == '\0' || *ptr == ' ')
1191 return NULL;
1192
1193 /* OK, we got the '/' ! */
1194 return ptr;
1195}
1196
Willy Tarreau71241ab2012-12-27 11:30:54 +01001197/* Returns a 302 for a redirectable request that reaches a server working in
1198 * in redirect mode. This may only be called just after the stream interface
1199 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1200 * follow normal proxy processing. NOTE: this function is designed to support
1201 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001203void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001204{
1205 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001206 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001207 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001208 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001209
1210 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001211 trash.len = strlen(HTTP_302);
1212 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001213
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001214 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001215
Willy Tarreauefb453c2008-10-26 20:49:47 +01001216 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001217 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001218 return;
1219
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001220 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001221 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001222 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1223 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001224 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001225
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001226 /* 3: add the request URI. Since it was already forwarded, we need
1227 * to temporarily rewind the buffer.
1228 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001229 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001230 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001231
Willy Tarreauefb453c2008-10-26 20:49:47 +01001232 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001233 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 +02001234
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001235 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001236
Willy Tarreauefb453c2008-10-26 20:49:47 +01001237 if (!path)
1238 return;
1239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001240 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001241 return;
1242
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001243 memcpy(trash.str + trash.len, path, len);
1244 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001245
1246 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001247 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1248 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001249 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001250 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1251 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001252 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001253
1254 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001255 si_shutr(si);
1256 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001257 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001258 si->state = SI_ST_CLO;
1259
1260 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001261 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001262
1263 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001264 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001265 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001266}
1267
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001268/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001269 * that the server side is closed. Note that err_type is actually a
1270 * bitmask, where almost only aborts may be cumulated with other
1271 * values. We consider that aborted operations are more important
1272 * than timeouts or errors due to the fact that nobody else in the
1273 * logs might explain incomplete retries. All others should avoid
1274 * being cumulated. It should normally not be possible to have multiple
1275 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001276 * Note that connection errors appearing on the second request of a keep-alive
1277 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001279void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001280{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001281 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001282
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001283 /* set s->txn->status for http_error_message(s) */
1284 s->txn->status = 503;
1285
Willy Tarreauefb453c2008-10-26 20:49:47 +01001286 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001287 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001288 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001289 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001290 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001291 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001292 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001293 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001294 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001295 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001296 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001297 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001298 503, http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001299 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001300 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001301 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001302 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001303 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001304 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1305 503, (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001306 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001307 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001308 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001309 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001310 http_error_message(s));
1311 else { /* SI_ET_CONN_OTHER and others */
1312 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001313 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001314 500, http_error_message(s));
1315 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001316}
1317
Willy Tarreau42250582007-04-01 01:30:43 +02001318extern const char sess_term_cond[8];
1319extern const char sess_fin_state[8];
1320extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001321struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001322struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001323struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001324struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325
Willy Tarreau117f59e2007-03-04 18:17:17 +01001326/*
1327 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001328 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001329 */
1330void capture_headers(char *som, struct hdr_idx *idx,
1331 char **cap, struct cap_hdr *cap_hdr)
1332{
1333 char *eol, *sol, *col, *sov;
1334 int cur_idx;
1335 struct cap_hdr *h;
1336 int len;
1337
1338 sol = som + hdr_idx_first_pos(idx);
1339 cur_idx = hdr_idx_first_idx(idx);
1340
1341 while (cur_idx) {
1342 eol = sol + idx->v[cur_idx].len;
1343
1344 col = sol;
1345 while (col < eol && *col != ':')
1346 col++;
1347
1348 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001349 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001350 sov++;
1351
1352 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001353 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001354 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1355 if (cap[h->index] == NULL)
1356 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001357 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001358
1359 if (cap[h->index] == NULL) {
1360 Alert("HTTP capture : out of memory.\n");
1361 continue;
1362 }
1363
1364 len = eol - sov;
1365 if (len > h->len)
1366 len = h->len;
1367
1368 memcpy(cap[h->index], sov, len);
1369 cap[h->index][len]=0;
1370 }
1371 }
1372 sol = eol + idx->v[cur_idx].cr + 1;
1373 cur_idx = idx->v[cur_idx].next;
1374 }
1375}
1376
1377
Willy Tarreau42250582007-04-01 01:30:43 +02001378/* either we find an LF at <ptr> or we jump to <bad>.
1379 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001380#define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0)
Willy Tarreau42250582007-04-01 01:30:43 +02001381
1382/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1383 * otherwise to <http_msg_ood> with <state> set to <st>.
1384 */
1385#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1386 ptr++; \
1387 if (likely(ptr < end)) \
1388 goto good; \
1389 else { \
1390 state = (st); \
1391 goto http_msg_ood; \
1392 } \
1393 } while (0)
1394
1395
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001397 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1399 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1400 * will give undefined results.
1401 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1402 * and that msg->sol points to the beginning of the response.
1403 * If a complete line is found (which implies that at least one CR or LF is
1404 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1405 * returned indicating an incomplete line (which does not mean that parts have
1406 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1407 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1408 * upon next call.
1409 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001410 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1412 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001413 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001415const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001416 enum ht_state state, const char *ptr, const char *end,
1417 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001418{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001419 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001424 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1426
1427 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001428 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1430 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001431 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001432 state = HTTP_MSG_ERROR;
1433 break;
1434
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001436 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001438 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 goto http_msg_rpcode;
1440 }
1441 if (likely(HTTP_IS_SPHT(*ptr)))
1442 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1443 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001444 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001445 state = HTTP_MSG_ERROR;
1446 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001447
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 if (likely(!HTTP_IS_LWS(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1452
1453 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001454 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1456 }
1457
1458 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001459 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001460 http_msg_rsp_reason:
1461 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001462 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 msg->sl.st.r_l = 0;
1464 goto http_msg_rpline_eol;
1465
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001469 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 goto http_msg_rpreason;
1471 }
1472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1474 /* so it's a CR/LF, so there is no reason phrase */
1475 goto http_msg_rsp_reason;
1476
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001478 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 if (likely(!HTTP_IS_CRLF(*ptr)))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001481 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 http_msg_rpline_eol:
1483 /* We have seen the end of line. Note that we do not
1484 * necessarily have the \n yet, but at least we know that we
1485 * have EITHER \r OR \n, otherwise the response would not be
1486 * complete. We can then record the response length and return
1487 * to the caller which will be able to register it.
1488 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001489 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001490 return ptr;
1491
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001493#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1495 exit(1);
1496#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001497 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001498 }
1499
1500 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001501 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 if (ret_state)
1503 *ret_state = state;
1504 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001505 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001507}
1508
Willy Tarreau8973c702007-01-21 23:58:29 +01001509/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 * This function parses a request line between <ptr> and <end>, starting with
1511 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1512 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1513 * will give undefined results.
1514 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1515 * and that msg->sol points to the beginning of the request.
1516 * If a complete line is found (which implies that at least one CR or LF is
1517 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1518 * returned indicating an incomplete line (which does not mean that parts have
1519 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1520 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1521 * upon next call.
1522 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001523 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1525 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001526 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001527 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001528const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001529 enum ht_state state, const char *ptr, const char *end,
1530 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001531{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001532 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 if (likely(HTTP_IS_TOKEN(*ptr)))
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001541 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1543 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 if (likely(HTTP_IS_CRLF(*ptr))) {
1546 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001547 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001549 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001551 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001553 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 msg->sl.rq.v_l = 0;
1555 goto http_msg_rqline_eol;
1556 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001557 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001558 state = HTTP_MSG_ERROR;
1559 break;
1560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001562 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001564 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 goto http_msg_rquri;
1566 }
1567 if (likely(HTTP_IS_SPHT(*ptr)))
1568 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1569 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1570 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001573 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001574#if defined(__x86_64__) || \
1575 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1576 defined(__ARM_ARCH_7A__)
1577 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1578 while (ptr <= end - sizeof(int)) {
1579 int x = *(int *)ptr - 0x21212121;
1580 if (x & 0x80808080)
1581 break;
1582
1583 x -= 0x5e5e5e5e;
1584 if (!(x & 0x80808080))
1585 break;
1586
1587 ptr += sizeof(int);
1588 }
1589#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001590 if (ptr >= end) {
1591 state = HTTP_MSG_RQURI;
1592 goto http_msg_ood;
1593 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001594 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001595 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001596 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001599 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001603 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001604 /* non-ASCII chars are forbidden unless option
1605 * accept-invalid-http-request is enabled in the frontend.
1606 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001607 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001608 if (msg->err_pos < -1)
1609 goto invalid_char;
1610 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001611 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001612 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1613 }
1614
1615 if (likely(HTTP_IS_CRLF(*ptr))) {
1616 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1617 goto http_msg_req09_uri_e;
1618 }
1619
1620 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001621 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001622 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001623 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001624 state = HTTP_MSG_ERROR;
1625 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001628 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001630 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 goto http_msg_rqver;
1632 }
1633 if (likely(HTTP_IS_SPHT(*ptr)))
1634 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1635 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1636 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001639 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001640 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001642
1643 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001644 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001645 http_msg_rqline_eol:
1646 /* We have seen the end of line. Note that we do not
1647 * necessarily have the \n yet, but at least we know that we
1648 * have EITHER \r OR \n, otherwise the request would not be
1649 * complete. We can then record the request length and return
1650 * to the caller which will be able to register it.
1651 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001652 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001653 return ptr;
1654 }
1655
1656 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001657 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001658 state = HTTP_MSG_ERROR;
1659 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001662#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1664 exit(1);
1665#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001666 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001670 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (ret_state)
1672 *ret_state = state;
1673 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001674 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001678/*
1679 * Returns the data from Authorization header. Function may be called more
1680 * than once so data is stored in txn->auth_data. When no header is found
1681 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001682 * searching again for something we are unable to find anyway. However, if
1683 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001684 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001685 */
1686
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001687/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1688 * set according to global.tune.bufsize.
1689 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001690char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001691
1692int
Willy Tarreau87b09662015-04-03 00:22:06 +02001693get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001694{
1695
Willy Tarreaueee5b512015-04-03 23:46:31 +02001696 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001697 struct chunk auth_method;
1698 struct hdr_ctx ctx;
1699 char *h, *p;
1700 int len;
1701
1702#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001703 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704#endif
1705
1706 if (txn->auth.method == HTTP_AUTH_WRONG)
1707 return 0;
1708
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001709 txn->auth.method = HTTP_AUTH_WRONG;
1710
1711 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001712
1713 if (txn->flags & TX_USE_PX_CONN) {
1714 h = "Proxy-Authorization";
1715 len = strlen(h);
1716 } else {
1717 h = "Authorization";
1718 len = strlen(h);
1719 }
1720
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001721 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001722 return 0;
1723
1724 h = ctx.line + ctx.val;
1725
1726 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001727 len = p - h;
1728 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001729 return 0;
1730
David Carlier7365f7d2016-04-04 11:54:42 +01001731 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1732 return 0;
1733
Willy Tarreau5c557d12016-03-13 08:17:02 +01001734 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001735
1736 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1737
1738 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001739 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001740
1741 if (len < 0)
1742 return 0;
1743
1744
1745 get_http_auth_buff[len] = '\0';
1746
1747 p = strchr(get_http_auth_buff, ':');
1748
1749 if (!p)
1750 return 0;
1751
1752 txn->auth.user = get_http_auth_buff;
1753 *p = '\0';
1754 txn->auth.pass = p+1;
1755
1756 txn->auth.method = HTTP_AUTH_BASIC;
1757 return 1;
1758 }
1759
1760 return 0;
1761}
1762
Willy Tarreau58f10d72006-12-04 02:26:12 +01001763
Willy Tarreau8973c702007-01-21 23:58:29 +01001764/*
1765 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001766 * depending on the initial msg->msg_state. The caller is responsible for
1767 * ensuring that the message does not wrap. The function can be preempted
1768 * everywhere when data are missing and recalled at the exact same location
1769 * with no information loss. The message may even be realigned between two
1770 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001771 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001772 * fields. Note that msg->sol will be initialized after completing the first
1773 * state, so that none of the msg pointers has to be initialized prior to the
1774 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001775 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001776void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777{
Willy Tarreau3770f232013-12-07 00:01:53 +01001778 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001781
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001782 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001784 ptr = buf->p + msg->next;
1785 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 if (unlikely(ptr >= end))
1788 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001791 /*
1792 * First, states that are specific to the response only.
1793 * We check them first so that request and headers are
1794 * closer to each other (accessed more often).
1795 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001797 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001798 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001799 /* we have a start of message, but we have to check
1800 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001801 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001802 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001803 if (unlikely(ptr != buf->p)) {
1804 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001805 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001806 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001807 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001808 }
Willy Tarreau26927362012-05-18 23:22:52 +02001809 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001810 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001811 hdr_idx_init(idx);
1812 state = HTTP_MSG_RPVER;
1813 goto http_msg_rpver;
1814 }
1815
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001816 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1817 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001818 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001819 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001820
1821 if (unlikely(*ptr == '\n'))
1822 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1823 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1824 /* stop here */
1825
Willy Tarreau8973c702007-01-21 23:58:29 +01001826 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001827 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001828 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001829 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1830 /* stop here */
1831
Willy Tarreau8973c702007-01-21 23:58:29 +01001832 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001833 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001834 case HTTP_MSG_RPVER_SP:
1835 case HTTP_MSG_RPCODE:
1836 case HTTP_MSG_RPCODE_SP:
1837 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001838 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001839 state, ptr, end,
1840 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001841 if (unlikely(!ptr))
1842 return;
1843
1844 /* we have a full response and we know that we have either a CR
1845 * or an LF at <ptr>.
1846 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001847 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1848
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001849 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001850 if (likely(*ptr == '\r'))
1851 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1852 goto http_msg_rpline_end;
1853
Willy Tarreau8973c702007-01-21 23:58:29 +01001854 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001855 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001856 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001857 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001858 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1859 /* stop here */
1860
1861 /*
1862 * Second, states that are specific to the request only
1863 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001865 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001866 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001867 /* we have a start of message, but we have to check
1868 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001869 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001870 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001871 if (likely(ptr != buf->p)) {
1872 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001873 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001874 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001875 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 }
Willy Tarreau26927362012-05-18 23:22:52 +02001877 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001878 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001879 state = HTTP_MSG_RQMETH;
1880 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001882
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001883 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1884 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001885 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001886 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 if (unlikely(*ptr == '\n'))
1889 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1890 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001891 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001892
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001894 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001895 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001897 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001898
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001900 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001901 case HTTP_MSG_RQMETH_SP:
1902 case HTTP_MSG_RQURI:
1903 case HTTP_MSG_RQURI_SP:
1904 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001905 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001906 state, ptr, end,
1907 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 if (unlikely(!ptr))
1909 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001910
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 /* we have a full request and we know that we have either a CR
1912 * or an LF at <ptr>.
1913 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001915
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 if (likely(*ptr == '\r'))
1918 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001920
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001922 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 /* check for HTTP/0.9 request : no version information available.
1924 * msg->sol must point to the first of CR or LF.
1925 */
1926 if (unlikely(msg->sl.rq.v_l == 0))
1927 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001928
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001929 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001931 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001932
Willy Tarreau8973c702007-01-21 23:58:29 +01001933 /*
1934 * Common states below
1935 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001937 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001938 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 if (likely(!HTTP_IS_CRLF(*ptr))) {
1940 goto http_msg_hdr_name;
1941 }
1942
1943 if (likely(*ptr == '\r'))
1944 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1945 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001948 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 /* assumes msg->sol points to the first char */
1950 if (likely(HTTP_IS_TOKEN(*ptr)))
1951 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001952
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001953 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001954 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001955
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001956 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1957 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001958 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001959 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001960
1961 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001962 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001963
1964 /* and we still accept this non-token character */
1965 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001966
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001967 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001968 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001969 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 if (likely(HTTP_IS_SPHT(*ptr)))
1971 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001976 if (likely(!HTTP_IS_CRLF(*ptr))) {
1977 goto http_msg_hdr_val;
1978 }
1979
1980 if (likely(*ptr == '\r'))
1981 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1982 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001983
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001984 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001985 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001986 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001987 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001988
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001990 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 if (likely(HTTP_IS_SPHT(*ptr))) {
1992 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001993 for (; buf->p + msg->sov < ptr; msg->sov++)
1994 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001995 goto http_msg_hdr_l1_sp;
1996 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001997 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001998 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 goto http_msg_complete_header;
2000
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002002 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002003 /* assumes msg->sol points to the first char, and msg->sov
2004 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002006
2007 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
2008 * and lower. In fact since most of the time is spent in the loop, we
2009 * also remove the sign bit test so that bytes 0x8e..0x0d break the
2010 * loop, but we don't care since they're very rare in header values.
2011 */
2012#if defined(__x86_64__)
2013 while (ptr <= end - sizeof(long)) {
2014 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
2015 goto http_msg_hdr_val2;
2016 ptr += sizeof(long);
2017 }
2018#endif
2019#if defined(__x86_64__) || \
2020 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
2021 defined(__ARM_ARCH_7A__)
2022 while (ptr <= end - sizeof(int)) {
2023 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
2024 goto http_msg_hdr_val2;
2025 ptr += sizeof(int);
2026 }
2027#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002028 if (ptr >= end) {
2029 state = HTTP_MSG_HDR_VAL;
2030 goto http_msg_ood;
2031 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002032 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002034 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002035
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002037 /* Note: we could also copy eol into ->eoh so that we have the
2038 * real header end in case it ends with lots of LWS, but is this
2039 * really needed ?
2040 */
2041 if (likely(*ptr == '\r'))
2042 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2043 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002045 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002046 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002047 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002048 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002050 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002051 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002052 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2053 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 for (; buf->p + msg->eol < ptr; msg->eol++)
2055 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 goto http_msg_hdr_val;
2057 }
2058 http_msg_complete_header:
2059 /*
2060 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002061 * Assumes msg->sol points to the first char, msg->sov points
2062 * to the first character of the value and msg->eol to the
2063 * first CR or LF so we know how the line ends. We insert last
2064 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002065 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002066 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002067 idx, idx->tail) < 0)) {
2068 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002070 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002071
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 if (likely(!HTTP_IS_CRLF(*ptr))) {
2074 goto http_msg_hdr_name;
2075 }
2076
2077 if (likely(*ptr == '\r'))
2078 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2079 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002082 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002083 /* Assumes msg->sol points to the first of either CR or LF.
2084 * Sets ->sov and ->next to the total header length, ->eoh to
2085 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2086 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002087 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002088 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002089 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002090 msg->eoh = msg->sol;
2091 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002092 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002093 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002094 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002095
2096 case HTTP_MSG_ERROR:
2097 /* this may only happen if we call http_msg_analyser() twice with an error */
2098 break;
2099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002101#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002102 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2103 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002104#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002105 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002106 }
2107 http_msg_ood:
2108 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002109 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002113 http_msg_invalid:
2114 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002115 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002116 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002118 return;
2119}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002120
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002121/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2122 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2123 * nothing is done and 1 is returned.
2124 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002125static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002126{
2127 int delta;
2128 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002129 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002130
2131 if (msg->sl.rq.v_l != 0)
2132 return 1;
2133
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002134 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2135 if (txn->meth != HTTP_METH_GET)
2136 return 0;
2137
Willy Tarreau9b28e032012-10-12 23:49:43 +02002138 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002139
2140 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002141 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2142 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002143 }
2144 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002145 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002146 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002147 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002148 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002149 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002150 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002151 NULL, NULL);
2152 if (unlikely(!cur_end))
2153 return 0;
2154
2155 /* we have a full HTTP/1.0 request now and we know that
2156 * we have either a CR or an LF at <ptr>.
2157 */
2158 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2159 return 1;
2160}
2161
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002162/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002163 * and "keep-alive" values. If we already know that some headers may safely
2164 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002165 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2166 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002167 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002168 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2169 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2170 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002171 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002172 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002173void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002174{
Willy Tarreau5b154472009-12-21 20:11:07 +01002175 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002176 const char *hdr_val = "Connection";
2177 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002178
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002179 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002180 return;
2181
Willy Tarreau88d349d2010-01-25 12:15:43 +01002182 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2183 hdr_val = "Proxy-Connection";
2184 hdr_len = 16;
2185 }
2186
Willy Tarreau5b154472009-12-21 20:11:07 +01002187 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002188 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002189 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002190 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2191 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002192 if (to_del & 2)
2193 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002194 else
2195 txn->flags |= TX_CON_KAL_SET;
2196 }
2197 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2198 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002199 if (to_del & 1)
2200 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002201 else
2202 txn->flags |= TX_CON_CLO_SET;
2203 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002204 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2205 txn->flags |= TX_HDR_CONN_UPG;
2206 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002207 }
2208
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002209 txn->flags |= TX_HDR_CONN_PRS;
2210 return;
2211}
Willy Tarreau5b154472009-12-21 20:11:07 +01002212
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002213/* Apply desired changes on the Connection: header. Values may be removed and/or
2214 * added depending on the <wanted> flags, which are exclusively composed of
2215 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2216 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2217 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002218void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002219{
2220 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002221 const char *hdr_val = "Connection";
2222 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002223
2224 ctx.idx = 0;
2225
Willy Tarreau88d349d2010-01-25 12:15:43 +01002226
2227 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2228 hdr_val = "Proxy-Connection";
2229 hdr_len = 16;
2230 }
2231
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002232 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002233 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002234 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2235 if (wanted & TX_CON_KAL_SET)
2236 txn->flags |= TX_CON_KAL_SET;
2237 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002238 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002239 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002240 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2241 if (wanted & TX_CON_CLO_SET)
2242 txn->flags |= TX_CON_CLO_SET;
2243 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002244 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002245 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002246 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002247
2248 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2249 return;
2250
2251 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2252 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002253 hdr_val = "Connection: close";
2254 hdr_len = 17;
2255 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2256 hdr_val = "Proxy-Connection: close";
2257 hdr_len = 23;
2258 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002259 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002260 }
2261
2262 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2263 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002264 hdr_val = "Connection: keep-alive";
2265 hdr_len = 22;
2266 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2267 hdr_val = "Proxy-Connection: keep-alive";
2268 hdr_len = 28;
2269 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002270 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002271 }
2272 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002273}
2274
Christopher Faulet113f7de2015-12-14 14:52:13 +01002275/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2276 * point to the first byte of data after the chunk size, so that we know we can
2277 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2278 * forming the chunk size. That way it is always possible to differentiate
2279 * between the start of the body and the start of the data. Return the number
2280 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2281 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002282 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002283static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002284{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002285 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002286 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002287 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002288 const char *end = buf->data + buf->size;
2289 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002290 unsigned int chunk = 0;
2291
2292 /* The chunk size is in the following form, though we are only
2293 * interested in the size and CRLF :
2294 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2295 */
2296 while (1) {
2297 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002298 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002299 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002300 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002301 if (c < 0) /* not a hex digit anymore */
2302 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002303 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002304 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002305 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002306 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002307 chunk = (chunk << 4) + c;
2308 }
2309
Willy Tarreaud98cf932009-12-27 22:54:55 +01002310 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002311 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002312 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002313
Willy Tarreau2235b262016-11-05 15:50:20 +01002314 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002315 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002316 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002317 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002318 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002319 }
2320
Willy Tarreaud98cf932009-12-27 22:54:55 +01002321 /* Up to there, we know that at least one byte is present at *ptr. Check
2322 * for the end of chunk size.
2323 */
2324 while (1) {
2325 if (likely(HTTP_IS_CRLF(*ptr))) {
2326 /* we now have a CR or an LF at ptr */
2327 if (likely(*ptr == '\r')) {
2328 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002329 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002330 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002331 return 0;
2332 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002333
Willy Tarreaud98cf932009-12-27 22:54:55 +01002334 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002335 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002336 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002337 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002338 /* done */
2339 break;
2340 }
2341 else if (*ptr == ';') {
2342 /* chunk extension, ends at next CRLF */
2343 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002344 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002345 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002346 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002347
2348 while (!HTTP_IS_CRLF(*ptr)) {
2349 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002350 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002351 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002352 return 0;
2353 }
2354 /* we have a CRLF now, loop above */
2355 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002356 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002357 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002358 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002359 }
2360
Christopher Faulet113f7de2015-12-14 14:52:13 +01002361 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2362 * or may not be present. We save the number of bytes parsed into
2363 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002364 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002365 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002366 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002367 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002368 msg->chunk_len = chunk;
2369 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002370 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002371 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002372 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002373 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002374}
2375
Christopher Faulet113f7de2015-12-14 14:52:13 +01002376/* This function skips trailers in the buffer associated with HTTP message
2377 * <msg>. The first visited position is msg->next. If the end of the trailers is
2378 * found, the function returns >0. So, the caller can automatically schedul it
2379 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2380 * data are available, the function does not change anything except maybe
2381 * msg->sol if it could parse some lines, and returns zero. If a parse error
2382 * is encountered, the function returns < 0 and does not change anything except
2383 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2384 * state before calling this function, which implies that all non-trailers data
2385 * have already been scheduled for forwarding, and that msg->next exactly
2386 * matches the length of trailers already parsed and not forwarded. It is also
2387 * important to note that this function is designed to be able to parse wrapped
2388 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002389 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002390static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002391{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002392 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002393
Christopher Faulet113f7de2015-12-14 14:52:13 +01002394 /* we have msg->next which points to next line. Look for CRLF. But
2395 * first, we reset msg->sol */
2396 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002397 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002398 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002399 const char *start = b_ptr(buf, msg->next + msg->sol);
2400 const char *stop = bi_end(buf);
2401 const char *ptr = start;
2402 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002403
2404 /* scan current line and stop at LF or CRLF */
2405 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002406 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002407 return 0;
2408
2409 if (*ptr == '\n') {
2410 if (!p1)
2411 p1 = ptr;
2412 p2 = ptr;
2413 break;
2414 }
2415
2416 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002417 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002418 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002419 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002420 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002421 p1 = ptr;
2422 }
2423
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
2429 /* after LF; point to beginning of next line */
2430 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002431 if (p2 >= buf->data + buf->size)
2432 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002433
Christopher Faulet2fb28802015-12-01 10:40:57 +01002434 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002435 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002436 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002437 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002438
Christopher Fauletd7c91962015-04-30 11:48:27 +02002439 /* LF/CRLF at beginning of line => end of trailers at p2.
2440 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002441 * from this message. */
2442 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002443 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002444
Willy Tarreaud98cf932009-12-27 22:54:55 +01002445 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002446 }
2447}
2448
Christopher Faulet113f7de2015-12-14 14:52:13 +01002449/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2450 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2451 * in order to include this part into the next forwarding phase. Note that the
2452 * caller must ensure that ->p points to the first byte to parse. It returns
2453 * the number of bytes parsed on success, so the caller can set msg_state to
2454 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2455 * change anything and returns zero. If a parse error is encountered, the
2456 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2457 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002458 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002459static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002460{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002461 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002462 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002463 int bytes;
2464
2465 /* NB: we'll check data availabilty at the end. It's not a
2466 * problem because whatever we match first will be checked
2467 * against the correct length.
2468 */
2469 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002470 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002471 if (*ptr == '\r') {
2472 bytes++;
2473 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002474 if (ptr >= buf->data + buf->size)
2475 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002476 }
2477
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002478 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002479 return 0;
2480
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002481 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002482 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002483 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002484 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002485 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002486}
Willy Tarreau5b154472009-12-21 20:11:07 +01002487
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002488/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2489 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2490 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2491 * Unparsable qvalues return 1000 as "q=1.000".
2492 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002493int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002494{
2495 int q = 1000;
2496
Willy Tarreau506c69a2014-07-08 00:59:48 +02002497 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002498 goto out;
2499 q = (*qvalue++ - '0') * 1000;
2500
2501 if (*qvalue++ != '.')
2502 goto out;
2503
Willy Tarreau506c69a2014-07-08 00:59:48 +02002504 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002505 goto out;
2506 q += (*qvalue++ - '0') * 100;
2507
Willy Tarreau506c69a2014-07-08 00:59:48 +02002508 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002509 goto out;
2510 q += (*qvalue++ - '0') * 10;
2511
Willy Tarreau506c69a2014-07-08 00:59:48 +02002512 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002513 goto out;
2514 q += (*qvalue++ - '0') * 1;
2515 out:
2516 if (q > 1000)
2517 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002518 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002519 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002520 return q;
2521}
William Lallemand82fe75c2012-10-23 10:25:10 +02002522
Willy Tarreau87b09662015-04-03 00:22:06 +02002523void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002524{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002525 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002526 int tmp = TX_CON_WANT_KAL;
2527
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002528 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2529 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2531 tmp = TX_CON_WANT_TUN;
2532
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002533 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002534 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2535 tmp = TX_CON_WANT_TUN;
2536 }
2537
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002538 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002539 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2540 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002541 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002542 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2543 tmp = TX_CON_WANT_CLO;
2544 else
2545 tmp = TX_CON_WANT_SCL;
2546 }
2547
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002548 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002549 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2550 tmp = TX_CON_WANT_CLO;
2551
2552 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2553 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2554
2555 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2556 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2557 /* parse the Connection header and possibly clean it */
2558 int to_del = 0;
2559 if ((msg->flags & HTTP_MSGF_VER_11) ||
2560 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002561 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002562 to_del |= 2; /* remove "keep-alive" */
2563 if (!(msg->flags & HTTP_MSGF_VER_11))
2564 to_del |= 1; /* remove "close" */
2565 http_parse_connection_header(txn, msg, to_del);
2566 }
2567
2568 /* check if client or config asks for explicit close in KAL/SCL */
2569 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2570 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2571 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2572 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2573 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002574 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002575 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2576}
William Lallemand82fe75c2012-10-23 10:25:10 +02002577
Willy Tarreaud787e662009-07-07 10:14:51 +02002578/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2579 * processing can continue on next analysers, or zero if it either needs more
2580 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002581 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002582 * when it has nothing left to do, and may remove any analyser when it wants to
2583 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002584 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002585int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002586{
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 /*
2588 * We will parse the partial (or complete) lines.
2589 * We will check the request syntax, and also join multi-line
2590 * headers. An index of all the lines will be elaborated while
2591 * parsing.
2592 *
2593 * For the parsing, we use a 28 states FSM.
2594 *
2595 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002596 * req->buf->p = beginning of request
2597 * req->buf->p + msg->eoh = end of processed headers / start of current one
2598 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002599 * msg->eol = end of current header or line (LF or CRLF)
2600 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002601 *
2602 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002603 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002604 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2605 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002609 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002610 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002612 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002613
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 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 +01002615 now_ms, __FUNCTION__,
2616 s,
2617 req,
2618 req->rex, req->wex,
2619 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002620 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002621 req->analysers);
2622
Willy Tarreau52a0c602009-08-16 22:45:38 +02002623 /* we're speaking HTTP here, so let's speak HTTP to the client */
2624 s->srv_error = http_return_srv_error;
2625
Willy Tarreau83e3af02009-12-28 17:39:57 +01002626 /* There's a protected area at the end of the buffer for rewriting
2627 * purposes. We don't want to start to parse the request if the
2628 * protected area is affected, because we may have to move processed
2629 * data later, which is much more complicated.
2630 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002631 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002632
2633 /* This point is executed when some data is avalaible for analysis,
2634 * so we log the end of the idle time. */
2635 if (s->logs.t_idle == -1)
2636 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2637
Willy Tarreau379357a2013-06-08 12:55:46 +02002638 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002639 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002640 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002641 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002642 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002643 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002644 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002645 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002646 return 0;
2647 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002648 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2649 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2650 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002651 }
2652
Willy Tarreau065e8332010-01-08 00:30:20 +01002653 /* Note that we have the same problem with the response ; we
2654 * may want to send a redirect, error or anything which requires
2655 * some spare space. So we'll ensure that we have at least
2656 * maxrewrite bytes available in the response buffer before
2657 * processing that one. This will only affect pipelined
2658 * keep-alive requests.
2659 */
2660 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002661 unlikely(!channel_is_rewritable(&s->res) ||
2662 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2663 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2664 if (s->res.buf->o) {
2665 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002666 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002667 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002668 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002669 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2670 s->res.flags |= CF_WAKE_WRITE;
2671 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002672 return 0;
2673 }
2674 }
2675
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002677 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002678 }
2679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 1: we might have to print this header in debug mode */
2681 if (unlikely((global.mode & MODE_DEBUG) &&
2682 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002683 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685
Willy Tarreau9b28e032012-10-12 23:49:43 +02002686 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002687 /* this is a bit complex : in case of error on the request line,
2688 * we know that rq.l is still zero, so we display only the part
2689 * up to the end of the line (truncated by debug_hdr).
2690 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002691 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002692 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 sol += hdr_idx_first_pos(&txn->hdr_idx);
2695 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 while (cur_idx) {
2698 eol = sol + txn->hdr_idx.v[cur_idx].len;
2699 debug_hdr("clihdr", s, sol, eol);
2700 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2701 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002702 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 }
2704
Willy Tarreau58f10d72006-12-04 02:26:12 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 /*
2707 * Now we quickly check if we have found a full valid request.
2708 * If not so, we check the FD and buffer states before leaving.
2709 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002710 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002711 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002712 * on a keep-alive stream, if we encounter and error, close, t/o,
2713 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002714 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 * Last, we may increase some tracked counters' http request errors on
2717 * the cases that are deliberately the client's fault. For instance,
2718 * a timeout or connection reset is not counted as an error. However
2719 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002721
Willy Tarreau655dce92009-11-08 13:10:58 +01002722 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002723 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002725 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002726 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002727 stream_inc_http_req_ctr(s);
2728 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002729 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* 1: Since we are in header mode, if there's no space
2734 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002735 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 * must terminate it now.
2737 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* FIXME: check if URI is set and return Status
2740 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002742 stream_inc_http_req_ctr(s);
2743 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002744 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002745 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002746 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 goto return_bad_req;
2748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002749
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002751 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002752 if (!(s->flags & SF_ERR_MASK))
2753 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002754
Willy Tarreaufcffa692010-01-10 14:21:19 +01002755 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002756 goto failed_keep_alive;
2757
Willy Tarreau0f228a02015-05-01 15:37:53 +02002758 if (sess->fe->options & PR_O_IGNORE_PRB)
2759 goto failed_keep_alive;
2760
Willy Tarreau59234e92008-11-30 23:51:27 +01002761 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002763 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002764 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002765 }
2766
Willy Tarreaudc979f22012-12-04 10:39:01 +01002767 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002768 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002770 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002771 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002772 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002773 proxy_inc_fe_req_ctr(sess->fe);
2774 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002775 if (sess->listener->counters)
2776 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreaue7dff022015-04-03 01:14:29 +02002778 if (!(s->flags & SF_FINST_MASK))
2779 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002780 return 0;
2781 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002782
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002784 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002785 if (!(s->flags & SF_ERR_MASK))
2786 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002787
Willy Tarreaufcffa692010-01-10 14:21:19 +01002788 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002789 goto failed_keep_alive;
2790
Willy Tarreau0f228a02015-05-01 15:37:53 +02002791 if (sess->fe->options & PR_O_IGNORE_PRB)
2792 goto failed_keep_alive;
2793
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002795 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002796 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002797 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002799 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002800 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002802 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002803 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002804
Willy Tarreau87b09662015-04-03 00:22:06 +02002805 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002806 proxy_inc_fe_req_ctr(sess->fe);
2807 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002808 if (sess->listener->counters)
2809 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002810
Willy Tarreaue7dff022015-04-03 01:14:29 +02002811 if (!(s->flags & SF_FINST_MASK))
2812 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 return 0;
2814 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002815
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002817 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_ERR_MASK))
2819 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002820
Willy Tarreaufcffa692010-01-10 14:21:19 +01002821 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002822 goto failed_keep_alive;
2823
Willy Tarreau0f228a02015-05-01 15:37:53 +02002824 if (sess->fe->options & PR_O_IGNORE_PRB)
2825 goto failed_keep_alive;
2826
Willy Tarreau4076a152009-04-02 15:18:36 +02002827 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002828 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002829 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002830 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002832 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002833 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002834 stream_inc_http_err_ctr(s);
2835 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002836 proxy_inc_fe_req_ctr(sess->fe);
2837 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002838 if (sess->listener->counters)
2839 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840
Willy Tarreaue7dff022015-04-03 01:14:29 +02002841 if (!(s->flags & SF_FINST_MASK))
2842 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002843 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002844 }
2845
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002846 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002847 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002848 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002849#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002850 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2851 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002852 /* We need more data, we have to re-enable quick-ack in case we
2853 * previously disabled it, otherwise we might cause the client
2854 * to delay next data.
2855 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002856 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002857 }
2858#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002859
Willy Tarreaufcffa692010-01-10 14:21:19 +01002860 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2861 /* If the client starts to talk, let's fall back to
2862 * request timeout processing.
2863 */
2864 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002865 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002866 }
2867
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002869 if (!tick_isset(req->analyse_exp)) {
2870 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2871 (txn->flags & TX_WAIT_NEXT_RQ) &&
2872 tick_isset(s->be->timeout.httpka))
2873 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2874 else
2875 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002877
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 /* we're not ready yet */
2879 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002880
2881 failed_keep_alive:
2882 /* Here we process low-level errors for keep-alive requests. In
2883 * short, if the request is not the first one and it experiences
2884 * a timeout, read error or shutdown, we just silently close so
2885 * that the client can try again.
2886 */
2887 txn->status = 0;
2888 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002889 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002890 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002891 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002892 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002893 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002894 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896
Willy Tarreaud787e662009-07-07 10:14:51 +02002897 /* OK now we have a complete HTTP request with indexed headers. Let's
2898 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002900 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002901 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002902 * byte after the last LF. msg->sov points to the first byte of data.
2903 * msg->eol cannot be trusted because it may have been left uninitialized
2904 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002905 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002906
Willy Tarreau87b09662015-04-03 00:22:06 +02002907 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002908 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002909
Willy Tarreaub16a5742010-01-10 14:46:16 +01002910 if (txn->flags & TX_WAIT_NEXT_RQ) {
2911 /* kill the pending keep-alive timeout */
2912 txn->flags &= ~TX_WAIT_NEXT_RQ;
2913 req->analyse_exp = TICK_ETERNITY;
2914 }
2915
2916
Willy Tarreaud787e662009-07-07 10:14:51 +02002917 /* Maybe we found in invalid header name while we were configured not
2918 * to block on that, so we have to capture it now.
2919 */
2920 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002921 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /*
2924 * 1: identify the method
2925 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002926 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002927
2928 /* we can make use of server redirect on GET and HEAD */
2929 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002930 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002931
Willy Tarreau59234e92008-11-30 23:51:27 +01002932 /*
2933 * 2: check if the URI matches the monitor_uri.
2934 * We have to do this for every request which gets in, because
2935 * the monitor-uri is defined by the frontend.
2936 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002937 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2938 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002939 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002940 sess->fe->monitor_uri,
2941 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002942 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002944 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002946
Willy Tarreaue7dff022015-04-03 01:14:29 +02002947 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002948 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002949
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002951 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002952 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002953
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 ret = acl_pass(ret);
2955 if (cond->pol == ACL_COND_UNLESS)
2956 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002957
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 if (ret) {
2959 /* we fail this request, let's return 503 service unavail */
2960 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002961 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002962 if (!(s->flags & SF_ERR_MASK))
2963 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002965 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002966 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002967
Willy Tarreau59234e92008-11-30 23:51:27 +01002968 /* nothing to fail, let's reply normaly */
2969 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002970 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002971 if (!(s->flags & SF_ERR_MASK))
2972 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 goto return_prx_cond;
2974 }
2975
2976 /*
2977 * 3: Maybe we have to copy the original REQURI for the logs ?
2978 * Note: we cannot log anymore if the request has been
2979 * classified as invalid.
2980 */
2981 if (unlikely(s->logs.logwait & LW_REQ)) {
2982 /* we have a complete HTTP request that we must log */
2983 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2984 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002985
Willy Tarreau59234e92008-11-30 23:51:27 +01002986 if (urilen >= REQURI_LEN)
2987 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002988 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002990
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002991 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 s->do_log(s);
2993 } else {
2994 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002995 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 }
Willy Tarreau06619262006-12-17 08:37:22 +01002997
Willy Tarreau91852eb2015-05-01 13:26:00 +02002998 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2999 * exactly one digit "." one digit. This check may be disabled using
3000 * option accept-invalid-http-request.
3001 */
3002 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3003 if (msg->sl.rq.v_l != 8) {
3004 msg->err_pos = msg->sl.rq.v;
3005 goto return_bad_req;
3006 }
3007
3008 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3009 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3010 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3011 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3012 msg->err_pos = msg->sl.rq.v + 4;
3013 goto return_bad_req;
3014 }
3015 }
Willy Tarreau13317662015-05-01 13:47:08 +02003016 else {
3017 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3018 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3019 goto return_bad_req;
3020 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003021
Willy Tarreau5b154472009-12-21 20:11:07 +01003022 /* ... and check if the request is HTTP/1.1 or above */
3023 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3025 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3026 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003027 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003028
3029 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003030 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 +01003031
Willy Tarreau88d349d2010-01-25 12:15:43 +01003032 /* if the frontend has "option http-use-proxy-header", we'll check if
3033 * we have what looks like a proxied connection instead of a connection,
3034 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3035 * Note that this is *not* RFC-compliant, however browsers and proxies
3036 * happen to do that despite being non-standard :-(
3037 * We consider that a request not beginning with either '/' or '*' is
3038 * a proxied connection, which covers both "scheme://location" and
3039 * CONNECT ip:port.
3040 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003041 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003042 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003043 txn->flags |= TX_USE_PX_CONN;
3044
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003045 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003046 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003047
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003049 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003050 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003051 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003052
Willy Tarreau557f1992015-05-01 10:05:17 +02003053 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3054 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003055 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003056 * The length of a message body is determined by one of the following
3057 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003058 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003059 * 1. Any response to a HEAD request and any response with a 1xx
3060 * (Informational), 204 (No Content), or 304 (Not Modified) status
3061 * code is always terminated by the first empty line after the
3062 * header fields, regardless of the header fields present in the
3063 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003064 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003065 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3066 * the connection will become a tunnel immediately after the empty
3067 * line that concludes the header fields. A client MUST ignore any
3068 * Content-Length or Transfer-Encoding header fields received in
3069 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003070 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003071 * 3. If a Transfer-Encoding header field is present and the chunked
3072 * transfer coding (Section 4.1) is the final encoding, the message
3073 * body length is determined by reading and decoding the chunked
3074 * data until the transfer coding indicates the data is complete.
3075 *
3076 * If a Transfer-Encoding header field is present in a response and
3077 * the chunked transfer coding is not the final encoding, the
3078 * message body length is determined by reading the connection until
3079 * it is closed by the server. If a Transfer-Encoding header field
3080 * is present in a request and the chunked transfer coding is not
3081 * the final encoding, the message body length cannot be determined
3082 * reliably; the server MUST respond with the 400 (Bad Request)
3083 * status code and then close the connection.
3084 *
3085 * If a message is received with both a Transfer-Encoding and a
3086 * Content-Length header field, the Transfer-Encoding overrides the
3087 * Content-Length. Such a message might indicate an attempt to
3088 * perform request smuggling (Section 9.5) or response splitting
3089 * (Section 9.4) and ought to be handled as an error. A sender MUST
3090 * remove the received Content-Length field prior to forwarding such
3091 * a message downstream.
3092 *
3093 * 4. If a message is received without Transfer-Encoding and with
3094 * either multiple Content-Length header fields having differing
3095 * field-values or a single Content-Length header field having an
3096 * invalid value, then the message framing is invalid and the
3097 * recipient MUST treat it as an unrecoverable error. If this is a
3098 * request message, the server MUST respond with a 400 (Bad Request)
3099 * status code and then close the connection. If this is a response
3100 * message received by a proxy, the proxy MUST close the connection
3101 * to the server, discard the received response, and send a 502 (Bad
3102 * Gateway) response to the client. If this is a response message
3103 * received by a user agent, the user agent MUST close the
3104 * connection to the server and discard the received response.
3105 *
3106 * 5. If a valid Content-Length header field is present without
3107 * Transfer-Encoding, its decimal value defines the expected message
3108 * body length in octets. If the sender closes the connection or
3109 * the recipient times out before the indicated number of octets are
3110 * received, the recipient MUST consider the message to be
3111 * incomplete and close the connection.
3112 *
3113 * 6. If this is a request message and none of the above are true, then
3114 * the message body length is zero (no message body is present).
3115 *
3116 * 7. Otherwise, this is a response message without a declared message
3117 * body length, so the message body length is determined by the
3118 * number of octets received prior to the server closing the
3119 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003120 */
3121
Willy Tarreau32b47f42009-10-18 20:55:02 +02003122 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003123 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003124 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003125 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003126 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3127 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003128 /* chunked not last, return badreq */
3129 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003130 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003131 }
3132
Willy Tarreau1c913912015-04-30 10:57:51 +02003133 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003134 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003135 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3136 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3137 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3138 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003139 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003140 signed long long cl;
3141
Willy Tarreauad14f752011-09-02 20:33:27 +02003142 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003143 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003144 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003145 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003146
Willy Tarreauad14f752011-09-02 20:33:27 +02003147 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003148 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003149 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003150 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003151
Willy Tarreauad14f752011-09-02 20:33:27 +02003152 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003153 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003154 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003155 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003156
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003157 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003158 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003159 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003160 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003161
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003162 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003163 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003164 }
3165
Willy Tarreau34dfc602015-05-01 10:09:49 +02003166 /* even bodyless requests have a known length */
3167 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003168
Willy Tarreau179085c2014-04-28 16:48:56 +02003169 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3170 * only change if both the request and the config reference something else.
3171 * Option httpclose by itself sets tunnel mode where headers are mangled.
3172 * However, if another mode is set, it will affect it (eg: server-close/
3173 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3174 * if FE and BE have the same settings (common). The method consists in
3175 * checking if options changed between the two calls (implying that either
3176 * one is non-null, or one of them is non-null and we are there for the first
3177 * time.
3178 */
3179 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003180 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003181 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003182
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003183 /* we may have to wait for the request's body */
3184 if ((s->be->options & PR_O_WREQ_BODY) &&
3185 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3186 req->analysers |= AN_REQ_HTTP_BODY;
3187
Willy Tarreaud787e662009-07-07 10:14:51 +02003188 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003189 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003190 req->analyse_exp = TICK_ETERNITY;
3191 return 1;
3192
3193 return_bad_req:
3194 /* We centralize bad requests processing here */
3195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3196 /* we detected a parsing error. We want to archive this request
3197 * in the dedicated proxy area for later troubleshooting.
3198 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003199 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003200 }
3201
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003202 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003203 txn->req.msg_state = HTTP_MSG_ERROR;
3204 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003205 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003206
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003207 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003208 if (sess->listener->counters)
3209 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003210
3211 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003212 if (!(s->flags & SF_ERR_MASK))
3213 s->flags |= SF_ERR_PRXCOND;
3214 if (!(s->flags & SF_FINST_MASK))
3215 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003216
Christopher Faulet0184ea72017-01-05 14:06:34 +01003217 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003218 req->analyse_exp = TICK_ETERNITY;
3219 return 0;
3220}
3221
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003222
Willy Tarreau347a35d2013-11-22 17:51:09 +01003223/* This function prepares an applet to handle the stats. It can deal with the
3224 * "100-continue" expectation, check that admin rules are met for POST requests,
3225 * and program a response message if something was unexpected. It cannot fail
3226 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003227 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003229 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003230 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003231int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003232{
3233 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003234 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003235 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003236 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003237 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003238 struct uri_auth *uri_auth = s->be->uri_auth;
3239 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003240 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003241
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003242 appctx = si_appctx(si);
3243 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3244 appctx->st1 = appctx->st2 = 0;
3245 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3246 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003247 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003248 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003249
3250 uri = msg->chn->buf->p + msg->sl.rq.u;
3251 lookup = uri + uri_auth->uri_len;
3252
3253 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3254 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003256 break;
3257 }
3258 }
3259
3260 if (uri_auth->refresh) {
3261 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3262 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264 break;
3265 }
3266 }
3267 }
3268
3269 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3270 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003272 break;
3273 }
3274 }
3275
Willy Tarreau1e62df92016-01-11 18:57:53 +01003276 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3277 if (memcmp(h, ";typed", 6) == 0) {
3278 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3279 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3280 break;
3281 }
3282 }
3283
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3285 if (memcmp(h, ";st=", 4) == 0) {
3286 int i;
3287 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003288 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003289 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3290 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003292 break;
3293 }
3294 }
3295 break;
3296 }
3297 }
3298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003299 appctx->ctx.stats.scope_str = 0;
3300 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003301 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3302 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3303 int itx = 0;
3304 const char *h2;
3305 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3306 const char *err;
3307
3308 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3309 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003310 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003311 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3312 itx++;
3313 h++;
3314 }
3315
3316 if (itx > STAT_SCOPE_TXT_MAXLEN)
3317 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003321 memcpy(scope_txt, h2, itx);
3322 scope_txt[itx] = '\0';
3323 err = invalid_char(scope_txt);
3324 if (err) {
3325 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 appctx->ctx.stats.scope_str = 0;
3327 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003328 }
3329 break;
3330 }
3331 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003332
3333 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003334 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003335 int ret = 1;
3336
3337 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003338 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 +01003339 ret = acl_pass(ret);
3340 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3341 ret = !ret;
3342 }
3343
3344 if (ret) {
3345 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003346 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003347 break;
3348 }
3349 }
3350
3351 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003352 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003354 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003355 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3356 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003357 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003358 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003359 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003360 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3361 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003362 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003363 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003364 else {
3365 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003366 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003367 }
3368
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003369 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003370 return 1;
3371}
3372
Lukas Tribus67db8df2013-06-23 17:37:13 +02003373/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3374 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3375 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003376void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003377{
3378#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003379 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003380 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3381#endif
3382#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003383 if (from->ss_family == AF_INET6) {
3384 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003385 /* v4-mapped addresses need IP_TOS */
3386 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3387 else
3388 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3389 }
3390#endif
3391}
3392
Willy Tarreau87b09662015-04-03 00:22:06 +02003393int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003394 const char* name, unsigned int name_len,
3395 const char *str, struct my_regex *re,
3396 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003397{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003398 struct hdr_ctx ctx;
3399 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003400 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003401 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3402 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003403 struct chunk *output = get_trash_chunk();
3404
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003405 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003406
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003407 /* Choose the header browsing function. */
3408 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003409 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003410 http_find_hdr_func = http_find_header2;
3411 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003412 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003413 http_find_hdr_func = http_find_full_header2;
3414 break;
3415 default: /* impossible */
3416 return -1;
3417 }
3418
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003419 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3420 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003421 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003422 char *val = ctx.line + ctx.val;
3423 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003424
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003425 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3426 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003427
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003428 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003429 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003430 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003431
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003432 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003433
3434 hdr->len += delta;
3435 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436
3437 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003438 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003439 }
3440
3441 return 0;
3442}
3443
Willy Tarreau87b09662015-04-03 00:22:06 +02003444static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003445 const char* name, unsigned int name_len,
3446 struct list *fmt, struct my_regex *re,
3447 int action)
3448{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003449 struct chunk *replace;
3450 int ret = -1;
3451
3452 replace = alloc_trash_chunk();
3453 if (!replace)
3454 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003455
3456 replace->len = build_logline(s, replace->str, replace->size, fmt);
3457 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003458 goto leave;
3459
3460 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003461
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003462 leave:
3463 free_trash_chunk(replace);
3464 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003465}
3466
Willy Tarreau87b09662015-04-03 00:22:06 +02003467/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003468 * transaction <txn>. Returns the verdict of the first rule that prevents
3469 * further processing of the request (auth, deny, ...), and defaults to
3470 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3471 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003472 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3473 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3474 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003475 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003476enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003477http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003478{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003479 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003480 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003481 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003482 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003483 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003484 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003485 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003486 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003487
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003488 /* If "the current_rule_list" match the executed rule list, we are in
3489 * resume condition. If a resume is needed it is always in the action
3490 * and never in the ACL or converters. In this case, we initialise the
3491 * current rule, and go to the action execution point.
3492 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003493 if (s->current_rule) {
3494 rule = s->current_rule;
3495 s->current_rule = NULL;
3496 if (s->current_rule_list == rules)
3497 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003498 }
3499 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003500
Willy Tarreauff011f22011-01-06 17:51:27 +01003501 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003502
Willy Tarreau96257ec2012-12-27 10:46:37 +01003503 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003504 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003505 int ret;
3506
Willy Tarreau192252e2015-04-04 01:47:55 +02003507 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003508 ret = acl_pass(ret);
3509
Willy Tarreauff011f22011-01-06 17:51:27 +01003510 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003512
3513 if (!ret) /* condition not matched */
3514 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003515 }
3516
Willy Tarreauacc98002015-09-27 23:34:39 +02003517 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003518resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003519 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003520 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003521 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003522
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003523 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003524 if (deny_status)
3525 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003526 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003528 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003529 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003530 if (deny_status)
3531 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003532 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003535 /* Auth might be performed on regular http-req rules as well as on stats */
3536 auth_realm = rule->arg.auth.realm;
3537 if (!auth_realm) {
3538 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3539 auth_realm = STATS_DEFAULT_REALM;
3540 else
3541 auth_realm = px->id;
3542 }
3543 /* send 401/407 depending on whether we use a proxy or not. We still
3544 * count one error, because normal browsing won't significantly
3545 * increase the counter but brute force attempts will.
3546 */
3547 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3548 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003549 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003550 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003551 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003552
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003553 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003554 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3555 return HTTP_RULE_RES_BADREQ;
3556 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003558 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003559 s->task->nice = rule->arg.nice;
3560 break;
3561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003562 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003563 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003564 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003565 break;
3566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003567 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003568#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003569 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003570 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003571#endif
3572 break;
3573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003574 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003575 s->logs.level = rule->arg.loglevel;
3576 break;
3577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003578 case ACT_HTTP_REPLACE_HDR:
3579 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003580 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3581 rule->arg.hdr_add.name_len,
3582 &rule->arg.hdr_add.fmt,
3583 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003584 return HTTP_RULE_RES_BADREQ;
3585 break;
3586
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003587 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003588 ctx.idx = 0;
3589 /* remove all occurrences of the header */
3590 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3591 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3592 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003593 }
Willy Tarreau85603282015-01-21 20:39:27 +01003594 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003595
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003596 case ACT_HTTP_SET_HDR:
3597 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003598 /* The scope of the trash buffer must be limited to this function. The
3599 * build_logline() function can execute a lot of other function which
3600 * can use the trash buffer. So for limiting the scope of this global
3601 * buffer, we build first the header value using build_logline, and
3602 * after we store the header name.
3603 */
3604 len = rule->arg.hdr_add.name_len + 2,
3605 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003606 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003607 trash.str[rule->arg.hdr_add.name_len] = ':';
3608 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3609 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003610
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003611 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003612 /* remove all occurrences of the header */
3613 ctx.idx = 0;
3614 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3615 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3616 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3617 }
3618 }
3619
Willy Tarreau96257ec2012-12-27 10:46:37 +01003620 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3621 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003622
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003623 case ACT_HTTP_DEL_ACL:
3624 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003625 struct pat_ref *ref;
3626 char *key;
3627 int len;
3628
3629 /* collect reference */
3630 ref = pat_ref_lookup(rule->arg.map.ref);
3631 if (!ref)
3632 continue;
3633
3634 /* collect key */
3635 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3636 key = trash.str;
3637 key[len] = '\0';
3638
3639 /* perform update */
3640 /* returned code: 1=ok, 0=ko */
3641 pat_ref_delete(ref, key);
3642
3643 break;
3644 }
3645
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003646 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003647 struct pat_ref *ref;
3648 char *key;
3649 struct chunk *trash_key;
3650 int len;
3651
3652 trash_key = get_trash_chunk();
3653
3654 /* collect reference */
3655 ref = pat_ref_lookup(rule->arg.map.ref);
3656 if (!ref)
3657 continue;
3658
3659 /* collect key */
3660 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3661 key = trash_key->str;
3662 key[len] = '\0';
3663
3664 /* perform update */
3665 /* add entry only if it does not already exist */
3666 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003667 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003668
3669 break;
3670 }
3671
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003672 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003673 struct pat_ref *ref;
3674 char *key, *value;
3675 struct chunk *trash_key, *trash_value;
3676 int len;
3677
3678 trash_key = get_trash_chunk();
3679 trash_value = get_trash_chunk();
3680
3681 /* collect reference */
3682 ref = pat_ref_lookup(rule->arg.map.ref);
3683 if (!ref)
3684 continue;
3685
3686 /* collect key */
3687 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3688 key = trash_key->str;
3689 key[len] = '\0';
3690
3691 /* collect value */
3692 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3693 value = trash_value->str;
3694 value[len] = '\0';
3695
3696 /* perform update */
3697 if (pat_ref_find_elt(ref, key) != NULL)
3698 /* update entry if it exists */
3699 pat_ref_set(ref, key, value, NULL);
3700 else
3701 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003702 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003703
3704 break;
3705 }
William Lallemand73025dd2014-04-24 14:38:37 +02003706
Thierry FOURNIER42148732015-09-02 17:17:33 +02003707 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003708 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3709 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003710
Willy Tarreauacc98002015-09-27 23:34:39 +02003711 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003712 case ACT_RET_ERR:
3713 case ACT_RET_CONT:
3714 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003715 case ACT_RET_STOP:
3716 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003717 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003718 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003719 return HTTP_RULE_RES_YIELD;
3720 }
William Lallemand73025dd2014-04-24 14:38:37 +02003721 break;
3722
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003723 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003724 /* Note: only the first valid tracking parameter of each
3725 * applies.
3726 */
3727
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003728 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003729 struct stktable *t;
3730 struct stksess *ts;
3731 struct stktable_key *key;
3732 void *ptr;
3733
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003734 t = rule->arg.trk_ctr.table.t;
3735 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 +02003736
3737 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003738 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003739
3740 /* let's count a new HTTP request as it's the first time we do it */
3741 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3742 if (ptr)
3743 stktable_data_cast(ptr, http_req_cnt)++;
3744
3745 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3746 if (ptr)
3747 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3748 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3749
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003750 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003751 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003752 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003753 }
3754 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003755 break;
3756
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003757 /* other flags exists, but normaly, they never be matched. */
3758 default:
3759 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003760 }
3761 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003762
3763 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003764 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003765}
3766
Willy Tarreau71241ab2012-12-27 11:30:54 +01003767
Willy Tarreau51d861a2015-05-22 17:30:48 +02003768/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3769 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3770 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3771 * is returned, the process can continue the evaluation of next rule list. If
3772 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3773 * is returned, it means the operation could not be processed and a server error
3774 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3775 * deny rule. If *YIELD is returned, the caller must call again the function
3776 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003777 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003778static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003779http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003780{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003781 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003782 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003783 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003784 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003785 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003786 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003787
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003788 /* If "the current_rule_list" match the executed rule list, we are in
3789 * resume condition. If a resume is needed it is always in the action
3790 * and never in the ACL or converters. In this case, we initialise the
3791 * current rule, and go to the action execution point.
3792 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003793 if (s->current_rule) {
3794 rule = s->current_rule;
3795 s->current_rule = NULL;
3796 if (s->current_rule_list == rules)
3797 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003798 }
3799 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003800
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
3803 /* check optional condition */
3804 if (rule->cond) {
3805 int ret;
3806
Willy Tarreau192252e2015-04-04 01:47:55 +02003807 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003808 ret = acl_pass(ret);
3809
3810 if (rule->cond->pol == ACL_COND_UNLESS)
3811 ret = !ret;
3812
3813 if (!ret) /* condition not matched */
3814 continue;
3815 }
3816
Willy Tarreauacc98002015-09-27 23:34:39 +02003817 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003818resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003819 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003820 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003821 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003823 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003824 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003825 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003826
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003827 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003828 s->task->nice = rule->arg.nice;
3829 break;
3830
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003831 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003832 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003833 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003834 break;
3835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003836 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003837#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003838 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003839 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003840#endif
3841 break;
3842
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003843 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003844 s->logs.level = rule->arg.loglevel;
3845 break;
3846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003847 case ACT_HTTP_REPLACE_HDR:
3848 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003849 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3850 rule->arg.hdr_add.name_len,
3851 &rule->arg.hdr_add.fmt,
3852 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003853 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003854 break;
3855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003856 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003857 ctx.idx = 0;
3858 /* remove all occurrences of the header */
3859 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3860 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3861 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3862 }
Willy Tarreau85603282015-01-21 20:39:27 +01003863 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003865 case ACT_HTTP_SET_HDR:
3866 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003867 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3868 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3869 trash.len = rule->arg.hdr_add.name_len;
3870 trash.str[trash.len++] = ':';
3871 trash.str[trash.len++] = ' ';
3872 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 +01003873
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003874 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003875 /* remove all occurrences of the header */
3876 ctx.idx = 0;
3877 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3878 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3879 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3880 }
3881 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003882 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3883 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003885 case ACT_HTTP_DEL_ACL:
3886 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003887 struct pat_ref *ref;
3888 char *key;
3889 int len;
3890
3891 /* collect reference */
3892 ref = pat_ref_lookup(rule->arg.map.ref);
3893 if (!ref)
3894 continue;
3895
3896 /* collect key */
3897 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3898 key = trash.str;
3899 key[len] = '\0';
3900
3901 /* perform update */
3902 /* returned code: 1=ok, 0=ko */
3903 pat_ref_delete(ref, key);
3904
3905 break;
3906 }
3907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003908 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003909 struct pat_ref *ref;
3910 char *key;
3911 struct chunk *trash_key;
3912 int len;
3913
3914 trash_key = get_trash_chunk();
3915
3916 /* collect reference */
3917 ref = pat_ref_lookup(rule->arg.map.ref);
3918 if (!ref)
3919 continue;
3920
3921 /* collect key */
3922 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3923 key = trash_key->str;
3924 key[len] = '\0';
3925
3926 /* perform update */
3927 /* check if the entry already exists */
3928 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003929 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003930
3931 break;
3932 }
3933
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003934 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003935 struct pat_ref *ref;
3936 char *key, *value;
3937 struct chunk *trash_key, *trash_value;
3938 int len;
3939
3940 trash_key = get_trash_chunk();
3941 trash_value = get_trash_chunk();
3942
3943 /* collect reference */
3944 ref = pat_ref_lookup(rule->arg.map.ref);
3945 if (!ref)
3946 continue;
3947
3948 /* collect key */
3949 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3950 key = trash_key->str;
3951 key[len] = '\0';
3952
3953 /* collect value */
3954 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3955 value = trash_value->str;
3956 value[len] = '\0';
3957
3958 /* perform update */
3959 if (pat_ref_find_elt(ref, key) != NULL)
3960 /* update entry if it exists */
3961 pat_ref_set(ref, key, value, NULL);
3962 else
3963 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003964 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003965
3966 break;
3967 }
William Lallemand73025dd2014-04-24 14:38:37 +02003968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003969 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003970 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3971 return HTTP_RULE_RES_BADREQ;
3972 return HTTP_RULE_RES_DONE;
3973
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003974 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3975 /* Note: only the first valid tracking parameter of each
3976 * applies.
3977 */
3978
3979 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3980 struct stktable *t;
3981 struct stksess *ts;
3982 struct stktable_key *key;
3983 void *ptr;
3984
3985 t = rule->arg.trk_ctr.table.t;
3986 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3987
3988 if (key && (ts = stktable_get_entry(t, key))) {
3989 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3990
3991 /* let's count a new HTTP request as it's the first time we do it */
3992 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3993 if (ptr)
3994 stktable_data_cast(ptr, http_req_cnt)++;
3995
3996 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3997 if (ptr)
3998 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3999 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
4000
4001 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
4002 if (sess->fe != s->be)
4003 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
4004
4005 /* When the client triggers a 4xx from the server, it's most often due
4006 * to a missing object or permission. These events should be tracked
4007 * because if they happen often, it may indicate a brute force or a
4008 * vulnerability scan. Normally this is done when receiving the response
4009 * but here we're tracking after this ought to have been done so we have
4010 * to do it on purpose.
4011 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02004012 if ((unsigned)(txn->status - 400) < 100) {
4013 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
4014 if (ptr)
4015 stktable_data_cast(ptr, http_err_cnt)++;
4016
4017 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
4018 if (ptr)
4019 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
4020 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
4021 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08004022 }
4023 }
4024 break;
4025
Thierry FOURNIER42148732015-09-02 17:17:33 +02004026 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004027 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4028 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004029
Willy Tarreauacc98002015-09-27 23:34:39 +02004030 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004031 case ACT_RET_ERR:
4032 case ACT_RET_CONT:
4033 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004034 case ACT_RET_STOP:
4035 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004036 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004037 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004038 return HTTP_RULE_RES_YIELD;
4039 }
William Lallemand73025dd2014-04-24 14:38:37 +02004040 break;
4041
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004042 /* other flags exists, but normaly, they never be matched. */
4043 default:
4044 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004045 }
4046 }
4047
4048 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004049 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004050}
4051
4052
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053/* Perform an HTTP redirect based on the information in <rule>. The function
4054 * returns non-zero on success, or zero in case of a, irrecoverable error such
4055 * as too large a request to build a valid response.
4056 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004057static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004058{
Willy Tarreaub329a312015-05-22 16:27:37 +02004059 struct http_msg *req = &txn->req;
4060 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004061 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004062 struct chunk *chunk;
4063 int ret = 0;
4064
4065 chunk = alloc_trash_chunk();
4066 if (!chunk)
4067 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004068
4069 /* build redirect message */
4070 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004071 case 308:
4072 msg_fmt = HTTP_308;
4073 break;
4074 case 307:
4075 msg_fmt = HTTP_307;
4076 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004077 case 303:
4078 msg_fmt = HTTP_303;
4079 break;
4080 case 301:
4081 msg_fmt = HTTP_301;
4082 break;
4083 case 302:
4084 default:
4085 msg_fmt = HTTP_302;
4086 break;
4087 }
4088
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004089 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4090 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004091
4092 switch(rule->type) {
4093 case REDIRECT_TYPE_SCHEME: {
4094 const char *path;
4095 const char *host;
4096 struct hdr_ctx ctx;
4097 int pathlen;
4098 int hostlen;
4099
4100 host = "";
4101 hostlen = 0;
4102 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004103 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004104 host = ctx.line + ctx.val;
4105 hostlen = ctx.vlen;
4106 }
4107
4108 path = http_get_path(txn);
4109 /* build message using path */
4110 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004111 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004112 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4113 int qs = 0;
4114 while (qs < pathlen) {
4115 if (path[qs] == '?') {
4116 pathlen = qs;
4117 break;
4118 }
4119 qs++;
4120 }
4121 }
4122 } else {
4123 path = "/";
4124 pathlen = 1;
4125 }
4126
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004127 if (rule->rdr_str) { /* this is an old "redirect" rule */
4128 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004129 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4130 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004132 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004133 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4134 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004135 }
4136 else {
4137 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004138 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004139
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004140 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004141 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4142 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004143 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004144 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004145 memcpy(chunk->str + chunk->len, "://", 3);
4146 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004147
4148 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004149 memcpy(chunk->str + chunk->len, host, hostlen);
4150 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004151
4152 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004153 memcpy(chunk->str + chunk->len, path, pathlen);
4154 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004155
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004156 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004157 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004159 if (chunk->len > chunk->size - 5)
4160 goto leave;
4161 chunk->str[chunk->len] = '/';
4162 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004163 }
4164
4165 break;
4166 }
4167 case REDIRECT_TYPE_PREFIX: {
4168 const char *path;
4169 int pathlen;
4170
4171 path = http_get_path(txn);
4172 /* build message using path */
4173 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004174 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4176 int qs = 0;
4177 while (qs < pathlen) {
4178 if (path[qs] == '?') {
4179 pathlen = qs;
4180 break;
4181 }
4182 qs++;
4183 }
4184 }
4185 } else {
4186 path = "/";
4187 pathlen = 1;
4188 }
4189
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004191 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4192 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004193
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004194 /* add prefix. Note that if prefix == "/", we don't want to
4195 * add anything, otherwise it makes it hard for the user to
4196 * configure a self-redirection.
4197 */
4198 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004199 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4200 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004201 }
4202 }
4203 else {
4204 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004205 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004206
4207 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004208 if (chunk->len + pathlen > chunk->size - 4)
4209 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004210 }
4211
4212 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004213 memcpy(chunk->str + chunk->len, path, pathlen);
4214 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004215
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004216 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004217 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004218 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004219 if (chunk->len > chunk->size - 5)
4220 goto leave;
4221 chunk->str[chunk->len] = '/';
4222 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004223 }
4224
4225 break;
4226 }
4227 case REDIRECT_TYPE_LOCATION:
4228 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004229 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004230 if (chunk->len + rule->rdr_len > chunk->size - 4)
4231 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004232
4233 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004234 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4235 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004236 }
4237 else {
4238 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004239 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004241 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004242 if (chunk->len > chunk->size - 4)
4243 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004244 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004245 break;
4246 }
4247
4248 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004249 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4250 chunk->len += 14;
4251 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4252 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 }
4254
Willy Tarreau19b14122017-02-28 09:48:11 +01004255 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004256 txn->status = rule->code;
4257 /* let's log the request time */
4258 s->logs.tv_request = now;
4259
Willy Tarreau19b14122017-02-28 09:48:11 +01004260 if ((req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004261 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004262 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4263 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4264 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004265 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004266 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004267 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4268 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004270 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4271 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004272 }
4273 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004274 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4275 chunk->len += 4;
4276 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4277 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004278 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004279 bi_fast_delete(req->chn->buf, req->sov);
4280 req->next -= req->sov;
4281 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004282 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
4283 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004284 req->msg_state = HTTP_MSG_CLOSED;
4285 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004286 /* Trim any possible response */
4287 res->chn->buf->i = 0;
4288 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004289 } else {
4290 /* keep-alive not possible */
4291 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004292 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4293 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004294 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004295 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4296 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004297 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004298 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004299 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004300 }
4301
Willy Tarreaue7dff022015-04-03 01:14:29 +02004302 if (!(s->flags & SF_ERR_MASK))
4303 s->flags |= SF_ERR_LOCAL;
4304 if (!(s->flags & SF_FINST_MASK))
4305 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004306
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004307 ret = 1;
4308 leave:
4309 free_trash_chunk(chunk);
4310 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004311}
4312
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313/* This stream analyser runs all HTTP request processing which is common to
4314 * frontends and backends, which means blocking ACLs, filters, connection-close,
4315 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004316 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004317 * either needs more data or wants to immediately abort the request (eg: deny,
4318 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004320int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004321{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004322 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004323 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004324 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004325 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004326 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004328 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004329
Willy Tarreau655dce92009-11-08 13:10:58 +01004330 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004331 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004332 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004333 }
4334
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 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 +02004336 now_ms, __FUNCTION__,
4337 s,
4338 req,
4339 req->rex, req->wex,
4340 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004341 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004342 req->analysers);
4343
Willy Tarreau65410832014-04-28 21:25:43 +02004344 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004345 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004346
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004347 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004348 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004349 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004350
Willy Tarreau0b748332014-04-29 00:13:29 +02004351 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004352 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4353 goto return_prx_yield;
4354
Willy Tarreau0b748332014-04-29 00:13:29 +02004355 case HTTP_RULE_RES_CONT:
4356 case HTTP_RULE_RES_STOP: /* nothing to do */
4357 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004358
Willy Tarreau0b748332014-04-29 00:13:29 +02004359 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4360 if (txn->flags & TX_CLTARPIT)
4361 goto tarpit;
4362 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004363
Willy Tarreau0b748332014-04-29 00:13:29 +02004364 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4365 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004366
Willy Tarreau0b748332014-04-29 00:13:29 +02004367 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004368 goto done;
4369
Willy Tarreau0b748332014-04-29 00:13:29 +02004370 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4371 goto return_bad_req;
4372 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373 }
4374
Willy Tarreau52542592014-04-28 18:33:26 +02004375 /* OK at this stage, we know that the request was accepted according to
4376 * the http-request rules, we can check for the stats. Note that the
4377 * URI is detected *before* the req* rules in order not to be affected
4378 * by a possible reqrep, while they are processed *after* so that a
4379 * reqdeny can still block them. This clearly needs to change in 1.6!
4380 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004381 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004382 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004383 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004384 txn->status = 500;
4385 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004386 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387
Willy Tarreaue7dff022015-04-03 01:14:29 +02004388 if (!(s->flags & SF_ERR_MASK))
4389 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004390 goto return_prx_cond;
4391 }
4392
4393 /* parse the whole stats request and extract the relevant information */
4394 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004395 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004396 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004397
Willy Tarreau0b748332014-04-29 00:13:29 +02004398 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4399 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004400
Willy Tarreau0b748332014-04-29 00:13:29 +02004401 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4402 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004403 }
4404
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004405 /* evaluate the req* rules except reqadd */
4406 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004407 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004408 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004409
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004410 if (txn->flags & TX_CLDENY)
4411 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004412
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004413 if (txn->flags & TX_CLTARPIT) {
4414 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004415 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004416 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004417 }
Willy Tarreau06619262006-12-17 08:37:22 +01004418
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004419 /* add request headers from the rule sets in the same order */
4420 list_for_each_entry(wl, &px->req_add, list) {
4421 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004422 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004423 ret = acl_pass(ret);
4424 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4425 ret = !ret;
4426 if (!ret)
4427 continue;
4428 }
4429
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004430 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004431 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004432 }
4433
Willy Tarreau52542592014-04-28 18:33:26 +02004434
4435 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004436 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004437 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004438 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4439 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004440
Willy Tarreaue7dff022015-04-03 01:14:29 +02004441 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4442 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4443 if (!(s->flags & SF_FINST_MASK))
4444 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004445
Willy Tarreau70730dd2014-04-24 18:06:27 +02004446 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004447 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4448 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004449 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004450 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004451 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004452
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004453 /* check whether we have some ACLs set to redirect this request */
4454 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004455 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004456 int ret;
4457
Willy Tarreau192252e2015-04-04 01:47:55 +02004458 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004459 ret = acl_pass(ret);
4460 if (rule->cond->pol == ACL_COND_UNLESS)
4461 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004462 if (!ret)
4463 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004464 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004465 if (!http_apply_redirect_rule(rule, s, txn))
4466 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004467 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004468 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004469
Willy Tarreau2be39392010-01-03 17:24:51 +01004470 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4471 * If this happens, then the data will not come immediately, so we must
4472 * send all what we have without waiting. Note that due to the small gain
4473 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004474 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004475 * itself once used.
4476 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004477 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004478
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004479 done: /* done with this analyser, continue with next ones that the calling
4480 * points will have set, if any.
4481 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004482 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004483 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4484 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004485 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004486
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487 tarpit:
4488 /* When a connection is tarpitted, we use the tarpit timeout,
4489 * which may be the same as the connect timeout if unspecified.
4490 * If unset, then set it to zero because we really want it to
4491 * eventually expire. We build the tarpit as an analyser.
4492 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004493 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004494
4495 /* wipe the request out so that we can drop the connection early
4496 * if the client closes first.
4497 */
4498 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004499
4500 /* Allow cookie logging
4501 */
4502 if (s->be->cookie_name || sess->fe->capture_name)
4503 manage_client_side_cookies(s, req);
4504
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004505 txn->status = http_err_codes[deny_status];
4506
Christopher Faulet0184ea72017-01-05 14:06:34 +01004507 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004508 req->analysers |= AN_REQ_HTTP_TARPIT;
4509 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4510 if (!req->analyse_exp)
4511 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004512 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004513 sess->fe->fe_counters.denied_req++;
4514 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004515 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004516 if (sess->listener->counters)
4517 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004518 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004519
4520 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004521
4522 /* Allow cookie logging
4523 */
4524 if (s->be->cookie_name || sess->fe->capture_name)
4525 manage_client_side_cookies(s, req);
4526
Willy Tarreau0b748332014-04-29 00:13:29 +02004527 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004528 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004529 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004530 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004531 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004532 sess->fe->fe_counters.denied_req++;
4533 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004534 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004535 if (sess->listener->counters)
4536 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004537 goto return_prx_cond;
4538
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539 return_bad_req:
4540 /* We centralize bad requests processing here */
4541 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4542 /* we detected a parsing error. We want to archive this request
4543 * in the dedicated proxy area for later troubleshooting.
4544 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004545 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004546 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004547
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004548 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004549 txn->req.msg_state = HTTP_MSG_ERROR;
4550 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004551 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004552
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004553 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004554 if (sess->listener->counters)
4555 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004556
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004557 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004558 if (!(s->flags & SF_ERR_MASK))
4559 s->flags |= SF_ERR_PRXCOND;
4560 if (!(s->flags & SF_FINST_MASK))
4561 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004562
Christopher Faulet0184ea72017-01-05 14:06:34 +01004563 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004564 req->analyse_exp = TICK_ETERNITY;
4565 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004566
4567 return_prx_yield:
4568 channel_dont_connect(req);
4569 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004570}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004571
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004572/* This function performs all the processing enabled for the current request.
4573 * It returns 1 if the processing can continue on next analysers, or zero if it
4574 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004575 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004577int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004578{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004579 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004580 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004581 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004582 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004583
Willy Tarreau655dce92009-11-08 13:10:58 +01004584 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004585 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004586 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004587 return 0;
4588 }
4589
Willy Tarreau87b09662015-04-03 00:22:06 +02004590 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 +02004591 now_ms, __FUNCTION__,
4592 s,
4593 req,
4594 req->rex, req->wex,
4595 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004596 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004597 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004598
Willy Tarreau59234e92008-11-30 23:51:27 +01004599 /*
4600 * Right now, we know that we have processed the entire headers
4601 * and that unwanted requests have been filtered out. We can do
4602 * whatever we want with the remaining request. Also, now we
4603 * may have separate values for ->fe, ->be.
4604 */
Willy Tarreau06619262006-12-17 08:37:22 +01004605
Willy Tarreau59234e92008-11-30 23:51:27 +01004606 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004607 * If HTTP PROXY is set we simply get remote server address parsing
4608 * incoming request. Note that this requires that a connection is
4609 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004610 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004611 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004612 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004613 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004614
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004615 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004616 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004617 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004618 txn->req.msg_state = HTTP_MSG_ERROR;
4619 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004620 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004621 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004622
Willy Tarreaue7dff022015-04-03 01:14:29 +02004623 if (!(s->flags & SF_ERR_MASK))
4624 s->flags |= SF_ERR_RESOURCE;
4625 if (!(s->flags & SF_FINST_MASK))
4626 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004627
4628 return 0;
4629 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004630
4631 path = http_get_path(txn);
4632 url2sa(req->buf->p + msg->sl.rq.u,
4633 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004634 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004635 /* if the path was found, we have to remove everything between
4636 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4637 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4638 * u_l characters by a single "/".
4639 */
4640 if (path) {
4641 char *cur_ptr = req->buf->p;
4642 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4643 int delta;
4644
4645 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4646 http_msg_move_end(&txn->req, delta);
4647 cur_end += delta;
4648 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4649 goto return_bad_req;
4650 }
4651 else {
4652 char *cur_ptr = req->buf->p;
4653 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4654 int delta;
4655
4656 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4657 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4658 http_msg_move_end(&txn->req, delta);
4659 cur_end += delta;
4660 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4661 goto return_bad_req;
4662 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004664
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004666 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 * Note that doing so might move headers in the request, but
4668 * the fields will stay coherent and the URI will not move.
4669 * This should only be performed in the backend.
4670 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004671 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004672 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004673
William Lallemanda73203e2012-03-12 12:48:57 +01004674 /* add unique-id if "header-unique-id" is specified */
4675
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004676 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004677 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4678 goto return_bad_req;
4679 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004680 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004681 }
William Lallemanda73203e2012-03-12 12:48:57 +01004682
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004683 if (sess->fe->header_unique_id && s->unique_id) {
4684 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004686 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004687 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004688 goto return_bad_req;
4689 }
4690
Cyril Bontéb21570a2009-11-29 20:04:48 +01004691 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 * 9: add X-Forwarded-For if either the frontend or the backend
4693 * asks for it.
4694 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004695 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004696 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004697 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4698 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4699 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004700 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004701 /* The header is set to be added only if none is present
4702 * and we found it, so don't do anything.
4703 */
4704 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004705 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 /* Add an X-Forwarded-For header unless the source IP is
4707 * in the 'except' network range.
4708 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004709 if ((!sess->fe->except_mask.s_addr ||
4710 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4711 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004712 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004713 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004714 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004715 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004717 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004718
4719 /* Note: we rely on the backend to get the header name to be used for
4720 * x-forwarded-for, because the header is really meant for the backends.
4721 * However, if the backend did not specify any option, we have to rely
4722 * on the frontend's header name.
4723 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004724 if (s->be->fwdfor_hdr_len) {
4725 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004726 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004727 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004728 len = sess->fe->fwdfor_hdr_len;
4729 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004730 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004731 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 +01004732
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004733 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004734 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004735 }
4736 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004738 /* FIXME: for the sake of completeness, we should also support
4739 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004740 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004741 int len;
4742 char pn[INET6_ADDRSTRLEN];
4743 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004744 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004745 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004746
Willy Tarreau59234e92008-11-30 23:51:27 +01004747 /* Note: we rely on the backend to get the header name to be used for
4748 * x-forwarded-for, because the header is really meant for the backends.
4749 * However, if the backend did not specify any option, we have to rely
4750 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004751 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004752 if (s->be->fwdfor_hdr_len) {
4753 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004754 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004755 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004756 len = sess->fe->fwdfor_hdr_len;
4757 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004758 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004759 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004760
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004762 goto return_bad_req;
4763 }
4764 }
4765
4766 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004767 * 10: add X-Original-To if either the frontend or the backend
4768 * asks for it.
4769 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004770 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004771
4772 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004773 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004774 /* Add an X-Original-To header unless the destination IP is
4775 * in the 'except' network range.
4776 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004777 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004778
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004779 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004780 ((!sess->fe->except_mask_to.s_addr ||
4781 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4782 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004783 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004784 (((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 +02004785 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004786 int len;
4787 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004788 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004789
4790 /* Note: we rely on the backend to get the header name to be used for
4791 * x-original-to, because the header is really meant for the backends.
4792 * However, if the backend did not specify any option, we have to rely
4793 * on the frontend's header name.
4794 */
4795 if (s->be->orgto_hdr_len) {
4796 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004797 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004798 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004799 len = sess->fe->orgto_hdr_len;
4800 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004801 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004802 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 +02004803
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004804 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004805 goto return_bad_req;
4806 }
4807 }
4808 }
4809
Willy Tarreau50fc7772012-11-11 22:19:57 +01004810 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4811 * If an "Upgrade" token is found, the header is left untouched in order not to have
4812 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4813 * "Upgrade" is present in the Connection header.
4814 */
4815 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4816 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004817 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004818 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004819 unsigned int want_flags = 0;
4820
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004821 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004822 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004823 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004824 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004825 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004826 want_flags |= TX_CON_CLO_SET;
4827 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004828 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004829 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004830 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004831 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004832 want_flags |= TX_CON_KAL_SET;
4833 }
4834
4835 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004836 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004837 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004838
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004839
Willy Tarreau522d6c02009-12-06 18:49:18 +01004840 /* If we have no server assigned yet and we're balancing on url_param
4841 * with a POST request, we may be interested in checking the body for
4842 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004843 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004844 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004845 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004846 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004847 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004848 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004849 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004850
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004851 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004852 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004853 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004854#ifdef TCP_QUICKACK
4855 /* We expect some data from the client. Unless we know for sure
4856 * we already have a full request, we have to re-enable quick-ack
4857 * in case we previously disabled it, otherwise we might cause
4858 * the client to delay further data.
4859 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004860 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004861 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004862 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004863 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004864 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004865#endif
4866 }
Willy Tarreau03945942009-12-22 16:50:27 +01004867
Willy Tarreau59234e92008-11-30 23:51:27 +01004868 /*************************************************************
4869 * OK, that's finished for the headers. We have done what we *
4870 * could. Let's switch to the DATA state. *
4871 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004872 req->analyse_exp = TICK_ETERNITY;
4873 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004874
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004875 /* if the server closes the connection, we want to immediately react
4876 * and close the socket to save packets and syscalls.
4877 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004878 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004879 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004880
Willy Tarreau59234e92008-11-30 23:51:27 +01004881 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004882 /* OK let's go on with the BODY now */
4883 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004884
Willy Tarreau59234e92008-11-30 23:51:27 +01004885 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004886 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004887 /* we detected a parsing error. We want to archive this request
4888 * in the dedicated proxy area for later troubleshooting.
4889 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004890 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004891 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004892
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004893 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004894 txn->req.msg_state = HTTP_MSG_ERROR;
4895 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004896 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004897 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004898
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004899 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004900 if (sess->listener->counters)
4901 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004902
Willy Tarreaue7dff022015-04-03 01:14:29 +02004903 if (!(s->flags & SF_ERR_MASK))
4904 s->flags |= SF_ERR_PRXCOND;
4905 if (!(s->flags & SF_FINST_MASK))
4906 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004907 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004908}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004909
Willy Tarreau60b85b02008-11-30 23:28:40 +01004910/* This function is an analyser which processes the HTTP tarpit. It always
4911 * returns zero, at the beginning because it prevents any other processing
4912 * from occurring, and at the end because it terminates the request.
4913 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004914int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004915{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004916 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004917
4918 /* This connection is being tarpitted. The CLIENT side has
4919 * already set the connect expiration date to the right
4920 * timeout. We just have to check that the client is still
4921 * there and that the timeout has not expired.
4922 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004923 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004924 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004925 !tick_is_expired(req->analyse_exp, now_ms))
4926 return 0;
4927
4928 /* We will set the queue timer to the time spent, just for
4929 * logging purposes. We fake a 500 server error, so that the
4930 * attacker will not suspect his connection has been tarpitted.
4931 * It will not cause trouble to the logs because we can exclude
4932 * the tarpitted connections by filtering on the 'PT' status flags.
4933 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004934 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4935
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004936 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004937 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004938
Christopher Faulet0184ea72017-01-05 14:06:34 +01004939 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004940 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004941
Willy Tarreaue7dff022015-04-03 01:14:29 +02004942 if (!(s->flags & SF_ERR_MASK))
4943 s->flags |= SF_ERR_PRXCOND;
4944 if (!(s->flags & SF_FINST_MASK))
4945 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004946 return 0;
4947}
4948
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004949/* This function is an analyser which waits for the HTTP request body. It waits
4950 * for either the buffer to be full, or the full advertised contents to have
4951 * reached the buffer. It must only be called after the standard HTTP request
4952 * processing has occurred, because it expects the request to be parsed and will
4953 * look for the Expect header. It may send a 100-Continue interim response. It
4954 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4955 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4956 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004957 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004958int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004959{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004960 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004961 struct http_txn *txn = s->txn;
4962 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004963
4964 /* We have to parse the HTTP request body to find any required data.
4965 * "balance url_param check_post" should have been the only way to get
4966 * into this. We were brought here after HTTP header analysis, so all
4967 * related structures are ready.
4968 */
4969
Willy Tarreau890988f2014-04-10 11:59:33 +02004970 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4971 /* This is the first call */
4972 if (msg->msg_state < HTTP_MSG_BODY)
4973 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004974
Willy Tarreau890988f2014-04-10 11:59:33 +02004975 if (msg->msg_state < HTTP_MSG_100_SENT) {
4976 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4977 * send an HTTP/1.1 100 Continue intermediate response.
4978 */
4979 if (msg->flags & HTTP_MSGF_VER_11) {
4980 struct hdr_ctx ctx;
4981 ctx.idx = 0;
4982 /* Expect is allowed in 1.1, look for it */
4983 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4984 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004985 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004986 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004987 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004988 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004989 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004990 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004991
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004992 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004993 * req->buf->p still points to the beginning of the message. We
4994 * must save the body in msg->next because it survives buffer
4995 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004996 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004997 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004998
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004999 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01005000 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5001 else
5002 msg->msg_state = HTTP_MSG_DATA;
5003 }
5004
Willy Tarreau890988f2014-04-10 11:59:33 +02005005 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
5006 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02005007 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02005008 goto missing_data;
5009
5010 /* OK we have everything we need now */
5011 goto http_end;
5012 }
5013
5014 /* OK here we're parsing a chunked-encoded message */
5015
Willy Tarreau522d6c02009-12-06 18:49:18 +01005016 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005017 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005018 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005019 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01005020 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005021 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01005022
Willy Tarreau115acb92009-12-26 13:56:06 +01005023 if (!ret)
5024 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005025 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005026 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01005027 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005028 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005029 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005030 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01005031 }
5032
Willy Tarreaud98cf932009-12-27 22:54:55 +01005033 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005034 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5035 * for at least a whole chunk or the whole content length bytes after
5036 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005037 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005038 if (msg->msg_state == HTTP_MSG_TRAILERS)
5039 goto http_end;
5040
Willy Tarreaue115b492015-05-01 23:05:14 +02005041 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005042 goto http_end;
5043
5044 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005045 /* we get here if we need to wait for more data. If the buffer is full,
5046 * we have the maximum we can expect.
5047 */
5048 if (buffer_full(req->buf, global.tune.maxrewrite))
5049 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005050
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005051 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005052 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005053 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005054
Willy Tarreaue7dff022015-04-03 01:14:29 +02005055 if (!(s->flags & SF_ERR_MASK))
5056 s->flags |= SF_ERR_CLITO;
5057 if (!(s->flags & SF_FINST_MASK))
5058 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005059 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005060 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005061
5062 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005063 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005064 /* Not enough data. We'll re-use the http-request
5065 * timeout here. Ideally, we should set the timeout
5066 * relative to the accept() date. We just set the
5067 * request timeout once at the beginning of the
5068 * request.
5069 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005070 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005071 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005072 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005073 return 0;
5074 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005075
5076 http_end:
5077 /* The situation will not evolve, so let's give up on the analysis. */
5078 s->logs.tv_request = now; /* update the request timer to reflect full request */
5079 req->analysers &= ~an_bit;
5080 req->analyse_exp = TICK_ETERNITY;
5081 return 1;
5082
5083 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005084 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005085 txn->req.msg_state = HTTP_MSG_ERROR;
5086 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005087 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005088
Willy Tarreaue7dff022015-04-03 01:14:29 +02005089 if (!(s->flags & SF_ERR_MASK))
5090 s->flags |= SF_ERR_PRXCOND;
5091 if (!(s->flags & SF_FINST_MASK))
5092 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005093
Willy Tarreau522d6c02009-12-06 18:49:18 +01005094 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005095 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005096 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005097 if (sess->listener->counters)
5098 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005099 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005100}
5101
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005102/* send a server's name with an outgoing request over an established connection.
5103 * Note: this function is designed to be called once the request has been scheduled
5104 * for being forwarded. This is the reason why it rewinds the buffer before
5105 * proceeding.
5106 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005107int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005108
5109 struct hdr_ctx ctx;
5110
Mark Lamourinec2247f02012-01-04 13:02:01 -05005111 char *hdr_name = be->server_id_hdr_name;
5112 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005113 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005114 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005115 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005116
William Lallemandd9e90662012-01-30 17:27:17 +01005117 ctx.idx = 0;
5118
Willy Tarreau211cdec2014-04-17 20:18:08 +02005119 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005120 if (old_o) {
5121 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005122 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005123 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005124 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005125 }
5126
Willy Tarreau9b28e032012-10-12 23:49:43 +02005127 old_i = chn->buf->i;
5128 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 -05005129 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005130 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005131 }
5132
5133 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005134 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005135 memcpy(hdr_val, hdr_name, hdr_name_len);
5136 hdr_val += hdr_name_len;
5137 *hdr_val++ = ':';
5138 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005139 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5140 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005141
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005142 if (old_o) {
5143 /* If this was a forwarded request, we must readjust the amount of
5144 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005145 * variations. Note that the current state is >= HTTP_MSG_BODY,
5146 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005147 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005148 old_o += chn->buf->i - old_i;
5149 b_adv(chn->buf, old_o);
5150 txn->req.next -= old_o;
5151 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005152 }
5153
Mark Lamourinec2247f02012-01-04 13:02:01 -05005154 return 0;
5155}
5156
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157/* Terminate current transaction and prepare a new one. This is very tricky
5158 * right now but it works.
5159 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005160void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005162 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005163 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005164 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005165 struct connection *srv_conn;
5166 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005167 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005168
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169 /* FIXME: We need a more portable way of releasing a backend's and a
5170 * server's connections. We need a safer way to reinitialize buffer
5171 * flags. We also need a more accurate method for computing per-request
5172 * data.
5173 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005174 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005175
Willy Tarreau4213a112013-12-15 10:25:42 +01005176 /* unless we're doing keep-alive, we want to quickly close the connection
5177 * to the server.
5178 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005179 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005180 !si_conn_ready(&s->si[1])) {
5181 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5182 si_shutr(&s->si[1]);
5183 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005184 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005185
Willy Tarreaue7dff022015-04-03 01:14:29 +02005186 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005187 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005188 if (unlikely(s->srv_conn))
5189 sess_change_server(s, NULL);
5190 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191
5192 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005193 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194
Willy Tarreaueee5b512015-04-03 23:46:31 +02005195 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 int n;
5197
Willy Tarreaueee5b512015-04-03 23:46:31 +02005198 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 if (n < 1 || n > 5)
5200 n = 0;
5201
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005202 if (fe->mode == PR_MODE_HTTP) {
5203 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005204 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005205 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005206 (be->mode == PR_MODE_HTTP)) {
5207 be->be_counters.p.http.rsp[n]++;
5208 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005209 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210 }
5211
5212 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005213 s->logs.bytes_in -= s->req.buf->i;
5214 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215
5216 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005217 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005218 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005219 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 s->do_log(s);
5221 }
5222
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005223 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005224 stream_stop_content_counters(s);
5225 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005226
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 s->logs.accept_date = date; /* user-visible date for logging */
5228 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005229 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5230 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231 tv_zero(&s->logs.tv_request);
5232 s->logs.t_queue = -1;
5233 s->logs.t_connect = -1;
5234 s->logs.t_data = -1;
5235 s->logs.t_close = 0;
5236 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5237 s->logs.srv_queue_size = 0; /* we will get this number soon */
5238
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005239 s->logs.bytes_in = s->req.total = s->req.buf->i;
5240 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005241
5242 if (s->pend_pos)
5243 pendconn_free(s->pend_pos);
5244
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005245 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005246 if (s->flags & SF_CURR_SESS) {
5247 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005248 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005250 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005251 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252 }
5253
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005254 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255
Willy Tarreau4213a112013-12-15 10:25:42 +01005256 /* only release our endpoint if we don't intend to reuse the
5257 * connection.
5258 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005259 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005260 !si_conn_ready(&s->si[1])) {
5261 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005262 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005263 }
5264
Willy Tarreau350f4872014-11-28 14:42:25 +01005265 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5266 s->si[1].err_type = SI_ET_NONE;
5267 s->si[1].conn_retries = 0; /* used for logging too */
5268 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005269 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 +01005270 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);
5271 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 +02005272 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5273 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5274 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005275
Willy Tarreaueee5b512015-04-03 23:46:31 +02005276 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005278 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005279
5280 if (prev_status == 401 || prev_status == 407) {
5281 /* In HTTP keep-alive mode, if we receive a 401, we still have
5282 * a chance of being able to send the visitor again to the same
5283 * server over the same connection. This is required by some
5284 * broken protocols such as NTLM, and anyway whenever there is
5285 * an opportunity for sending the challenge to the proper place,
5286 * it's better to do it (at least it helps with debugging).
5287 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005288 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005289 if (srv_conn)
5290 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005291 }
5292
Willy Tarreau53f96852016-02-02 18:50:47 +01005293 /* Never ever allow to reuse a connection from a non-reuse backend */
5294 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5295 srv_conn->flags |= CO_FL_PRIVATE;
5296
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005297 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005298 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005300 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005301 s->req.flags |= CF_NEVER_WAIT;
5302 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005303 }
5304
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 /* if the request buffer is not empty, it means we're
5306 * about to process another request, so send pending
5307 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005308 * Just don't do this if the buffer is close to be full,
5309 * because the request will wait for it to flush a little
5310 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005312 if (s->req.buf->i) {
5313 if (s->res.buf->o &&
5314 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5315 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5316 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005317 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005318
Willy Tarreau714ea782015-11-25 20:11:11 +01005319 /* we're removing the analysers, we MUST re-enable events detection.
5320 * We don't enable close on the response channel since it's either
5321 * already closed, or in keep-alive with an idle connection handler.
5322 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005323 channel_auto_read(&s->req);
5324 channel_auto_close(&s->req);
5325 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005327 /* we're in keep-alive with an idle connection, monitor it if not already done */
5328 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005329 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005330 if (!srv)
5331 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005332 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005333 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005334 else if (prev_flags & TX_NOT_FIRST)
5335 /* note: we check the request, not the connection, but
5336 * this is valid for strategies SAFE and AGGR, and in
5337 * case of ALWS, we don't care anyway.
5338 */
5339 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005340 else
5341 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005342 }
Willy Tarreau27375622013-12-17 00:00:28 +01005343
Christopher Faulet3e344292015-11-24 16:24:13 +01005344 if (HAS_FILTERS(s)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005345 s->req.analysers &= AN_REQ_FLT_END;
5346 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005347 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005348 else {
5349 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5350 s->res.analysers = 0;
5351 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352}
5353
5354
5355/* This function updates the request state machine according to the response
5356 * state machine and buffer flags. It returns 1 if it changes anything (flag
5357 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5358 * it is only used to find when a request/response couple is complete. Both
5359 * this function and its equivalent should loop until both return zero. It
5360 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5361 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005362int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005363{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005364 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005365 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005367 unsigned int old_state = txn->req.msg_state;
5368
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5370 return 0;
5371
5372 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005373 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005374 * We can shut the read side unless we want to abort_on_close,
5375 * or we have a POST request. The issue with POST requests is
5376 * that some browsers still send a CRLF after the request, and
5377 * this CRLF must be read so that it does not remain in the kernel
5378 * buffers, otherwise a close could cause an RST on some systems
5379 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005380 * Note that if we're using keep-alive on the client side, we'd
5381 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005382 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005383 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005384 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005385 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5386 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5387 !(s->be->options & PR_O_ABRT_CLOSE) &&
5388 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005389 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005390
Willy Tarreau40f151a2012-12-20 12:10:09 +01005391 /* if the server closes the connection, we want to immediately react
5392 * and close the socket to save packets and syscalls.
5393 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005394 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005395
Willy Tarreau7f876a12015-11-18 11:59:55 +01005396 /* In any case we've finished parsing the request so we must
5397 * disable Nagle when sending data because 1) we're not going
5398 * to shut this side, and 2) the server is waiting for us to
5399 * send pending data.
5400 */
5401 chn->flags |= CF_NEVER_WAIT;
5402
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5404 goto wait_other_side;
5405
5406 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5407 /* The server has not finished to respond, so we
5408 * don't want to move in order not to upset it.
5409 */
5410 goto wait_other_side;
5411 }
5412
5413 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5414 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005415 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005416 txn->req.msg_state = HTTP_MSG_TUNNEL;
5417 goto wait_other_side;
5418 }
5419
5420 /* When we get here, it means that both the request and the
5421 * response have finished receiving. Depending on the connection
5422 * mode, we'll have to wait for the last bytes to leave in either
5423 * direction, and sometimes for a close to be effective.
5424 */
5425
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005426 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5427 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005428 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5429 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005430 }
5431 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5432 /* Option forceclose is set, or either side wants to close,
5433 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005434 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005435 * once both states are CLOSED.
5436 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005437 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5438 channel_shutr_now(chn);
5439 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005440 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005441 }
5442 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005443 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5444 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005445 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005446 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5447 channel_auto_read(chn);
5448 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005449 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 }
5451
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005454 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005455
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005456 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 txn->req.msg_state = HTTP_MSG_CLOSING;
5458 goto http_msg_closing;
5459 }
5460 else {
5461 txn->req.msg_state = HTTP_MSG_CLOSED;
5462 goto http_msg_closed;
5463 }
5464 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005465 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 }
5467
5468 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5469 http_msg_closing:
5470 /* nothing else to forward, just waiting for the output buffer
5471 * to be empty and for the shutw_now to take effect.
5472 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005473 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474 txn->req.msg_state = HTTP_MSG_CLOSED;
5475 goto http_msg_closed;
5476 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005477 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005478 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005479 txn->req.msg_state = HTTP_MSG_ERROR;
5480 goto wait_other_side;
5481 }
5482 }
5483
5484 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5485 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005486 /* see above in MSG_DONE why we only do this in these states */
5487 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5488 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5489 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005490 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005491 goto wait_other_side;
5492 }
5493
5494 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005495 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005496}
5497
5498
5499/* This function updates the response state machine according to the request
5500 * state machine and buffer flags. It returns 1 if it changes anything (flag
5501 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5502 * it is only used to find when a request/response couple is complete. Both
5503 * this function and its equivalent should loop until both return zero. It
5504 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5505 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005506int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005507{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005508 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005509 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005510 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005511 unsigned int old_state = txn->rsp.msg_state;
5512
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5514 return 0;
5515
5516 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5517 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005518 * still monitor the server connection for a possible close
5519 * while the request is being uploaded, so we don't disable
5520 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005522 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005523
5524 if (txn->req.msg_state == HTTP_MSG_ERROR)
5525 goto wait_other_side;
5526
5527 if (txn->req.msg_state < HTTP_MSG_DONE) {
5528 /* The client seems to still be sending data, probably
5529 * because we got an error response during an upload.
5530 * We have the choice of either breaking the connection
5531 * or letting it pass through. Let's do the later.
5532 */
5533 goto wait_other_side;
5534 }
5535
5536 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5537 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005538 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005540 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 goto wait_other_side;
5542 }
5543
5544 /* When we get here, it means that both the request and the
5545 * response have finished receiving. Depending on the connection
5546 * mode, we'll have to wait for the last bytes to leave in either
5547 * direction, and sometimes for a close to be effective.
5548 */
5549
5550 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5551 /* Server-close mode : shut read and wait for the request
5552 * side to close its output buffer. The caller will detect
5553 * when we're in DONE and the other is in CLOSED and will
5554 * catch that for the final cleanup.
5555 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005556 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5557 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005559 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5560 /* Option forceclose is set, or either side wants to close,
5561 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005562 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005563 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005565 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5566 channel_shutr_now(chn);
5567 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005568 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 }
5570 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005571 /* The last possible modes are keep-alive and tunnel. Tunnel will
5572 * need to forward remaining data. Keep-alive will need to monitor
5573 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005574 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005575 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005576 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005577 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5578 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 }
5580
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005581 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005582 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005583 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005584 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5585 goto http_msg_closing;
5586 }
5587 else {
5588 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5589 goto http_msg_closed;
5590 }
5591 }
5592 goto wait_other_side;
5593 }
5594
5595 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5596 http_msg_closing:
5597 /* nothing else to forward, just waiting for the output buffer
5598 * to be empty and for the shutw_now to take effect.
5599 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005600 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5602 goto http_msg_closed;
5603 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005604 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005605 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005606 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005607 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005608 if (objt_server(s->target))
5609 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005610 goto wait_other_side;
5611 }
5612 }
5613
5614 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5615 http_msg_closed:
5616 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005617 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005618 channel_auto_close(chn);
5619 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005620 goto wait_other_side;
5621 }
5622
5623 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005624 /* We force the response to leave immediately if we're waiting for the
5625 * other side, since there is no pending shutdown to push it out.
5626 */
5627 if (!channel_is_empty(chn))
5628 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005629 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005630}
5631
5632
5633/* Resync the request and response state machines. Return 1 if either state
5634 * changes.
5635 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005636int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005637{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005638 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 int old_req_state = txn->req.msg_state;
5640 int old_res_state = txn->rsp.msg_state;
5641
Willy Tarreau610ecce2010-01-04 21:15:02 +01005642 http_sync_req_state(s);
5643 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005644 if (!http_sync_res_state(s))
5645 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005646 if (!http_sync_req_state(s))
5647 break;
5648 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005649
Willy Tarreau610ecce2010-01-04 21:15:02 +01005650 /* OK, both state machines agree on a compatible state.
5651 * There are a few cases we're interested in :
5652 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5653 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5654 * directions, so let's simply disable both analysers.
5655 * - HTTP_MSG_CLOSED on the response only means we must abort the
5656 * request.
5657 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5658 * with server-close mode means we've completed one request and we
5659 * must re-initialize the server connection.
5660 */
5661
5662 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5663 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5664 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5665 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005666 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005667 channel_auto_close(&s->req);
5668 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005669 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005670 channel_auto_close(&s->res);
5671 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005672 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005673 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005674 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005675 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005676 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005677 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005678 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005679 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005680 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005681 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005682 channel_auto_close(&s->res);
5683 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005684 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005685 channel_abort(&s->req);
5686 channel_auto_close(&s->req);
5687 channel_auto_read(&s->req);
5688 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005689 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005690 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5691 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005692 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005693 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5694 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5695 /* server-close/keep-alive: terminate this transaction,
5696 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005697 * a fresh-new transaction, but only once we're sure there's
5698 * enough room in the request and response buffer to process
5699 * another request. The request buffer must not hold any
5700 * pending output data and the request buffer must not have
5701 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005702 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005703 if (s->req.buf->o)
5704 s->req.flags |= CF_WAKE_WRITE;
5705 else if (channel_congested(&s->res))
5706 s->res.flags |= CF_WAKE_WRITE;
5707 else
5708 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005709 }
5710
Willy Tarreau610ecce2010-01-04 21:15:02 +01005711 return txn->req.msg_state != old_req_state ||
5712 txn->rsp.msg_state != old_res_state;
5713}
5714
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715/* This function is an analyser which forwards request body (including chunk
5716 * sizes if any). It is called as soon as we must forward, even if we forward
5717 * zero byte. The only situation where it must not be called is when we're in
5718 * tunnel mode and we want to forward till the close. It's used both to forward
5719 * remaining data and to resync after end of body. It expects the msg_state to
5720 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005721 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005722 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005723 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005724 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005725int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005726{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005727 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005728 struct http_txn *txn = s->txn;
5729 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005730 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005731
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005732 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5733 return 0;
5734
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005735 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005736 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005737 /* Output closed while we were sending data. We must abort and
5738 * wake the other side up.
5739 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005740 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005741 msg->msg_state = HTTP_MSG_ERROR;
5742 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005743 return 1;
5744 }
5745
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746 /* Note that we don't have to send 100-continue back because we don't
5747 * need the data to complete our job, and it's up to the server to
5748 * decide whether to return 100, 417 or anything else in return of
5749 * an "Expect: 100-continue" header.
5750 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005751 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5753 ? HTTP_MSG_CHUNK_SIZE
5754 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005755
5756 /* TODO/filters: when http-buffer-request option is set or if a
5757 * rule on url_param exists, the first chunk size could be
5758 * already parsed. In that case, msg->next is after the chunk
5759 * size (including the CRLF after the size). So this case should
5760 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005761 }
5762
Willy Tarreau7ba23542014-04-17 21:50:00 +02005763 /* Some post-connect processing might want us to refrain from starting to
5764 * forward data. Currently, the only reason for this is "balance url_param"
5765 * whichs need to parse/process the request after we've enabled forwarding.
5766 */
5767 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005768 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005769 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005770 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005771 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005772 }
5773 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5774 }
5775
Willy Tarreau80a92c02014-03-12 10:41:13 +01005776 /* in most states, we should abort in case of early close */
5777 channel_auto_close(req);
5778
Willy Tarreauefdf0942014-04-24 20:08:57 +02005779 if (req->to_forward) {
5780 /* We can't process the buffer's contents yet */
5781 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005783 }
5784
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005785 if (msg->msg_state < HTTP_MSG_DONE) {
5786 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5787 ? http_msg_forward_chunked_body(s, msg)
5788 : http_msg_forward_body(s, msg));
5789 if (!ret)
5790 goto missing_data_or_waiting;
5791 if (ret < 0)
5792 goto return_bad_req;
5793 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005794
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005795 /* other states, DONE...TUNNEL */
5796 /* we don't want to forward closes on DONE except in tunnel mode. */
5797 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5798 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005799
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005800 if (http_resync_states(s)) {
5801 /* some state changes occurred, maybe the analyser
5802 * was disabled too. */
5803 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5804 if (req->flags & CF_SHUTW) {
5805 /* request errors are most likely due to the
5806 * server aborting the transfer. */
5807 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005808 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005809 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005810 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005811 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005812 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005814 }
5815
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005816 /* If "option abortonclose" is set on the backend, we want to monitor
5817 * the client's connection and forward any shutdown notification to the
5818 * server, which will decide whether to close or to go on processing the
5819 * request. We only do that in tunnel mode, and not in other modes since
5820 * it can be abused to exhaust source ports. */
5821 if (s->be->options & PR_O_ABRT_CLOSE) {
5822 channel_auto_read(req);
5823 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5824 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5825 s->si[1].flags |= SI_FL_NOLINGER;
5826 channel_auto_close(req);
5827 }
5828 else if (s->txn->meth == HTTP_METH_POST) {
5829 /* POST requests may require to read extra CRLF sent by broken
5830 * browsers and which could cause an RST to be sent upon close
5831 * on some systems (eg: Linux). */
5832 channel_auto_read(req);
5833 }
5834 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005835
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005836 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005837 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005838 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005839 if (!(s->flags & SF_ERR_MASK))
5840 s->flags |= SF_ERR_CLICL;
5841 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005842 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005843 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005844 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005845 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005846 }
5847
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005848 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005849 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005850 if (objt_server(s->target))
5851 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852
5853 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005854 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005855
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005856 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005857 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005858 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005859
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005860 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005861 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005862 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005863 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005864 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005865
Willy Tarreau5c620922011-05-11 19:56:11 +02005866 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005867 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005868 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005869 * modes are already handled by the stream sock layer. We must not do
5870 * this in content-length mode because it could present the MSG_MORE
5871 * flag with the last block of forwarded data, which would cause an
5872 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005873 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005874 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005875 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005876
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005877 return 0;
5878
Willy Tarreaud98cf932009-12-27 22:54:55 +01005879 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005880 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005881 if (sess->listener->counters)
5882 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005883
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005884 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005885 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005886 txn->req.msg_state = HTTP_MSG_ERROR;
5887 if (txn->status) {
5888 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005889 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005890 } else {
5891 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005892 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005893 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005894 req->analysers &= AN_REQ_FLT_END;
5895 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005896
Willy Tarreaue7dff022015-04-03 01:14:29 +02005897 if (!(s->flags & SF_ERR_MASK))
5898 s->flags |= SF_ERR_PRXCOND;
5899 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005900 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005901 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005902 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005903 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005904 }
5905 return 0;
5906
5907 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005908 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005909 txn->req.msg_state = HTTP_MSG_ERROR;
5910 if (txn->status) {
5911 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005912 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005913 } else {
5914 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005915 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005916 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005917 req->analysers &= AN_REQ_FLT_END;
5918 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005919
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005920 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005921 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005922 if (objt_server(s->target))
5923 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005924
Willy Tarreaue7dff022015-04-03 01:14:29 +02005925 if (!(s->flags & SF_ERR_MASK))
5926 s->flags |= SF_ERR_SRVCL;
5927 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005928 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005929 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005930 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005931 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005932 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005933 return 0;
5934}
5935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5937 * processing can continue on next analysers, or zero if it either needs more
5938 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005939 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 * when it has nothing left to do, and may remove any analyser when it wants to
5941 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005942 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005943int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005944{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005945 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005946 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005947 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005948 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005949 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005950 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005951 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005952
Willy Tarreau87b09662015-04-03 00:22:06 +02005953 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 +02005954 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005956 rep,
5957 rep->rex, rep->wex,
5958 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005959 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005960 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005961
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005962 /*
5963 * Now parse the partial (or complete) lines.
5964 * We will check the response syntax, and also join multi-line
5965 * headers. An index of all the lines will be elaborated while
5966 * parsing.
5967 *
5968 * For the parsing, we use a 28 states FSM.
5969 *
5970 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005971 * rep->buf->p = beginning of response
5972 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5973 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005974 * msg->eol = end of current header or line (LF or CRLF)
5975 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 */
5977
Willy Tarreau628c40c2014-04-24 19:11:26 +02005978 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005979 /* There's a protected area at the end of the buffer for rewriting
5980 * purposes. We don't want to start to parse the request if the
5981 * protected area is affected, because we may have to move processed
5982 * data later, which is much more complicated.
5983 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005984 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005985 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005986 /* some data has still not left the buffer, wake us once that's done */
5987 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5988 goto abort_response;
5989 channel_dont_close(rep);
5990 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005991 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005992 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005993 }
5994
Willy Tarreau379357a2013-06-08 12:55:46 +02005995 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5996 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5997 buffer_slow_realign(rep->buf);
5998
Willy Tarreau9b28e032012-10-12 23:49:43 +02005999 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006000 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006001 }
6002
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006003 /* 1: we might have to print this header in debug mode */
6004 if (unlikely((global.mode & MODE_DEBUG) &&
6005 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006006 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008
Willy Tarreau9b28e032012-10-12 23:49:43 +02006009 sol = rep->buf->p;
6010 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006012
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 sol += hdr_idx_first_pos(&txn->hdr_idx);
6014 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006015
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 while (cur_idx) {
6017 eol = sol + txn->hdr_idx.v[cur_idx].len;
6018 debug_hdr("srvhdr", s, sol, eol);
6019 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6020 cur_idx = txn->hdr_idx.v[cur_idx].next;
6021 }
6022 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006023
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 /*
6025 * Now we quickly check if we have found a full valid response.
6026 * If not so, we check the FD and buffer states before leaving.
6027 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006028 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029 * responses are checked first.
6030 *
6031 * Depending on whether the client is still there or not, we
6032 * may send an error response back or not. Note that normally
6033 * we should only check for HTTP status there, and check I/O
6034 * errors somewhere else.
6035 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036
Willy Tarreau655dce92009-11-08 13:10:58 +01006037 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 /* Invalid response */
6039 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6040 /* we detected a parsing error. We want to archive this response
6041 * in the dedicated proxy area for later troubleshooting.
6042 */
6043 hdr_response_bad:
6044 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006045 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006047 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006048 if (objt_server(s->target)) {
6049 objt_server(s->target)->counters.failed_resp++;
6050 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006051 }
Willy Tarreau64648412010-03-05 10:41:54 +01006052 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006053 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006054 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006056 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006057 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006058 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059
Willy Tarreaue7dff022015-04-03 01:14:29 +02006060 if (!(s->flags & SF_ERR_MASK))
6061 s->flags |= SF_ERR_PRXCOND;
6062 if (!(s->flags & SF_FINST_MASK))
6063 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006064
6065 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006066 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006069 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006070 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006071 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006072 goto hdr_response_bad;
6073 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006076 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006078 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006079 else if (txn->flags & TX_NOT_FIRST)
6080 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006081
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006082 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006083 if (objt_server(s->target)) {
6084 objt_server(s->target)->counters.failed_resp++;
6085 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006086 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006087
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006088 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006089 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006091 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006092 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006093 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006094
Willy Tarreaue7dff022015-04-03 01:14:29 +02006095 if (!(s->flags & SF_ERR_MASK))
6096 s->flags |= SF_ERR_SRVCL;
6097 if (!(s->flags & SF_FINST_MASK))
6098 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006099 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006100 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006101
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006102 /* read timeout : return a 504 to the client. */
6103 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006105 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006106
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006107 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006108 if (objt_server(s->target)) {
6109 objt_server(s->target)->counters.failed_resp++;
6110 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006111 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006112
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006113 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006114 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006115 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006116 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006117 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006118 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006119
Willy Tarreaue7dff022015-04-03 01:14:29 +02006120 if (!(s->flags & SF_ERR_MASK))
6121 s->flags |= SF_ERR_SRVTO;
6122 if (!(s->flags & SF_FINST_MASK))
6123 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006124 return 0;
6125 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006126
Willy Tarreauf003d372012-11-26 13:35:37 +01006127 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006128 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006129 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006130 s->be->be_counters.cli_aborts++;
6131 if (objt_server(s->target))
6132 objt_server(s->target)->counters.cli_aborts++;
6133
Christopher Faulet0184ea72017-01-05 14:06:34 +01006134 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006135 channel_auto_close(rep);
6136
6137 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006138 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006139 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006140
Willy Tarreaue7dff022015-04-03 01:14:29 +02006141 if (!(s->flags & SF_ERR_MASK))
6142 s->flags |= SF_ERR_CLICL;
6143 if (!(s->flags & SF_FINST_MASK))
6144 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006145
Willy Tarreau87b09662015-04-03 00:22:06 +02006146 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006147 return 0;
6148 }
6149
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006150 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006151 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006152 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006153 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006154 else if (txn->flags & TX_NOT_FIRST)
6155 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006156
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006157 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006158 if (objt_server(s->target)) {
6159 objt_server(s->target)->counters.failed_resp++;
6160 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006161 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006162
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006163 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006164 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006165 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006166 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006167 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006168 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006169
Willy Tarreaue7dff022015-04-03 01:14:29 +02006170 if (!(s->flags & SF_ERR_MASK))
6171 s->flags |= SF_ERR_SRVCL;
6172 if (!(s->flags & SF_FINST_MASK))
6173 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006174 return 0;
6175 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006176
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006177 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006178 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006179 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006180 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006181 else if (txn->flags & TX_NOT_FIRST)
6182 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006183
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006184 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006185 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006186 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006187
Willy Tarreaue7dff022015-04-03 01:14:29 +02006188 if (!(s->flags & SF_ERR_MASK))
6189 s->flags |= SF_ERR_CLICL;
6190 if (!(s->flags & SF_FINST_MASK))
6191 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006192
Willy Tarreau87b09662015-04-03 00:22:06 +02006193 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006194 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006195 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006196
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006197 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006198 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006199 return 0;
6200 }
6201
6202 /* More interesting part now : we know that we have a complete
6203 * response which at least looks like HTTP. We have an indicator
6204 * of each header's length, so we can parse them quickly.
6205 */
6206
6207 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006208 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210 /*
6211 * 1: get the status code
6212 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006213 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006214 if (n < 1 || n > 5)
6215 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006216 /* when the client triggers a 4xx from the server, it's most often due
6217 * to a missing object or permission. These events should be tracked
6218 * because if they happen often, it may indicate a brute force or a
6219 * vulnerability scan.
6220 */
6221 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006222 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006223
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006224 if (objt_server(s->target))
6225 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006226
Willy Tarreau91852eb2015-05-01 13:26:00 +02006227 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6228 * exactly one digit "." one digit. This check may be disabled using
6229 * option accept-invalid-http-response.
6230 */
6231 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6232 if (msg->sl.st.v_l != 8) {
6233 msg->err_pos = 0;
6234 goto hdr_response_bad;
6235 }
6236
6237 if (rep->buf->p[4] != '/' ||
6238 !isdigit((unsigned char)rep->buf->p[5]) ||
6239 rep->buf->p[6] != '.' ||
6240 !isdigit((unsigned char)rep->buf->p[7])) {
6241 msg->err_pos = 4;
6242 goto hdr_response_bad;
6243 }
6244 }
6245
Willy Tarreau5b154472009-12-21 20:11:07 +01006246 /* check if the response is HTTP/1.1 or above */
6247 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006248 ((rep->buf->p[5] > '1') ||
6249 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006250 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006251
6252 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006253 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 +01006254
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006255 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006256 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006257
Willy Tarreau9b28e032012-10-12 23:49:43 +02006258 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006259
Willy Tarreau39650402010-03-15 19:44:39 +01006260 /* Adjust server's health based on status code. Note: status codes 501
6261 * and 505 are triggered on demand by client request, so we must not
6262 * count them as server failures.
6263 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006264 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006265 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006266 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006267 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006268 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006269 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006270
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006271 /*
6272 * 2: check for cacheability.
6273 */
6274
6275 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006276 case 100:
6277 /*
6278 * We may be facing a 100-continue response, in which case this
6279 * is not the right response, and we're waiting for the next one.
6280 * Let's allow this response to go to the client and wait for the
6281 * next one.
6282 */
6283 hdr_idx_init(&txn->hdr_idx);
6284 msg->next -= channel_forward(rep, msg->next);
6285 msg->msg_state = HTTP_MSG_RPBEFORE;
6286 txn->status = 0;
6287 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006288 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006289 goto next_one;
6290
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006291 case 200:
6292 case 203:
6293 case 206:
6294 case 300:
6295 case 301:
6296 case 410:
6297 /* RFC2616 @13.4:
6298 * "A response received with a status code of
6299 * 200, 203, 206, 300, 301 or 410 MAY be stored
6300 * by a cache (...) unless a cache-control
6301 * directive prohibits caching."
6302 *
6303 * RFC2616 @9.5: POST method :
6304 * "Responses to this method are not cacheable,
6305 * unless the response includes appropriate
6306 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006307 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006308 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006309 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006310 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6311 break;
6312 default:
6313 break;
6314 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006315
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006316 /*
6317 * 3: we may need to capture headers
6318 */
6319 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006320 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006321 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006322 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006323
Willy Tarreau557f1992015-05-01 10:05:17 +02006324 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6325 * by RFC7230#3.3.3 :
6326 *
6327 * The length of a message body is determined by one of the following
6328 * (in order of precedence):
6329 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006330 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6331 * the connection will become a tunnel immediately after the empty
6332 * line that concludes the header fields. A client MUST ignore
6333 * any Content-Length or Transfer-Encoding header fields received
6334 * in such a message. Any 101 response (Switching Protocols) is
6335 * managed in the same manner.
6336 *
6337 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006338 * (Informational), 204 (No Content), or 304 (Not Modified) status
6339 * code is always terminated by the first empty line after the
6340 * header fields, regardless of the header fields present in the
6341 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006342 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006343 * 3. If a Transfer-Encoding header field is present and the chunked
6344 * transfer coding (Section 4.1) is the final encoding, the message
6345 * body length is determined by reading and decoding the chunked
6346 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006347 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006348 * If a Transfer-Encoding header field is present in a response and
6349 * the chunked transfer coding is not the final encoding, the
6350 * message body length is determined by reading the connection until
6351 * it is closed by the server. If a Transfer-Encoding header field
6352 * is present in a request and the chunked transfer coding is not
6353 * the final encoding, the message body length cannot be determined
6354 * reliably; the server MUST respond with the 400 (Bad Request)
6355 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006357 * If a message is received with both a Transfer-Encoding and a
6358 * Content-Length header field, the Transfer-Encoding overrides the
6359 * Content-Length. Such a message might indicate an attempt to
6360 * perform request smuggling (Section 9.5) or response splitting
6361 * (Section 9.4) and ought to be handled as an error. A sender MUST
6362 * remove the received Content-Length field prior to forwarding such
6363 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006364 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006365 * 4. If a message is received without Transfer-Encoding and with
6366 * either multiple Content-Length header fields having differing
6367 * field-values or a single Content-Length header field having an
6368 * invalid value, then the message framing is invalid and the
6369 * recipient MUST treat it as an unrecoverable error. If this is a
6370 * request message, the server MUST respond with a 400 (Bad Request)
6371 * status code and then close the connection. If this is a response
6372 * message received by a proxy, the proxy MUST close the connection
6373 * to the server, discard the received response, and send a 502 (Bad
6374 * Gateway) response to the client. If this is a response message
6375 * received by a user agent, the user agent MUST close the
6376 * connection to the server and discard the received response.
6377 *
6378 * 5. If a valid Content-Length header field is present without
6379 * Transfer-Encoding, its decimal value defines the expected message
6380 * body length in octets. If the sender closes the connection or
6381 * the recipient times out before the indicated number of octets are
6382 * received, the recipient MUST consider the message to be
6383 * incomplete and close the connection.
6384 *
6385 * 6. If this is a request message and none of the above are true, then
6386 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006387 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006388 * 7. Otherwise, this is a response message without a declared message
6389 * body length, so the message body length is determined by the
6390 * number of octets received prior to the server closing the
6391 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006392 */
6393
6394 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006395 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006396 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006397 * FIXME: should we parse anyway and return an error on chunked encoding ?
6398 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006399 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6400 txn->status == 101)) {
6401 /* Either we've established an explicit tunnel, or we're
6402 * switching the protocol. In both cases, we're very unlikely
6403 * to understand the next protocols. We have to switch to tunnel
6404 * mode, so that we transfer the request and responses then let
6405 * this protocol pass unmodified. When we later implement specific
6406 * parsers for such protocols, we'll want to check the Upgrade
6407 * header which contains information about that protocol for
6408 * responses with status 101 (eg: see RFC2817 about TLS).
6409 */
6410 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6411 msg->flags |= HTTP_MSGF_XFER_LEN;
6412 goto end;
6413 }
6414
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006415 if (txn->meth == HTTP_METH_HEAD ||
6416 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006417 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006418 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006419 goto skip_content_length;
6420 }
6421
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006422 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006423 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006424 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006425 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006426 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6427 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006428 /* bad transfer-encoding (chunked followed by something else) */
6429 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006430 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006431 break;
6432 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006433 }
6434
Willy Tarreau1c913912015-04-30 10:57:51 +02006435 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006436 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006437 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006438 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6439 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6440 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006441 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442 signed long long cl;
6443
Willy Tarreauad14f752011-09-02 20:33:27 +02006444 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006445 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006446 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006447 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006448
Willy Tarreauad14f752011-09-02 20:33:27 +02006449 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006450 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006451 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006452 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006453
Willy Tarreauad14f752011-09-02 20:33:27 +02006454 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006455 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006456 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006457 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006458
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006459 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006460 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006461 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006462 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006463
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006464 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006465 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006466 }
6467
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006468 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006469 /* Now we have to check if we need to modify the Connection header.
6470 * This is more difficult on the response than it is on the request,
6471 * because we can have two different HTTP versions and we don't know
6472 * how the client will interprete a response. For instance, let's say
6473 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6474 * HTTP/1.1 response without any header. Maybe it will bound itself to
6475 * HTTP/1.0 because it only knows about it, and will consider the lack
6476 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6477 * the lack of header as a keep-alive. Thus we will use two flags
6478 * indicating how a request MAY be understood by the client. In case
6479 * of multiple possibilities, we'll fix the header to be explicit. If
6480 * ambiguous cases such as both close and keepalive are seen, then we
6481 * will fall back to explicit close. Note that we won't take risks with
6482 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006483 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006484 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006485 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6486 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6487 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6488 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006489 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006490
Willy Tarreau70dffda2014-01-30 03:07:23 +01006491 /* this situation happens when combining pretend-keepalive with httpclose. */
6492 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006493 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006494 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006495 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6496
Willy Tarreau60466522010-01-18 19:08:45 +01006497 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006498 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006499 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6500 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006501
Willy Tarreau60466522010-01-18 19:08:45 +01006502 /* now adjust header transformations depending on current state */
6503 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6504 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6505 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006506 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006507 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006508 }
Willy Tarreau60466522010-01-18 19:08:45 +01006509 else { /* SCL / KAL */
6510 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006511 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006512 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006513 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006514
Willy Tarreau60466522010-01-18 19:08:45 +01006515 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006516 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006517
Willy Tarreau60466522010-01-18 19:08:45 +01006518 /* Some keep-alive responses are converted to Server-close if
6519 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006520 */
Willy Tarreau60466522010-01-18 19:08:45 +01006521 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6522 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006523 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006524 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006525 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006526 }
6527
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006528 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006529 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006530 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006531
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006532 /* end of job, return OK */
6533 rep->analysers &= ~an_bit;
6534 rep->analyse_exp = TICK_ETERNITY;
6535 channel_auto_close(rep);
6536 return 1;
6537
6538 abort_keep_alive:
6539 /* A keep-alive request to the server failed on a network error.
6540 * The client is required to retry. We need to close without returning
6541 * any other information so that the client retries.
6542 */
6543 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006544 rep->analysers &= AN_RES_FLT_END;
6545 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006546 channel_auto_close(rep);
6547 s->logs.logwait = 0;
6548 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006549 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006550 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006551 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006552 return 0;
6553}
6554
6555/* This function performs all the processing enabled for the current response.
6556 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006557 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006558 * other functions. It works like process_request (see indications above).
6559 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006560int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006561{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006562 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006563 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006564 struct http_msg *msg = &txn->rsp;
6565 struct proxy *cur_proxy;
6566 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006567 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006568
Willy Tarreau87b09662015-04-03 00:22:06 +02006569 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 +02006570 now_ms, __FUNCTION__,
6571 s,
6572 rep,
6573 rep->rex, rep->wex,
6574 rep->flags,
6575 rep->buf->i,
6576 rep->analysers);
6577
6578 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6579 return 0;
6580
Willy Tarreau70730dd2014-04-24 18:06:27 +02006581 /* The stats applet needs to adjust the Connection header but we don't
6582 * apply any filter there.
6583 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006584 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6585 rep->analysers &= ~an_bit;
6586 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006587 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006588 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006589
Willy Tarreau58975672014-04-24 21:13:57 +02006590 /*
6591 * We will have to evaluate the filters.
6592 * As opposed to version 1.2, now they will be evaluated in the
6593 * filters order and not in the header order. This means that
6594 * each filter has to be validated among all headers.
6595 *
6596 * Filters are tried with ->be first, then with ->fe if it is
6597 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006598 *
6599 * Maybe we are in resume condiion. In this case I choose the
6600 * "struct proxy" which contains the rule list matching the resume
6601 * pointer. If none of theses "struct proxy" match, I initialise
6602 * the process with the first one.
6603 *
6604 * In fact, I check only correspondance betwwen the current list
6605 * pointer and the ->fe rule list. If it doesn't match, I initialize
6606 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006607 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006608 if (s->current_rule_list == &sess->fe->http_res_rules)
6609 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006610 else
6611 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006612 while (1) {
6613 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006614
Willy Tarreau58975672014-04-24 21:13:57 +02006615 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006616 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006617 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006618
Willy Tarreau51d861a2015-05-22 17:30:48 +02006619 if (ret == HTTP_RULE_RES_BADREQ)
6620 goto return_srv_prx_502;
6621
6622 if (ret == HTTP_RULE_RES_DONE) {
6623 rep->analysers &= ~an_bit;
6624 rep->analyse_exp = TICK_ETERNITY;
6625 return 1;
6626 }
6627 }
6628
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006629 /* we need to be called again. */
6630 if (ret == HTTP_RULE_RES_YIELD) {
6631 channel_dont_close(rep);
6632 return 0;
6633 }
6634
Willy Tarreau58975672014-04-24 21:13:57 +02006635 /* try headers filters */
6636 if (rule_set->rsp_exp != NULL) {
6637 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6638 return_bad_resp:
6639 if (objt_server(s->target)) {
6640 objt_server(s->target)->counters.failed_resp++;
6641 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006642 }
Willy Tarreau58975672014-04-24 21:13:57 +02006643 s->be->be_counters.failed_resp++;
6644 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006645 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006646 txn->status = 502;
6647 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006648 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006649 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006650 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006651 if (!(s->flags & SF_ERR_MASK))
6652 s->flags |= SF_ERR_PRXCOND;
6653 if (!(s->flags & SF_FINST_MASK))
6654 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006655 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006656 }
Willy Tarreau58975672014-04-24 21:13:57 +02006657 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006658
Willy Tarreau58975672014-04-24 21:13:57 +02006659 /* has the response been denied ? */
6660 if (txn->flags & TX_SVDENY) {
6661 if (objt_server(s->target))
6662 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006663
Willy Tarreau58975672014-04-24 21:13:57 +02006664 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006665 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006666 if (sess->listener->counters)
6667 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006668
Willy Tarreau58975672014-04-24 21:13:57 +02006669 goto return_srv_prx_502;
6670 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006671
Willy Tarreau58975672014-04-24 21:13:57 +02006672 /* add response headers from the rule sets in the same order */
6673 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006674 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006675 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006676 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006677 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006678 ret = acl_pass(ret);
6679 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6680 ret = !ret;
6681 if (!ret)
6682 continue;
6683 }
6684 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6685 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006686 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006689 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006690 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006691 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006692 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006693
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006694 /* After this point, this anayzer can't return yield, so we can
6695 * remove the bit corresponding to this analyzer from the list.
6696 *
6697 * Note that the intermediate returns and goto found previously
6698 * reset the analyzers.
6699 */
6700 rep->analysers &= ~an_bit;
6701 rep->analyse_exp = TICK_ETERNITY;
6702
Willy Tarreau58975672014-04-24 21:13:57 +02006703 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006704 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006705 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006706
Willy Tarreau58975672014-04-24 21:13:57 +02006707 /*
6708 * Now check for a server cookie.
6709 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006710 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006711 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006712
Willy Tarreau58975672014-04-24 21:13:57 +02006713 /*
6714 * Check for cache-control or pragma headers if required.
6715 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006716 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 +02006717 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006718
Willy Tarreau58975672014-04-24 21:13:57 +02006719 /*
6720 * Add server cookie in the response if needed
6721 */
6722 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6723 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006724 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006725 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6726 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6727 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6728 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6729 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006730 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006731 /* the server is known, it's not the one the client requested, or the
6732 * cookie's last seen date needs to be refreshed. We have to
6733 * insert a set-cookie here, except if we want to insert only on POST
6734 * requests and this one isn't. Note that servers which don't have cookies
6735 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736 */
Willy Tarreau58975672014-04-24 21:13:57 +02006737 if (!objt_server(s->target)->cookie) {
6738 chunk_printf(&trash,
6739 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6740 s->be->cookie_name);
6741 }
6742 else {
6743 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744
Willy Tarreau58975672014-04-24 21:13:57 +02006745 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6746 /* emit last_date, which is mandatory */
6747 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6748 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6749 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006750
Willy Tarreau58975672014-04-24 21:13:57 +02006751 if (s->be->cookie_maxlife) {
6752 /* emit first_date, which is either the original one or
6753 * the current date.
6754 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006755 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006756 s30tob64(txn->cookie_first_date ?
6757 txn->cookie_first_date >> 2 :
6758 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006759 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006760 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006761 }
Willy Tarreau58975672014-04-24 21:13:57 +02006762 chunk_appendf(&trash, "; path=/");
6763 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (s->be->cookie_domain)
6766 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 if (s->be->ck_opts & PR_CK_HTTPONLY)
6769 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 if (s->be->ck_opts & PR_CK_SECURE)
6772 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006773
Willy Tarreau58975672014-04-24 21:13:57 +02006774 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6775 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006776
Willy Tarreau58975672014-04-24 21:13:57 +02006777 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006778 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006779 /* the server did not change, only the date was updated */
6780 txn->flags |= TX_SCK_UPDATED;
6781 else
6782 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006783
Willy Tarreau58975672014-04-24 21:13:57 +02006784 /* Here, we will tell an eventual cache on the client side that we don't
6785 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6786 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6787 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006788 */
Willy Tarreau58975672014-04-24 21:13:57 +02006789 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006792
Willy Tarreau58975672014-04-24 21:13:57 +02006793 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6794 "Cache-control: private", 22) < 0))
6795 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006796 }
Willy Tarreau58975672014-04-24 21:13:57 +02006797 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798
Willy Tarreau58975672014-04-24 21:13:57 +02006799 /*
6800 * Check if result will be cacheable with a cookie.
6801 * We'll block the response if security checks have caught
6802 * nasty things such as a cacheable cookie.
6803 */
6804 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6805 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6806 (s->be->options & PR_O_CHK_CACHE)) {
6807 /* we're in presence of a cacheable response containing
6808 * a set-cookie header. We'll block it as requested by
6809 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 */
Willy Tarreau58975672014-04-24 21:13:57 +02006811 if (objt_server(s->target))
6812 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006813
Willy Tarreau58975672014-04-24 21:13:57 +02006814 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006815 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006816 if (sess->listener->counters)
6817 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818
Willy Tarreau58975672014-04-24 21:13:57 +02006819 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6820 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6821 send_log(s->be, LOG_ALERT,
6822 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6823 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6824 goto return_srv_prx_502;
6825 }
Willy Tarreau03945942009-12-22 16:50:27 +01006826
Willy Tarreau70730dd2014-04-24 18:06:27 +02006827 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006828 /*
6829 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6830 * If an "Upgrade" token is found, the header is left untouched in order
6831 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006832 * if anything but "Upgrade" is present in the Connection header. We don't
6833 * want to touch any 101 response either since it's switching to another
6834 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006835 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006836 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006837 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006838 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006839 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6840 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006841
Willy Tarreau58975672014-04-24 21:13:57 +02006842 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6843 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6844 /* we want a keep-alive response here. Keep-alive header
6845 * required if either side is not 1.1.
6846 */
6847 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6848 want_flags |= TX_CON_KAL_SET;
6849 }
6850 else {
6851 /* we want a close response here. Close header required if
6852 * the server is 1.1, regardless of the client.
6853 */
6854 if (msg->flags & HTTP_MSGF_VER_11)
6855 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006856 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857
Willy Tarreau58975672014-04-24 21:13:57 +02006858 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6859 http_change_connection_header(txn, msg, want_flags);
6860 }
6861
6862 skip_header_mangling:
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006863 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006864 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006865 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006866 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006867 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868
Willy Tarreau58975672014-04-24 21:13:57 +02006869 /* if the user wants to log as soon as possible, without counting
6870 * bytes from the server, then this is the right moment. We have
6871 * to temporarily assign bytes_out to log what we currently have.
6872 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006873 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006874 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6875 s->logs.bytes_out = txn->rsp.eoh;
6876 s->do_log(s);
6877 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006878 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006879 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006880}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881
Willy Tarreaud98cf932009-12-27 22:54:55 +01006882/* This function is an analyser which forwards response body (including chunk
6883 * sizes if any). It is called as soon as we must forward, even if we forward
6884 * zero byte. The only situation where it must not be called is when we're in
6885 * tunnel mode and we want to forward till the close. It's used both to forward
6886 * remaining data and to resync after end of body. It expects the msg_state to
6887 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006888 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006889 *
6890 * It is capable of compressing response data both in content-length mode and
6891 * in chunked mode. The state machines follows different flows depending on
6892 * whether content-length and chunked modes are used, since there are no
6893 * trailers in content-length :
6894 *
6895 * chk-mode cl-mode
6896 * ,----- BODY -----.
6897 * / \
6898 * V size > 0 V chk-mode
6899 * .--> SIZE -------------> DATA -------------> CRLF
6900 * | | size == 0 | last byte |
6901 * | v final crlf v inspected |
6902 * | TRAILERS -----------> DONE |
6903 * | |
6904 * `----------------------------------------------'
6905 *
6906 * Compression only happens in the DATA state, and must be flushed in final
6907 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6908 * is performed at once on final states for all bytes parsed, or when leaving
6909 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006910 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006911int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006912{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006913 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006914 struct http_txn *txn = s->txn;
6915 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006916 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006917
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006918 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6919 return 0;
6920
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006921 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006922 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006923 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006924 /* Output closed while we were sending data. We must abort and
6925 * wake the other side up.
6926 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006927 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006928 msg->msg_state = HTTP_MSG_ERROR;
6929 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006930 return 1;
6931 }
6932
Willy Tarreau4fe41902010-06-07 22:27:41 +02006933 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006934 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006935
Christopher Fauletd7c91962015-04-30 11:48:27 +02006936 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006937 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6938 ? HTTP_MSG_CHUNK_SIZE
6939 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006940 }
6941
Willy Tarreauefdf0942014-04-24 20:08:57 +02006942 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006943 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006944 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006945 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006946 }
6947
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006948 if (msg->msg_state < HTTP_MSG_DONE) {
6949 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6950 ? http_msg_forward_chunked_body(s, msg)
6951 : http_msg_forward_body(s, msg));
6952 if (!ret)
6953 goto missing_data_or_waiting;
6954 if (ret < 0)
6955 goto return_bad_res;
6956 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006957
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006958 /* other states, DONE...TUNNEL */
6959 /* for keep-alive we don't want to forward closes on DONE */
6960 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6961 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6962 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006963
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006964 if (http_resync_states(s)) {
6965 /* some state changes occurred, maybe the analyser was disabled
6966 * too. */
6967 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6968 if (res->flags & CF_SHUTW) {
6969 /* response errors are most likely due to the
6970 * client aborting the transfer. */
6971 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006972 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006973 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006974 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006975 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006976 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006977 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006978 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006979 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006980
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006981 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006982 if (res->flags & CF_SHUTW)
6983 goto aborted_xfer;
6984
6985 /* stop waiting for data if the input is closed before the end. If the
6986 * client side was already closed, it means that the client has aborted,
6987 * so we don't want to count this as a server abort. Otherwise it's a
6988 * server abort.
6989 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006990 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006991 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006992 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006993 /* If we have some pending data, we continue the processing */
6994 if (!buffer_pending(res->buf)) {
6995 if (!(s->flags & SF_ERR_MASK))
6996 s->flags |= SF_ERR_SRVCL;
6997 s->be->be_counters.srv_aborts++;
6998 if (objt_server(s->target))
6999 objt_server(s->target)->counters.srv_aborts++;
7000 goto return_bad_res_stats_ok;
7001 }
Willy Tarreau40dba092010-03-04 18:14:51 +01007002 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007003
Willy Tarreau40dba092010-03-04 18:14:51 +01007004 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007005 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007006 goto return_bad_res;
7007
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007008 /* When TE: chunked is used, we need to get there again to parse
7009 * remaining chunks even if the server has closed, so we don't want to
7010 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
7011 * keep-alive is set on the client side or if there are filters
7012 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007013 */
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007014 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) ||
7015 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007016 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7017 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007018 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007019
Willy Tarreau5c620922011-05-11 19:56:11 +02007020 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007021 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007022 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007023 * modes are already handled by the stream sock layer. We must not do
7024 * this in content-length mode because it could present the MSG_MORE
7025 * flag with the last block of forwarded data, which would cause an
7026 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007027 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007028 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007029 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007030
Willy Tarreau87b09662015-04-03 00:22:06 +02007031 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007032 return 0;
7033
Willy Tarreau40dba092010-03-04 18:14:51 +01007034 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007035 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007036 if (objt_server(s->target))
7037 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007038
7039 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007040 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007041 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007042 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007043 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007044 res->analysers &= AN_RES_FLT_END;
7045 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007046 if (objt_server(s->target))
7047 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007048
Willy Tarreaue7dff022015-04-03 01:14:29 +02007049 if (!(s->flags & SF_ERR_MASK))
7050 s->flags |= SF_ERR_PRXCOND;
7051 if (!(s->flags & SF_FINST_MASK))
7052 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007053 return 0;
7054
7055 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007056 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007057 txn->rsp.msg_state = HTTP_MSG_ERROR;
7058 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007059 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007060 res->analysers &= AN_RES_FLT_END;
7061 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007062
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007063 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007064 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007065 if (objt_server(s->target))
7066 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007067
Willy Tarreaue7dff022015-04-03 01:14:29 +02007068 if (!(s->flags & SF_ERR_MASK))
7069 s->flags |= SF_ERR_CLICL;
7070 if (!(s->flags & SF_FINST_MASK))
7071 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007072 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007073}
7074
7075
7076static inline int
7077http_msg_forward_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 HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7083
7084 if (msg->msg_state == HTTP_MSG_ENDING)
7085 goto ending;
7086
7087 /* Neither content-length, nor transfer-encoding was found, so we must
7088 * read the body until the server connection is closed. In that case, we
7089 * eat data as they come. Of course, this happens for response only. */
7090 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7091 unsigned long long len = (chn->buf->i - msg->next);
7092 msg->chunk_len += len;
7093 msg->body_len += len;
7094 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007095 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7096 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7097 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007098 msg->next += ret;
7099 msg->chunk_len -= ret;
7100 if (msg->chunk_len) {
7101 /* input empty or output full */
7102 if (chn->buf->i > msg->next)
7103 chn->flags |= CF_WAKE_WRITE;
7104 goto missing_data_or_waiting;
7105 }
7106
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007107 /* The server still sending data that should be filtered */
7108 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007109 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007110
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007111 msg->msg_state = HTTP_MSG_ENDING;
7112
7113 ending:
7114 /* we may have some pending data starting at res->buf->p such as a last
7115 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007116 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7117 /* default_ret */ msg->next,
7118 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007119 b_adv(chn->buf, ret);
7120 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007121 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7122 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007123 if (msg->next)
7124 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007125
Christopher Fauletda02e172015-12-04 09:25:05 +01007126 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7127 /* default_ret */ 1,
7128 /* on_error */ goto error,
7129 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007130 msg->msg_state = HTTP_MSG_DONE;
7131 return 1;
7132
7133 missing_data_or_waiting:
7134 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007135 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7136 /* default_ret */ msg->next,
7137 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007138 b_adv(chn->buf, ret);
7139 msg->next -= ret;
7140 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7141 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007142 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007143 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007144 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007145 return 0;
7146 error:
7147 return -1;
7148}
7149
7150static inline int
7151http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7152{
7153 struct channel *chn = msg->chn;
7154 int ret;
7155
7156 /* Here we have the guarantee to be in one of the following state:
7157 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7158 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7159
7160 switch_states:
7161 switch (msg->msg_state) {
7162 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007163 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7164 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7165 /* on_error */ goto error);
7166 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007167 msg->chunk_len -= ret;
7168 if (msg->chunk_len) {
7169 /* input empty or output full */
7170 if (chn->buf->i > msg->next)
7171 chn->flags |= CF_WAKE_WRITE;
7172 goto missing_data_or_waiting;
7173 }
7174
7175 /* nothing left to forward for this chunk*/
7176 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7177 /* fall through for HTTP_MSG_CHUNK_CRLF */
7178
7179 case HTTP_MSG_CHUNK_CRLF:
7180 /* we want the CRLF after the data */
7181 ret = http_skip_chunk_crlf(msg);
7182 if (ret == 0)
7183 goto missing_data_or_waiting;
7184 if (ret < 0)
7185 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007186 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007187 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7188 /* fall through for HTTP_MSG_CHUNK_SIZE */
7189
7190 case HTTP_MSG_CHUNK_SIZE:
7191 /* read the chunk size and assign it to ->chunk_len,
7192 * then set ->next to point to the body and switch to
7193 * DATA or TRAILERS state.
7194 */
7195 ret = http_parse_chunk_size(msg);
7196 if (ret == 0)
7197 goto missing_data_or_waiting;
7198 if (ret < 0)
7199 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007200 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007201 if (msg->chunk_len) {
7202 msg->msg_state = HTTP_MSG_DATA;
7203 goto switch_states;
7204 }
7205 msg->msg_state = HTTP_MSG_TRAILERS;
7206 /* fall through for HTTP_MSG_TRAILERS */
7207
7208 case HTTP_MSG_TRAILERS:
7209 ret = http_forward_trailers(msg);
7210 if (ret < 0)
7211 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007212 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7213 /* default_ret */ 1,
7214 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007215 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007216 if (!ret)
7217 goto missing_data_or_waiting;
7218 break;
7219
7220 case HTTP_MSG_ENDING:
7221 goto ending;
7222
7223 default:
7224 /* This should no happen in this function */
7225 goto error;
7226 }
7227
7228 msg->msg_state = HTTP_MSG_ENDING;
7229 ending:
7230 /* we may have some pending data starting at res->buf->p such as a last
7231 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007232 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007233 /* default_ret */ msg->next,
7234 /* on_error */ goto error);
7235 b_adv(chn->buf, ret);
7236 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007237 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7238 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007239 if (msg->next)
7240 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007241
Christopher Fauletda02e172015-12-04 09:25:05 +01007242 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007243 /* default_ret */ 1,
7244 /* on_error */ goto error,
7245 /* on_wait */ goto waiting);
7246 msg->msg_state = HTTP_MSG_DONE;
7247 return 1;
7248
7249 missing_data_or_waiting:
7250 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007251 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007252 /* default_ret */ msg->next,
7253 /* on_error */ goto error);
7254 b_adv(chn->buf, ret);
7255 msg->next -= ret;
7256 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7257 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007258 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007259 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007260 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007261 return 0;
7262
7263 chunk_parsing_error:
7264 if (msg->err_pos >= 0) {
7265 if (chn->flags & CF_ISRESP)
7266 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7267 msg->msg_state, strm_fe(s));
7268 else
7269 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7270 msg, msg->msg_state, s->be);
7271 }
7272 error:
7273 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007274}
7275
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277/* Iterate the same filter through all request headers.
7278 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 * Since it can manage the switch to another backend, it updates the per-proxy
7280 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007282int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007283{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 char *cur_ptr, *cur_end, *cur_next;
7285 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007286 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007288 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 last_hdr = 0;
7291
Willy Tarreau9b28e032012-10-12 23:49:43 +02007292 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 old_idx = 0;
7294
7295 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007296 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 (exp->action == ACT_ALLOW ||
7300 exp->action == ACT_DENY ||
7301 exp->action == ACT_TARPIT))
7302 return 0;
7303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007304 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 if (!cur_idx)
7306 break;
7307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007308 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 cur_ptr = cur_next;
7310 cur_end = cur_ptr + cur_hdr->len;
7311 cur_next = cur_end + cur_hdr->cr + 1;
7312
7313 /* Now we have one header between cur_ptr and cur_end,
7314 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 */
7316
Willy Tarreau15a53a42015-01-21 13:39:42 +01007317 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007320 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 last_hdr = 1;
7322 break;
7323
7324 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007325 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 break;
7328
7329 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007330 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 break;
7333
7334 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007335 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7336 if (trash.len < 0)
7337 return -1;
7338
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007339 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007340 /* FIXME: if the user adds a newline in the replacement, the
7341 * index will not be recalculated for now, and the new line
7342 * will not be counted as a new header.
7343 */
7344
7345 cur_end += delta;
7346 cur_next += delta;
7347 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007348 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 break;
7350
7351 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007352 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 cur_next += delta;
7354
Willy Tarreaufa355d42009-11-29 18:12:29 +01007355 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007356 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7357 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358 cur_hdr->len = 0;
7359 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007360 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 break;
7362
7363 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364 }
7365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 /* keep the link from this header to next one in case of later
7367 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369 old_idx = cur_idx;
7370 }
7371 return 0;
7372}
7373
7374
7375/* Apply the filter to the request line.
7376 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7377 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378 * Since it can manage the switch to another backend, it updates the per-proxy
7379 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007381int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007383 char *cur_ptr, *cur_end;
7384 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007385 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007386 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387
Willy Tarreau3d300592007-03-18 18:34:41 +01007388 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007390 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 (exp->action == ACT_ALLOW ||
7392 exp->action == ACT_DENY ||
7393 exp->action == ACT_TARPIT))
7394 return 0;
7395 else if (exp->action == ACT_REMOVE)
7396 return 0;
7397
7398 done = 0;
7399
Willy Tarreau9b28e032012-10-12 23:49:43 +02007400 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007401 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007402
7403 /* Now we have the request line between cur_ptr and cur_end */
7404
Willy Tarreau15a53a42015-01-21 13:39:42 +01007405 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007408 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 done = 1;
7410 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007413 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 done = 1;
7415 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007418 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007419 done = 1;
7420 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007423 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7424 if (trash.len < 0)
7425 return -1;
7426
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007427 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 /* FIXME: if the user adds a newline in the replacement, the
7429 * index will not be recalculated for now, and the new line
7430 * will not be counted as a new header.
7431 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007432
Willy Tarreaufa355d42009-11-29 18:12:29 +01007433 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007435 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 HTTP_MSG_RQMETH,
7437 cur_ptr, cur_end + 1,
7438 NULL, NULL);
7439 if (unlikely(!cur_end))
7440 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 /* we have a full request and we know that we have either a CR
7443 * or an LF at <ptr>.
7444 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007445 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7446 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447 /* there is no point trying this regex on headers */
7448 return 1;
7449 }
7450 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007451 return done;
7452}
Willy Tarreau97de6242006-12-27 17:18:38 +01007453
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454
Willy Tarreau58f10d72006-12-04 02:26:12 +01007455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007456/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007457 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007458 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459 * unparsable request. Since it can manage the switch to another backend, it
7460 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007461 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007462int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007463{
Willy Tarreau192252e2015-04-04 01:47:55 +02007464 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007465 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007466 struct hdr_exp *exp;
7467
7468 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007469 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007471 /*
7472 * The interleaving of transformations and verdicts
7473 * makes it difficult to decide to continue or stop
7474 * the evaluation.
7475 */
7476
Willy Tarreau6c123b12010-01-28 20:22:06 +01007477 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7478 break;
7479
Willy Tarreau3d300592007-03-18 18:34:41 +01007480 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007481 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007482 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007483 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007484
7485 /* if this filter had a condition, evaluate it now and skip to
7486 * next filter if the condition does not match.
7487 */
7488 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007489 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007490 ret = acl_pass(ret);
7491 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7492 ret = !ret;
7493
7494 if (!ret)
7495 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007496 }
7497
7498 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007499 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007500 if (unlikely(ret < 0))
7501 return -1;
7502
7503 if (likely(ret == 0)) {
7504 /* The filter did not match the request, it can be
7505 * iterated through all headers.
7506 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007507 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7508 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007511 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512}
7513
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007514
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515/* Find the end of a cookie value contained between <s> and <e>. It works the
7516 * same way as with headers above except that the semi-colon also ends a token.
7517 * See RFC2965 for more information. Note that it requires a valid header to
7518 * return a valid result.
7519 */
7520char *find_cookie_value_end(char *s, const char *e)
7521{
7522 int quoted, qdpair;
7523
7524 quoted = qdpair = 0;
7525 for (; s < e; s++) {
7526 if (qdpair) qdpair = 0;
7527 else if (quoted) {
7528 if (*s == '\\') qdpair = 1;
7529 else if (*s == '"') quoted = 0;
7530 }
7531 else if (*s == '"') quoted = 1;
7532 else if (*s == ',' || *s == ';') return s;
7533 }
7534 return s;
7535}
7536
7537/* Delete a value in a header between delimiters <from> and <next> in buffer
7538 * <buf>. The number of characters displaced is returned, and the pointer to
7539 * the first delimiter is updated if required. The function tries as much as
7540 * possible to respect the following principles :
7541 * - replace <from> delimiter by the <next> one unless <from> points to a
7542 * colon, in which case <next> is simply removed
7543 * - set exactly one space character after the new first delimiter, unless
7544 * there are not enough characters in the block being moved to do so.
7545 * - remove unneeded spaces before the previous delimiter and after the new
7546 * one.
7547 *
7548 * It is the caller's responsibility to ensure that :
7549 * - <from> points to a valid delimiter or the colon ;
7550 * - <next> points to a valid delimiter or the final CR/LF ;
7551 * - there are non-space chars before <from> ;
7552 * - there is a CR/LF at or after <next>.
7553 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007554int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555{
7556 char *prev = *from;
7557
7558 if (*prev == ':') {
7559 /* We're removing the first value, preserve the colon and add a
7560 * space if possible.
7561 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007562 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007563 next++;
7564 prev++;
7565 if (prev < next)
7566 *prev++ = ' ';
7567
Willy Tarreau2235b262016-11-05 15:50:20 +01007568 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 next++;
7570 } else {
7571 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007572 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 prev--;
7574 *from = prev;
7575
7576 /* copy the delimiter and if possible a space if we're
7577 * not at the end of the line.
7578 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007579 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 *prev++ = *next++;
7581 if (prev + 1 < next)
7582 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007583 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 next++;
7585 }
7586 }
7587 return buffer_replace2(buf, prev, next, NULL, 0);
7588}
7589
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007590/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007591 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 * desirable to call it only when needed. This code is quite complex because
7593 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7594 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007596void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007597{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007598 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007599 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007601 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7603 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007606 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007607 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007609 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007610 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007611 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007613 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 hdr_beg = hdr_next;
7615 hdr_end = hdr_beg + cur_hdr->len;
7616 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 /* We have one full header between hdr_beg and hdr_end, and the
7619 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007620 * "Cookie:" headers.
7621 */
7622
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007624 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007625 old_idx = cur_idx;
7626 continue;
7627 }
7628
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007629 del_from = NULL; /* nothing to be deleted */
7630 preserve_hdr = 0; /* assume we may kill the whole header */
7631
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632 /* Now look for cookies. Conforming to RFC2109, we have to support
7633 * attributes whose name begin with a '$', and associate them with
7634 * the right cookie, if we want to delete this cookie.
7635 * So there are 3 cases for each cookie read :
7636 * 1) it's a special attribute, beginning with a '$' : ignore it.
7637 * 2) it's a server id cookie that we *MAY* want to delete : save
7638 * some pointers on it (last semi-colon, beginning of cookie...)
7639 * 3) it's an application cookie : we *MAY* have to delete a previous
7640 * "special" cookie.
7641 * At the end of loop, if a "special" cookie remains, we may have to
7642 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 * *MUST* delete it.
7644 *
7645 * Note: RFC2965 is unclear about the processing of spaces around
7646 * the equal sign in the ATTR=VALUE form. A careful inspection of
7647 * the RFC explicitly allows spaces before it, and not within the
7648 * tokens (attrs or values). An inspection of RFC2109 allows that
7649 * too but section 10.1.3 lets one think that spaces may be allowed
7650 * after the equal sign too, resulting in some (rare) buggy
7651 * implementations trying to do that. So let's do what servers do.
7652 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7653 * allowed quoted strings in values, with any possible character
7654 * after a backslash, including control chars and delimitors, which
7655 * causes parsing to become ambiguous. Browsers also allow spaces
7656 * within values even without quotes.
7657 *
7658 * We have to keep multiple pointers in order to support cookie
7659 * removal at the beginning, middle or end of header without
7660 * corrupting the header. All of these headers are valid :
7661 *
7662 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7663 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7664 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7665 * | | | | | | | | |
7666 * | | | | | | | | hdr_end <--+
7667 * | | | | | | | +--> next
7668 * | | | | | | +----> val_end
7669 * | | | | | +-----------> val_beg
7670 * | | | | +--------------> equal
7671 * | | | +----------------> att_end
7672 * | | +---------------------> att_beg
7673 * | +--------------------------> prev
7674 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675 */
7676
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7678 /* Iterate through all cookies on this line */
7679
7680 /* find att_beg */
7681 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007682 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 att_beg++;
7684
7685 /* find att_end : this is the first character after the last non
7686 * space before the equal. It may be equal to hdr_end.
7687 */
7688 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 while (equal < hdr_end) {
7691 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007692 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007693 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 continue;
7695 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696 }
7697
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007698 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7699 * is between <att_beg> and <equal>, both may be identical.
7700 */
7701
7702 /* look for end of cookie if there is an equal sign */
7703 if (equal < hdr_end && *equal == '=') {
7704 /* look for the beginning of the value */
7705 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007706 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 val_beg++;
7708
7709 /* find the end of the value, respecting quotes */
7710 next = find_cookie_value_end(val_beg, hdr_end);
7711
7712 /* make val_end point to the first white space or delimitor after the value */
7713 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007714 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 val_end--;
7716 } else {
7717 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007718 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007719
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 /* We have nothing to do with attributes beginning with '$'. However,
7721 * they will automatically be removed if a header before them is removed,
7722 * since they're supposed to be linked together.
7723 */
7724 if (*att_beg == '$')
7725 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007727 /* Ignore cookies with no equal sign */
7728 if (equal == next) {
7729 /* This is not our cookie, so we must preserve it. But if we already
7730 * scheduled another cookie for removal, we cannot remove the
7731 * complete header, but we can remove the previous block itself.
7732 */
7733 preserve_hdr = 1;
7734 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007735 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 val_end += delta;
7737 next += delta;
7738 hdr_end += delta;
7739 hdr_next += delta;
7740 cur_hdr->len += delta;
7741 http_msg_move_end(&txn->req, delta);
7742 prev = del_from;
7743 del_from = NULL;
7744 }
7745 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007746 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 /* if there are spaces around the equal sign, we need to
7749 * strip them otherwise we'll get trouble for cookie captures,
7750 * or even for rewrites. Since this happens extremely rarely,
7751 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7754 int stripped_before = 0;
7755 int stripped_after = 0;
7756
7757 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007758 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 equal += stripped_before;
7760 val_beg += stripped_before;
7761 }
7762
7763 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007764 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 val_beg += stripped_after;
7766 stripped_before += stripped_after;
7767 }
7768
7769 val_end += stripped_before;
7770 next += stripped_before;
7771 hdr_end += stripped_before;
7772 hdr_next += stripped_before;
7773 cur_hdr->len += stripped_before;
7774 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007775 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 /* First, let's see if we want to capture this cookie. We check
7779 * that we don't already have a client side cookie, because we
7780 * can only capture one. Also as an optimisation, we ignore
7781 * cookies shorter than the declared name.
7782 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007783 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7784 (val_end - att_beg >= sess->fe->capture_namelen) &&
7785 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 int log_len = val_end - att_beg;
7787
7788 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7789 Alert("HTTP logging : out of memory.\n");
7790 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007791 if (log_len > sess->fe->capture_len)
7792 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 memcpy(txn->cli_cookie, att_beg, log_len);
7794 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007795 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 /* Persistence cookies in passive, rewrite or insert mode have the
7799 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007800 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007801 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007802 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007803 * For cookies in prefix mode, the form is :
7804 *
7805 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007806 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007807 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7808 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7809 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007811
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007812 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7813 * have the server ID between val_beg and delim, and the original cookie between
7814 * delim+1 and val_end. Otherwise, delim==val_end :
7815 *
7816 * Cookie: NAME=SRV; # in all but prefix modes
7817 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7818 * | || || | |+-> next
7819 * | || || | +--> val_end
7820 * | || || +---------> delim
7821 * | || |+------------> val_beg
7822 * | || +-------------> att_end = equal
7823 * | |+-----------------> att_beg
7824 * | +------------------> prev
7825 * +-------------------------> hdr_beg
7826 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007827
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007828 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007829 for (delim = val_beg; delim < val_end; delim++)
7830 if (*delim == COOKIE_DELIM)
7831 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007832 } else {
7833 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007834 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007835 /* Now check if the cookie contains a date field, which would
7836 * appear after a vertical bar ('|') just after the server name
7837 * and before the delimiter.
7838 */
7839 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7840 if (vbar1) {
7841 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007842 * right is the last seen date. It is a base64 encoded
7843 * 30-bit value representing the UNIX date since the
7844 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007845 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007846 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007847 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007848 if (val_end - vbar1 >= 5) {
7849 val = b64tos30(vbar1);
7850 if (val > 0)
7851 txn->cookie_last_date = val << 2;
7852 }
7853 /* look for a second vertical bar */
7854 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7855 if (vbar1 && (val_end - vbar1 > 5)) {
7856 val = b64tos30(vbar1 + 1);
7857 if (val > 0)
7858 txn->cookie_first_date = val << 2;
7859 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007860 }
7861 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007862
Willy Tarreauf64d1412010-10-07 20:06:11 +02007863 /* if the cookie has an expiration date and the proxy wants to check
7864 * it, then we do that now. We first check if the cookie is too old,
7865 * then only if it has expired. We detect strict overflow because the
7866 * time resolution here is not great (4 seconds). Cookies with dates
7867 * in the future are ignored if their offset is beyond one day. This
7868 * allows an admin to fix timezone issues without expiring everyone
7869 * and at the same time avoids keeping unwanted side effects for too
7870 * long.
7871 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007872 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7873 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007874 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007875 txn->flags &= ~TX_CK_MASK;
7876 txn->flags |= TX_CK_OLD;
7877 delim = val_beg; // let's pretend we have not found the cookie
7878 txn->cookie_first_date = 0;
7879 txn->cookie_last_date = 0;
7880 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007881 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7882 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007883 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007884 txn->flags &= ~TX_CK_MASK;
7885 txn->flags |= TX_CK_EXPIRED;
7886 delim = val_beg; // let's pretend we have not found the cookie
7887 txn->cookie_first_date = 0;
7888 txn->cookie_last_date = 0;
7889 }
7890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 /* Here, we'll look for the first running server which supports the cookie.
7892 * This allows to share a same cookie between several servers, for example
7893 * to dedicate backup servers to specific servers only.
7894 * However, to prevent clients from sticking to cookie-less backup server
7895 * when they have incidentely learned an empty cookie, we simply ignore
7896 * empty cookies and mark them as invalid.
7897 * The same behaviour is applied when persistence must be ignored.
7898 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007899 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007900 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007901
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007902 while (srv) {
7903 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7904 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007905 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007906 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007907 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007908 /* we found the server and we can use it */
7909 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007910 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007911 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007912 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007913 break;
7914 } else {
7915 /* we found a server, but it's down,
7916 * mark it as such and go on in case
7917 * another one is available.
7918 */
7919 txn->flags &= ~TX_CK_MASK;
7920 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007923 srv = srv->next;
7924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925
Willy Tarreauf64d1412010-10-07 20:06:11 +02007926 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007927 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007928 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007929 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007930 txn->flags |= TX_CK_UNUSED;
7931 else
7932 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007933 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007935 /* depending on the cookie mode, we may have to either :
7936 * - delete the complete cookie if we're in insert+indirect mode, so that
7937 * the server never sees it ;
7938 * - remove the server id from the cookie value, and tag the cookie as an
7939 * application cookie so that it does not get accidentely removed later,
7940 * if we're in cookie prefix mode
7941 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007942 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007943 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944
Willy Tarreau9b28e032012-10-12 23:49:43 +02007945 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007946 val_end += delta;
7947 next += delta;
7948 hdr_end += delta;
7949 hdr_next += delta;
7950 cur_hdr->len += delta;
7951 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 del_from = NULL;
7954 preserve_hdr = 1; /* we want to keep this cookie */
7955 }
7956 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007957 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007958 del_from = prev;
7959 }
7960 } else {
7961 /* This is not our cookie, so we must preserve it. But if we already
7962 * scheduled another cookie for removal, we cannot remove the
7963 * complete header, but we can remove the previous block itself.
7964 */
7965 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007968 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007969 if (att_beg >= del_from)
7970 att_beg += delta;
7971 if (att_end >= del_from)
7972 att_end += delta;
7973 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007974 val_end += delta;
7975 next += delta;
7976 hdr_end += delta;
7977 hdr_next += delta;
7978 cur_hdr->len += delta;
7979 http_msg_move_end(&txn->req, delta);
7980 prev = del_from;
7981 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007983 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007984
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007985 /* continue with next cookie on this header line */
7986 att_beg = next;
7987 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007989 /* There are no more cookies on this line.
7990 * We may still have one (or several) marked for deletion at the
7991 * end of the line. We must do this now in two ways :
7992 * - if some cookies must be preserved, we only delete from the
7993 * mark to the end of line ;
7994 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007995 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007996 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007998 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008000 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008001 cur_hdr->len += delta;
8002 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008003 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008004
8005 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01008006 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8007 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008008 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008009 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008010 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008011 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008012 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01008013 }
8014
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008015 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01008016 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02008017 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008018}
8019
8020
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021/* Iterate the same filter through all response headers contained in <rtr>.
8022 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008024int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 char *cur_ptr, *cur_end, *cur_next;
8027 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008028 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008030 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031
8032 last_hdr = 0;
8033
Willy Tarreau9b28e032012-10-12 23:49:43 +02008034 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 old_idx = 0;
8036
8037 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008038 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008040 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 (exp->action == ACT_ALLOW ||
8042 exp->action == ACT_DENY))
8043 return 0;
8044
8045 cur_idx = txn->hdr_idx.v[old_idx].next;
8046 if (!cur_idx)
8047 break;
8048
8049 cur_hdr = &txn->hdr_idx.v[cur_idx];
8050 cur_ptr = cur_next;
8051 cur_end = cur_ptr + cur_hdr->len;
8052 cur_next = cur_end + cur_hdr->cr + 1;
8053
8054 /* Now we have one header between cur_ptr and cur_end,
8055 * and the next header starts at cur_next.
8056 */
8057
Willy Tarreau15a53a42015-01-21 13:39:42 +01008058 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059 switch (exp->action) {
8060 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008061 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 last_hdr = 1;
8063 break;
8064
8065 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008066 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 last_hdr = 1;
8068 break;
8069
8070 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008071 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8072 if (trash.len < 0)
8073 return -1;
8074
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008075 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 /* FIXME: if the user adds a newline in the replacement, the
8077 * index will not be recalculated for now, and the new line
8078 * will not be counted as a new header.
8079 */
8080
8081 cur_end += delta;
8082 cur_next += delta;
8083 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008084 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 break;
8086
8087 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008088 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 cur_next += delta;
8090
Willy Tarreaufa355d42009-11-29 18:12:29 +01008091 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8093 txn->hdr_idx.used--;
8094 cur_hdr->len = 0;
8095 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008096 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097 break;
8098
8099 }
8100 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101
8102 /* keep the link from this header to next one in case of later
8103 * removal of next header.
8104 */
8105 old_idx = cur_idx;
8106 }
8107 return 0;
8108}
8109
8110
8111/* Apply the filter to the status line in the response buffer <rtr>.
8112 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8113 * or -1 if a replacement resulted in an invalid status line.
8114 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008115int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117 char *cur_ptr, *cur_end;
8118 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008119 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008120 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121
8122
Willy Tarreau3d300592007-03-18 18:34:41 +01008123 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008125 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126 (exp->action == ACT_ALLOW ||
8127 exp->action == ACT_DENY))
8128 return 0;
8129 else if (exp->action == ACT_REMOVE)
8130 return 0;
8131
8132 done = 0;
8133
Willy Tarreau9b28e032012-10-12 23:49:43 +02008134 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008135 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136
8137 /* Now we have the status line between cur_ptr and cur_end */
8138
Willy Tarreau15a53a42015-01-21 13:39:42 +01008139 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 switch (exp->action) {
8141 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008142 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143 done = 1;
8144 break;
8145
8146 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008147 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148 done = 1;
8149 break;
8150
8151 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008152 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8153 if (trash.len < 0)
8154 return -1;
8155
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008156 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 /* FIXME: if the user adds a newline in the replacement, the
8158 * index will not be recalculated for now, and the new line
8159 * will not be counted as a new header.
8160 */
8161
Willy Tarreaufa355d42009-11-29 18:12:29 +01008162 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008164 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008165 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 cur_ptr, cur_end + 1,
8167 NULL, NULL);
8168 if (unlikely(!cur_end))
8169 return -1;
8170
8171 /* we have a full respnse and we know that we have either a CR
8172 * or an LF at <ptr>.
8173 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008174 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008175 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176 /* there is no point trying this regex on headers */
8177 return 1;
8178 }
8179 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 return done;
8181}
8182
8183
8184
8185/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008186 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008187 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8188 * unparsable response.
8189 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008190int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191{
Willy Tarreau192252e2015-04-04 01:47:55 +02008192 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008193 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008194 struct hdr_exp *exp;
8195
8196 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 int ret;
8198
8199 /*
8200 * The interleaving of transformations and verdicts
8201 * makes it difficult to decide to continue or stop
8202 * the evaluation.
8203 */
8204
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008205 if (txn->flags & TX_SVDENY)
8206 break;
8207
Willy Tarreau3d300592007-03-18 18:34:41 +01008208 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8210 exp->action == ACT_PASS)) {
8211 exp = exp->next;
8212 continue;
8213 }
8214
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008215 /* if this filter had a condition, evaluate it now and skip to
8216 * next filter if the condition does not match.
8217 */
8218 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008219 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008220 ret = acl_pass(ret);
8221 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8222 ret = !ret;
8223 if (!ret)
8224 continue;
8225 }
8226
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008228 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229 if (unlikely(ret < 0))
8230 return -1;
8231
8232 if (likely(ret == 0)) {
8233 /* The filter did not match the response, it can be
8234 * iterated through all headers.
8235 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008236 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8237 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008238 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 }
8240 return 0;
8241}
8242
8243
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008245 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 * desirable to call it only when needed. This function is also used when we
8247 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008249void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008251 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008252 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008253 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008254 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 char *hdr_beg, *hdr_end, *hdr_next;
8257 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 /* Iterate through the headers.
8260 * we start with the start line.
8261 */
8262 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008263 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264
8265 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8266 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008267 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268
8269 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 hdr_beg = hdr_next;
8271 hdr_end = hdr_beg + cur_hdr->len;
8272 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 /* We have one full header between hdr_beg and hdr_end, and the
8275 * next header starts at hdr_next. We're only interested in
8276 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277 */
8278
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 is_cookie2 = 0;
8280 prev = hdr_beg + 10;
8281 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008282 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008283 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8284 if (!val) {
8285 old_idx = cur_idx;
8286 continue;
8287 }
8288 is_cookie2 = 1;
8289 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 }
8291
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8293 * <prev> points to the colon.
8294 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008295 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296
Willy Tarreau24581ba2010-08-31 22:39:35 +02008297 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8298 * check-cache is enabled) and we are not interested in checking
8299 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008300 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008301 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008302 return;
8303
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 /* OK so now we know we have to process this response cookie.
8305 * The format of the Set-Cookie header is slightly different
8306 * from the format of the Cookie header in that it does not
8307 * support the comma as a cookie delimiter (thus the header
8308 * cannot be folded) because the Expires attribute described in
8309 * the original Netscape's spec may contain an unquoted date
8310 * with a comma inside. We have to live with this because
8311 * many browsers don't support Max-Age and some browsers don't
8312 * support quoted strings. However the Set-Cookie2 header is
8313 * clean.
8314 *
8315 * We have to keep multiple pointers in order to support cookie
8316 * removal at the beginning, middle or end of header without
8317 * corrupting the header (in case of set-cookie2). A special
8318 * pointer, <scav> points to the beginning of the set-cookie-av
8319 * fields after the first semi-colon. The <next> pointer points
8320 * either to the end of line (set-cookie) or next unquoted comma
8321 * (set-cookie2). All of these headers are valid :
8322 *
8323 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8324 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8325 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8326 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8327 * | | | | | | | | | |
8328 * | | | | | | | | +-> next hdr_end <--+
8329 * | | | | | | | +------------> scav
8330 * | | | | | | +--------------> val_end
8331 * | | | | | +--------------------> val_beg
8332 * | | | | +----------------------> equal
8333 * | | | +------------------------> att_end
8334 * | | +----------------------------> att_beg
8335 * | +------------------------------> prev
8336 * +-----------------------------------------> hdr_beg
8337 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008338
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 for (; prev < hdr_end; prev = next) {
8340 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 /* find att_beg */
8343 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008344 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008345 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008346
Willy Tarreau24581ba2010-08-31 22:39:35 +02008347 /* find att_end : this is the first character after the last non
8348 * space before the equal. It may be equal to hdr_end.
8349 */
8350 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008351
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 while (equal < hdr_end) {
8353 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8354 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008355 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008356 continue;
8357 att_end = equal;
8358 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008359
Willy Tarreau24581ba2010-08-31 22:39:35 +02008360 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8361 * is between <att_beg> and <equal>, both may be identical.
8362 */
8363
8364 /* look for end of cookie if there is an equal sign */
8365 if (equal < hdr_end && *equal == '=') {
8366 /* look for the beginning of the value */
8367 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008368 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008369 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 /* find the end of the value, respecting quotes */
8372 next = find_cookie_value_end(val_beg, hdr_end);
8373
8374 /* make val_end point to the first white space or delimitor after the value */
8375 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008376 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 val_end--;
8378 } else {
8379 /* <equal> points to next comma, semi-colon or EOL */
8380 val_beg = val_end = next = equal;
8381 }
8382
8383 if (next < hdr_end) {
8384 /* Set-Cookie2 supports multiple cookies, and <next> points to
8385 * a colon or semi-colon before the end. So skip all attr-value
8386 * pairs and look for the next comma. For Set-Cookie, since
8387 * commas are permitted in values, skip to the end.
8388 */
8389 if (is_cookie2)
8390 next = find_hdr_value_end(next, hdr_end);
8391 else
8392 next = hdr_end;
8393 }
8394
8395 /* Now everything is as on the diagram above */
8396
8397 /* Ignore cookies with no equal sign */
8398 if (equal == val_end)
8399 continue;
8400
8401 /* If there are spaces around the equal sign, we need to
8402 * strip them otherwise we'll get trouble for cookie captures,
8403 * or even for rewrites. Since this happens extremely rarely,
8404 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008406 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8407 int stripped_before = 0;
8408 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008409
Willy Tarreau24581ba2010-08-31 22:39:35 +02008410 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 equal += stripped_before;
8413 val_beg += stripped_before;
8414 }
8415
8416 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008417 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008418 val_beg += stripped_after;
8419 stripped_before += stripped_after;
8420 }
8421
8422 val_end += stripped_before;
8423 next += stripped_before;
8424 hdr_end += stripped_before;
8425 hdr_next += stripped_before;
8426 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008427 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008428 }
8429
8430 /* First, let's see if we want to capture this cookie. We check
8431 * that we don't already have a server side cookie, because we
8432 * can only capture one. Also as an optimisation, we ignore
8433 * cookies shorter than the declared name.
8434 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008435 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008436 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008437 (val_end - att_beg >= sess->fe->capture_namelen) &&
8438 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008439 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008440 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441 Alert("HTTP logging : out of memory.\n");
8442 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008443 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008444 if (log_len > sess->fe->capture_len)
8445 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008446 memcpy(txn->srv_cookie, att_beg, log_len);
8447 txn->srv_cookie[log_len] = 0;
8448 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008449 }
8450
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008452 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008453 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008454 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8455 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008456 /* assume passive cookie by default */
8457 txn->flags &= ~TX_SCK_MASK;
8458 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459
8460 /* If the cookie is in insert mode on a known server, we'll delete
8461 * this occurrence because we'll insert another one later.
8462 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008463 * a direct access.
8464 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008466 /* The "preserve" flag was set, we don't want to touch the
8467 * server's cookie.
8468 */
8469 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008470 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008471 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008472 /* this cookie must be deleted */
8473 if (*prev == ':' && next == hdr_end) {
8474 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008475 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008476 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8477 txn->hdr_idx.used--;
8478 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008479 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 hdr_next += delta;
8481 http_msg_move_end(&txn->rsp, delta);
8482 /* note: while both invalid now, <next> and <hdr_end>
8483 * are still equal, so the for() will stop as expected.
8484 */
8485 } else {
8486 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008487 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008488 next = prev;
8489 hdr_end += delta;
8490 hdr_next += delta;
8491 cur_hdr->len += delta;
8492 http_msg_move_end(&txn->rsp, delta);
8493 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008494 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008495 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008496 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008499 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 * with this server since we know it.
8501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008502 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008503 next += delta;
8504 hdr_end += delta;
8505 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008506 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008507 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508
Willy Tarreauf1348312010-10-07 15:54:11 +02008509 txn->flags &= ~TX_SCK_MASK;
8510 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008512 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008514 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008515 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008516 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008517 next += delta;
8518 hdr_end += delta;
8519 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008521 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522
Willy Tarreau827aee92011-03-10 16:55:02 +01008523 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008524 txn->flags &= ~TX_SCK_MASK;
8525 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526 }
8527 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008528 /* that's done for this cookie, check the next one on the same
8529 * line when next != hdr_end (only if is_cookie2).
8530 */
8531 }
8532 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008533 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008534 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008535}
8536
8537
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008539 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008540 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008541void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008543 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008544 char *p1, *p2;
8545
8546 char *cur_ptr, *cur_end, *cur_next;
8547 int cur_idx;
8548
Willy Tarreau5df51872007-11-25 16:20:08 +01008549 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008550 return;
8551
8552 /* Iterate through the headers.
8553 * we start with the start line.
8554 */
8555 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008556 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008557
8558 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8559 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008560 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008561
8562 cur_hdr = &txn->hdr_idx.v[cur_idx];
8563 cur_ptr = cur_next;
8564 cur_end = cur_ptr + cur_hdr->len;
8565 cur_next = cur_end + cur_hdr->cr + 1;
8566
8567 /* We have one full header between cur_ptr and cur_end, and the
8568 * next header starts at cur_next. We're only interested in
8569 * "Cookie:" headers.
8570 */
8571
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008572 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8573 if (val) {
8574 if ((cur_end - (cur_ptr + val) >= 8) &&
8575 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8576 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8577 return;
8578 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008579 }
8580
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008581 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8582 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008583 continue;
8584
8585 /* OK, right now we know we have a cache-control header at cur_ptr */
8586
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008587 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008588
8589 if (p1 >= cur_end) /* no more info */
8590 continue;
8591
8592 /* p1 is at the beginning of the value */
8593 p2 = p1;
8594
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008595 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008596 p2++;
8597
8598 /* we have a complete value between p1 and p2 */
8599 if (p2 < cur_end && *p2 == '=') {
8600 /* we have something of the form no-cache="set-cookie" */
8601 if ((cur_end - p1 >= 21) &&
8602 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8603 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008604 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008605 continue;
8606 }
8607
8608 /* OK, so we know that either p2 points to the end of string or to a comma */
8609 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008610 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008611 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8612 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8613 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008614 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008615 return;
8616 }
8617
8618 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008619 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008620 continue;
8621 }
8622 }
8623}
8624
Willy Tarreau58f10d72006-12-04 02:26:12 +01008625
Willy Tarreaub2513902006-12-17 14:52:38 +01008626/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008627 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008628 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008629 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008630 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008631 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008632 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008633 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008634 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008635int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008636{
8637 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008638 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008639 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008640
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008641 if (!uri_auth)
8642 return 0;
8643
Cyril Bonté70be45d2010-10-12 00:14:35 +02008644 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008645 return 0;
8646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008647 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008648 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008649 return 0;
8650
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008651 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008652 return 0;
8653
Willy Tarreaub2513902006-12-17 14:52:38 +01008654 return 1;
8655}
8656
Willy Tarreau4076a152009-04-02 15:18:36 +02008657/*
8658 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008659 * By default it tries to report the error position as msg->err_pos. However if
8660 * this one is not set, it will then report msg->next, which is the last known
8661 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008662 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008663 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008664void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008665 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008666 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008667{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008668 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008669 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008670 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008671
Willy Tarreauf3764b72016-03-31 13:45:10 +02008672 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008673 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008674 len1 = MIN(len1, es->len);
8675 len2 = es->len - len1; /* remaining data if buffer wraps */
8676
Willy Tarreauf3764b72016-03-31 13:45:10 +02008677 if (!es->buf)
8678 es->buf = malloc(global.tune.bufsize);
8679
8680 if (es->buf) {
8681 memcpy(es->buf, chn->buf->p, len1);
8682 if (len2)
8683 memcpy(es->buf + len1, chn->buf->data, len2);
8684 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008685
Willy Tarreau4076a152009-04-02 15:18:36 +02008686 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008687 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008688 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008689 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008690
Willy Tarreau4076a152009-04-02 15:18:36 +02008691 es->when = date; // user-visible date
8692 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008693 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008694 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008695 if (objt_conn(sess->origin))
8696 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008697 else
8698 memset(&es->src, 0, sizeof(es->src));
8699
Willy Tarreau078272e2010-12-12 12:46:33 +01008700 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008701 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008702 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008703 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008704 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008705 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008706 es->b_out = chn->buf->o;
8707 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008708 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008709 es->m_clen = msg->chunk_len;
8710 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008711}
Willy Tarreaub2513902006-12-17 14:52:38 +01008712
Willy Tarreau294c4732011-12-16 21:35:50 +01008713/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8714 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8715 * performed over the whole headers. Otherwise it must contain a valid header
8716 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8717 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8718 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8719 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008720 * -1. The value fetch stops at commas, so this function is suited for use with
8721 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008724unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 struct hdr_idx *idx, int occ,
8726 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008727{
Willy Tarreau294c4732011-12-16 21:35:50 +01008728 struct hdr_ctx local_ctx;
8729 char *ptr_hist[MAX_HDR_HISTORY];
8730 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008732 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008733
Willy Tarreau294c4732011-12-16 21:35:50 +01008734 if (!ctx) {
8735 local_ctx.idx = 0;
8736 ctx = &local_ctx;
8737 }
8738
Willy Tarreaubce70882009-09-07 11:51:47 +02008739 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008740 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008741 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008742 occ--;
8743 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008744 *vptr = ctx->line + ctx->val;
8745 *vlen = ctx->vlen;
8746 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008747 }
8748 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008749 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008750 }
8751
8752 /* negative occurrence, we scan all the list then walk back */
8753 if (-occ > MAX_HDR_HISTORY)
8754 return 0;
8755
Willy Tarreau294c4732011-12-16 21:35:50 +01008756 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008757 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008758 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8759 len_hist[hist_ptr] = ctx->vlen;
8760 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008761 hist_ptr = 0;
8762 found++;
8763 }
8764 if (-occ > found)
8765 return 0;
8766 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008767 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8768 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8769 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008770 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008771 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008772 if (hist_ptr >= MAX_HDR_HISTORY)
8773 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008774 *vptr = ptr_hist[hist_ptr];
8775 *vlen = len_hist[hist_ptr];
8776 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008777}
8778
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008779/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8780 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8781 * performed over the whole headers. Otherwise it must contain a valid header
8782 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8783 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8784 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8785 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8786 * -1. This function differs from http_get_hdr() in that it only returns full
8787 * line header values and does not stop at commas.
8788 * The return value is 0 if nothing was found, or non-zero otherwise.
8789 */
8790unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8791 struct hdr_idx *idx, int occ,
8792 struct hdr_ctx *ctx, char **vptr, int *vlen)
8793{
8794 struct hdr_ctx local_ctx;
8795 char *ptr_hist[MAX_HDR_HISTORY];
8796 int len_hist[MAX_HDR_HISTORY];
8797 unsigned int hist_ptr;
8798 int found;
8799
8800 if (!ctx) {
8801 local_ctx.idx = 0;
8802 ctx = &local_ctx;
8803 }
8804
8805 if (occ >= 0) {
8806 /* search from the beginning */
8807 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8808 occ--;
8809 if (occ <= 0) {
8810 *vptr = ctx->line + ctx->val;
8811 *vlen = ctx->vlen;
8812 return 1;
8813 }
8814 }
8815 return 0;
8816 }
8817
8818 /* negative occurrence, we scan all the list then walk back */
8819 if (-occ > MAX_HDR_HISTORY)
8820 return 0;
8821
8822 found = hist_ptr = 0;
8823 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8824 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8825 len_hist[hist_ptr] = ctx->vlen;
8826 if (++hist_ptr >= MAX_HDR_HISTORY)
8827 hist_ptr = 0;
8828 found++;
8829 }
8830 if (-occ > found)
8831 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008832
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008833 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008834 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8835 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8836 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008837 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008838 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008839 if (hist_ptr >= MAX_HDR_HISTORY)
8840 hist_ptr -= MAX_HDR_HISTORY;
8841 *vptr = ptr_hist[hist_ptr];
8842 *vlen = len_hist[hist_ptr];
8843 return 1;
8844}
8845
Willy Tarreaubaaee002006-06-26 02:48:02 +02008846/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008847 * Print a debug line with a header. Always stop at the first CR or LF char,
8848 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8849 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008850 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008851void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008852{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008853 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008854 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008855
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008856 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008857 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008858 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008859 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 +02008860
8861 for (max = 0; start + max < end; max++)
8862 if (start[max] == '\r' || start[max] == '\n')
8863 break;
8864
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008865 UBOUND(max, trash.size - trash.len - 3);
8866 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8867 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008868 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008869}
8870
Willy Tarreaueee5b512015-04-03 23:46:31 +02008871
8872/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8873 * The hdr_idx is allocated as well. In case of allocation failure, everything
8874 * allocated is freed and NULL is returned. Otherwise the new transaction is
8875 * assigned to the stream and returned.
8876 */
8877struct http_txn *http_alloc_txn(struct stream *s)
8878{
8879 struct http_txn *txn = s->txn;
8880
8881 if (txn)
8882 return txn;
8883
8884 txn = pool_alloc2(pool2_http_txn);
8885 if (!txn)
8886 return txn;
8887
8888 txn->hdr_idx.size = global.tune.max_http_hdr;
8889 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8890 if (!txn->hdr_idx.v) {
8891 pool_free2(pool2_http_txn, txn);
8892 return NULL;
8893 }
8894
8895 s->txn = txn;
8896 return txn;
8897}
8898
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008899void http_txn_reset_req(struct http_txn *txn)
8900{
8901 txn->req.flags = 0;
8902 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8903 txn->req.next = 0;
8904 txn->req.chunk_len = 0LL;
8905 txn->req.body_len = 0LL;
8906 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8907}
8908
8909void http_txn_reset_res(struct http_txn *txn)
8910{
8911 txn->rsp.flags = 0;
8912 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8913 txn->rsp.next = 0;
8914 txn->rsp.chunk_len = 0LL;
8915 txn->rsp.body_len = 0LL;
8916 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8917}
8918
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008920 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008921 * the required fields are properly allocated and that we only need to (re)init
8922 * them. This should be used before processing any new request.
8923 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008924void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008926 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008927 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008928
8929 txn->flags = 0;
8930 txn->status = -1;
8931
Willy Tarreauf64d1412010-10-07 20:06:11 +02008932 txn->cookie_first_date = 0;
8933 txn->cookie_last_date = 0;
8934
Willy Tarreaueee5b512015-04-03 23:46:31 +02008935 txn->srv_cookie = NULL;
8936 txn->cli_cookie = NULL;
8937 txn->uri = NULL;
8938
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008939 http_txn_reset_req(txn);
8940 http_txn_reset_res(txn);
8941
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008942 txn->req.chn = &s->req;
8943 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008944
8945 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008946
8947 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8948 if (fe->options2 & PR_O2_REQBUG_OK)
8949 txn->req.err_pos = -1; /* let buggy requests pass */
8950
Willy Tarreau0937bc42009-12-22 15:03:09 +01008951 if (txn->hdr_idx.v)
8952 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008953
8954 vars_init(&s->vars_txn, SCOPE_TXN);
8955 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956}
8957
8958/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008959void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008960{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008961 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008962 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008963
8964 /* these ones will have been dynamically allocated */
8965 pool_free2(pool2_requri, txn->uri);
8966 pool_free2(pool2_capture, txn->cli_cookie);
8967 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008968 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008969
William Lallemanda73203e2012-03-12 12:48:57 +01008970 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008971 txn->uri = NULL;
8972 txn->srv_cookie = NULL;
8973 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008974
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008975 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008976 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008977 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008978 pool_free2(h->pool, s->req_cap[h->index]);
8979 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008980 }
8981
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008982 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008983 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008984 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008985 pool_free2(h->pool, s->res_cap[h->index]);
8986 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008987 }
8988
Willy Tarreau6204cd92016-03-10 16:33:04 +01008989 vars_prune(&s->vars_txn, s->sess, s);
8990 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008991}
8992
8993/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008994void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995{
8996 http_end_txn(s);
8997 http_init_txn(s);
8998
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008999 /* reinitialise the current rule list pointer to NULL. We are sure that
9000 * any rulelist match the NULL pointer.
9001 */
9002 s->current_rule_list = NULL;
9003
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009004 s->be = strm_fe(s);
9005 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009006 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009007 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009008 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009009 /* re-init store persistence */
9010 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009011 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009012
Willy Tarreau0937bc42009-12-22 15:03:09 +01009013 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009014
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009015 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009016
Willy Tarreau739cfba2010-01-25 23:11:14 +01009017 /* We must trim any excess data from the response buffer, because we
9018 * may have blocked an invalid response from a server that we don't
9019 * want to accidentely forward once we disable the analysers, nor do
9020 * we want those data to come along with next response. A typical
9021 * example of such data would be from a buggy server responding to
9022 * a HEAD with some data, or sending more than the advertised
9023 * content-length.
9024 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009025 if (unlikely(s->res.buf->i))
9026 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009027
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009028 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009029 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009030
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009031 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009032 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009033
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009034 s->req.rex = TICK_ETERNITY;
9035 s->req.wex = TICK_ETERNITY;
9036 s->req.analyse_exp = TICK_ETERNITY;
9037 s->res.rex = TICK_ETERNITY;
9038 s->res.wex = TICK_ETERNITY;
9039 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009040}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009041
Sasha Pachev218f0642014-06-16 12:05:59 -06009042void free_http_res_rules(struct list *r)
9043{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009044 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009045
9046 list_for_each_entry_safe(pr, tr, r, list) {
9047 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009048 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009049 free(pr);
9050 }
9051}
9052
9053void free_http_req_rules(struct list *r)
9054{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009055 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009056
9057 list_for_each_entry_safe(pr, tr, r, list) {
9058 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009059 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009060 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009061
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009062 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009063 free(pr);
9064 }
9065}
9066
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009067/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009068struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009069{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009070 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009071 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009072 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009073 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009074
Vincent Bernat02779b62016-04-03 13:48:43 +02009075 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009076 if (!rule) {
9077 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009078 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009079 }
9080
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009081 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009082 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009083 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009084 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009085 int code;
9086 int hc;
9087
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009088 if (!strcmp(args[0], "tarpit")) {
9089 rule->action = ACT_HTTP_REQ_TARPIT;
9090 rule->deny_status = HTTP_ERR_500;
9091 }
9092 else {
9093 rule->action = ACT_ACTION_DENY;
9094 rule->deny_status = HTTP_ERR_403;
9095 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009096 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009097 if (strcmp(args[cur_arg], "deny_status") == 0) {
9098 cur_arg++;
9099 if (!args[cur_arg]) {
9100 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9101 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9102 goto out_err;
9103 }
9104
9105 code = atol(args[cur_arg]);
9106 cur_arg++;
9107 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9108 if (http_err_codes[hc] == code) {
9109 rule->deny_status = hc;
9110 break;
9111 }
9112 }
9113
9114 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009115 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9116 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009117 }
9118 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009119 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009120 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009121 cur_arg = 1;
9122
9123 while(*args[cur_arg]) {
9124 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009125 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009126 cur_arg+=2;
9127 continue;
9128 } else
9129 break;
9130 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009131 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009132 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009133 cur_arg = 1;
9134
9135 if (!*args[cur_arg] ||
9136 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9137 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9138 file, linenum, args[0]);
9139 goto out_err;
9140 }
9141 rule->arg.nice = atoi(args[cur_arg]);
9142 if (rule->arg.nice < -1024)
9143 rule->arg.nice = -1024;
9144 else if (rule->arg.nice > 1024)
9145 rule->arg.nice = 1024;
9146 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009147 } else if (!strcmp(args[0], "set-tos")) {
9148#ifdef IP_TOS
9149 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009150 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009151 cur_arg = 1;
9152
9153 if (!*args[cur_arg] ||
9154 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9155 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9156 file, linenum, args[0]);
9157 goto out_err;
9158 }
9159
9160 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9161 if (err && *err != '\0') {
9162 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9163 file, linenum, err, args[0]);
9164 goto out_err;
9165 }
9166 cur_arg++;
9167#else
9168 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9169 goto out_err;
9170#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009171 } else if (!strcmp(args[0], "set-mark")) {
9172#ifdef SO_MARK
9173 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009174 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009175 cur_arg = 1;
9176
9177 if (!*args[cur_arg] ||
9178 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9179 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9180 file, linenum, args[0]);
9181 goto out_err;
9182 }
9183
9184 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9185 if (err && *err != '\0') {
9186 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9187 file, linenum, err, args[0]);
9188 goto out_err;
9189 }
9190 cur_arg++;
9191 global.last_checks |= LSTCHK_NETADM;
9192#else
9193 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9194 goto out_err;
9195#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009196 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009197 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009198 cur_arg = 1;
9199
9200 if (!*args[cur_arg] ||
9201 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9202 bad_log_level:
9203 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9204 file, linenum, args[0]);
9205 goto out_err;
9206 }
9207 if (strcmp(args[cur_arg], "silent") == 0)
9208 rule->arg.loglevel = -1;
9209 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9210 goto bad_log_level;
9211 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009212 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009213 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009214 cur_arg = 1;
9215
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009216 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9217 (*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 +01009218 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9219 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009220 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009221 }
9222
9223 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9224 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9225 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009226
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009227 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009228 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009229 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 +01009230 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9231 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9232 file, linenum, args[0], error);
9233 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009234 goto out_err;
9235 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009236 free(proxy->conf.lfs_file);
9237 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9238 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009239 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009240 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009241 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009242 cur_arg = 1;
9243
9244 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009245 (*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 -06009246 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9247 file, linenum, args[0]);
9248 goto out_err;
9249 }
9250
9251 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9252 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9253 LIST_INIT(&rule->arg.hdr_add.fmt);
9254
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009255 error = NULL;
9256 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9257 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9258 args[cur_arg + 1], error);
9259 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009260 goto out_err;
9261 }
9262
9263 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009264 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009265 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 +01009266 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9267 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9268 file, linenum, args[0], error);
9269 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009270 goto out_err;
9271 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009272
9273 free(proxy->conf.lfs_file);
9274 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9275 proxy->conf.lfs_line = proxy->conf.args.line;
9276 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009277 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009278 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009279 cur_arg = 1;
9280
9281 if (!*args[cur_arg] ||
9282 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9283 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9284 file, linenum, args[0]);
9285 goto out_err;
9286 }
9287
9288 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9289 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9290
9291 proxy->conf.args.ctx = ARGC_HRQ;
9292 free(proxy->conf.lfs_file);
9293 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9294 proxy->conf.lfs_line = proxy->conf.args.line;
9295 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009296 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9297 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009298 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009299 struct sample_expr *expr;
9300 unsigned int where;
9301 char *err = NULL;
9302
9303 cur_arg = 1;
9304 proxy->conf.args.ctx = ARGC_TRK;
9305
9306 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9307 if (!expr) {
9308 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9309 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9310 free(err);
9311 goto out_err;
9312 }
9313
9314 where = 0;
9315 if (proxy->cap & PR_CAP_FE)
9316 where |= SMP_VAL_FE_HRQ_HDR;
9317 if (proxy->cap & PR_CAP_BE)
9318 where |= SMP_VAL_BE_HRQ_HDR;
9319
9320 if (!(expr->fetch->val & where)) {
9321 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9322 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9323 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9324 args[cur_arg-1], sample_src_names(expr->fetch->use));
9325 free(expr);
9326 goto out_err;
9327 }
9328
9329 if (strcmp(args[cur_arg], "table") == 0) {
9330 cur_arg++;
9331 if (!args[cur_arg]) {
9332 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9333 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9334 free(expr);
9335 goto out_err;
9336 }
9337 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009338 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009339 cur_arg++;
9340 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009341 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009342 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009343 } else if (strcmp(args[0], "redirect") == 0) {
9344 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009345 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009346
Willy Tarreaube4653b2015-05-28 15:26:58 +02009347 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009348 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9349 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9350 goto out_err;
9351 }
9352
9353 /* this redirect rule might already contain a parsed condition which
9354 * we'll pass to the http-request rule.
9355 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009356 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009357 rule->arg.redir = redir;
9358 rule->cond = redir->cond;
9359 redir->cond = NULL;
9360 cur_arg = 2;
9361 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009362 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9363 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009364 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009365 /*
9366 * '+ 8' for 'add-acl('
9367 * '- 9' for 'add-acl(' + trailing ')'
9368 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009369 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009370
9371 cur_arg = 1;
9372
9373 if (!*args[cur_arg] ||
9374 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9375 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9376 file, linenum, args[0]);
9377 goto out_err;
9378 }
9379
9380 LIST_INIT(&rule->arg.map.key);
9381 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009382 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009383 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009384 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9385 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9386 file, linenum, args[0], error);
9387 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009388 goto out_err;
9389 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009390 free(proxy->conf.lfs_file);
9391 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9392 proxy->conf.lfs_line = proxy->conf.args.line;
9393 cur_arg += 1;
9394 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9395 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009396 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009397 /*
9398 * '+ 8' for 'del-acl('
9399 * '- 9' for 'del-acl(' + trailing ')'
9400 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009401 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009402
9403 cur_arg = 1;
9404
9405 if (!*args[cur_arg] ||
9406 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9407 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9408 file, linenum, args[0]);
9409 goto out_err;
9410 }
9411
9412 LIST_INIT(&rule->arg.map.key);
9413 proxy->conf.args.ctx = ARGC_HRQ;
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], proxy, &rule->arg.map.key, 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': %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 cur_arg += 1;
9426 } else if (strncmp(args[0], "del-map", 7) == 0) {
9427 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009428 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009429 /*
9430 * '+ 8' for 'del-map('
9431 * '- 9' for 'del-map(' + trailing ')'
9432 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009433 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009434
9435 cur_arg = 1;
9436
9437 if (!*args[cur_arg] ||
9438 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9439 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9440 file, linenum, args[0]);
9441 goto out_err;
9442 }
9443
9444 LIST_INIT(&rule->arg.map.key);
9445 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009446 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009447 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009448 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9449 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9450 file, linenum, args[0], error);
9451 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009452 goto out_err;
9453 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009454 free(proxy->conf.lfs_file);
9455 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9456 proxy->conf.lfs_line = proxy->conf.args.line;
9457 cur_arg += 1;
9458 } else if (strncmp(args[0], "set-map", 7) == 0) {
9459 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009460 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009461 /*
9462 * '+ 8' for 'set-map('
9463 * '- 9' for 'set-map(' + trailing ')'
9464 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009465 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009466
9467 cur_arg = 1;
9468
9469 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9470 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9471 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9472 file, linenum, args[0]);
9473 goto out_err;
9474 }
9475
9476 LIST_INIT(&rule->arg.map.key);
9477 LIST_INIT(&rule->arg.map.value);
9478 proxy->conf.args.ctx = ARGC_HRQ;
9479
9480 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009481 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009482 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009483 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9484 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9485 file, linenum, args[0], error);
9486 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009487 goto out_err;
9488 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009489
9490 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009491 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009492 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 +01009493 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9494 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9495 file, linenum, args[0], error);
9496 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009497 goto out_err;
9498 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009499 free(proxy->conf.lfs_file);
9500 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9501 proxy->conf.lfs_line = proxy->conf.args.line;
9502
9503 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009504 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9505 char *errmsg = NULL;
9506 cur_arg = 1;
9507 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009508 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009509 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009510 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009511 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9512 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9513 free(errmsg);
9514 goto out_err;
9515 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009516 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009517 action_build_list(&http_req_keywords.list, &trash);
9518 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9519 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009520 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009521 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009522 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009523 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009524 }
9525
9526 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9527 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009528 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009529
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009530 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9531 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9532 file, linenum, args[0], errmsg);
9533 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009534 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009535 }
9536 rule->cond = cond;
9537 }
9538 else if (*args[cur_arg]) {
9539 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9540 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9541 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009542 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009543 }
9544
9545 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009546 out_err:
9547 free(rule);
9548 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009549}
9550
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009551/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009552struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009553{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009554 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009555 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009556 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009557 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009558
9559 rule = calloc(1, sizeof(*rule));
9560 if (!rule) {
9561 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9562 goto out_err;
9563 }
9564
9565 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009566 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009567 cur_arg = 1;
9568 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009569 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009570 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009571 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009572 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009573 cur_arg = 1;
9574
9575 if (!*args[cur_arg] ||
9576 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9577 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9578 file, linenum, args[0]);
9579 goto out_err;
9580 }
9581 rule->arg.nice = atoi(args[cur_arg]);
9582 if (rule->arg.nice < -1024)
9583 rule->arg.nice = -1024;
9584 else if (rule->arg.nice > 1024)
9585 rule->arg.nice = 1024;
9586 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009587 } else if (!strcmp(args[0], "set-tos")) {
9588#ifdef IP_TOS
9589 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009590 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009591 cur_arg = 1;
9592
9593 if (!*args[cur_arg] ||
9594 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9595 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9596 file, linenum, args[0]);
9597 goto out_err;
9598 }
9599
9600 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9601 if (err && *err != '\0') {
9602 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9603 file, linenum, err, args[0]);
9604 goto out_err;
9605 }
9606 cur_arg++;
9607#else
9608 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9609 goto out_err;
9610#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009611 } else if (!strcmp(args[0], "set-mark")) {
9612#ifdef SO_MARK
9613 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009614 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009615 cur_arg = 1;
9616
9617 if (!*args[cur_arg] ||
9618 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9619 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9620 file, linenum, args[0]);
9621 goto out_err;
9622 }
9623
9624 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9625 if (err && *err != '\0') {
9626 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9627 file, linenum, err, args[0]);
9628 goto out_err;
9629 }
9630 cur_arg++;
9631 global.last_checks |= LSTCHK_NETADM;
9632#else
9633 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9634 goto out_err;
9635#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009636 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009637 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009638 cur_arg = 1;
9639
9640 if (!*args[cur_arg] ||
9641 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9642 bad_log_level:
9643 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9644 file, linenum, args[0]);
9645 goto out_err;
9646 }
9647 if (strcmp(args[cur_arg], "silent") == 0)
9648 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009649 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009650 goto bad_log_level;
9651 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009652 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009653 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009654 cur_arg = 1;
9655
9656 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9657 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9658 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9659 file, linenum, args[0]);
9660 goto out_err;
9661 }
9662
9663 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9664 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9665 LIST_INIT(&rule->arg.hdr_add.fmt);
9666
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009667 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009668 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009669 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 +01009670 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9671 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9672 file, linenum, args[0], error);
9673 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009674 goto out_err;
9675 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009676 free(proxy->conf.lfs_file);
9677 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9678 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009679 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009680 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009681 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009682 cur_arg = 1;
9683
9684 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009685 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9686 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009687 file, linenum, args[0]);
9688 goto out_err;
9689 }
9690
9691 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9692 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9693 LIST_INIT(&rule->arg.hdr_add.fmt);
9694
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009695 error = NULL;
9696 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9697 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9698 args[cur_arg + 1], error);
9699 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009700 goto out_err;
9701 }
9702
9703 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009704 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009705 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 +01009706 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9707 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9708 file, linenum, args[0], error);
9709 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009710 goto out_err;
9711 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009712
9713 free(proxy->conf.lfs_file);
9714 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9715 proxy->conf.lfs_line = proxy->conf.args.line;
9716 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009717 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009718 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009719 cur_arg = 1;
9720
9721 if (!*args[cur_arg] ||
9722 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9723 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9724 file, linenum, args[0]);
9725 goto out_err;
9726 }
9727
9728 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9729 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9730
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731 proxy->conf.args.ctx = ARGC_HRS;
9732 free(proxy->conf.lfs_file);
9733 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9734 proxy->conf.lfs_line = proxy->conf.args.line;
9735 cur_arg += 1;
9736 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9737 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009738 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009739 /*
9740 * '+ 8' for 'add-acl('
9741 * '- 9' for 'add-acl(' + trailing ')'
9742 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009743 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009744
9745 cur_arg = 1;
9746
9747 if (!*args[cur_arg] ||
9748 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9749 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9750 file, linenum, args[0]);
9751 goto out_err;
9752 }
9753
9754 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009755 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009756 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009757 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009758 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9759 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9760 file, linenum, args[0], error);
9761 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009762 goto out_err;
9763 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009764 free(proxy->conf.lfs_file);
9765 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9766 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009767
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009768 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009769 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9770 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009771 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009772 /*
9773 * '+ 8' for 'del-acl('
9774 * '- 9' for 'del-acl(' + trailing ')'
9775 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009776 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009777
9778 cur_arg = 1;
9779
9780 if (!*args[cur_arg] ||
9781 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9782 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9783 file, linenum, args[0]);
9784 goto out_err;
9785 }
9786
9787 LIST_INIT(&rule->arg.map.key);
9788 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009789 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009790 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009791 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9792 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9793 file, linenum, args[0], error);
9794 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009795 goto out_err;
9796 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009797 free(proxy->conf.lfs_file);
9798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9799 proxy->conf.lfs_line = proxy->conf.args.line;
9800 cur_arg += 1;
9801 } else if (strncmp(args[0], "del-map", 7) == 0) {
9802 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009803 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009804 /*
9805 * '+ 8' for 'del-map('
9806 * '- 9' for 'del-map(' + trailing ')'
9807 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009808 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009809
9810 cur_arg = 1;
9811
9812 if (!*args[cur_arg] ||
9813 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9814 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9815 file, linenum, args[0]);
9816 goto out_err;
9817 }
9818
9819 LIST_INIT(&rule->arg.map.key);
9820 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009821 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009822 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009823 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9824 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9825 file, linenum, args[0], error);
9826 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009827 goto out_err;
9828 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009829 free(proxy->conf.lfs_file);
9830 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9831 proxy->conf.lfs_line = proxy->conf.args.line;
9832 cur_arg += 1;
9833 } else if (strncmp(args[0], "set-map", 7) == 0) {
9834 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009835 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009836 /*
9837 * '+ 8' for 'set-map('
9838 * '- 9' for 'set-map(' + trailing ')'
9839 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009840 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009841
9842 cur_arg = 1;
9843
9844 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9845 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9846 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9847 file, linenum, args[0]);
9848 goto out_err;
9849 }
9850
9851 LIST_INIT(&rule->arg.map.key);
9852 LIST_INIT(&rule->arg.map.value);
9853
9854 proxy->conf.args.ctx = ARGC_HRS;
9855
9856 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009857 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009858 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009859 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9860 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9861 file, linenum, args[0], error);
9862 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009863 goto out_err;
9864 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009865
9866 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009867 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009868 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 +01009869 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9870 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9871 file, linenum, args[0], error);
9872 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009873 goto out_err;
9874 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009875
9876 free(proxy->conf.lfs_file);
9877 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9878 proxy->conf.lfs_line = proxy->conf.args.line;
9879
9880 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009881 } else if (strcmp(args[0], "redirect") == 0) {
9882 struct redirect_rule *redir;
9883 char *errmsg = NULL;
9884
9885 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9886 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9887 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9888 goto out_err;
9889 }
9890
9891 /* this redirect rule might already contain a parsed condition which
9892 * we'll pass to the http-request rule.
9893 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009894 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009895 rule->arg.redir = redir;
9896 rule->cond = redir->cond;
9897 redir->cond = NULL;
9898 cur_arg = 2;
9899 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009900 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9901 args[0][9] == '\0' && args[0][8] >= '0' &&
9902 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9903 struct sample_expr *expr;
9904 unsigned int where;
9905 char *err = NULL;
9906
9907 cur_arg = 1;
9908 proxy->conf.args.ctx = ARGC_TRK;
9909
9910 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9911 if (!expr) {
9912 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9913 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9914 free(err);
9915 goto out_err;
9916 }
9917
9918 where = 0;
9919 if (proxy->cap & PR_CAP_FE)
9920 where |= SMP_VAL_FE_HRS_HDR;
9921 if (proxy->cap & PR_CAP_BE)
9922 where |= SMP_VAL_BE_HRS_HDR;
9923
9924 if (!(expr->fetch->val & where)) {
9925 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9926 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9927 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9928 args[cur_arg-1], sample_src_names(expr->fetch->use));
9929 free(expr);
9930 goto out_err;
9931 }
9932
9933 if (strcmp(args[cur_arg], "table") == 0) {
9934 cur_arg++;
9935 if (!args[cur_arg]) {
9936 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9937 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9938 free(expr);
9939 goto out_err;
9940 }
9941 /* we copy the table name for now, it will be resolved later */
9942 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9943 cur_arg++;
9944 }
9945 rule->arg.trk_ctr.expr = expr;
9946 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009947 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9948 char *errmsg = NULL;
9949 cur_arg = 1;
9950 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009951 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009952 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009953 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009954 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9955 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9956 free(errmsg);
9957 goto out_err;
9958 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009959 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009960 action_build_list(&http_res_keywords.list, &trash);
9961 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9962 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009963 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009964 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009965 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009966 goto out_err;
9967 }
9968
9969 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9970 struct acl_cond *cond;
9971 char *errmsg = NULL;
9972
9973 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9974 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9975 file, linenum, args[0], errmsg);
9976 free(errmsg);
9977 goto out_err;
9978 }
9979 rule->cond = cond;
9980 }
9981 else if (*args[cur_arg]) {
9982 Alert("parsing [%s:%d]: 'http-response %s' expects"
9983 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9984 file, linenum, args[0], args[cur_arg]);
9985 goto out_err;
9986 }
9987
9988 return rule;
9989 out_err:
9990 free(rule);
9991 return NULL;
9992}
9993
Willy Tarreau4baae242012-12-27 12:00:31 +01009994/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009995 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009996 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9997 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009998 */
9999struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010000 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +010010001{
10002 struct redirect_rule *rule;
10003 int cur_arg;
10004 int type = REDIRECT_TYPE_NONE;
10005 int code = 302;
10006 const char *destination = NULL;
10007 const char *cookie = NULL;
10008 int cookie_set = 0;
10009 unsigned int flags = REDIRECT_FLAG_NONE;
10010 struct acl_cond *cond = NULL;
10011
10012 cur_arg = 0;
10013 while (*(args[cur_arg])) {
10014 if (strcmp(args[cur_arg], "location") == 0) {
10015 if (!*args[cur_arg + 1])
10016 goto missing_arg;
10017
10018 type = REDIRECT_TYPE_LOCATION;
10019 cur_arg++;
10020 destination = args[cur_arg];
10021 }
10022 else if (strcmp(args[cur_arg], "prefix") == 0) {
10023 if (!*args[cur_arg + 1])
10024 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010025 type = REDIRECT_TYPE_PREFIX;
10026 cur_arg++;
10027 destination = args[cur_arg];
10028 }
10029 else if (strcmp(args[cur_arg], "scheme") == 0) {
10030 if (!*args[cur_arg + 1])
10031 goto missing_arg;
10032
10033 type = REDIRECT_TYPE_SCHEME;
10034 cur_arg++;
10035 destination = args[cur_arg];
10036 }
10037 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10038 if (!*args[cur_arg + 1])
10039 goto missing_arg;
10040
10041 cur_arg++;
10042 cookie = args[cur_arg];
10043 cookie_set = 1;
10044 }
10045 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10046 if (!*args[cur_arg + 1])
10047 goto missing_arg;
10048
10049 cur_arg++;
10050 cookie = args[cur_arg];
10051 cookie_set = 0;
10052 }
10053 else if (strcmp(args[cur_arg], "code") == 0) {
10054 if (!*args[cur_arg + 1])
10055 goto missing_arg;
10056
10057 cur_arg++;
10058 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010059 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010060 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010061 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010062 args[cur_arg - 1], args[cur_arg]);
10063 return NULL;
10064 }
10065 }
10066 else if (!strcmp(args[cur_arg],"drop-query")) {
10067 flags |= REDIRECT_FLAG_DROP_QS;
10068 }
10069 else if (!strcmp(args[cur_arg],"append-slash")) {
10070 flags |= REDIRECT_FLAG_APPEND_SLASH;
10071 }
10072 else if (strcmp(args[cur_arg], "if") == 0 ||
10073 strcmp(args[cur_arg], "unless") == 0) {
10074 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10075 if (!cond) {
10076 memprintf(errmsg, "error in condition: %s", *errmsg);
10077 return NULL;
10078 }
10079 break;
10080 }
10081 else {
10082 memprintf(errmsg,
10083 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10084 args[cur_arg]);
10085 return NULL;
10086 }
10087 cur_arg++;
10088 }
10089
10090 if (type == REDIRECT_TYPE_NONE) {
10091 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10092 return NULL;
10093 }
10094
Willy Tarreaube4653b2015-05-28 15:26:58 +020010095 if (dir && type != REDIRECT_TYPE_LOCATION) {
10096 memprintf(errmsg, "response only supports redirect type 'location'");
10097 return NULL;
10098 }
10099
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010100 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010101 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010102 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010103
10104 if (!use_fmt) {
10105 /* old-style static redirect rule */
10106 rule->rdr_str = strdup(destination);
10107 rule->rdr_len = strlen(destination);
10108 }
10109 else {
10110 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010111
10112 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10113 * if prefix == "/", we don't want to add anything, otherwise it
10114 * makes it hard for the user to configure a self-redirection.
10115 */
Godbachd9722032014-12-18 15:44:58 +080010116 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010117 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010118 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10119 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 +010010120 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10121 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010122 return NULL;
10123 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010124 free(curproxy->conf.lfs_file);
10125 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10126 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010127 }
10128 }
10129
Willy Tarreau4baae242012-12-27 12:00:31 +010010130 if (cookie) {
10131 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10132 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10133 */
10134 rule->cookie_len = strlen(cookie);
10135 if (cookie_set) {
10136 rule->cookie_str = malloc(rule->cookie_len + 10);
10137 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10138 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10139 rule->cookie_len += 9;
10140 } else {
10141 rule->cookie_str = malloc(rule->cookie_len + 21);
10142 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10143 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10144 rule->cookie_len += 20;
10145 }
10146 }
10147 rule->type = type;
10148 rule->code = code;
10149 rule->flags = flags;
10150 LIST_INIT(&rule->list);
10151 return rule;
10152
10153 missing_arg:
10154 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10155 return NULL;
10156}
10157
Willy Tarreau8797c062007-05-07 00:55:35 +020010158/************************************************************************/
10159/* The code below is dedicated to ACL parsing and matching */
10160/************************************************************************/
10161
10162
Willy Tarreau14174bc2012-04-16 14:34:04 +020010163/* This function ensures that the prerequisites for an L7 fetch are ready,
10164 * which means that a request or response is ready. If some data is missing,
10165 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010166 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10167 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010168 *
10169 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010170 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10171 * decide whether or not an HTTP message is present ;
10172 * 0 if the requested data cannot be fetched or if it is certain that
10173 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174 * 1 if an HTTP message is ready
10175 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010176int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010177 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010178{
Willy Tarreau192252e2015-04-04 01:47:55 +020010179 struct http_txn *txn;
10180 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010181
Willy Tarreaube508f12016-03-10 11:47:01 +010010182 /* Note: it is possible that <s> is NULL when called before stream
10183 * initialization (eg: tcp-request connection), so this function is the
10184 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010185 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010186 if (!s)
10187 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010188
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010189 if (!s->txn) {
10190 if (unlikely(!http_alloc_txn(s)))
10191 return 0; /* not enough memory */
10192 http_init_txn(s);
10193 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010194 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010195 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196
10197 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010198 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010200 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010201 /* If the buffer does not leave enough free space at the end,
10202 * we must first realign it.
10203 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010204 if (s->req.buf->p > s->req.buf->data &&
10205 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10206 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010207
Willy Tarreau14174bc2012-04-16 14:34:04 +020010208 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010209 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010210 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010211
10212 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010213 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010214 http_msg_analyzer(msg, &txn->hdr_idx);
10215
10216 /* Still no valid request ? */
10217 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010218 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010219 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010220 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010221 }
10222 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010223 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010224 return 0;
10225 }
10226
10227 /* OK we just got a valid HTTP request. We have some minor
10228 * preparation to perform so that further checks can rely
10229 * on HTTP tests.
10230 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010231
10232 /* If the request was parsed but was too large, we must absolutely
10233 * return an error so that it is not processed. At the moment this
10234 * cannot happen, but if the parsers are to change in the future,
10235 * we want this check to be maintained.
10236 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010237 if (unlikely(s->req.buf->i + s->req.buf->p >
10238 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010239 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010240 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010241 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010242 return 1;
10243 }
10244
Willy Tarreau9b28e032012-10-12 23:49:43 +020010245 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010246 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010247 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010248
Willy Tarreau506d0502013-07-06 13:29:24 +020010249 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10250 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010251 }
10252
Willy Tarreau506d0502013-07-06 13:29:24 +020010253 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010254 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010255 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010256
10257 /* otherwise everything's ready for the request */
10258 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010259 else {
10260 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010261 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10262 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010263 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010264 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010265 }
10266
10267 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010268 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010269 return 1;
10270}
Willy Tarreau8797c062007-05-07 00:55:35 +020010271
Willy Tarreau8797c062007-05-07 00:55:35 +020010272/* 1. Check on METHOD
10273 * We use the pre-parsed method if it is known, and store its number as an
10274 * integer. If it is unknown, we use the pointer and the length.
10275 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010276static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010277{
10278 int len, meth;
10279
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010280 len = strlen(text);
10281 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010282
10283 pattern->val.i = meth;
10284 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010285 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 pattern->len = len;
10287 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010288 else {
10289 pattern->ptr.str = NULL;
10290 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010291 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010292 return 1;
10293}
10294
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010295/* This function fetches the method of current HTTP request and stores
10296 * it in the global pattern struct as a chunk. There are two possibilities :
10297 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10298 * in <len> and <ptr> is NULL ;
10299 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10300 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010301 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010302 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010303static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010304smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010305{
10306 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010307 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308
Willy Tarreau24e32d82012-04-23 23:55:44 +020010309 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010310
Willy Tarreaube508f12016-03-10 11:47:01 +010010311 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010312 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010313 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010314 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010315 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010316 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10317 /* ensure the indexes are not affected */
10318 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010319 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010320 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10321 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010322 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010323 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010324 return 1;
10325}
10326
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010327/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010328static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010329{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010330 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010331 struct pattern_list *lst;
10332 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010333
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010334 list_for_each_entry(lst, &expr->patterns, list) {
10335 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010336
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010337 /* well-known method */
10338 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010339 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010340 return pattern;
10341 else
10342 continue;
10343 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010344
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010345 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010346 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010347 continue;
10348
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010349 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10351 (!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 +010010352 return pattern;
10353 }
10354 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010355}
10356
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010357static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010358smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010359{
Willy Tarreaube508f12016-03-10 11:47:01 +010010360 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010361 char *ptr;
10362 int len;
10363
Willy Tarreauc0239e02012-04-16 14:42:55 +020010364 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010365
Willy Tarreaube508f12016-03-10 11:47:01 +010010366 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010367 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010368 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010369
10370 while ((len-- > 0) && (*ptr++ != '/'));
10371 if (len <= 0)
10372 return 0;
10373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010374 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010375 smp->data.u.str.str = ptr;
10376 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010377
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010378 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010379 return 1;
10380}
10381
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010383smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010384{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010385 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010386 char *ptr;
10387 int len;
10388
Willy Tarreauc0239e02012-04-16 14:42:55 +020010389 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010390
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010391 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010392 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10393 return 0;
10394
Willy Tarreau8797c062007-05-07 00:55:35 +020010395 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010396 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010397
10398 while ((len-- > 0) && (*ptr++ != '/'));
10399 if (len <= 0)
10400 return 0;
10401
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010402 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010403 smp->data.u.str.str = ptr;
10404 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010405
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010406 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010407 return 1;
10408}
10409
10410/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010411static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010412smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010413{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010414 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010415 char *ptr;
10416 int len;
10417
Willy Tarreauc0239e02012-04-16 14:42:55 +020010418 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010419
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010420 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010421 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10422 return 0;
10423
Willy Tarreau8797c062007-05-07 00:55:35 +020010424 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010425 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010426
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010427 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010428 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010429 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010430 return 1;
10431}
10432
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010433static int
10434smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10435{
10436 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10437 return 0;
10438
10439 if (!smp->strm->unique_id) {
10440 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10441 return 0;
10442 smp->strm->unique_id[0] = '\0';
10443 }
10444 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10445 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10446
10447 smp->data.type = SMP_T_STR;
10448 smp->data.u.str.str = smp->strm->unique_id;
10449 smp->flags = SMP_F_CONST;
10450 return 1;
10451}
10452
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010453/* returns the longest available part of the body. This requires that the body
10454 * has been waited for using http-buffer-request.
10455 */
10456static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010457smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010458{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010459 struct http_msg *msg;
10460 unsigned long len;
10461 unsigned long block1;
10462 char *body;
10463 struct chunk *temp;
10464
10465 CHECK_HTTP_MESSAGE_FIRST();
10466
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010467 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010468 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010469 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010470 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010471
10472 len = http_body_bytes(msg);
10473 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10474
10475 block1 = len;
10476 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10477 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10478
10479 if (block1 == len) {
10480 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010481 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010482 smp->data.u.str.str = body;
10483 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010484 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10485 }
10486 else {
10487 /* buffer is wrapped, we need to defragment it */
10488 temp = get_trash_chunk();
10489 memcpy(temp->str, body, block1);
10490 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010491 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010492 smp->data.u.str.str = temp->str;
10493 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010494 smp->flags = SMP_F_VOL_TEST;
10495 }
10496 return 1;
10497}
10498
10499
10500/* returns the available length of the body. This requires that the body
10501 * has been waited for using http-buffer-request.
10502 */
10503static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010504smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010505{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010506 struct http_msg *msg;
10507
10508 CHECK_HTTP_MESSAGE_FIRST();
10509
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010510 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010511 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010512 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010513 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010514
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010515 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010516 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010517
10518 smp->flags = SMP_F_VOL_TEST;
10519 return 1;
10520}
10521
10522
10523/* returns the advertised length of the body, or the advertised size of the
10524 * chunks available in the buffer. This requires that the body has been waited
10525 * for using http-buffer-request.
10526 */
10527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010529{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010530 struct http_msg *msg;
10531
10532 CHECK_HTTP_MESSAGE_FIRST();
10533
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010534 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010535 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010536 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010537 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010538
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010539 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010540 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010541
10542 smp->flags = SMP_F_VOL_TEST;
10543 return 1;
10544}
10545
10546
Willy Tarreau8797c062007-05-07 00:55:35 +020010547/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010548static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010549smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010550{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010551 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010552
Willy Tarreauc0239e02012-04-16 14:42:55 +020010553 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010554
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010555 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010556 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010557 smp->data.u.str.len = txn->req.sl.rq.u_l;
10558 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010559 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010560 return 1;
10561}
10562
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010563static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010564smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010565{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010566 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010567 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010568
Willy Tarreauc0239e02012-04-16 14:42:55 +020010569 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010570
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010571 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010572 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 +020010573 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010574 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010575
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010576 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010577 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010578 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010579 return 1;
10580}
10581
10582static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010583smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010584{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010585 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010586 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010587
Willy Tarreauc0239e02012-04-16 14:42:55 +020010588 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010589
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010590 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010591 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 +020010592 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10593 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010594
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010595 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010596 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010597 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010598 return 1;
10599}
10600
Willy Tarreau185b5c42012-04-26 15:11:51 +020010601/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10602 * Accepts an optional argument of type string containing the header field name,
10603 * and an optional argument of type signed or unsigned integer to request an
10604 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010605 * headers are considered from the first one. It does not stop on commas and
10606 * returns full lines instead (useful for User-Agent or Date for example).
10607 */
10608static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010609smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010610{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010611 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010612 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010613 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010614 int occ = 0;
10615 const char *name_str = NULL;
10616 int name_len = 0;
10617
10618 if (!ctx) {
10619 /* first call */
10620 ctx = &static_hdr_ctx;
10621 ctx->idx = 0;
10622 smp->ctx.a[0] = ctx;
10623 }
10624
10625 if (args) {
10626 if (args[0].type != ARGT_STR)
10627 return 0;
10628 name_str = args[0].data.str.str;
10629 name_len = args[0].data.str.len;
10630
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010631 if (args[1].type == ARGT_SINT)
10632 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010633 }
10634
10635 CHECK_HTTP_MESSAGE_FIRST();
10636
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010637 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010638 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 +020010639
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010640 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10641 /* search for header from the beginning */
10642 ctx->idx = 0;
10643
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010644 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010645 /* no explicit occurrence and single fetch => last header by default */
10646 occ = -1;
10647
10648 if (!occ)
10649 /* prepare to report multiple occurrences for ACL fetches */
10650 smp->flags |= SMP_F_NOT_LAST;
10651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010652 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010653 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010654 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 +020010655 return 1;
10656
10657 smp->flags &= ~SMP_F_NOT_LAST;
10658 return 0;
10659}
10660
10661/* 6. Check on HTTP header count. The number of occurrences is returned.
10662 * Accepts exactly 1 argument of type string. It does not stop on commas and
10663 * returns full lines instead (useful for User-Agent or Date for example).
10664 */
10665static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010666smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010667{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010668 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010669 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010670 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010671 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010672 const char *name = NULL;
10673 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010674
Willy Tarreau601a4d12015-04-01 19:16:09 +020010675 if (args && args->type == ARGT_STR) {
10676 name = args->data.str.str;
10677 len = args->data.str.len;
10678 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010679
10680 CHECK_HTTP_MESSAGE_FIRST();
10681
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010682 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010683 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 +020010684
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010685 ctx.idx = 0;
10686 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010687 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010688 cnt++;
10689
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010690 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010691 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010692 smp->flags = SMP_F_VOL_HDR;
10693 return 1;
10694}
10695
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010696static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010697smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010698{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010699 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010700 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010701 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010702 struct chunk *temp;
10703 char del = ',';
10704
10705 if (args && args->type == ARGT_STR)
10706 del = *args[0].data.str.str;
10707
10708 CHECK_HTTP_MESSAGE_FIRST();
10709
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010710 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010711 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 +020010712
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010713 temp = get_trash_chunk();
10714
10715 ctx.idx = 0;
10716 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10717 if (temp->len)
10718 temp->str[temp->len++] = del;
10719 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10720 temp->len += ctx.del;
10721 }
10722
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010723 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010724 smp->data.u.str.str = temp->str;
10725 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010726 smp->flags = SMP_F_VOL_HDR;
10727 return 1;
10728}
10729
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010730/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10731 * Accepts an optional argument of type string containing the header field name,
10732 * and an optional argument of type signed or unsigned integer to request an
10733 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010734 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010735 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010736static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010737smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010738{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010739 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010740 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010741 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010742 int occ = 0;
10743 const char *name_str = NULL;
10744 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010745
Willy Tarreaua890d072013-04-02 12:01:06 +020010746 if (!ctx) {
10747 /* first call */
10748 ctx = &static_hdr_ctx;
10749 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010750 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010751 }
10752
Willy Tarreau185b5c42012-04-26 15:11:51 +020010753 if (args) {
10754 if (args[0].type != ARGT_STR)
10755 return 0;
10756 name_str = args[0].data.str.str;
10757 name_len = args[0].data.str.len;
10758
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010759 if (args[1].type == ARGT_SINT)
10760 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010761 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010762
Willy Tarreaue333ec92012-04-16 16:26:40 +020010763 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010764
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010765 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766 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 +020010767
Willy Tarreau185b5c42012-04-26 15:11:51 +020010768 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010769 /* search for header from the beginning */
10770 ctx->idx = 0;
10771
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010773 /* no explicit occurrence and single fetch => last header by default */
10774 occ = -1;
10775
10776 if (!occ)
10777 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010778 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010779
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010780 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010781 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010782 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 +020010783 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010784
Willy Tarreau37406352012-04-23 16:16:37 +020010785 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010786 return 0;
10787}
10788
Willy Tarreauc11416f2007-06-17 16:58:38 +020010789/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010790 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010791 */
10792static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010793smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010794{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010795 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010796 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010797 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010798 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010799 const char *name = NULL;
10800 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010801
Willy Tarreau601a4d12015-04-01 19:16:09 +020010802 if (args && args->type == ARGT_STR) {
10803 name = args->data.str.str;
10804 len = args->data.str.len;
10805 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010806
Willy Tarreaue333ec92012-04-16 16:26:40 +020010807 CHECK_HTTP_MESSAGE_FIRST();
10808
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010809 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010810 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 +020010811
Willy Tarreau33a7e692007-06-10 19:45:56 +020010812 ctx.idx = 0;
10813 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010814 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010815 cnt++;
10816
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010817 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010818 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010819 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010820 return 1;
10821}
10822
Willy Tarreau185b5c42012-04-26 15:11:51 +020010823/* Fetch an HTTP header's integer value. The integer value is returned. It
10824 * takes a mandatory argument of type string and an optional one of type int
10825 * to designate a specific occurrence. It returns an unsigned integer, which
10826 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010827 */
10828static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010829smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010830{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010831 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010832
Willy Tarreauf853c462012-04-23 18:53:56 +020010833 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010834 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010835 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010836 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010837
Willy Tarreaud53e2422012-04-16 17:21:11 +020010838 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010839}
10840
Cyril Bonté69fa9922012-10-25 00:01:06 +020010841/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10842 * and an optional one of type int to designate a specific occurrence.
10843 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010844 */
10845static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010846smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010847{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010848 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010849
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010850 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010851 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010852 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010853 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010854 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010855 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010856 if (smp->data.u.str.len < temp->size - 1) {
10857 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10858 temp->str[smp->data.u.str.len] = '\0';
10859 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010860 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010861 break;
10862 }
10863 }
10864 }
10865
Willy Tarreaud53e2422012-04-16 17:21:11 +020010866 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010867 if (!(smp->flags & SMP_F_NOT_LAST))
10868 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010869 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010870 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010871}
10872
Willy Tarreau737b0c12007-06-10 21:28:46 +020010873/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10874 * the first '/' after the possible hostname, and ends before the possible '?'.
10875 */
10876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010877smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010878{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010879 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010880 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010881
Willy Tarreauc0239e02012-04-16 14:42:55 +020010882 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010883
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010884 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010885 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010886 ptr = http_get_path(txn);
10887 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010888 return 0;
10889
10890 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010891 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010892 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010893
10894 while (ptr < end && *ptr != '?')
10895 ptr++;
10896
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010897 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010898 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010899 return 1;
10900}
10901
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010902/* This produces a concatenation of the first occurrence of the Host header
10903 * followed by the path component if it begins with a slash ('/'). This means
10904 * that '*' will not be added, resulting in exactly the first Host entry.
10905 * If no Host header is found, then the path is returned as-is. The returned
10906 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010907 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010908 */
10909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010910smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010911{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010912 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010913 char *ptr, *end, *beg;
10914 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010915 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010916
10917 CHECK_HTTP_MESSAGE_FIRST();
10918
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010919 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010920 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010921 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010922 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010923
10924 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010925 temp = get_trash_chunk();
10926 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010927 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010928 smp->data.u.str.str = temp->str;
10929 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010930
10931 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010932 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010933 beg = http_get_path(txn);
10934 if (!beg)
10935 beg = end;
10936
10937 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10938
10939 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010940 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10941 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010942 }
10943
10944 smp->flags = SMP_F_VOL_1ST;
10945 return 1;
10946}
10947
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010948/* This produces a 32-bit hash of the concatenation of the first occurrence of
10949 * the Host header followed by the path component if it begins with a slash ('/').
10950 * This means that '*' will not be added, resulting in exactly the first Host
10951 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010952 * is hashed using the path hash followed by a full avalanche hash and provides a
10953 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010954 * high-traffic sites without having to store whole paths.
10955 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010956int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010957smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010958{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010959 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010960 struct hdr_ctx ctx;
10961 unsigned int hash = 0;
10962 char *ptr, *beg, *end;
10963 int len;
10964
10965 CHECK_HTTP_MESSAGE_FIRST();
10966
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010967 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010968 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010969 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010970 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10971 ptr = ctx.line + ctx.val;
10972 len = ctx.vlen;
10973 while (len--)
10974 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10975 }
10976
10977 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010978 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010979 beg = http_get_path(txn);
10980 if (!beg)
10981 beg = end;
10982
10983 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10984
10985 if (beg < ptr && *beg == '/') {
10986 while (beg < ptr)
10987 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10988 }
10989 hash = full_hash(hash);
10990
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010991 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010992 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010993 smp->flags = SMP_F_VOL_1ST;
10994 return 1;
10995}
10996
Willy Tarreau4a550602012-12-09 14:53:32 +010010997/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010998 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10999 * per-path counters. The result is a binary block from 8 to 20 bytes depending
11000 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010011001 * that in environments where IPv6 is insignificant, truncating the output to
11002 * 8 bytes would still work.
11003 */
11004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011005smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011006{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011007 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011008 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011009
11010 if (!cli_conn)
11011 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011012
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011013 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011014 return 0;
11015
Willy Tarreau47ca5452012-12-23 20:22:19 +010011016 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011017 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011018 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011019
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011020 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011021 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011022 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011023 temp->len += 4;
11024 break;
11025 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011026 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011027 temp->len += 16;
11028 break;
11029 default:
11030 return 0;
11031 }
11032
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011033 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011034 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011035 return 1;
11036}
11037
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011038/* Extracts the query string, which comes after the question mark '?'. If no
11039 * question mark is found, nothing is returned. Otherwise it returns a sample
11040 * of type string carrying the whole query string.
11041 */
11042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011043smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011044{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011045 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011046 char *ptr, *end;
11047
11048 CHECK_HTTP_MESSAGE_FIRST();
11049
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011050 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011051 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11052 end = ptr + txn->req.sl.rq.u_l;
11053
11054 /* look up the '?' */
11055 do {
11056 if (ptr == end)
11057 return 0;
11058 } while (*ptr++ != '?');
11059
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011060 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011061 smp->data.u.str.str = ptr;
11062 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011063 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11064 return 1;
11065}
11066
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011067static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011068smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011069{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011070 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11071 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11072 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011073
Willy Tarreau24e32d82012-04-23 23:55:44 +020011074 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011075
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011076 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011077 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011078 return 1;
11079}
11080
Willy Tarreau7f18e522010-10-22 20:04:13 +020011081/* return a valid test if the current request is the first one on the connection */
11082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011083smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011084{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011085 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011086 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011087 return 1;
11088}
11089
Willy Tarreau34db1082012-04-19 17:16:54 +020011090/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011091static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011092smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011093{
11094
Willy Tarreau24e32d82012-04-23 23:55:44 +020011095 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011096 return 0;
11097
Willy Tarreauc0239e02012-04-16 14:42:55 +020011098 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011099
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011100 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011101 return 0;
11102
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011103 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011104 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011105 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011106 return 1;
11107}
Willy Tarreau8797c062007-05-07 00:55:35 +020011108
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011109/* Accepts exactly 1 argument of type userlist */
11110static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011111smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011112{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011113 if (!args || args->type != ARGT_USR)
11114 return 0;
11115
11116 CHECK_HTTP_MESSAGE_FIRST();
11117
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011118 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011119 return 0;
11120
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011121 /* if the user does not belong to the userlist or has a wrong password,
11122 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011123 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011124 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011125 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11126 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011127 return 0;
11128
11129 /* pat_match_auth() will need the user list */
11130 smp->ctx.a[0] = args->data.usr;
11131
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011132 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011133 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011134 smp->data.u.str.str = smp->strm->txn->auth.user;
11135 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011136
11137 return 1;
11138}
11139
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011140/* Try to find the next occurrence of a cookie name in a cookie header value.
11141 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11142 * the cookie value is returned into *value and *value_l, and the function
11143 * returns a pointer to the next pointer to search from if the value was found.
11144 * Otherwise if the cookie was not found, NULL is returned and neither value
11145 * nor value_l are touched. The input <hdr> string should first point to the
11146 * header's value, and the <hdr_end> pointer must point to the first character
11147 * not part of the value. <list> must be non-zero if value may represent a list
11148 * of values (cookie headers). This makes it faster to abort parsing when no
11149 * list is expected.
11150 */
David Carlier4686f792015-09-25 14:10:50 +010011151char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011152extract_cookie_value(char *hdr, const char *hdr_end,
11153 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011154 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011155{
11156 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11157 char *next;
11158
11159 /* we search at least a cookie name followed by an equal, and more
11160 * generally something like this :
11161 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11162 */
11163 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11164 /* Iterate through all cookies on this line */
11165
Willy Tarreau2235b262016-11-05 15:50:20 +010011166 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011167 att_beg++;
11168
11169 /* find att_end : this is the first character after the last non
11170 * space before the equal. It may be equal to hdr_end.
11171 */
11172 equal = att_end = att_beg;
11173
11174 while (equal < hdr_end) {
11175 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11176 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011177 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011178 continue;
11179 att_end = equal;
11180 }
11181
11182 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11183 * is between <att_beg> and <equal>, both may be identical.
11184 */
11185
11186 /* look for end of cookie if there is an equal sign */
11187 if (equal < hdr_end && *equal == '=') {
11188 /* look for the beginning of the value */
11189 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011190 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011191 val_beg++;
11192
11193 /* find the end of the value, respecting quotes */
11194 next = find_cookie_value_end(val_beg, hdr_end);
11195
11196 /* make val_end point to the first white space or delimitor after the value */
11197 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011198 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011199 val_end--;
11200 } else {
11201 val_beg = val_end = next = equal;
11202 }
11203
11204 /* We have nothing to do with attributes beginning with '$'. However,
11205 * they will automatically be removed if a header before them is removed,
11206 * since they're supposed to be linked together.
11207 */
11208 if (*att_beg == '$')
11209 continue;
11210
11211 /* Ignore cookies with no equal sign */
11212 if (equal == next)
11213 continue;
11214
11215 /* Now we have the cookie name between att_beg and att_end, and
11216 * its value between val_beg and val_end.
11217 */
11218
11219 if (att_end - att_beg == cookie_name_l &&
11220 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11221 /* let's return this value and indicate where to go on from */
11222 *value = val_beg;
11223 *value_l = val_end - val_beg;
11224 return next + 1;
11225 }
11226
11227 /* Set-Cookie headers only have the name in the first attr=value part */
11228 if (!list)
11229 break;
11230 }
11231
11232 return NULL;
11233}
11234
William Lallemanda43ba4e2014-01-28 18:14:25 +010011235/* Fetch a captured HTTP request header. The index is the position of
11236 * the "capture" option in the configuration file
11237 */
11238static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011239smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011240{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011241 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011242 int idx;
11243
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011244 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011245 return 0;
11246
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011247 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011248
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011249 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 +010011250 return 0;
11251
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011252 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011253 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str.str = smp->strm->req_cap[idx];
11255 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011256
11257 return 1;
11258}
11259
11260/* Fetch a captured HTTP response header. The index is the position of
11261 * the "capture" option in the configuration file
11262 */
11263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011264smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011265{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011266 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011267 int idx;
11268
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011269 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011270 return 0;
11271
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011272 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011273
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011274 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 +010011275 return 0;
11276
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011277 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011278 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011279 smp->data.u.str.str = smp->strm->res_cap[idx];
11280 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011281
11282 return 1;
11283}
11284
William Lallemand65ad6e12014-01-31 15:08:02 +010011285/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11286static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011287smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011288{
11289 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011290 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011291 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011292
Willy Tarreau15e91e12015-04-04 00:52:09 +020011293 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011294 return 0;
11295
William Lallemand96a77852014-02-05 00:30:02 +010011296 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011297
William Lallemand96a77852014-02-05 00:30:02 +010011298 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11299 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011300
William Lallemand96a77852014-02-05 00:30:02 +010011301 temp = get_trash_chunk();
11302 temp->str = txn->uri;
11303 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011304 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011305 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011306 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011307
11308 return 1;
11309
11310}
11311
11312/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11313static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011314smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011315{
11316 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011317 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011318 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011319
Willy Tarreau15e91e12015-04-04 00:52:09 +020011320 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011321 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011322
William Lallemand65ad6e12014-01-31 15:08:02 +010011323 ptr = txn->uri;
11324
11325 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11326 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011327
William Lallemand65ad6e12014-01-31 15:08:02 +010011328 if (!*ptr)
11329 return 0;
11330
11331 ptr++; /* skip the space */
11332
11333 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011334 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011335 if (!ptr)
11336 return 0;
11337 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11338 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011339
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011340 smp->data.u.str = *temp;
11341 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011342 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011343 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011344
11345 return 1;
11346}
11347
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011348/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11349 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11350 */
11351static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011352smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011353{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011354 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011355
Willy Tarreau15e91e12015-04-04 00:52:09 +020011356 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011357 return 0;
11358
11359 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011360 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011361 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011362 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011363
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011364 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011365 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011366 smp->flags = SMP_F_CONST;
11367 return 1;
11368
11369}
11370
11371/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11372 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11373 */
11374static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011375smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011376{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011377 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011378
Willy Tarreau15e91e12015-04-04 00:52:09 +020011379 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011380 return 0;
11381
11382 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011383 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011384 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011385 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011386
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011387 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011388 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011389 smp->flags = SMP_F_CONST;
11390 return 1;
11391
11392}
11393
William Lallemand65ad6e12014-01-31 15:08:02 +010011394
Willy Tarreaue333ec92012-04-16 16:26:40 +020011395/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011396 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011397 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011398 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011399 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011400 * Accepts exactly 1 argument of type string. If the input options indicate
11401 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011402 * The returned sample is of type CSTR. Can be used to parse cookies in other
11403 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011404 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011405int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011406{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011407 struct http_txn *txn;
11408 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011409 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011410 const struct http_msg *msg;
11411 const char *hdr_name;
11412 int hdr_name_len;
11413 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011414 int occ = 0;
11415 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011416
Willy Tarreau24e32d82012-04-23 23:55:44 +020011417 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011418 return 0;
11419
Willy Tarreaua890d072013-04-02 12:01:06 +020011420 if (!ctx) {
11421 /* first call */
11422 ctx = &static_hdr_ctx;
11423 ctx->idx = 0;
11424 smp->ctx.a[2] = ctx;
11425 }
11426
Willy Tarreaue333ec92012-04-16 16:26:40 +020011427 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011428
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011429 txn = smp->strm->txn;
11430 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011431
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011432 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011433 msg = &txn->req;
11434 hdr_name = "Cookie";
11435 hdr_name_len = 6;
11436 } else {
11437 msg = &txn->rsp;
11438 hdr_name = "Set-Cookie";
11439 hdr_name_len = 10;
11440 }
11441
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011442 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011443 /* no explicit occurrence and single fetch => last cookie by default */
11444 occ = -1;
11445
11446 /* OK so basically here, either we want only one value and it's the
11447 * last one, or we want to iterate over all of them and we fetch the
11448 * next one.
11449 */
11450
Willy Tarreau9b28e032012-10-12 23:49:43 +020011451 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011452 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011453 /* search for the header from the beginning, we must first initialize
11454 * the search parameters.
11455 */
Willy Tarreau37406352012-04-23 16:16:37 +020011456 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011457 ctx->idx = 0;
11458 }
11459
Willy Tarreau28376d62012-04-26 21:26:10 +020011460 smp->flags |= SMP_F_VOL_HDR;
11461
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011462 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011463 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11464 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011465 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11466 goto out;
11467
Willy Tarreau24e32d82012-04-23 23:55:44 +020011468 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011469 continue;
11470
Willy Tarreau37406352012-04-23 16:16:37 +020011471 smp->ctx.a[0] = ctx->line + ctx->val;
11472 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011473 }
11474
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011475 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011476 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011477 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011478 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011479 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011480 &smp->data.u.str.str,
11481 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011482 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011483 found = 1;
11484 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011485 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011486 smp->flags |= SMP_F_NOT_LAST;
11487 return 1;
11488 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011489 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011490 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011491 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011492 /* all cookie headers and values were scanned. If we're looking for the
11493 * last occurrence, we may return it now.
11494 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011495 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011496 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011497 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011498}
11499
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011500/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011501 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011502 * multiple cookies may be parsed on the same line. The returned sample is of
11503 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011504 */
11505static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011506smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011507{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011508 struct http_txn *txn;
11509 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011510 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011511 const struct http_msg *msg;
11512 const char *hdr_name;
11513 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011514 int cnt;
11515 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011516 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011517
Willy Tarreau24e32d82012-04-23 23:55:44 +020011518 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011519 return 0;
11520
Willy Tarreaue333ec92012-04-16 16:26:40 +020011521 CHECK_HTTP_MESSAGE_FIRST();
11522
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011523 txn = smp->strm->txn;
11524 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011525
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011526 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011527 msg = &txn->req;
11528 hdr_name = "Cookie";
11529 hdr_name_len = 6;
11530 } else {
11531 msg = &txn->rsp;
11532 hdr_name = "Set-Cookie";
11533 hdr_name_len = 10;
11534 }
11535
Willy Tarreau9b28e032012-10-12 23:49:43 +020011536 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011537 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011538 ctx.idx = 0;
11539 cnt = 0;
11540
11541 while (1) {
11542 /* Note: val_beg == NULL every time we need to fetch a new header */
11543 if (!val_beg) {
11544 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11545 break;
11546
Willy Tarreau24e32d82012-04-23 23:55:44 +020011547 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011548 continue;
11549
11550 val_beg = ctx.line + ctx.val;
11551 val_end = val_beg + ctx.vlen;
11552 }
11553
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011554 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011555 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011556 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011557 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011558 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011559 &smp->data.u.str.str,
11560 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011561 cnt++;
11562 }
11563 }
11564
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011565 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011566 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011567 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011568 return 1;
11569}
11570
Willy Tarreau51539362012-05-08 12:46:28 +020011571/* Fetch an cookie's integer value. The integer value is returned. It
11572 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11573 */
11574static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011575smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011576{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011577 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011578
11579 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011580 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011581 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011582 }
11583
11584 return ret;
11585}
11586
Willy Tarreau8797c062007-05-07 00:55:35 +020011587/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011588/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011589/************************************************************************/
11590
David Cournapeau16023ee2010-12-23 20:55:41 +090011591/*
11592 * Given a path string and its length, find the position of beginning of the
11593 * query string. Returns NULL if no query string is found in the path.
11594 *
11595 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11596 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011597 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011598 */
bedis4c75cca2012-10-05 08:38:24 +020011599static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011600{
11601 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011602
bedis4c75cca2012-10-05 08:38:24 +020011603 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011604 return p ? p + 1 : NULL;
11605}
11606
bedis4c75cca2012-10-05 08:38:24 +020011607static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011608{
bedis4c75cca2012-10-05 08:38:24 +020011609 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011610}
11611
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011612/* after increasing a pointer value, it can exceed the first buffer
11613 * size. This function transform the value of <ptr> according with
11614 * the expected position. <chunks> is an array of the one or two
11615 * avalaible chunks. The first value is the start of the first chunk,
11616 * the second value if the end+1 of the first chunks. The third value
11617 * is NULL or the start of the second chunk and the fourth value is
11618 * the end+1 of the second chunk. The function returns 1 if does a
11619 * wrap, else returns 0.
11620 */
11621static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11622{
11623 if (*ptr < chunks[1])
11624 return 0;
11625 if (!chunks[2])
11626 return 0;
11627 *ptr = chunks[2] + ( *ptr - chunks[1] );
11628 return 1;
11629}
11630
David Cournapeau16023ee2010-12-23 20:55:41 +090011631/*
11632 * Given a url parameter, find the starting position of the first occurence,
11633 * or NULL if the parameter is not found.
11634 *
11635 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11636 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011637 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011638 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011639 * or the second chunk. The caller must be check the position before using the
11640 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011641 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011642static const char *
11643find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011644 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011645 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011646{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011647 const char *pos, *last, *equal;
11648 const char **bufs = chunks;
11649 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011650
David Cournapeau16023ee2010-12-23 20:55:41 +090011651
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011652 pos = bufs[0];
11653 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011654 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011655 /* Check the equal. */
11656 equal = pos + url_param_name_l;
11657 if (fix_pointer_if_wrap(chunks, &equal)) {
11658 if (equal >= chunks[3])
11659 return NULL;
11660 } else {
11661 if (equal >= chunks[1])
11662 return NULL;
11663 }
11664 if (*equal == '=') {
11665 if (pos + url_param_name_l > last) {
11666 /* process wrap case, we detect a wrap. In this case, the
11667 * comparison is performed in two parts.
11668 */
11669
11670 /* This is the end, we dont have any other chunk. */
11671 if (bufs != chunks || !bufs[2])
11672 return NULL;
11673
11674 /* Compute the length of each part of the comparison. */
11675 l1 = last - pos;
11676 l2 = url_param_name_l - l1;
11677
11678 /* The second buffer is too short to contain the compared string. */
11679 if (bufs[2] + l2 > bufs[3])
11680 return NULL;
11681
11682 if (memcmp(pos, url_param_name, l1) == 0 &&
11683 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11684 return pos;
11685
11686 /* Perform wrapping and jump the string who fail the comparison. */
11687 bufs += 2;
11688 pos = bufs[0] + l2;
11689 last = bufs[1];
11690
11691 } else {
11692 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011693 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11694 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011695 pos += url_param_name_l + 1;
11696 if (fix_pointer_if_wrap(chunks, &pos))
11697 last = bufs[2];
11698 }
11699 }
11700
11701 while (1) {
11702 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011703 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011704 pos++;
11705 if (pos < last)
11706 break;
11707 /* process buffer wrapping. */
11708 if (bufs != chunks || !bufs[2])
11709 return NULL;
11710 bufs += 2;
11711 pos = bufs[0];
11712 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011713 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011714 pos++;
11715 }
11716 return NULL;
11717}
11718
11719/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011720 * Given a url parameter name and a query string, find the next value.
11721 * An empty url_param_name matches the first available parameter.
11722 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11723 * respectively provide a pointer to the value and its end.
11724 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011725 */
11726static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011727find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011728 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011729 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011730{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011731 const char *arg_start, *qs_end;
11732 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011733
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011734 arg_start = chunks[0];
11735 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011736 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 /* Looks for an argument name. */
11738 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011739 url_param_name, url_param_name_l,
11740 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011741 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011742 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011743 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011744 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011745 if (!arg_start)
11746 return 0;
11747
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011748 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011749 while (1) {
11750 /* looks for the first argument. */
11751 value_start = memchr(arg_start, '=', qs_end - arg_start);
11752 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011753 /* Check for wrapping. */
11754 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011755 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011756 chunks[2]) {
11757 arg_start = chunks[2];
11758 qs_end = chunks[3];
11759 continue;
11760 }
11761 return 0;
11762 }
11763 break;
11764 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011765 value_start++;
11766 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011767 else {
11768 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011769 value_start = arg_start + url_param_name_l + 1;
11770
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011771 /* Check for pointer wrapping. */
11772 if (fix_pointer_if_wrap(chunks, &value_start)) {
11773 /* Update the end pointer. */
11774 qs_end = chunks[3];
11775
11776 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011777 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778 return 0;
11779 }
11780 }
11781
David Cournapeau16023ee2010-12-23 20:55:41 +090011782 value_end = value_start;
11783
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011784 while (1) {
11785 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11786 value_end++;
11787 if (value_end < qs_end)
11788 break;
11789 /* process buffer wrapping. */
11790 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011791 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011792 chunks[2]) {
11793 value_end = chunks[2];
11794 qs_end = chunks[3];
11795 continue;
11796 }
11797 break;
11798 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011799
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011800 *vstart = value_start;
11801 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011802 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011803}
11804
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011805/* This scans a URL-encoded query string. It takes an optionally wrapping
11806 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11807 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11808 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011809 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011810static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011811smp_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 +090011812{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011813 const char *vstart, *vend;
11814 struct chunk *temp;
11815 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011816
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011818 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011819 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011820 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011821 return 0;
11822
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011823 /* Create sample. If the value is contiguous, return the pointer as CONST,
11824 * if the value is wrapped, copy-it in a buffer.
11825 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011826 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011827 if (chunks[2] &&
11828 vstart >= chunks[0] && vstart <= chunks[1] &&
11829 vend >= chunks[2] && vend <= chunks[3]) {
11830 /* Wrapped case. */
11831 temp = get_trash_chunk();
11832 memcpy(temp->str, vstart, chunks[1] - vstart);
11833 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011834 smp->data.u.str.str = temp->str;
11835 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011836 } else {
11837 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011838 smp->data.u.str.str = (char *)vstart;
11839 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011840 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11841 }
11842
11843 /* Update context, check wrapping. */
11844 chunks[0] = vend;
11845 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11846 chunks[1] = chunks[3];
11847 chunks[2] = NULL;
11848 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011849
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011850 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011851 smp->flags |= SMP_F_NOT_LAST;
11852
David Cournapeau16023ee2010-12-23 20:55:41 +090011853 return 1;
11854}
11855
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011856/* This function iterates over each parameter of the query string. It uses
11857 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011858 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11859 * An optional parameter name is passed in args[0], otherwise any parameter is
11860 * considered. It supports an optional delimiter argument for the beginning of
11861 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011862 */
11863static int
11864smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11865{
11866 struct http_msg *msg;
11867 char delim = '?';
11868 const char *name;
11869 int name_len;
11870
Dragan Dosen26f77e52015-05-25 10:02:11 +020011871 if (!args ||
11872 (args[0].type && args[0].type != ARGT_STR) ||
11873 (args[1].type && args[1].type != ARGT_STR))
11874 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011875
Dragan Dosen26f77e52015-05-25 10:02:11 +020011876 name = "";
11877 name_len = 0;
11878 if (args->type == ARGT_STR) {
11879 name = args->data.str.str;
11880 name_len = args->data.str.len;
11881 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011882
Dragan Dosen26f77e52015-05-25 10:02:11 +020011883 if (args[1].type)
11884 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011885
Dragan Dosen26f77e52015-05-25 10:02:11 +020011886 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011887 CHECK_HTTP_MESSAGE_FIRST();
11888
11889 msg = &smp->strm->txn->req;
11890
11891 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11892 msg->sl.rq.u_l, delim);
11893 if (!smp->ctx.a[0])
11894 return 0;
11895
11896 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011897
11898 /* Assume that the context is filled with NULL pointer
11899 * before the first call.
11900 * smp->ctx.a[2] = NULL;
11901 * smp->ctx.a[3] = NULL;
11902 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011903 }
11904
11905 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11906}
11907
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011908/* This function iterates over each parameter of the body. This requires
11909 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011910 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11911 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11912 * optional second part if the body wraps at the end of the buffer. An optional
11913 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011914 */
11915static int
11916smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11917{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011918 struct http_msg *msg;
11919 unsigned long len;
11920 unsigned long block1;
11921 char *body;
11922 const char *name;
11923 int name_len;
11924
11925 if (!args || (args[0].type && args[0].type != ARGT_STR))
11926 return 0;
11927
11928 name = "";
11929 name_len = 0;
11930 if (args[0].type == ARGT_STR) {
11931 name = args[0].data.str.str;
11932 name_len = args[0].data.str.len;
11933 }
11934
11935 if (!smp->ctx.a[0]) { // first call, find the query string
11936 CHECK_HTTP_MESSAGE_FIRST();
11937
11938 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011939 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011940 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011941 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011942
11943 len = http_body_bytes(msg);
11944 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11945
11946 block1 = len;
11947 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11948 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11949
11950 if (block1 == len) {
11951 /* buffer is not wrapped (or empty) */
11952 smp->ctx.a[0] = body;
11953 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011954
11955 /* Assume that the context is filled with NULL pointer
11956 * before the first call.
11957 * smp->ctx.a[2] = NULL;
11958 * smp->ctx.a[3] = NULL;
11959 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011960 }
11961 else {
11962 /* buffer is wrapped, we need to defragment it */
11963 smp->ctx.a[0] = body;
11964 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011965 smp->ctx.a[2] = msg->chn->buf->data;
11966 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011967 }
11968 }
11969 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11970}
11971
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011972/* Return the signed integer value for the specified url parameter (see url_param
11973 * above).
11974 */
11975static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011976smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011977{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011978 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011979
11980 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011981 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011982 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011983 }
11984
11985 return ret;
11986}
11987
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011988/* This produces a 32-bit hash of the concatenation of the first occurrence of
11989 * the Host header followed by the path component if it begins with a slash ('/').
11990 * This means that '*' will not be added, resulting in exactly the first Host
11991 * entry. If no Host header is found, then the path is used. The resulting value
11992 * is hashed using the url hash followed by a full avalanche hash and provides a
11993 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11994 * high-traffic sites without having to store whole paths.
11995 * this differs from the base32 functions in that it includes the url parameters
11996 * as well as the path
11997 */
11998static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011999smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012000{
Willy Tarreau15e91e12015-04-04 00:52:09 +020012001 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012002 struct hdr_ctx ctx;
12003 unsigned int hash = 0;
12004 char *ptr, *beg, *end;
12005 int len;
12006
12007 CHECK_HTTP_MESSAGE_FIRST();
12008
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012009 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012010 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012011 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012012 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12013 ptr = ctx.line + ctx.val;
12014 len = ctx.vlen;
12015 while (len--)
12016 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12017 }
12018
12019 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012020 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 +000012021 beg = http_get_path(txn);
12022 if (!beg)
12023 beg = end;
12024
12025 for (ptr = beg; ptr < end ; ptr++);
12026
12027 if (beg < ptr && *beg == '/') {
12028 while (beg < ptr)
12029 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12030 }
12031 hash = full_hash(hash);
12032
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012033 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012034 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012035 smp->flags = SMP_F_VOL_1ST;
12036 return 1;
12037}
12038
12039/* This concatenates the source address with the 32-bit hash of the Host and
12040 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12041 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12042 * on the source address length. The URL hash is stored before the address so
12043 * that in environments where IPv6 is insignificant, truncating the output to
12044 * 8 bytes would still work.
12045 */
12046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012047smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012048{
12049 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012050 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012051
Dragan Dosendb5af612016-06-16 11:23:01 +020012052 if (!cli_conn)
12053 return 0;
12054
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012055 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012056 return 0;
12057
12058 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012059 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12060 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012061
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012062 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012063 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012064 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012065 temp->len += 4;
12066 break;
12067 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012068 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012069 temp->len += 16;
12070 break;
12071 default:
12072 return 0;
12073 }
12074
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012075 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012076 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012077 return 1;
12078}
12079
Willy Tarreau185b5c42012-04-26 15:11:51 +020012080/* This function is used to validate the arguments passed to any "hdr" fetch
12081 * keyword. These keywords support an optional positive or negative occurrence
12082 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12083 * is assumed that the types are already the correct ones. Returns 0 on error,
12084 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12085 * error message in case of error, that the caller is responsible for freeing.
12086 * The initial location must either be freeable or NULL.
12087 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012088int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012089{
12090 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012091 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012092 return 0;
12093 }
12094 return 1;
12095}
12096
Willy Tarreau276fae92013-07-25 14:36:01 +020012097/* takes an UINT value on input supposed to represent the time since EPOCH,
12098 * adds an optional offset found in args[0] and emits a string representing
12099 * the date in RFC-1123/5322 format.
12100 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012101static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012102{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012103 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012104 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12105 struct chunk *temp;
12106 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012107 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012108 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012109
12110 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012111 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012112 curr_date += args[0].data.sint;
12113
12114 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012115 if (!tm)
12116 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012117
12118 temp = get_trash_chunk();
12119 temp->len = snprintf(temp->str, temp->size - temp->len,
12120 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12121 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12122 tm->tm_hour, tm->tm_min, tm->tm_sec);
12123
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012124 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012125 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012126 return 1;
12127}
12128
Thierry FOURNIERad903512014-04-11 17:51:01 +020012129/* Match language range with language tag. RFC2616 14.4:
12130 *
12131 * A language-range matches a language-tag if it exactly equals
12132 * the tag, or if it exactly equals a prefix of the tag such
12133 * that the first tag character following the prefix is "-".
12134 *
12135 * Return 1 if the strings match, else return 0.
12136 */
12137static inline int language_range_match(const char *range, int range_len,
12138 const char *tag, int tag_len)
12139{
12140 const char *end = range + range_len;
12141 const char *tend = tag + tag_len;
12142 while (range < end) {
12143 if (*range == '-' && tag == tend)
12144 return 1;
12145 if (*range != *tag || tag == tend)
12146 return 0;
12147 range++;
12148 tag++;
12149 }
12150 /* Return true only if the last char of the tag is matched. */
12151 return tag == tend;
12152}
12153
12154/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012155static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012156{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012157 const char *al = smp->data.u.str.str;
12158 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012159 const char *token;
12160 int toklen;
12161 int qvalue;
12162 const char *str;
12163 const char *w;
12164 int best_q = 0;
12165
12166 /* Set the constant to the sample, because the output of the
12167 * function will be peek in the constant configuration string.
12168 */
12169 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012170 smp->data.u.str.size = 0;
12171 smp->data.u.str.str = "";
12172 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012173
12174 /* Parse the accept language */
12175 while (1) {
12176
12177 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012178 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012179 al++;
12180 if (al >= end)
12181 break;
12182
12183 /* Start of the fisrt word. */
12184 token = al;
12185
12186 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012187 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012188 al++;
12189 if (al == token)
12190 goto expect_comma;
12191
12192 /* Length of the token. */
12193 toklen = al - token;
12194 qvalue = 1000;
12195
12196 /* Check if the token exists in the list. If the token not exists,
12197 * jump to the next token.
12198 */
12199 str = args[0].data.str.str;
12200 w = str;
12201 while (1) {
12202 if (*str == ';' || *str == '\0') {
12203 if (language_range_match(token, toklen, w, str-w))
12204 goto look_for_q;
12205 if (*str == '\0')
12206 goto expect_comma;
12207 w = str + 1;
12208 }
12209 str++;
12210 }
12211 goto expect_comma;
12212
12213look_for_q:
12214
12215 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012216 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012217 al++;
12218 if (al >= end)
12219 goto process_value;
12220
12221 /* If ',' is found, process the result */
12222 if (*al == ',')
12223 goto process_value;
12224
12225 /* If the character is different from ';', look
12226 * for the end of the header part in best effort.
12227 */
12228 if (*al != ';')
12229 goto expect_comma;
12230
12231 /* Assumes that the char is ';', now expect "q=". */
12232 al++;
12233
12234 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012235 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012236 al++;
12237 if (al >= end)
12238 goto process_value;
12239
12240 /* Expect 'q'. If no 'q', continue in best effort */
12241 if (*al != 'q')
12242 goto process_value;
12243 al++;
12244
12245 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012246 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012247 al++;
12248 if (al >= end)
12249 goto process_value;
12250
12251 /* Expect '='. If no '=', continue in best effort */
12252 if (*al != '=')
12253 goto process_value;
12254 al++;
12255
12256 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012257 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012258 al++;
12259 if (al >= end)
12260 goto process_value;
12261
12262 /* Parse the q value. */
12263 qvalue = parse_qvalue(al, &al);
12264
12265process_value:
12266
12267 /* If the new q value is the best q value, then store the associated
12268 * language in the response. If qvalue is the biggest value (1000),
12269 * break the process.
12270 */
12271 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012272 smp->data.u.str.str = (char *)w;
12273 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012274 if (qvalue >= 1000)
12275 break;
12276 best_q = qvalue;
12277 }
12278
12279expect_comma:
12280
12281 /* Expect comma or end. If the end is detected, quit the loop. */
12282 while (al < end && *al != ',')
12283 al++;
12284 if (al >= end)
12285 break;
12286
12287 /* Comma is found, jump it and restart the analyzer. */
12288 al++;
12289 }
12290
12291 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012292 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12293 smp->data.u.str.str = args[1].data.str.str;
12294 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012295 }
12296
12297 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012298 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012299}
12300
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012301/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012302static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012303{
12304 /* If the constant flag is set or if not size is avalaible at
12305 * the end of the buffer, copy the string in other buffer
12306 * before decoding.
12307 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012308 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012309 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012310 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12311 smp->data.u.str.str = str->str;
12312 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012313 smp->flags &= ~SMP_F_CONST;
12314 }
12315
12316 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012317 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12318 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012319 return 1;
12320}
12321
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012322static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12323{
12324 struct proxy *fe = strm_fe(smp->strm);
12325 int idx, i;
12326 struct cap_hdr *hdr;
12327 int len;
12328
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012329 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012330 return 0;
12331
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012332 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012333
12334 /* Check the availibity of the capture id. */
12335 if (idx > fe->nb_req_cap - 1)
12336 return 0;
12337
12338 /* Look for the original configuration. */
12339 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12340 hdr != NULL && i != idx ;
12341 i--, hdr = hdr->next);
12342 if (!hdr)
12343 return 0;
12344
12345 /* check for the memory allocation */
12346 if (smp->strm->req_cap[hdr->index] == NULL)
12347 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12348 if (smp->strm->req_cap[hdr->index] == NULL)
12349 return 0;
12350
12351 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012352 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012353 if (len > hdr->len)
12354 len = hdr->len;
12355
12356 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012357 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012358 smp->strm->req_cap[idx][len] = '\0';
12359
12360 return 1;
12361}
12362
12363static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12364{
12365 struct proxy *fe = strm_fe(smp->strm);
12366 int idx, i;
12367 struct cap_hdr *hdr;
12368 int len;
12369
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012370 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012371 return 0;
12372
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012373 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012374
12375 /* Check the availibity of the capture id. */
12376 if (idx > fe->nb_rsp_cap - 1)
12377 return 0;
12378
12379 /* Look for the original configuration. */
12380 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12381 hdr != NULL && i != idx ;
12382 i--, hdr = hdr->next);
12383 if (!hdr)
12384 return 0;
12385
12386 /* check for the memory allocation */
12387 if (smp->strm->res_cap[hdr->index] == NULL)
12388 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12389 if (smp->strm->res_cap[hdr->index] == NULL)
12390 return 0;
12391
12392 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012393 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012394 if (len > hdr->len)
12395 len = hdr->len;
12396
12397 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012398 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012399 smp->strm->res_cap[idx][len] = '\0';
12400
12401 return 1;
12402}
12403
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012404/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012405 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406 * the relevant part of the request line accordingly. Then it updates various
12407 * pointers to the next elements which were moved, and the total buffer length.
12408 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012409 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12410 * error, though this can be revisited when this code is finally exploited.
12411 *
12412 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12413 * query string and 3 to replace uri.
12414 *
12415 * In query string case, the mark question '?' must be set at the start of the
12416 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012417 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012418int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012419 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012420{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012421 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012422 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012423 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012424 int delta;
12425
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012426 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012428 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012429 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12430
12431 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012432 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433 txn->req.sl.rq.m_l += delta;
12434 txn->req.sl.rq.u += delta;
12435 txn->req.sl.rq.v += delta;
12436 break;
12437
12438 case 1: // path
12439 cur_ptr = http_get_path(txn);
12440 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012441 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442
12443 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012444 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 +010012445 cur_end++;
12446
12447 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012448 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012449 txn->req.sl.rq.u_l += delta;
12450 txn->req.sl.rq.v += delta;
12451 break;
12452
12453 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012454 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012455 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012456 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12457 while (cur_ptr < cur_end && *cur_ptr != '?')
12458 cur_ptr++;
12459
12460 /* skip the question mark or indicate that we must insert it
12461 * (but only if the format string is not empty then).
12462 */
12463 if (cur_ptr < cur_end)
12464 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012465 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012466 offset = 0;
12467
12468 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012469 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012470 txn->req.sl.rq.u_l += delta;
12471 txn->req.sl.rq.v += delta;
12472 break;
12473
12474 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012475 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012476 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12477
12478 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012479 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012480 txn->req.sl.rq.u_l += delta;
12481 txn->req.sl.rq.v += delta;
12482 break;
12483
12484 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012485 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012486 }
12487
12488 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012489 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012490 txn->req.sl.rq.l += delta;
12491 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012492 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012493 return 0;
12494}
12495
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012496/* This function replace the HTTP status code and the associated message. The
12497 * variable <status> contains the new status code. This function never fails.
12498 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012499void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012500{
12501 struct http_txn *txn = s->txn;
12502 char *cur_ptr, *cur_end;
12503 int delta;
12504 char *res;
12505 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012506 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012507 int msg_len;
12508
12509 chunk_reset(&trash);
12510
12511 res = ultoa_o(status, trash.str, trash.size);
12512 c_l = res - trash.str;
12513
12514 trash.str[c_l] = ' ';
12515 trash.len = c_l + 1;
12516
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012517 /* Do we have a custom reason format string? */
12518 if (msg == NULL)
12519 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012520 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012521 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12522 trash.len += msg_len;
12523
12524 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12525 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12526
12527 /* commit changes and adjust message */
12528 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12529
12530 /* adjust res line offsets and lengths */
12531 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12532 txn->rsp.sl.st.c_l = c_l;
12533 txn->rsp.sl.st.r_l = msg_len;
12534
12535 delta = trash.len - (cur_end - cur_ptr);
12536 txn->rsp.sl.st.l += delta;
12537 txn->hdr_idx.v[0].len += delta;
12538 http_msg_move_end(&txn->rsp, delta);
12539}
12540
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012541/* This function executes one of the set-{method,path,query,uri} actions. It
12542 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012543 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012544 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012545 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12546 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012547 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012548enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012549 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012550{
12551 chunk_reset(&trash);
12552
12553 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012554 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012555 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012556 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012557
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012558 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012559 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012560}
12561
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012562/* This function is just a compliant action wrapper for "set-status". */
12563enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012564 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012565{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012566 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012567 return ACT_RET_CONT;
12568}
12569
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012570/* parse an http-request action among :
12571 * set-method
12572 * set-path
12573 * set-query
12574 * set-uri
12575 *
12576 * All of them accept a single argument of type string representing a log-format.
12577 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12578 * 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 +020012579 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012580 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012581enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12582 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012583{
12584 int cur_arg = *orig_arg;
12585
Thierry FOURNIER42148732015-09-02 17:17:33 +020012586 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012587
12588 switch (args[0][4]) {
12589 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012590 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012591 rule->action_ptr = http_action_set_req_line;
12592 break;
12593 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012594 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012595 rule->action_ptr = http_action_set_req_line;
12596 break;
12597 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012598 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012599 rule->action_ptr = http_action_set_req_line;
12600 break;
12601 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012602 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012603 rule->action_ptr = http_action_set_req_line;
12604 break;
12605 default:
12606 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012607 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012608 }
12609
12610 if (!*args[cur_arg] ||
12611 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12612 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012613 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012614 }
12615
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012616 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012617 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012618 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012619 (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 +010012620 return ACT_RET_PRS_ERR;
12621 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012622
12623 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012624 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012625}
12626
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012627/* parse set-status action:
12628 * This action accepts a single argument of type int representing
12629 * an http status code. It returns ACT_RET_PRS_OK on success,
12630 * ACT_RET_PRS_ERR on error.
12631 */
12632enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12633 struct act_rule *rule, char **err)
12634{
12635 char *error;
12636
Thierry FOURNIER42148732015-09-02 17:17:33 +020012637 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012638 rule->action_ptr = action_http_set_status;
12639
12640 /* Check if an argument is available */
12641 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012642 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012643 return ACT_RET_PRS_ERR;
12644 }
12645
12646 /* convert status code as integer */
12647 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12648 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12649 memprintf(err, "expects an integer status code between 100 and 999");
12650 return ACT_RET_PRS_ERR;
12651 }
12652
12653 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012654
12655 /* set custom reason string */
12656 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12657 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12658 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12659 (*orig_arg)++;
12660 rule->arg.status.reason = strdup(args[*orig_arg]);
12661 (*orig_arg)++;
12662 }
12663
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012664 return ACT_RET_PRS_OK;
12665}
12666
Willy Tarreaua9083d02015-05-08 15:27:59 +020012667/* This function executes the "capture" action. It executes a fetch expression,
12668 * turns the result into a string and puts it in a capture slot. It always
12669 * returns 1. If an error occurs the action is cancelled, but the rule
12670 * processing continues.
12671 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012672enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012673 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012674{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012675 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012676 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012677 char **cap = s->req_cap;
12678 int len;
12679
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012680 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 +020012681 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012682 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012683
12684 if (cap[h->index] == NULL)
12685 cap[h->index] = pool_alloc2(h->pool);
12686
12687 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012688 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012689
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012690 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691 if (len > h->len)
12692 len = h->len;
12693
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012694 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012695 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012696 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012697}
12698
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012699/* This function executes the "capture" action and store the result in a
12700 * capture slot if exists. It executes a fetch expression, turns the result
12701 * into a string and puts it in a capture slot. It always returns 1. If an
12702 * error occurs the action is cancelled, but the rule processing continues.
12703 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012705 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012706{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012708 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012709 char **cap = s->req_cap;
12710 struct proxy *fe = strm_fe(s);
12711 int len;
12712 int i;
12713
12714 /* Look for the original configuration. */
12715 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012716 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012717 i--, h = h->next);
12718 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012719 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012720
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012721 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 +020012722 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012723 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012724
12725 if (cap[h->index] == NULL)
12726 cap[h->index] = pool_alloc2(h->pool);
12727
12728 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012729 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012730
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012731 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012732 if (len > h->len)
12733 len = h->len;
12734
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012735 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012736 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012737 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012738}
12739
Willy Tarreaua9083d02015-05-08 15:27:59 +020012740/* parse an "http-request capture" action. It takes a single argument which is
12741 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012742 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012743 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012744 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012745enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12746 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012747{
12748 struct sample_expr *expr;
12749 struct cap_hdr *hdr;
12750 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012751 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012752
12753 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12754 if (strcmp(args[cur_arg], "if") == 0 ||
12755 strcmp(args[cur_arg], "unless") == 0)
12756 break;
12757
12758 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012759 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012760 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012761 }
12762
Willy Tarreaua9083d02015-05-08 15:27:59 +020012763 cur_arg = *orig_arg;
12764 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12765 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012766 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012767
12768 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12769 memprintf(err,
12770 "fetch method '%s' extracts information from '%s', none of which is available here",
12771 args[cur_arg-1], sample_src_names(expr->fetch->use));
12772 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012773 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012774 }
12775
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012776 if (!args[cur_arg] || !*args[cur_arg]) {
12777 memprintf(err, "expects 'len or 'id'");
12778 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012779 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012780 }
12781
Willy Tarreaua9083d02015-05-08 15:27:59 +020012782 if (strcmp(args[cur_arg], "len") == 0) {
12783 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012784
12785 if (!(px->cap & PR_CAP_FE)) {
12786 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012787 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012788 }
12789
12790 proxy->conf.args.ctx = ARGC_CAP;
12791
Willy Tarreaua9083d02015-05-08 15:27:59 +020012792 if (!args[cur_arg]) {
12793 memprintf(err, "missing length value");
12794 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012795 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012796 }
12797 /* we copy the table name for now, it will be resolved later */
12798 len = atoi(args[cur_arg]);
12799 if (len <= 0) {
12800 memprintf(err, "length must be > 0");
12801 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012802 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012803 }
12804 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012805
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012806 if (!len) {
12807 memprintf(err, "a positive 'len' argument is mandatory");
12808 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012809 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012810 }
12811
Vincent Bernat02779b62016-04-03 13:48:43 +020012812 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012813 hdr->next = px->req_cap;
12814 hdr->name = NULL; /* not a header capture */
12815 hdr->namelen = 0;
12816 hdr->len = len;
12817 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12818 hdr->index = px->nb_req_cap++;
12819
12820 px->req_cap = hdr;
12821 px->to_log |= LW_REQHDR;
12822
Thierry FOURNIER42148732015-09-02 17:17:33 +020012823 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012824 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012825 rule->arg.cap.expr = expr;
12826 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012827 }
12828
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012829 else if (strcmp(args[cur_arg], "id") == 0) {
12830 int id;
12831 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012832
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012833 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012834
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012835 if (!args[cur_arg]) {
12836 memprintf(err, "missing id value");
12837 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012838 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012839 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012840
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012841 id = strtol(args[cur_arg], &error, 10);
12842 if (*error != '\0') {
12843 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12844 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012845 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012846 }
12847 cur_arg++;
12848
12849 proxy->conf.args.ctx = ARGC_CAP;
12850
Thierry FOURNIER42148732015-09-02 17:17:33 +020012851 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012852 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012853 rule->arg.capid.expr = expr;
12854 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012855 }
12856
12857 else {
12858 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12859 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012860 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012861 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012862
12863 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012864 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012865}
12866
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012867/* This function executes the "capture" action and store the result in a
12868 * capture slot if exists. It executes a fetch expression, turns the result
12869 * into a string and puts it in a capture slot. It always returns 1. If an
12870 * error occurs the action is cancelled, but the rule processing continues.
12871 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012872enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012873 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012874{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012875 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012876 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012877 char **cap = s->res_cap;
12878 struct proxy *fe = strm_fe(s);
12879 int len;
12880 int i;
12881
12882 /* Look for the original configuration. */
12883 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012884 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012885 i--, h = h->next);
12886 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012887 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012888
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012889 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 +020012890 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012891 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012892
12893 if (cap[h->index] == NULL)
12894 cap[h->index] = pool_alloc2(h->pool);
12895
12896 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012897 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012898
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012899 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012900 if (len > h->len)
12901 len = h->len;
12902
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012903 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012904 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012905 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012906}
12907
12908/* parse an "http-response capture" action. It takes a single argument which is
12909 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12910 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012911 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012912 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012913enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12914 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012915{
12916 struct sample_expr *expr;
12917 int cur_arg;
12918 int id;
12919 char *error;
12920
12921 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12922 if (strcmp(args[cur_arg], "if") == 0 ||
12923 strcmp(args[cur_arg], "unless") == 0)
12924 break;
12925
12926 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012927 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012928 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012929 }
12930
12931 cur_arg = *orig_arg;
12932 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12933 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012934 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012935
12936 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12937 memprintf(err,
12938 "fetch method '%s' extracts information from '%s', none of which is available here",
12939 args[cur_arg-1], sample_src_names(expr->fetch->use));
12940 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012941 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012942 }
12943
12944 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012945 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012946 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012947 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012948 }
12949
12950 if (strcmp(args[cur_arg], "id") != 0) {
12951 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12952 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012953 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012954 }
12955
12956 cur_arg++;
12957
12958 if (!args[cur_arg]) {
12959 memprintf(err, "missing id value");
12960 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012961 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012962 }
12963
12964 id = strtol(args[cur_arg], &error, 10);
12965 if (*error != '\0') {
12966 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12967 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012968 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012969 }
12970 cur_arg++;
12971
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012972 proxy->conf.args.ctx = ARGC_CAP;
12973
Thierry FOURNIER42148732015-09-02 17:17:33 +020012974 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012975 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012976 rule->arg.capid.expr = expr;
12977 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012978
12979 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012980 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012981}
12982
William Lallemand73025dd2014-04-24 14:38:37 +020012983/*
12984 * Return the struct http_req_action_kw associated to a keyword.
12985 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012986struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012987{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012988 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012989}
12990
12991/*
12992 * Return the struct http_res_action_kw associated to a keyword.
12993 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012994struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012995{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012996 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012997}
12998
Willy Tarreau12207b32016-11-22 19:48:51 +010012999
13000/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
13001 * now.
13002 */
13003static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13004{
13005 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13006 return 1;
13007
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013008 if (*args[2]) {
13009 struct proxy *px;
13010
13011 px = proxy_find_by_name(args[2], 0, 0);
13012 if (px)
13013 appctx->ctx.errors.iid = px->uuid;
13014 else
13015 appctx->ctx.errors.iid = atoi(args[2]);
13016
13017 if (!appctx->ctx.errors.iid) {
13018 appctx->ctx.cli.msg = "No such proxy.\n";
13019 appctx->st0 = CLI_ST_PRINT;
13020 return 1;
13021 }
13022 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013023 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013024 appctx->ctx.errors.iid = -1; // dump all proxies
13025
Willy Tarreau35069f82016-11-25 09:16:37 +010013026 appctx->ctx.errors.flag = 0;
13027 if (strcmp(args[3], "request") == 0)
13028 appctx->ctx.errors.flag |= 4; // ignore response
13029 else if (strcmp(args[3], "response") == 0)
13030 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013031 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013032 return 0;
13033}
13034
13035/* This function dumps all captured errors onto the stream interface's
13036 * read buffer. It returns 0 if the output buffer is full and it needs
13037 * to be called again, otherwise non-zero.
13038 */
13039static int cli_io_handler_show_errors(struct appctx *appctx)
13040{
13041 struct stream_interface *si = appctx->owner;
13042 extern const char *monthname[12];
13043
13044 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13045 return 1;
13046
13047 chunk_reset(&trash);
13048
13049 if (!appctx->ctx.errors.px) {
13050 /* the function had not been called yet, let's prepare the
13051 * buffer for a response.
13052 */
13053 struct tm tm;
13054
13055 get_localtime(date.tv_sec, &tm);
13056 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13057 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13058 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13059 error_snapshot_id);
13060
13061 if (bi_putchk(si_ic(si), &trash) == -1) {
13062 /* Socket buffer full. Let's try again later from the same point */
13063 si_applet_cant_put(si);
13064 return 0;
13065 }
13066
13067 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013068 appctx->ctx.errors.bol = 0;
13069 appctx->ctx.errors.ptr = -1;
13070 }
13071
13072 /* we have two inner loops here, one for the proxy, the other one for
13073 * the buffer.
13074 */
13075 while (appctx->ctx.errors.px) {
13076 struct error_snapshot *es;
13077
Willy Tarreau35069f82016-11-25 09:16:37 +010013078 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013079 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013080 if (appctx->ctx.errors.flag & 2) // skip req
13081 goto next;
13082 }
13083 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013084 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013085 if (appctx->ctx.errors.flag & 4) // skip resp
13086 goto next;
13087 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013088
13089 if (!es->when.tv_sec)
13090 goto next;
13091
13092 if (appctx->ctx.errors.iid >= 0 &&
13093 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13094 es->oe->uuid != appctx->ctx.errors.iid)
13095 goto next;
13096
13097 if (appctx->ctx.errors.ptr < 0) {
13098 /* just print headers now */
13099
13100 char pn[INET6_ADDRSTRLEN];
13101 struct tm tm;
13102 int port;
13103
13104 get_localtime(es->when.tv_sec, &tm);
13105 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13106 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13107 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13108
13109 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13110 case AF_INET:
13111 case AF_INET6:
13112 port = get_host_port(&es->src);
13113 break;
13114 default:
13115 port = 0;
13116 }
13117
Willy Tarreau35069f82016-11-25 09:16:37 +010013118 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013119 case 0:
13120 chunk_appendf(&trash,
13121 " frontend %s (#%d): invalid request\n"
13122 " backend %s (#%d)",
13123 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13124 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13125 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13126 break;
13127 case 1:
13128 chunk_appendf(&trash,
13129 " backend %s (#%d): invalid response\n"
13130 " frontend %s (#%d)",
13131 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13132 es->oe->id, es->oe->uuid);
13133 break;
13134 }
13135
13136 chunk_appendf(&trash,
13137 ", server %s (#%d), event #%u\n"
13138 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013139 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013140 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13141 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13142 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13143 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13144 es->ev_id,
13145 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013146 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013147 es->m_clen, es->m_blen,
13148 es->b_flags, es->b_out, es->b_tot,
13149 es->len, es->b_wrap, es->pos);
13150
13151 if (bi_putchk(si_ic(si), &trash) == -1) {
13152 /* Socket buffer full. Let's try again later from the same point */
13153 si_applet_cant_put(si);
13154 return 0;
13155 }
13156 appctx->ctx.errors.ptr = 0;
13157 appctx->ctx.errors.sid = es->sid;
13158 }
13159
13160 if (appctx->ctx.errors.sid != es->sid) {
13161 /* the snapshot changed while we were dumping it */
13162 chunk_appendf(&trash,
13163 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13164 if (bi_putchk(si_ic(si), &trash) == -1) {
13165 si_applet_cant_put(si);
13166 return 0;
13167 }
13168 goto next;
13169 }
13170
13171 /* OK, ptr >= 0, so we have to dump the current line */
13172 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13173 int newptr;
13174 int newline;
13175
13176 newline = appctx->ctx.errors.bol;
13177 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13178 if (newptr == appctx->ctx.errors.ptr)
13179 return 0;
13180
13181 if (bi_putchk(si_ic(si), &trash) == -1) {
13182 /* Socket buffer full. Let's try again later from the same point */
13183 si_applet_cant_put(si);
13184 return 0;
13185 }
13186 appctx->ctx.errors.ptr = newptr;
13187 appctx->ctx.errors.bol = newline;
13188 };
13189 next:
13190 appctx->ctx.errors.bol = 0;
13191 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013192 appctx->ctx.errors.flag ^= 1;
13193 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013194 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013195 }
13196
13197 /* dump complete */
13198 return 1;
13199}
13200
13201/* register cli keywords */
13202static struct cli_kw_list cli_kws = {{ },{
13203 { { "show", "errors", NULL },
13204 "show errors : report last request and response errors for each proxy",
13205 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13206 },
13207 {{},}
13208}};
13209
Willy Tarreau4a568972010-05-12 08:08:50 +020013210/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013211/* All supported ACL keywords must be declared here. */
13212/************************************************************************/
13213
13214/* Note: must not be declared <const> as its list will be overwritten.
13215 * Please take care of keeping this list alphabetically sorted.
13216 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013217static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013218 { "base", "base", PAT_MATCH_STR },
13219 { "base_beg", "base", PAT_MATCH_BEG },
13220 { "base_dir", "base", PAT_MATCH_DIR },
13221 { "base_dom", "base", PAT_MATCH_DOM },
13222 { "base_end", "base", PAT_MATCH_END },
13223 { "base_len", "base", PAT_MATCH_LEN },
13224 { "base_reg", "base", PAT_MATCH_REG },
13225 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013226
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013227 { "cook", "req.cook", PAT_MATCH_STR },
13228 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13229 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13230 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13231 { "cook_end", "req.cook", PAT_MATCH_END },
13232 { "cook_len", "req.cook", PAT_MATCH_LEN },
13233 { "cook_reg", "req.cook", PAT_MATCH_REG },
13234 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013235
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013236 { "hdr", "req.hdr", PAT_MATCH_STR },
13237 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13238 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13239 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13240 { "hdr_end", "req.hdr", PAT_MATCH_END },
13241 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13242 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13243 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013244
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013245 /* these two declarations uses strings with list storage (in place
13246 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13247 * and delete functions are relative to the list management. The parse
13248 * and match method are related to the corresponding fetch methods. This
13249 * is very particular ACL declaration mode.
13250 */
13251 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13252 { "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 +020013253
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013254 { "path", "path", PAT_MATCH_STR },
13255 { "path_beg", "path", PAT_MATCH_BEG },
13256 { "path_dir", "path", PAT_MATCH_DIR },
13257 { "path_dom", "path", PAT_MATCH_DOM },
13258 { "path_end", "path", PAT_MATCH_END },
13259 { "path_len", "path", PAT_MATCH_LEN },
13260 { "path_reg", "path", PAT_MATCH_REG },
13261 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013262
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013263 { "req_ver", "req.ver", PAT_MATCH_STR },
13264 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013265
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013266 { "scook", "res.cook", PAT_MATCH_STR },
13267 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13268 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13269 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13270 { "scook_end", "res.cook", PAT_MATCH_END },
13271 { "scook_len", "res.cook", PAT_MATCH_LEN },
13272 { "scook_reg", "res.cook", PAT_MATCH_REG },
13273 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013274
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013275 { "shdr", "res.hdr", PAT_MATCH_STR },
13276 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13277 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13278 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13279 { "shdr_end", "res.hdr", PAT_MATCH_END },
13280 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13281 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13282 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013283
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013284 { "url", "url", PAT_MATCH_STR },
13285 { "url_beg", "url", PAT_MATCH_BEG },
13286 { "url_dir", "url", PAT_MATCH_DIR },
13287 { "url_dom", "url", PAT_MATCH_DOM },
13288 { "url_end", "url", PAT_MATCH_END },
13289 { "url_len", "url", PAT_MATCH_LEN },
13290 { "url_reg", "url", PAT_MATCH_REG },
13291 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013292
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013293 { "urlp", "urlp", PAT_MATCH_STR },
13294 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13295 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13296 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13297 { "urlp_end", "urlp", PAT_MATCH_END },
13298 { "urlp_len", "urlp", PAT_MATCH_LEN },
13299 { "urlp_reg", "urlp", PAT_MATCH_REG },
13300 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013301
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013302 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013303}};
13304
13305/************************************************************************/
13306/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013307/************************************************************************/
13308/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013309static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013310 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013311 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013312 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13313
Willy Tarreau87b09662015-04-03 00:22:06 +020013314 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013315 { "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 +020013316
13317 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013318 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13319 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13320 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013321
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013322 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13323 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013324
Willy Tarreau409bcde2013-01-08 00:31:00 +010013325 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13326 * are only here to match the ACL's name, are request-only and are used
13327 * for ACL compatibility only.
13328 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013329 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13330 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013331 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13332 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013333
13334 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13335 * only here to match the ACL's name, are request-only and are used for
13336 * ACL compatibility only.
13337 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013338 { "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 +020013339 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013340 { "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 +020013341 { "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 +010013342
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013343 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013344 { "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 +010013345 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013346 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013347 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013348 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013349
13350 /* HTTP protocol on the request path */
13351 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013352 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013353
13354 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013355 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13356 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013357
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013358 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013359 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13360 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013361 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013362
Willy Tarreau18ed2562013-01-14 15:56:36 +010013363 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013364 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13365 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013366
Willy Tarreau18ed2562013-01-14 15:56:36 +010013367 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013368 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013369 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13370 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013371
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013372 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013373 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013374 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013375 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013376 { "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 +010013377 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013378 { "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 +010013379
13380 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013381 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013382 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13383 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013384
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013385 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013386 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013387 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013388 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013389 { "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 +010013390 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013391 { "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 +010013392
Willy Tarreau409bcde2013-01-08 00:31:00 +010013393 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013394 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013395 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13396 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013397 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013398
13399 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013400 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013401 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013402 { "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 +020013403 { "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 +010013404
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013405 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013406 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013407 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013408 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013409 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013410 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013411 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013412 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13413 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013414 { "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 +010013415 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013416}};
13417
Willy Tarreau8797c062007-05-07 00:55:35 +020013418
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013419/************************************************************************/
13420/* All supported converter keywords must be declared here. */
13421/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013422/* Note: must not be declared <const> as its list will be overwritten */
13423static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013424 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013425 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013426 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13427 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013428 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013429 { NULL, NULL, 0, 0, 0 },
13430}};
13431
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013432
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013433/************************************************************************/
13434/* All supported http-request action keywords must be declared here. */
13435/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013436struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013437 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013438 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013439 { "set-method", parse_set_req_line },
13440 { "set-path", parse_set_req_line },
13441 { "set-query", parse_set_req_line },
13442 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013443 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013444 }
13445};
13446
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013447struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013448 .kw = {
13449 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013450 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013451 { NULL, NULL }
13452 }
13453};
13454
Willy Tarreau8797c062007-05-07 00:55:35 +020013455__attribute__((constructor))
13456static void __http_protocol_init(void)
13457{
13458 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013459 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013460 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013461 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013462 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013463 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013464}
13465
13466
Willy Tarreau58f10d72006-12-04 02:26:12 +010013467/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013468 * Local variables:
13469 * c-indent-level: 8
13470 * c-basic-offset: 8
13471 * End:
13472 */