blob: c83e9428b51d3d678152d3a53deb0d3db51ed455 [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);
William Lallemanda73203e2012-03-12 12:48:57 +0100462 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
Willy Tarreau53b6c742006-12-17 13:37:46 +0100465/*
466 * We have 26 list of methods (1 per first letter), each of which can have
467 * up to 3 entries (2 valid, 1 null).
468 */
469struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100470 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100471 int len;
472 const char text[8];
473};
474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100475const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100476 ['C' - 'A'] = {
477 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
478 },
479 ['D' - 'A'] = {
480 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
481 },
482 ['G' - 'A'] = {
483 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
484 },
485 ['H' - 'A'] = {
486 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
487 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200488 ['O' - 'A'] = {
489 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
490 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100491 ['P' - 'A'] = {
492 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
493 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
494 },
495 ['T' - 'A'] = {
496 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
497 },
498 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200499 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100500 */
501};
502
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100503const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
505 [HTTP_METH_GET] = { "GET", 3 },
506 [HTTP_METH_HEAD] = { "HEAD", 4 },
507 [HTTP_METH_POST] = { "POST", 4 },
508 [HTTP_METH_PUT] = { "PUT", 3 },
509 [HTTP_METH_DELETE] = { "DELETE", 6 },
510 [HTTP_METH_TRACE] = { "TRACE", 5 },
511 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
512};
513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100514/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200515 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100516 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
517 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
518 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
519 * digit. Note: please do not overwrite values in assignment since gcc-2.95
520 * will not handle them correctly. It's worth noting that chars 128..255 are
521 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100522 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100523const unsigned char http_char_classes[256] = {
524 [ 0] = HTTP_FLG_CTL,
525 [ 1] = HTTP_FLG_CTL,
526 [ 2] = HTTP_FLG_CTL,
527 [ 3] = HTTP_FLG_CTL,
528 [ 4] = HTTP_FLG_CTL,
529 [ 5] = HTTP_FLG_CTL,
530 [ 6] = HTTP_FLG_CTL,
531 [ 7] = HTTP_FLG_CTL,
532 [ 8] = HTTP_FLG_CTL,
533 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
534 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
535 [ 11] = HTTP_FLG_CTL,
536 [ 12] = HTTP_FLG_CTL,
537 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
538 [ 14] = HTTP_FLG_CTL,
539 [ 15] = HTTP_FLG_CTL,
540 [ 16] = HTTP_FLG_CTL,
541 [ 17] = HTTP_FLG_CTL,
542 [ 18] = HTTP_FLG_CTL,
543 [ 19] = HTTP_FLG_CTL,
544 [ 20] = HTTP_FLG_CTL,
545 [ 21] = HTTP_FLG_CTL,
546 [ 22] = HTTP_FLG_CTL,
547 [ 23] = HTTP_FLG_CTL,
548 [ 24] = HTTP_FLG_CTL,
549 [ 25] = HTTP_FLG_CTL,
550 [ 26] = HTTP_FLG_CTL,
551 [ 27] = HTTP_FLG_CTL,
552 [ 28] = HTTP_FLG_CTL,
553 [ 29] = HTTP_FLG_CTL,
554 [ 30] = HTTP_FLG_CTL,
555 [ 31] = HTTP_FLG_CTL,
556 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
557 ['!'] = HTTP_FLG_TOK,
558 ['"'] = HTTP_FLG_SEP,
559 ['#'] = HTTP_FLG_TOK,
560 ['$'] = HTTP_FLG_TOK,
561 ['%'] = HTTP_FLG_TOK,
562 ['&'] = HTTP_FLG_TOK,
563 [ 39] = HTTP_FLG_TOK,
564 ['('] = HTTP_FLG_SEP,
565 [')'] = HTTP_FLG_SEP,
566 ['*'] = HTTP_FLG_TOK,
567 ['+'] = HTTP_FLG_TOK,
568 [','] = HTTP_FLG_SEP,
569 ['-'] = HTTP_FLG_TOK,
570 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
571 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
572 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
573 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 [':'] = HTTP_FLG_SEP,
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 ['A'] = HTTP_FLG_TOK,
590 ['B'] = HTTP_FLG_TOK,
591 ['C'] = HTTP_FLG_TOK,
592 ['D'] = HTTP_FLG_TOK,
593 ['E'] = HTTP_FLG_TOK,
594 ['F'] = HTTP_FLG_TOK,
595 ['G'] = HTTP_FLG_TOK,
596 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
597 ['I'] = HTTP_FLG_TOK,
598 ['J'] = HTTP_FLG_TOK,
599 ['K'] = HTTP_FLG_TOK,
600 ['L'] = HTTP_FLG_TOK,
601 ['M'] = HTTP_FLG_TOK,
602 ['N'] = HTTP_FLG_TOK,
603 ['O'] = HTTP_FLG_TOK,
604 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
605 ['Q'] = HTTP_FLG_TOK,
606 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
607 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['U'] = HTTP_FLG_TOK,
610 ['V'] = HTTP_FLG_TOK,
611 ['W'] = HTTP_FLG_TOK,
612 ['X'] = HTTP_FLG_TOK,
613 ['Y'] = HTTP_FLG_TOK,
614 ['Z'] = HTTP_FLG_TOK,
615 ['['] = HTTP_FLG_SEP,
616 [ 92] = HTTP_FLG_SEP,
617 [']'] = HTTP_FLG_SEP,
618 ['^'] = HTTP_FLG_TOK,
619 ['_'] = HTTP_FLG_TOK,
620 ['`'] = HTTP_FLG_TOK,
621 ['a'] = HTTP_FLG_TOK,
622 ['b'] = HTTP_FLG_TOK,
623 ['c'] = HTTP_FLG_TOK,
624 ['d'] = HTTP_FLG_TOK,
625 ['e'] = HTTP_FLG_TOK,
626 ['f'] = HTTP_FLG_TOK,
627 ['g'] = HTTP_FLG_TOK,
628 ['h'] = HTTP_FLG_TOK,
629 ['i'] = HTTP_FLG_TOK,
630 ['j'] = HTTP_FLG_TOK,
631 ['k'] = HTTP_FLG_TOK,
632 ['l'] = HTTP_FLG_TOK,
633 ['m'] = HTTP_FLG_TOK,
634 ['n'] = HTTP_FLG_TOK,
635 ['o'] = HTTP_FLG_TOK,
636 ['p'] = HTTP_FLG_TOK,
637 ['q'] = HTTP_FLG_TOK,
638 ['r'] = HTTP_FLG_TOK,
639 ['s'] = HTTP_FLG_TOK,
640 ['t'] = HTTP_FLG_TOK,
641 ['u'] = HTTP_FLG_TOK,
642 ['v'] = HTTP_FLG_TOK,
643 ['w'] = HTTP_FLG_TOK,
644 ['x'] = HTTP_FLG_TOK,
645 ['y'] = HTTP_FLG_TOK,
646 ['z'] = HTTP_FLG_TOK,
647 ['{'] = HTTP_FLG_SEP,
648 ['|'] = HTTP_FLG_TOK,
649 ['}'] = HTTP_FLG_SEP,
650 ['~'] = HTTP_FLG_TOK,
651 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100652};
653
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100654/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100655 * Adds a header and its CRLF at the tail of the message's buffer, just before
656 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100657 * The header is also automatically added to the index <hdr_idx>, and the end
658 * of headers is automatically adjusted. The number of bytes added is returned
659 * on success, otherwise <0 is returned indicating an error.
660 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100661int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100662{
663 int bytes, len;
664
665 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200666 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100667 if (!bytes)
668 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100669 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100670 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
671}
672
673/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100674 * Adds a header and its CRLF at the tail of the message's buffer, just before
675 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100676 * the buffer is only opened and the space reserved, but nothing is copied.
677 * The header is also automatically added to the index <hdr_idx>, and the end
678 * of headers is automatically adjusted. The number of bytes added is returned
679 * on success, otherwise <0 is returned indicating an error.
680 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100681int http_header_add_tail2(struct http_msg *msg,
682 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100683{
684 int bytes;
685
Willy Tarreau9b28e032012-10-12 23:49:43 +0200686 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100687 if (!bytes)
688 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100689 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100690 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
691}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692
693/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100694 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
695 * If so, returns the position of the first non-space character relative to
696 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
697 * to return a pointer to the place after the first space. Returns 0 if the
698 * header name does not match. Checks are case-insensitive.
699 */
700int http_header_match2(const char *hdr, const char *end,
701 const char *name, int len)
702{
703 const char *val;
704
705 if (hdr + len >= end)
706 return 0;
707 if (hdr[len] != ':')
708 return 0;
709 if (strncasecmp(hdr, name, len) != 0)
710 return 0;
711 val = hdr + len + 1;
712 while (val < end && HTTP_IS_SPHT(*val))
713 val++;
714 if ((val >= end) && (len + 2 <= end - hdr))
715 return len + 2; /* we may replace starting from second space */
716 return val - hdr;
717}
718
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200719/* Find the first or next occurrence of header <name> in message buffer <sol>
720 * using headers index <idx>, and return it in the <ctx> structure. This
721 * structure holds everything necessary to use the header and find next
722 * occurrence. If its <idx> member is 0, the header is searched from the
723 * beginning. Otherwise, the next occurrence is returned. The function returns
724 * 1 when it finds a value, and 0 when there is no more. It is very similar to
725 * http_find_header2() except that it is designed to work with full-line headers
726 * whose comma is not a delimiter but is part of the syntax. As a special case,
727 * if ctx->val is NULL when searching for a new values of a header, the current
728 * header is rescanned. This allows rescanning after a header deletion.
729 */
730int http_find_full_header2(const char *name, int len,
731 char *sol, struct hdr_idx *idx,
732 struct hdr_ctx *ctx)
733{
734 char *eol, *sov;
735 int cur_idx, old_idx;
736
737 cur_idx = ctx->idx;
738 if (cur_idx) {
739 /* We have previously returned a header, let's search another one */
740 sol = ctx->line;
741 eol = sol + idx->v[cur_idx].len;
742 goto next_hdr;
743 }
744
745 /* first request for this header */
746 sol += hdr_idx_first_pos(idx);
747 old_idx = 0;
748 cur_idx = hdr_idx_first_idx(idx);
749 while (cur_idx) {
750 eol = sol + idx->v[cur_idx].len;
751
752 if (len == 0) {
753 /* No argument was passed, we want any header.
754 * To achieve this, we simply build a fake request. */
755 while (sol + len < eol && sol[len] != ':')
756 len++;
757 name = sol;
758 }
759
760 if ((len < eol - sol) &&
761 (sol[len] == ':') &&
762 (strncasecmp(sol, name, len) == 0)) {
763 ctx->del = len;
764 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100765 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200766 sov++;
767
768 ctx->line = sol;
769 ctx->prev = old_idx;
770 ctx->idx = cur_idx;
771 ctx->val = sov - sol;
772 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100773 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200774 eol--;
775 ctx->tws++;
776 }
777 ctx->vlen = eol - sov;
778 return 1;
779 }
780 next_hdr:
781 sol = eol + idx->v[cur_idx].cr + 1;
782 old_idx = cur_idx;
783 cur_idx = idx->v[cur_idx].next;
784 }
785 return 0;
786}
787
Willy Tarreauc90dc232015-02-20 13:51:36 +0100788/* Find the first or next header field in message buffer <sol> using headers
789 * index <idx>, and return it in the <ctx> structure. This structure holds
790 * everything necessary to use the header and find next occurrence. If its
791 * <idx> member is 0, the first header is retrieved. Otherwise, the next
792 * occurrence is returned. The function returns 1 when it finds a value, and
793 * 0 when there is no more. It is equivalent to http_find_full_header2() with
794 * no header name.
795 */
796int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
797{
798 char *eol, *sov;
799 int cur_idx, old_idx;
800 int len;
801
802 cur_idx = ctx->idx;
803 if (cur_idx) {
804 /* We have previously returned a header, let's search another one */
805 sol = ctx->line;
806 eol = sol + idx->v[cur_idx].len;
807 goto next_hdr;
808 }
809
810 /* first request for this header */
811 sol += hdr_idx_first_pos(idx);
812 old_idx = 0;
813 cur_idx = hdr_idx_first_idx(idx);
814 while (cur_idx) {
815 eol = sol + idx->v[cur_idx].len;
816
817 len = 0;
818 while (1) {
819 if (len >= eol - sol)
820 goto next_hdr;
821 if (sol[len] == ':')
822 break;
823 len++;
824 }
825
826 ctx->del = len;
827 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100828 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100829 sov++;
830
831 ctx->line = sol;
832 ctx->prev = old_idx;
833 ctx->idx = cur_idx;
834 ctx->val = sov - sol;
835 ctx->tws = 0;
836
Willy Tarreau2235b262016-11-05 15:50:20 +0100837 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100838 eol--;
839 ctx->tws++;
840 }
841 ctx->vlen = eol - sov;
842 return 1;
843
844 next_hdr:
845 sol = eol + idx->v[cur_idx].cr + 1;
846 old_idx = cur_idx;
847 cur_idx = idx->v[cur_idx].next;
848 }
849 return 0;
850}
851
Lukas Tribus23953682017-04-28 13:24:30 +0000852/* Find the end of the header value contained between <s> and <e>. See RFC7230,
853 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100854 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200855 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100856char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200857{
858 int quoted, qdpair;
859
860 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100861
862#if defined(__x86_64__) || \
863 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
864 defined(__ARM_ARCH_7A__)
865 /* speedup: skip everything not a comma nor a double quote */
866 for (; s <= e - sizeof(int); s += sizeof(int)) {
867 unsigned int c = *(int *)s; // comma
868 unsigned int q = c; // quote
869
870 c ^= 0x2c2c2c2c; // contains one zero on a comma
871 q ^= 0x22222222; // contains one zero on a quote
872
873 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
874 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
875
876 if ((c | q) & 0x80808080)
877 break; // found a comma or a quote
878 }
879#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200880 for (; s < e; s++) {
881 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200882 else if (quoted) {
883 if (*s == '\\') qdpair = 1;
884 else if (*s == '"') quoted = 0;
885 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200886 else if (*s == '"') quoted = 1;
887 else if (*s == ',') return s;
888 }
889 return s;
890}
891
892/* Find the first or next occurrence of header <name> in message buffer <sol>
893 * using headers index <idx>, and return it in the <ctx> structure. This
894 * structure holds everything necessary to use the header and find next
895 * occurrence. If its <idx> member is 0, the header is searched from the
896 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 * 1 when it finds a value, and 0 when there is no more. It is designed to work
898 * with headers defined as comma-separated lists. As a special case, if ctx->val
899 * is NULL when searching for a new values of a header, the current header is
900 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200901 */
902int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200904 struct hdr_ctx *ctx)
905{
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 char *eol, *sov;
907 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200908
Willy Tarreau68085d82010-01-18 14:54:04 +0100909 cur_idx = ctx->idx;
910 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200911 /* We have previously returned a value, let's search
912 * another one on the same line.
913 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200914 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200915 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200917 eol = sol + idx->v[cur_idx].len;
918
919 if (sov >= eol)
920 /* no more values in this header */
921 goto next_hdr;
922
Willy Tarreau68085d82010-01-18 14:54:04 +0100923 /* values remaining for this header, skip the comma but save it
924 * for later use (eg: for header deletion).
925 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200926 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100927 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200928 sov++;
929
930 goto return_hdr;
931 }
932
933 /* first request for this header */
934 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100935 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200936 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 while (cur_idx) {
938 eol = sol + idx->v[cur_idx].len;
939
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200940 if (len == 0) {
941 /* No argument was passed, we want any header.
942 * To achieve this, we simply build a fake request. */
943 while (sol + len < eol && sol[len] != ':')
944 len++;
945 name = sol;
946 }
947
Willy Tarreau33a7e692007-06-10 19:45:56 +0200948 if ((len < eol - sol) &&
949 (sol[len] == ':') &&
950 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100951 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200952 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100953 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200954 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100955
Willy Tarreau33a7e692007-06-10 19:45:56 +0200956 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100957 ctx->prev = old_idx;
958 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200959 ctx->idx = cur_idx;
960 ctx->val = sov - sol;
961
962 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200963 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100964 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200965 eol--;
966 ctx->tws++;
967 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200968 ctx->vlen = eol - sov;
969 return 1;
970 }
971 next_hdr:
972 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100973 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200974 cur_idx = idx->v[cur_idx].next;
975 }
976 return 0;
977}
978
979int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100980 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200981 struct hdr_ctx *ctx)
982{
983 return http_find_header2(name, strlen(name), sol, idx, ctx);
984}
985
Willy Tarreau68085d82010-01-18 14:54:04 +0100986/* Remove one value of a header. This only works on a <ctx> returned by one of
987 * the http_find_header functions. The value is removed, as well as surrounding
988 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100989 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100990 * message <msg>. The new index is returned. If it is zero, it means there is
991 * no more header, so any processing may stop. The ctx is always left in a form
992 * that can be handled by http_find_header2() to find next occurrence.
993 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100994int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100995{
996 int cur_idx = ctx->idx;
997 char *sol = ctx->line;
998 struct hdr_idx_elem *hdr;
999 int delta, skip_comma;
1000
1001 if (!cur_idx)
1002 return 0;
1003
1004 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001005 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001006 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001007 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001008 http_msg_move_end(msg, delta);
1009 idx->used--;
1010 hdr->len = 0; /* unused entry */
1011 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001012 if (idx->tail == ctx->idx)
1013 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001014 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001015 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001016 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001017 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001018 return ctx->idx;
1019 }
1020
1021 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001022 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1023 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001024 */
1025
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001026 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001027 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001028 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001029 NULL, 0);
1030 hdr->len += delta;
1031 http_msg_move_end(msg, delta);
1032 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001033 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001034 return ctx->idx;
1035}
1036
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001037/* This function handles a server error at the stream interface level. The
1038 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +01001039 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001040 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001041 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001043static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +01001044 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001045{
Willy Tarreau2019f952017-03-14 11:07:31 +01001046 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001047 channel_auto_read(si_oc(si));
1048 channel_abort(si_oc(si));
1049 channel_auto_close(si_oc(si));
1050 channel_erase(si_oc(si));
1051 channel_auto_close(si_ic(si));
1052 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001053 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001054 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001055 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001056 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001057 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001058 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059}
1060
Willy Tarreau87b09662015-04-03 00:22:06 +02001061/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001062 * and message.
1063 */
1064
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001065struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001066{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001067 const int msgnum = http_get_status_idx(s->txn->status);
1068
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001069 if (s->be->errmsg[msgnum].str)
1070 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001071 else if (strm_fe(s)->errmsg[msgnum].str)
1072 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001073 else
1074 return &http_err_chunks[msgnum];
1075}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001077void
1078http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1079{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001080 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001081 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001082 stream_int_retnclose(&s->si[0], msg);
1083}
1084
Willy Tarreau53b6c742006-12-17 13:37:46 +01001085/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001086 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1087 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001088 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001089enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001090{
1091 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001093
1094 m = ((unsigned)*str - 'A');
1095
1096 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 for (h = http_methods[m]; h->len > 0; h++) {
1098 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001099 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001101 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001103 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001104 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001105}
1106
Willy Tarreau21d2af32008-02-14 20:25:24 +01001107/* Parse the URI from the given transaction (which is assumed to be in request
1108 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1109 * It is returned otherwise.
1110 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001111char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001112{
1113 char *ptr, *end;
1114
Willy Tarreau9b28e032012-10-12 23:49:43 +02001115 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001116 end = ptr + txn->req.sl.rq.u_l;
1117
1118 if (ptr >= end)
1119 return NULL;
1120
Lukas Tribus23953682017-04-28 13:24:30 +00001121 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +01001122 * Request-URI = "*" | absuri | abspath | authority
1123 */
1124
1125 if (*ptr == '*')
1126 return NULL;
1127
1128 if (isalpha((unsigned char)*ptr)) {
1129 /* this is a scheme as described by RFC3986, par. 3.1 */
1130 ptr++;
1131 while (ptr < end &&
1132 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1133 ptr++;
1134 /* skip '://' */
1135 if (ptr == end || *ptr++ != ':')
1136 return NULL;
1137 if (ptr == end || *ptr++ != '/')
1138 return NULL;
1139 if (ptr == end || *ptr++ != '/')
1140 return NULL;
1141 }
1142 /* skip [user[:passwd]@]host[:[port]] */
1143
1144 while (ptr < end && *ptr != '/')
1145 ptr++;
1146
1147 if (ptr == end)
1148 return NULL;
1149
1150 /* OK, we got the '/' ! */
1151 return ptr;
1152}
1153
William Lallemand65ad6e12014-01-31 15:08:02 +01001154/* Parse the URI from the given string and look for the "/" beginning the PATH.
1155 * If not found, return NULL. It is returned otherwise.
1156 */
1157static char *
1158http_get_path_from_string(char *str)
1159{
1160 char *ptr = str;
1161
1162 /* RFC2616, par. 5.1.2 :
1163 * Request-URI = "*" | absuri | abspath | authority
1164 */
1165
1166 if (*ptr == '*')
1167 return NULL;
1168
1169 if (isalpha((unsigned char)*ptr)) {
1170 /* this is a scheme as described by RFC3986, par. 3.1 */
1171 ptr++;
1172 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1173 ptr++;
1174 /* skip '://' */
1175 if (*ptr == '\0' || *ptr++ != ':')
1176 return NULL;
1177 if (*ptr == '\0' || *ptr++ != '/')
1178 return NULL;
1179 if (*ptr == '\0' || *ptr++ != '/')
1180 return NULL;
1181 }
1182 /* skip [user[:passwd]@]host[:[port]] */
1183
1184 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1185 ptr++;
1186
1187 if (*ptr == '\0' || *ptr == ' ')
1188 return NULL;
1189
1190 /* OK, we got the '/' ! */
1191 return ptr;
1192}
1193
Willy Tarreau71241ab2012-12-27 11:30:54 +01001194/* Returns a 302 for a redirectable request that reaches a server working in
1195 * in redirect mode. This may only be called just after the stream interface
1196 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1197 * follow normal proxy processing. NOTE: this function is designed to support
1198 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001199 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001200void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001201{
1202 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001203 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001204 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001205 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001206
1207 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001208 trash.len = strlen(HTTP_302);
1209 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001210
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001211 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001212
Willy Tarreauefb453c2008-10-26 20:49:47 +01001213 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001214 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001215 return;
1216
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001217 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001218 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001219 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1220 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001221 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001222
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001223 /* 3: add the request URI. Since it was already forwarded, we need
1224 * to temporarily rewind the buffer.
1225 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001226 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001227 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001228
Willy Tarreauefb453c2008-10-26 20:49:47 +01001229 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001230 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 +02001231
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001232 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001233
Willy Tarreauefb453c2008-10-26 20:49:47 +01001234 if (!path)
1235 return;
1236
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001237 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001238 return;
1239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001240 memcpy(trash.str + trash.len, path, len);
1241 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001242
1243 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001244 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1245 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001246 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001247 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1248 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001249 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001250
1251 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001252 si_shutr(si);
1253 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001254 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001255 si->state = SI_ST_CLO;
1256
1257 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001258 txn->status = 302;
1259 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001260
1261 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001262 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001263 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001264}
1265
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001266/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001267 * that the server side is closed. Note that err_type is actually a
1268 * bitmask, where almost only aborts may be cumulated with other
1269 * values. We consider that aborted operations are more important
1270 * than timeouts or errors due to the fact that nobody else in the
1271 * logs might explain incomplete retries. All others should avoid
1272 * being cumulated. It should normally not be possible to have multiple
1273 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001274 * Note that connection errors appearing on the second request of a keep-alive
1275 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001276 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001277void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001279 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001280
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001281 /* set s->txn->status for http_error_message(s) */
1282 s->txn->status = 503;
1283
Willy Tarreauefb453c2008-10-26 20:49:47 +01001284 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001285 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001286 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001287 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001288 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001289 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001290 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001291 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001292 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001293 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001294 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001295 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001296 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001297 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001298 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001299 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001300 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001301 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001302 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001303 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001304 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001305 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001306 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001307 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001308 http_error_message(s));
1309 else { /* SI_ET_CONN_OTHER and others */
1310 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001311 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001312 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001313 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001314}
1315
Willy Tarreau42250582007-04-01 01:30:43 +02001316extern const char sess_term_cond[8];
1317extern const char sess_fin_state[8];
1318extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001319struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001320struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001321struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001322struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323
Willy Tarreau117f59e2007-03-04 18:17:17 +01001324/*
1325 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001326 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001327 */
1328void capture_headers(char *som, struct hdr_idx *idx,
1329 char **cap, struct cap_hdr *cap_hdr)
1330{
1331 char *eol, *sol, *col, *sov;
1332 int cur_idx;
1333 struct cap_hdr *h;
1334 int len;
1335
1336 sol = som + hdr_idx_first_pos(idx);
1337 cur_idx = hdr_idx_first_idx(idx);
1338
1339 while (cur_idx) {
1340 eol = sol + idx->v[cur_idx].len;
1341
1342 col = sol;
1343 while (col < eol && *col != ':')
1344 col++;
1345
1346 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001347 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001348 sov++;
1349
1350 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001351 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001352 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1353 if (cap[h->index] == NULL)
1354 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001355 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001356
1357 if (cap[h->index] == NULL) {
1358 Alert("HTTP capture : out of memory.\n");
1359 continue;
1360 }
1361
1362 len = eol - sov;
1363 if (len > h->len)
1364 len = h->len;
1365
1366 memcpy(cap[h->index], sov, len);
1367 cap[h->index][len]=0;
1368 }
1369 }
1370 sol = eol + idx->v[cur_idx].cr + 1;
1371 cur_idx = idx->v[cur_idx].next;
1372 }
1373}
1374
Willy Tarreaubaaee002006-06-26 02:48:02 +02001375/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001376 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1378 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1379 * will give undefined results.
1380 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1381 * and that msg->sol points to the beginning of the response.
1382 * If a complete line is found (which implies that at least one CR or LF is
1383 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1384 * returned indicating an incomplete line (which does not mean that parts have
1385 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1386 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1387 * upon next call.
1388 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001389 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1391 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001392 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001394const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001395 enum ht_state state, const char *ptr, const char *end,
1396 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001397{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001398 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001399
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001402 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001403 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001404 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, HTTP_MSG_RPVER);
Willy Tarreau8973c702007-01-21 23:58:29 +01001405
1406 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001407 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreaua5480692017-07-26 08:07:15 +02001408 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, HTTP_MSG_RPVER_SP);
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001410 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001411 state = HTTP_MSG_ERROR;
1412 break;
1413
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001415 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001417 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 goto http_msg_rpcode;
1419 }
1420 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001421 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, HTTP_MSG_RPVER_SP);
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001423 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001424 state = HTTP_MSG_ERROR;
1425 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001426
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001428 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001429 if (likely(!HTTP_IS_LWS(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001430 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, HTTP_MSG_RPCODE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001431
1432 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001433 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreaua5480692017-07-26 08:07:15 +02001434 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, HTTP_MSG_RPCODE_SP);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 }
1436
1437 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001438 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 http_msg_rsp_reason:
1440 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001441 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 msg->sl.st.r_l = 0;
1443 goto http_msg_rpline_eol;
1444
Willy Tarreau8973c702007-01-21 23:58:29 +01001445 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001446 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001448 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 goto http_msg_rpreason;
1450 }
1451 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001452 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, HTTP_MSG_RPCODE_SP);
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 /* so it's a CR/LF, so there is no reason phrase */
1454 goto http_msg_rsp_reason;
1455
Willy Tarreau8973c702007-01-21 23:58:29 +01001456 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001458 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001459 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001460 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 http_msg_rpline_eol:
1462 /* We have seen the end of line. Note that we do not
1463 * necessarily have the \n yet, but at least we know that we
1464 * have EITHER \r OR \n, otherwise the response would not be
1465 * complete. We can then record the response length and return
1466 * to the caller which will be able to register it.
1467 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001468 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 return ptr;
1470
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001472#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1474 exit(1);
1475#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001476 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 }
1478
1479 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001480 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 if (ret_state)
1482 *ret_state = state;
1483 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001484 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001485 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001486}
1487
Willy Tarreau8973c702007-01-21 23:58:29 +01001488/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 * This function parses a request line between <ptr> and <end>, starting with
1490 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1491 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1492 * will give undefined results.
1493 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1494 * and that msg->sol points to the beginning of the request.
1495 * If a complete line is found (which implies that at least one CR or LF is
1496 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1497 * returned indicating an incomplete line (which does not mean that parts have
1498 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1499 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1500 * upon next call.
1501 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001502 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1504 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001505 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001507const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001508 enum ht_state state, const char *ptr, const char *end,
1509 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001510{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001511 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (likely(HTTP_IS_TOKEN(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001517 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001520 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreaua5480692017-07-26 08:07:15 +02001521 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, HTTP_MSG_RQMETH_SP);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (likely(HTTP_IS_CRLF(*ptr))) {
1525 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001526 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001528 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001530 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001532 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 msg->sl.rq.v_l = 0;
1534 goto http_msg_rqline_eol;
1535 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001536 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001537 state = HTTP_MSG_ERROR;
1538 break;
1539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001543 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 goto http_msg_rquri;
1545 }
1546 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001547 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, HTTP_MSG_RQMETH_SP);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1549 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001553#if defined(__x86_64__) || \
1554 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1555 defined(__ARM_ARCH_7A__)
1556 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1557 while (ptr <= end - sizeof(int)) {
1558 int x = *(int *)ptr - 0x21212121;
1559 if (x & 0x80808080)
1560 break;
1561
1562 x -= 0x5e5e5e5e;
1563 if (!(x & 0x80808080))
1564 break;
1565
1566 ptr += sizeof(int);
1567 }
1568#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001569 if (ptr >= end) {
1570 state = HTTP_MSG_RQURI;
1571 goto http_msg_ood;
1572 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001573 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001574 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreaua5480692017-07-26 08:07:15 +02001575 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001578 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreaua5480692017-07-26 08:07:15 +02001579 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, HTTP_MSG_RQURI_SP);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001582 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001583 /* non-ASCII chars are forbidden unless option
1584 * accept-invalid-http-request is enabled in the frontend.
1585 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001586 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001587 if (msg->err_pos < -1)
1588 goto invalid_char;
1589 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001590 msg->err_pos = ptr - msg_start;
Willy Tarreaua5480692017-07-26 08:07:15 +02001591 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, HTTP_MSG_RQURI);
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001592 }
1593
1594 if (likely(HTTP_IS_CRLF(*ptr))) {
1595 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1596 goto http_msg_req09_uri_e;
1597 }
1598
1599 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001600 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001601 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001602 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001603 state = HTTP_MSG_ERROR;
1604 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001607 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001609 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 goto http_msg_rqver;
1611 }
1612 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001613 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, HTTP_MSG_RQURI_SP);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1615 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001618 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001619 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001620 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001621
1622 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001623 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001624 http_msg_rqline_eol:
1625 /* We have seen the end of line. Note that we do not
1626 * necessarily have the \n yet, but at least we know that we
1627 * have EITHER \r OR \n, otherwise the request would not be
1628 * complete. We can then record the request length and return
1629 * to the caller which will be able to register it.
1630 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001631 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001632 return ptr;
1633 }
1634
1635 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001636 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001637 state = HTTP_MSG_ERROR;
1638 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001641#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1643 exit(1);
1644#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001645 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001649 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 if (ret_state)
1651 *ret_state = state;
1652 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001653 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001657/*
1658 * Returns the data from Authorization header. Function may be called more
1659 * than once so data is stored in txn->auth_data. When no header is found
1660 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001661 * searching again for something we are unable to find anyway. However, if
1662 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001663 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001664 */
1665
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001666int
Willy Tarreau87b09662015-04-03 00:22:06 +02001667get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001668{
1669
Willy Tarreaueee5b512015-04-03 23:46:31 +02001670 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001671 struct chunk auth_method;
1672 struct hdr_ctx ctx;
1673 char *h, *p;
1674 int len;
1675
1676#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001677 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001678#endif
1679
1680 if (txn->auth.method == HTTP_AUTH_WRONG)
1681 return 0;
1682
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001683 txn->auth.method = HTTP_AUTH_WRONG;
1684
1685 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001686
1687 if (txn->flags & TX_USE_PX_CONN) {
1688 h = "Proxy-Authorization";
1689 len = strlen(h);
1690 } else {
1691 h = "Authorization";
1692 len = strlen(h);
1693 }
1694
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001695 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001696 return 0;
1697
1698 h = ctx.line + ctx.val;
1699
1700 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001701 len = p - h;
1702 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001703 return 0;
1704
David Carlier7365f7d2016-04-04 11:54:42 +01001705 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1706 return 0;
1707
Willy Tarreau5c557d12016-03-13 08:17:02 +01001708 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001709
1710 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001711 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001712
1713 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001714 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001715
1716 if (len < 0)
1717 return 0;
1718
1719
Christopher Faulet6988f672017-07-27 15:18:52 +02001720 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001721
Christopher Faulet6988f672017-07-27 15:18:52 +02001722 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001723
1724 if (!p)
1725 return 0;
1726
Christopher Faulet6988f672017-07-27 15:18:52 +02001727 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001728 *p = '\0';
1729 txn->auth.pass = p+1;
1730
1731 txn->auth.method = HTTP_AUTH_BASIC;
1732 return 1;
1733 }
1734
1735 return 0;
1736}
1737
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738
Willy Tarreau8973c702007-01-21 23:58:29 +01001739/*
1740 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001741 * depending on the initial msg->msg_state. The caller is responsible for
1742 * ensuring that the message does not wrap. The function can be preempted
1743 * everywhere when data are missing and recalled at the exact same location
1744 * with no information loss. The message may even be realigned between two
1745 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001746 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001747 * fields. Note that msg->sol will be initialized after completing the first
1748 * state, so that none of the msg pointers has to be initialized prior to the
1749 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001750 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001751void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752{
Willy Tarreau3770f232013-12-07 00:01:53 +01001753 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001755 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001756
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001757 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001758 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001759 ptr = buf->p + msg->next;
1760 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 if (unlikely(ptr >= end))
1763 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001764
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001766 /*
1767 * First, states that are specific to the response only.
1768 * We check them first so that request and headers are
1769 * closer to each other (accessed more often).
1770 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001771 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001772 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001773 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001774 /* we have a start of message, but we have to check
1775 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001776 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001777 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001778 if (unlikely(ptr != buf->p)) {
1779 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001780 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001781 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001782 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001783 }
Willy Tarreau26927362012-05-18 23:22:52 +02001784 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001785 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001786 hdr_idx_init(idx);
1787 state = HTTP_MSG_RPVER;
1788 goto http_msg_rpver;
1789 }
1790
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001791 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1792 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001793 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001794 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001795
1796 if (unlikely(*ptr == '\n'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001797 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, HTTP_MSG_RPBEFORE);
1798 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore_cr, http_msg_ood, state, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001799 /* stop here */
1800
Willy Tarreau8973c702007-01-21 23:58:29 +01001801 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001802 http_msg_rpbefore_cr:
Willy Tarreaua5480692017-07-26 08:07:15 +02001803 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RPBEFORE_CR);
1804 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, HTTP_MSG_RPBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001805 /* stop here */
1806
Willy Tarreau8973c702007-01-21 23:58:29 +01001807 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001808 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001809 case HTTP_MSG_RPVER_SP:
1810 case HTTP_MSG_RPCODE:
1811 case HTTP_MSG_RPCODE_SP:
1812 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001813 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001814 state, ptr, end,
1815 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001816 if (unlikely(!ptr))
1817 return;
1818
1819 /* we have a full response and we know that we have either a CR
1820 * or an LF at <ptr>.
1821 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001822 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1823
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001824 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001825 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001826 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpline_end, http_msg_ood, state, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001827 goto http_msg_rpline_end;
1828
Willy Tarreau8973c702007-01-21 23:58:29 +01001829 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001830 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001831 /* msg->sol must point to the first of CR or LF. */
Willy Tarreaua5480692017-07-26 08:07:15 +02001832 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RPLINE_END);
1833 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001834 /* stop here */
1835
1836 /*
1837 * Second, states that are specific to the request only
1838 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001840 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001842 /* we have a start of message, but we have to check
1843 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001844 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001845 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001846 if (likely(ptr != buf->p)) {
1847 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001848 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001849 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001850 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 }
Willy Tarreau26927362012-05-18 23:22:52 +02001852 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001853 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001854 state = HTTP_MSG_RQMETH;
1855 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001857
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001858 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1859 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001861 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(*ptr == '\n'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001864 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, HTTP_MSG_RQBEFORE);
1865 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001866 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001867
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001869 http_msg_rqbefore_cr:
Willy Tarreaua5480692017-07-26 08:07:15 +02001870 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RQBEFORE_CR);
1871 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001872 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001875 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 case HTTP_MSG_RQMETH_SP:
1877 case HTTP_MSG_RQURI:
1878 case HTTP_MSG_RQURI_SP:
1879 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001880 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001881 state, ptr, end,
1882 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 if (unlikely(!ptr))
1884 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 /* we have a full request and we know that we have either a CR
1887 * or an LF at <ptr>.
1888 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001890
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001893 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 /* check for HTTP/0.9 request : no version information available.
1899 * msg->sol must point to the first of CR or LF.
1900 */
1901 if (unlikely(msg->sl.rq.v_l == 0))
1902 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001903
Willy Tarreaua5480692017-07-26 08:07:15 +02001904 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RQLINE_END);
1905 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001906 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001907
Willy Tarreau8973c702007-01-21 23:58:29 +01001908 /*
1909 * Common states below
1910 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001911 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001912 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 if (likely(!HTTP_IS_CRLF(*ptr))) {
1915 goto http_msg_hdr_name;
1916 }
1917
1918 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001919 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001923 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001924 /* assumes msg->sol points to the first char */
1925 if (likely(HTTP_IS_TOKEN(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001926 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001928 if (likely(*ptr == ':'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001929 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001930
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001931 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1932 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001933 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001934 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001935
1936 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001938
1939 /* and we still accept this non-token character */
Willy Tarreaua5480692017-07-26 08:07:15 +02001940 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001943 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001944 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 if (likely(HTTP_IS_SPHT(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02001946 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001947
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001948 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001949 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001950
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001951 if (likely(!HTTP_IS_CRLF(*ptr))) {
1952 goto http_msg_hdr_val;
1953 }
1954
1955 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02001956 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lf, http_msg_ood, state, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001960 http_msg_hdr_l1_lf:
Willy Tarreaua5480692017-07-26 08:07:15 +02001961 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_HDR_L1_LF);
1962 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lws, http_msg_ood, state, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001963
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001965 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 if (likely(HTTP_IS_SPHT(*ptr))) {
1967 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001968 for (; buf->p + msg->sov < ptr; msg->sov++)
1969 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 goto http_msg_hdr_l1_sp;
1971 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001972 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001973 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 goto http_msg_complete_header;
1975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001976 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001977 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001978 /* assumes msg->sol points to the first char, and msg->sov
1979 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001980 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001981
1982 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1983 * and lower. In fact since most of the time is spent in the loop, we
1984 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1985 * loop, but we don't care since they're very rare in header values.
1986 */
1987#if defined(__x86_64__)
1988 while (ptr <= end - sizeof(long)) {
1989 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1990 goto http_msg_hdr_val2;
1991 ptr += sizeof(long);
1992 }
1993#endif
1994#if defined(__x86_64__) || \
1995 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1996 defined(__ARM_ARCH_7A__)
1997 while (ptr <= end - sizeof(int)) {
1998 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1999 goto http_msg_hdr_val2;
2000 ptr += sizeof(int);
2001 }
2002#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002003 if (ptr >= end) {
2004 state = HTTP_MSG_HDR_VAL;
2005 goto http_msg_ood;
2006 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002007 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002008 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreaua5480692017-07-26 08:07:15 +02002009 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_val2, http_msg_ood, state, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002010
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002011 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002012 /* Note: we could also copy eol into ->eoh so that we have the
2013 * real header end in case it ends with lots of LWS, but is this
2014 * really needed ?
2015 */
2016 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02002017 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lf, http_msg_ood, state, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002018 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002019
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002020 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002021 http_msg_hdr_l2_lf:
Willy Tarreaua5480692017-07-26 08:07:15 +02002022 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_HDR_L2_LF);
2023 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lws, http_msg_ood, state, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002024
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002026 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002027 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2028 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002029 for (; buf->p + msg->eol < ptr; msg->eol++)
2030 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002031 goto http_msg_hdr_val;
2032 }
2033 http_msg_complete_header:
2034 /*
2035 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002036 * Assumes msg->sol points to the first char, msg->sov points
2037 * to the first character of the value and msg->eol to the
2038 * first CR or LF so we know how the line ends. We insert last
2039 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002040 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002042 idx, idx->tail) < 0)) {
2043 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002045 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002046
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002047 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002048 if (likely(!HTTP_IS_CRLF(*ptr))) {
2049 goto http_msg_hdr_name;
2050 }
2051
2052 if (likely(*ptr == '\r'))
Willy Tarreaua5480692017-07-26 08:07:15 +02002053 EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002057 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002058 /* Assumes msg->sol points to the first of either CR or LF.
2059 * Sets ->sov and ->next to the total header length, ->eoh to
2060 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2061 */
Willy Tarreaua5480692017-07-26 08:07:15 +02002062 EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002065 msg->eoh = msg->sol;
2066 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002067 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002068 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002070
2071 case HTTP_MSG_ERROR:
2072 /* this may only happen if we call http_msg_analyser() twice with an error */
2073 break;
2074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002075 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002076#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002077 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2078 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002079#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002080 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002081 }
2082 http_msg_ood:
2083 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002084 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002086 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002088 http_msg_invalid:
2089 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002090 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002091 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002093 return;
2094}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002095
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002096/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2097 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2098 * nothing is done and 1 is returned.
2099 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002100static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002101{
2102 int delta;
2103 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002104 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002105
2106 if (msg->sl.rq.v_l != 0)
2107 return 1;
2108
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002109 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2110 if (txn->meth != HTTP_METH_GET)
2111 return 0;
2112
Willy Tarreau9b28e032012-10-12 23:49:43 +02002113 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002114
2115 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002116 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2117 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002118 }
2119 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002120 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002121 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002122 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002123 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002124 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002125 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002126 NULL, NULL);
2127 if (unlikely(!cur_end))
2128 return 0;
2129
2130 /* we have a full HTTP/1.0 request now and we know that
2131 * we have either a CR or an LF at <ptr>.
2132 */
2133 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2134 return 1;
2135}
2136
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002137/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002138 * and "keep-alive" values. If we already know that some headers may safely
2139 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002140 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2141 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002142 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002143 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2144 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2145 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002146 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002147 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002148void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002149{
Willy Tarreau5b154472009-12-21 20:11:07 +01002150 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002151 const char *hdr_val = "Connection";
2152 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002153
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002154 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002155 return;
2156
Willy Tarreau88d349d2010-01-25 12:15:43 +01002157 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2158 hdr_val = "Proxy-Connection";
2159 hdr_len = 16;
2160 }
2161
Willy Tarreau5b154472009-12-21 20:11:07 +01002162 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002163 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002164 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002165 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2166 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002167 if (to_del & 2)
2168 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002169 else
2170 txn->flags |= TX_CON_KAL_SET;
2171 }
2172 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2173 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002174 if (to_del & 1)
2175 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002176 else
2177 txn->flags |= TX_CON_CLO_SET;
2178 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002179 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2180 txn->flags |= TX_HDR_CONN_UPG;
2181 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002182 }
2183
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002184 txn->flags |= TX_HDR_CONN_PRS;
2185 return;
2186}
Willy Tarreau5b154472009-12-21 20:11:07 +01002187
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002188/* Apply desired changes on the Connection: header. Values may be removed and/or
2189 * added depending on the <wanted> flags, which are exclusively composed of
2190 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2191 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2192 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002193void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002194{
2195 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002196 const char *hdr_val = "Connection";
2197 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002198
2199 ctx.idx = 0;
2200
Willy Tarreau88d349d2010-01-25 12:15:43 +01002201
2202 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2203 hdr_val = "Proxy-Connection";
2204 hdr_len = 16;
2205 }
2206
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002207 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002208 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002209 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2210 if (wanted & TX_CON_KAL_SET)
2211 txn->flags |= TX_CON_KAL_SET;
2212 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002213 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002214 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002215 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2216 if (wanted & TX_CON_CLO_SET)
2217 txn->flags |= TX_CON_CLO_SET;
2218 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002219 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002220 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002221 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002222
2223 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2224 return;
2225
2226 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2227 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002228 hdr_val = "Connection: close";
2229 hdr_len = 17;
2230 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2231 hdr_val = "Proxy-Connection: close";
2232 hdr_len = 23;
2233 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002234 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002235 }
2236
2237 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2238 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002239 hdr_val = "Connection: keep-alive";
2240 hdr_len = 22;
2241 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2242 hdr_val = "Proxy-Connection: keep-alive";
2243 hdr_len = 28;
2244 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002245 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002246 }
2247 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002248}
2249
Christopher Faulet113f7de2015-12-14 14:52:13 +01002250/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2251 * point to the first byte of data after the chunk size, so that we know we can
2252 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2253 * forming the chunk size. That way it is always possible to differentiate
2254 * between the start of the body and the start of the data. Return the number
2255 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2256 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002257 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002258static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002259{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002260 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002261 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002262 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002263 const char *end = buf->data + buf->size;
2264 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002265 unsigned int chunk = 0;
2266
2267 /* The chunk size is in the following form, though we are only
2268 * interested in the size and CRLF :
2269 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2270 */
2271 while (1) {
2272 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002273 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002274 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002276 if (c < 0) /* not a hex digit anymore */
2277 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002278 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002279 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002280 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002281 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002282 chunk = (chunk << 4) + c;
2283 }
2284
Willy Tarreaud98cf932009-12-27 22:54:55 +01002285 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002286 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002287 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288
Willy Tarreau2235b262016-11-05 15:50:20 +01002289 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002290 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002291 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002292 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002293 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002294 }
2295
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296 /* Up to there, we know that at least one byte is present at *ptr. Check
2297 * for the end of chunk size.
2298 */
2299 while (1) {
2300 if (likely(HTTP_IS_CRLF(*ptr))) {
2301 /* we now have a CR or an LF at ptr */
2302 if (likely(*ptr == '\r')) {
2303 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002304 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002305 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002306 return 0;
2307 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002308
Willy Tarreaud98cf932009-12-27 22:54:55 +01002309 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002310 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002311 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002312 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002313 /* done */
2314 break;
2315 }
2316 else if (*ptr == ';') {
2317 /* chunk extension, ends at next CRLF */
2318 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002319 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002320 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002321 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002322
2323 while (!HTTP_IS_CRLF(*ptr)) {
2324 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002325 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002326 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002327 return 0;
2328 }
2329 /* we have a CRLF now, loop above */
2330 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002331 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002332 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002333 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002334 }
2335
Christopher Faulet113f7de2015-12-14 14:52:13 +01002336 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2337 * or may not be present. We save the number of bytes parsed into
2338 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002339 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002340 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002341 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002342 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002343 msg->chunk_len = chunk;
2344 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002345 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002346 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002347 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002348 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002349}
2350
Christopher Faulet113f7de2015-12-14 14:52:13 +01002351/* This function skips trailers in the buffer associated with HTTP message
2352 * <msg>. The first visited position is msg->next. If the end of the trailers is
2353 * found, the function returns >0. So, the caller can automatically schedul it
2354 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2355 * data are available, the function does not change anything except maybe
2356 * msg->sol if it could parse some lines, and returns zero. If a parse error
2357 * is encountered, the function returns < 0 and does not change anything except
2358 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2359 * state before calling this function, which implies that all non-trailers data
2360 * have already been scheduled for forwarding, and that msg->next exactly
2361 * matches the length of trailers already parsed and not forwarded. It is also
2362 * important to note that this function is designed to be able to parse wrapped
2363 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002364 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002365static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002366{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002367 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002368
Christopher Faulet113f7de2015-12-14 14:52:13 +01002369 /* we have msg->next which points to next line. Look for CRLF. But
2370 * first, we reset msg->sol */
2371 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002373 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002374 const char *start = b_ptr(buf, msg->next + msg->sol);
2375 const char *stop = bi_end(buf);
2376 const char *ptr = start;
2377 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002378
2379 /* scan current line and stop at LF or CRLF */
2380 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002381 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002382 return 0;
2383
2384 if (*ptr == '\n') {
2385 if (!p1)
2386 p1 = ptr;
2387 p2 = ptr;
2388 break;
2389 }
2390
2391 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002392 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002393 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002394 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002395 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002396 p1 = ptr;
2397 }
2398
2399 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002400 if (ptr >= buf->data + buf->size)
2401 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002402 }
2403
2404 /* after LF; point to beginning of next line */
2405 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002406 if (p2 >= buf->data + buf->size)
2407 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002408
Christopher Faulet2fb28802015-12-01 10:40:57 +01002409 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002410 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002411 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002412 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002413
Christopher Fauletd7c91962015-04-30 11:48:27 +02002414 /* LF/CRLF at beginning of line => end of trailers at p2.
2415 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002416 * from this message. */
2417 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002418 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002419
Willy Tarreaud98cf932009-12-27 22:54:55 +01002420 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002421 }
2422}
2423
Christopher Faulet113f7de2015-12-14 14:52:13 +01002424/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2425 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2426 * in order to include this part into the next forwarding phase. Note that the
2427 * caller must ensure that ->p points to the first byte to parse. It returns
2428 * the number of bytes parsed on success, so the caller can set msg_state to
2429 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2430 * change anything and returns zero. If a parse error is encountered, the
2431 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2432 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002433 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002434static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002435{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002436 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002437 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002438 int bytes;
2439
2440 /* NB: we'll check data availabilty at the end. It's not a
2441 * problem because whatever we match first will be checked
2442 * against the correct length.
2443 */
2444 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002445 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002446 if (*ptr == '\r') {
2447 bytes++;
2448 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002449 if (ptr >= buf->data + buf->size)
2450 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002451 }
2452
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002453 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002454 return 0;
2455
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002456 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002457 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002458 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002459 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002460 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002461}
Willy Tarreau5b154472009-12-21 20:11:07 +01002462
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002463/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2464 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2465 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2466 * Unparsable qvalues return 1000 as "q=1.000".
2467 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002468int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002469{
2470 int q = 1000;
2471
Willy Tarreau506c69a2014-07-08 00:59:48 +02002472 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002473 goto out;
2474 q = (*qvalue++ - '0') * 1000;
2475
2476 if (*qvalue++ != '.')
2477 goto out;
2478
Willy Tarreau506c69a2014-07-08 00:59:48 +02002479 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002480 goto out;
2481 q += (*qvalue++ - '0') * 100;
2482
Willy Tarreau506c69a2014-07-08 00:59:48 +02002483 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002484 goto out;
2485 q += (*qvalue++ - '0') * 10;
2486
Willy Tarreau506c69a2014-07-08 00:59:48 +02002487 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002488 goto out;
2489 q += (*qvalue++ - '0') * 1;
2490 out:
2491 if (q > 1000)
2492 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002493 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002494 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002495 return q;
2496}
William Lallemand82fe75c2012-10-23 10:25:10 +02002497
Willy Tarreau87b09662015-04-03 00:22:06 +02002498void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002500 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002501 int tmp = TX_CON_WANT_KAL;
2502
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002503 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2504 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002505 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2506 tmp = TX_CON_WANT_TUN;
2507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2510 tmp = TX_CON_WANT_TUN;
2511 }
2512
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002513 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002514 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2515 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002516 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002517 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2518 tmp = TX_CON_WANT_CLO;
2519 else
2520 tmp = TX_CON_WANT_SCL;
2521 }
2522
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002523 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2525 tmp = TX_CON_WANT_CLO;
2526
2527 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2528 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2529
2530 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2531 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2532 /* parse the Connection header and possibly clean it */
2533 int to_del = 0;
2534 if ((msg->flags & HTTP_MSGF_VER_11) ||
2535 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002536 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002537 to_del |= 2; /* remove "keep-alive" */
2538 if (!(msg->flags & HTTP_MSGF_VER_11))
2539 to_del |= 1; /* remove "close" */
2540 http_parse_connection_header(txn, msg, to_del);
2541 }
2542
2543 /* check if client or config asks for explicit close in KAL/SCL */
2544 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2545 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2546 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2547 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2548 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002549 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002550 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2551}
William Lallemand82fe75c2012-10-23 10:25:10 +02002552
Willy Tarreaud787e662009-07-07 10:14:51 +02002553/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2554 * processing can continue on next analysers, or zero if it either needs more
2555 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002556 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002557 * when it has nothing left to do, and may remove any analyser when it wants to
2558 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002559 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002560int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002561{
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 /*
2563 * We will parse the partial (or complete) lines.
2564 * We will check the request syntax, and also join multi-line
2565 * headers. An index of all the lines will be elaborated while
2566 * parsing.
2567 *
2568 * For the parsing, we use a 28 states FSM.
2569 *
2570 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002571 * req->buf->p = beginning of request
2572 * req->buf->p + msg->eoh = end of processed headers / start of current one
2573 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002574 * msg->eol = end of current header or line (LF or CRLF)
2575 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002576 *
2577 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002578 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002579 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2580 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002582
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002584 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002585 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002587 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002588
Willy Tarreau87b09662015-04-03 00:22:06 +02002589 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 +01002590 now_ms, __FUNCTION__,
2591 s,
2592 req,
2593 req->rex, req->wex,
2594 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002595 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002596 req->analysers);
2597
Willy Tarreau52a0c602009-08-16 22:45:38 +02002598 /* we're speaking HTTP here, so let's speak HTTP to the client */
2599 s->srv_error = http_return_srv_error;
2600
Willy Tarreau83e3af02009-12-28 17:39:57 +01002601 /* There's a protected area at the end of the buffer for rewriting
2602 * purposes. We don't want to start to parse the request if the
2603 * protected area is affected, because we may have to move processed
2604 * data later, which is much more complicated.
2605 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002606 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002607
2608 /* This point is executed when some data is avalaible for analysis,
2609 * so we log the end of the idle time. */
2610 if (s->logs.t_idle == -1)
2611 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2612
Willy Tarreau379357a2013-06-08 12:55:46 +02002613 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002614 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002615 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002616 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002617 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002618 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002619 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002620 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002621 return 0;
2622 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002623 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2624 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2625 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002626 }
2627
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002629 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002630 }
2631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 /* 1: we might have to print this header in debug mode */
2633 if (unlikely((global.mode & MODE_DEBUG) &&
2634 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002635 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002637
Willy Tarreau9b28e032012-10-12 23:49:43 +02002638 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002639 /* this is a bit complex : in case of error on the request line,
2640 * we know that rq.l is still zero, so we display only the part
2641 * up to the end of the line (truncated by debug_hdr).
2642 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002643 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002645
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 sol += hdr_idx_first_pos(&txn->hdr_idx);
2647 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 while (cur_idx) {
2650 eol = sol + txn->hdr_idx.v[cur_idx].len;
2651 debug_hdr("clihdr", s, sol, eol);
2652 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2653 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002654 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 }
2656
Willy Tarreau58f10d72006-12-04 02:26:12 +01002657
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 /*
2659 * Now we quickly check if we have found a full valid request.
2660 * If not so, we check the FD and buffer states before leaving.
2661 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002662 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002663 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002664 * on a keep-alive stream, if we encounter and error, close, t/o,
2665 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002666 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002667 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002668 * Last, we may increase some tracked counters' http request errors on
2669 * the cases that are deliberately the client's fault. For instance,
2670 * a timeout or connection reset is not counted as an error. However
2671 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002673
Willy Tarreau655dce92009-11-08 13:10:58 +01002674 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002675 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002678 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002679 stream_inc_http_req_ctr(s);
2680 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002681 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 /* 1: Since we are in header mode, if there's no space
2686 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002687 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 * must terminate it now.
2689 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002690 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 /* FIXME: check if URI is set and return Status
2692 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002693 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002694 stream_inc_http_req_ctr(s);
2695 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002696 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002697 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002698 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002699 goto return_bad_req;
2700 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002701
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002703 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002704 if (!(s->flags & SF_ERR_MASK))
2705 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002706
Willy Tarreaufcffa692010-01-10 14:21:19 +01002707 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002708 goto failed_keep_alive;
2709
Willy Tarreau0f228a02015-05-01 15:37:53 +02002710 if (sess->fe->options & PR_O_IGNORE_PRB)
2711 goto failed_keep_alive;
2712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002714 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002715 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002716 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717 }
2718
Willy Tarreaudc979f22012-12-04 10:39:01 +01002719 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002720 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002722 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002723 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002724 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 proxy_inc_fe_req_ctr(sess->fe);
2726 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002727 if (sess->listener->counters)
2728 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002729
Willy Tarreaue7dff022015-04-03 01:14:29 +02002730 if (!(s->flags & SF_FINST_MASK))
2731 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 return 0;
2733 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002734
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002736 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002737 if (!(s->flags & SF_ERR_MASK))
2738 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002739
Willy Tarreaufcffa692010-01-10 14:21:19 +01002740 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002741 goto failed_keep_alive;
2742
Willy Tarreau0f228a02015-05-01 15:37:53 +02002743 if (sess->fe->options & PR_O_IGNORE_PRB)
2744 goto failed_keep_alive;
2745
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002747 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002748 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002749 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002750 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002752 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002754 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002755 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002756
Willy Tarreau87b09662015-04-03 00:22:06 +02002757 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002758 proxy_inc_fe_req_ctr(sess->fe);
2759 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002760 if (sess->listener->counters)
2761 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002762
Willy Tarreaue7dff022015-04-03 01:14:29 +02002763 if (!(s->flags & SF_FINST_MASK))
2764 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 return 0;
2766 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002767
Willy Tarreau59234e92008-11-30 23:51:27 +01002768 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002769 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002770 if (!(s->flags & SF_ERR_MASK))
2771 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002772
Willy Tarreaufcffa692010-01-10 14:21:19 +01002773 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002774 goto failed_keep_alive;
2775
Willy Tarreau0f228a02015-05-01 15:37:53 +02002776 if (sess->fe->options & PR_O_IGNORE_PRB)
2777 goto failed_keep_alive;
2778
Willy Tarreau4076a152009-04-02 15:18:36 +02002779 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002780 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002782 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002784 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002785 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002786 stream_inc_http_err_ctr(s);
2787 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002788 proxy_inc_fe_req_ctr(sess->fe);
2789 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002790 if (sess->listener->counters)
2791 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002792
Willy Tarreaue7dff022015-04-03 01:14:29 +02002793 if (!(s->flags & SF_FINST_MASK))
2794 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002795 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002796 }
2797
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002798 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002799 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002800 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002801#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002802 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2803 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002804 /* We need more data, we have to re-enable quick-ack in case we
2805 * previously disabled it, otherwise we might cause the client
2806 * to delay next data.
2807 */
Willy Tarreau585744b2017-08-24 14:31:19 +02002808 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002809 }
2810#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002811
Willy Tarreaufcffa692010-01-10 14:21:19 +01002812 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2813 /* If the client starts to talk, let's fall back to
2814 * request timeout processing.
2815 */
2816 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002817 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002818 }
2819
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002821 if (!tick_isset(req->analyse_exp)) {
2822 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2823 (txn->flags & TX_WAIT_NEXT_RQ) &&
2824 tick_isset(s->be->timeout.httpka))
2825 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2826 else
2827 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2828 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* we're not ready yet */
2831 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002832
2833 failed_keep_alive:
2834 /* Here we process low-level errors for keep-alive requests. In
2835 * short, if the request is not the first one and it experiences
2836 * a timeout, read error or shutdown, we just silently close so
2837 * that the client can try again.
2838 */
2839 txn->status = 0;
2840 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002841 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002842 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002843 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002844 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002845 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002846 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002848
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 /* OK now we have a complete HTTP request with indexed headers. Let's
2850 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002851 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002852 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002853 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002854 * byte after the last LF. msg->sov points to the first byte of data.
2855 * msg->eol cannot be trusted because it may have been left uninitialized
2856 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002857 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002858
Willy Tarreau87b09662015-04-03 00:22:06 +02002859 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002860 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002861
Willy Tarreaub16a5742010-01-10 14:46:16 +01002862 if (txn->flags & TX_WAIT_NEXT_RQ) {
2863 /* kill the pending keep-alive timeout */
2864 txn->flags &= ~TX_WAIT_NEXT_RQ;
2865 req->analyse_exp = TICK_ETERNITY;
2866 }
2867
2868
Willy Tarreaud787e662009-07-07 10:14:51 +02002869 /* Maybe we found in invalid header name while we were configured not
2870 * to block on that, so we have to capture it now.
2871 */
2872 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002873 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002874
Willy Tarreau59234e92008-11-30 23:51:27 +01002875 /*
2876 * 1: identify the method
2877 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002878 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002879
2880 /* we can make use of server redirect on GET and HEAD */
2881 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002882 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002883
Willy Tarreau59234e92008-11-30 23:51:27 +01002884 /*
2885 * 2: check if the URI matches the monitor_uri.
2886 * We have to do this for every request which gets in, because
2887 * the monitor-uri is defined by the frontend.
2888 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002889 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2890 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002891 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002892 sess->fe->monitor_uri,
2893 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002894 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002897 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002898
Willy Tarreaue7dff022015-04-03 01:14:29 +02002899 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002900 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002901
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002903 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002904 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002905
Willy Tarreau59234e92008-11-30 23:51:27 +01002906 ret = acl_pass(ret);
2907 if (cond->pol == ACL_COND_UNLESS)
2908 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002909
Willy Tarreau59234e92008-11-30 23:51:27 +01002910 if (ret) {
2911 /* we fail this request, let's return 503 service unavail */
2912 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002913 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002914 if (!(s->flags & SF_ERR_MASK))
2915 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002917 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002918 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002919
Willy Tarreau59234e92008-11-30 23:51:27 +01002920 /* nothing to fail, let's reply normaly */
2921 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002922 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002923 if (!(s->flags & SF_ERR_MASK))
2924 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002925 goto return_prx_cond;
2926 }
2927
2928 /*
2929 * 3: Maybe we have to copy the original REQURI for the logs ?
2930 * Note: we cannot log anymore if the request has been
2931 * classified as invalid.
2932 */
2933 if (unlikely(s->logs.logwait & LW_REQ)) {
2934 /* we have a complete HTTP request that we must log */
2935 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2936 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002937
Stéphane Cottin23e9e932017-05-18 08:58:41 +02002938 if (urilen >= global.tune.requri_len )
2939 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002940 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002941 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002942
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002943 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 s->do_log(s);
2945 } else {
2946 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002947 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 }
Willy Tarreau06619262006-12-17 08:37:22 +01002949
Willy Tarreau91852eb2015-05-01 13:26:00 +02002950 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2951 * exactly one digit "." one digit. This check may be disabled using
2952 * option accept-invalid-http-request.
2953 */
2954 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2955 if (msg->sl.rq.v_l != 8) {
2956 msg->err_pos = msg->sl.rq.v;
2957 goto return_bad_req;
2958 }
2959
2960 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2961 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2962 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2963 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2964 msg->err_pos = msg->sl.rq.v + 4;
2965 goto return_bad_req;
2966 }
2967 }
Willy Tarreau13317662015-05-01 13:47:08 +02002968 else {
2969 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2970 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2971 goto return_bad_req;
2972 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002973
Willy Tarreau5b154472009-12-21 20:11:07 +01002974 /* ... and check if the request is HTTP/1.1 or above */
2975 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002976 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2977 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2978 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002979 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002980
2981 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002982 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 +01002983
Willy Tarreau88d349d2010-01-25 12:15:43 +01002984 /* if the frontend has "option http-use-proxy-header", we'll check if
2985 * we have what looks like a proxied connection instead of a connection,
2986 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2987 * Note that this is *not* RFC-compliant, however browsers and proxies
2988 * happen to do that despite being non-standard :-(
2989 * We consider that a request not beginning with either '/' or '*' is
2990 * a proxied connection, which covers both "scheme://location" and
2991 * CONNECT ip:port.
2992 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002993 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002994 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002995 txn->flags |= TX_USE_PX_CONN;
2996
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002997 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002998 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002999
Willy Tarreau59234e92008-11-30 23:51:27 +01003000 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003001 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003002 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003003 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003004
Willy Tarreau557f1992015-05-01 10:05:17 +02003005 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3006 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003007 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003008 * The length of a message body is determined by one of the following
3009 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003010 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003011 * 1. Any response to a HEAD request and any response with a 1xx
3012 * (Informational), 204 (No Content), or 304 (Not Modified) status
3013 * code is always terminated by the first empty line after the
3014 * header fields, regardless of the header fields present in the
3015 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003016 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003017 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3018 * the connection will become a tunnel immediately after the empty
3019 * line that concludes the header fields. A client MUST ignore any
3020 * Content-Length or Transfer-Encoding header fields received in
3021 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003022 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003023 * 3. If a Transfer-Encoding header field is present and the chunked
3024 * transfer coding (Section 4.1) is the final encoding, the message
3025 * body length is determined by reading and decoding the chunked
3026 * data until the transfer coding indicates the data is complete.
3027 *
3028 * If a Transfer-Encoding header field is present in a response and
3029 * the chunked transfer coding is not the final encoding, the
3030 * message body length is determined by reading the connection until
3031 * it is closed by the server. If a Transfer-Encoding header field
3032 * is present in a request and the chunked transfer coding is not
3033 * the final encoding, the message body length cannot be determined
3034 * reliably; the server MUST respond with the 400 (Bad Request)
3035 * status code and then close the connection.
3036 *
3037 * If a message is received with both a Transfer-Encoding and a
3038 * Content-Length header field, the Transfer-Encoding overrides the
3039 * Content-Length. Such a message might indicate an attempt to
3040 * perform request smuggling (Section 9.5) or response splitting
3041 * (Section 9.4) and ought to be handled as an error. A sender MUST
3042 * remove the received Content-Length field prior to forwarding such
3043 * a message downstream.
3044 *
3045 * 4. If a message is received without Transfer-Encoding and with
3046 * either multiple Content-Length header fields having differing
3047 * field-values or a single Content-Length header field having an
3048 * invalid value, then the message framing is invalid and the
3049 * recipient MUST treat it as an unrecoverable error. If this is a
3050 * request message, the server MUST respond with a 400 (Bad Request)
3051 * status code and then close the connection. If this is a response
3052 * message received by a proxy, the proxy MUST close the connection
3053 * to the server, discard the received response, and send a 502 (Bad
3054 * Gateway) response to the client. If this is a response message
3055 * received by a user agent, the user agent MUST close the
3056 * connection to the server and discard the received response.
3057 *
3058 * 5. If a valid Content-Length header field is present without
3059 * Transfer-Encoding, its decimal value defines the expected message
3060 * body length in octets. If the sender closes the connection or
3061 * the recipient times out before the indicated number of octets are
3062 * received, the recipient MUST consider the message to be
3063 * incomplete and close the connection.
3064 *
3065 * 6. If this is a request message and none of the above are true, then
3066 * the message body length is zero (no message body is present).
3067 *
3068 * 7. Otherwise, this is a response message without a declared message
3069 * body length, so the message body length is determined by the
3070 * number of octets received prior to the server closing the
3071 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003072 */
3073
Willy Tarreau32b47f42009-10-18 20:55:02 +02003074 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003075 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003076 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003077 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02003078 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003079 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003080 /* chunked not last, return badreq */
3081 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003082 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003083 }
3084
Willy Tarreau1c913912015-04-30 10:57:51 +02003085 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003086 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003087 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3088 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3089 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3090 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003091 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003092 signed long long cl;
3093
Willy Tarreauad14f752011-09-02 20:33:27 +02003094 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003095 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003096 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003097 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003098
Willy Tarreauad14f752011-09-02 20:33:27 +02003099 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003100 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003101 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003102 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003103
Willy Tarreauad14f752011-09-02 20:33:27 +02003104 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003105 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003106 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003107 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003108
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003109 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003110 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003111 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003112 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003113
Christopher Fauletbe821b92017-03-30 11:21:53 +02003114 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003115 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003116 }
3117
Willy Tarreau34dfc602015-05-01 10:09:49 +02003118 /* even bodyless requests have a known length */
3119 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003120
Willy Tarreau179085c2014-04-28 16:48:56 +02003121 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3122 * only change if both the request and the config reference something else.
3123 * Option httpclose by itself sets tunnel mode where headers are mangled.
3124 * However, if another mode is set, it will affect it (eg: server-close/
3125 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3126 * if FE and BE have the same settings (common). The method consists in
3127 * checking if options changed between the two calls (implying that either
3128 * one is non-null, or one of them is non-null and we are there for the first
3129 * time.
3130 */
3131 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003132 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003133 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003134
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003135 /* we may have to wait for the request's body */
3136 if ((s->be->options & PR_O_WREQ_BODY) &&
3137 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3138 req->analysers |= AN_REQ_HTTP_BODY;
3139
Willy Tarreaud787e662009-07-07 10:14:51 +02003140 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003141 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003142 req->analyse_exp = TICK_ETERNITY;
3143 return 1;
3144
3145 return_bad_req:
3146 /* We centralize bad requests processing here */
3147 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3148 /* we detected a parsing error. We want to archive this request
3149 * in the dedicated proxy area for later troubleshooting.
3150 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003151 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003152 }
3153
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003154 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003155 txn->req.msg_state = HTTP_MSG_ERROR;
3156 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003157 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003158
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003159 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003160 if (sess->listener->counters)
3161 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003162
3163 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003164 if (!(s->flags & SF_ERR_MASK))
3165 s->flags |= SF_ERR_PRXCOND;
3166 if (!(s->flags & SF_FINST_MASK))
3167 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003168
Christopher Faulet0184ea72017-01-05 14:06:34 +01003169 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003170 req->analyse_exp = TICK_ETERNITY;
3171 return 0;
3172}
3173
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003174
Willy Tarreau347a35d2013-11-22 17:51:09 +01003175/* This function prepares an applet to handle the stats. It can deal with the
3176 * "100-continue" expectation, check that admin rules are met for POST requests,
3177 * and program a response message if something was unexpected. It cannot fail
3178 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003179 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003181 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003182 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003183int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184{
3185 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003186 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003187 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003188 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003189 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003190 struct uri_auth *uri_auth = s->be->uri_auth;
3191 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003192 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003193
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003194 appctx = si_appctx(si);
3195 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3196 appctx->st1 = appctx->st2 = 0;
3197 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3198 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003199 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003200 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003201
3202 uri = msg->chn->buf->p + msg->sl.rq.u;
3203 lookup = uri + uri_auth->uri_len;
3204
3205 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3206 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003208 break;
3209 }
3210 }
3211
3212 if (uri_auth->refresh) {
3213 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3214 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003215 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003216 break;
3217 }
3218 }
3219 }
3220
3221 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3222 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003223 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003224 break;
3225 }
3226 }
3227
Willy Tarreau1e62df92016-01-11 18:57:53 +01003228 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3229 if (memcmp(h, ";typed", 6) == 0) {
3230 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3231 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3232 break;
3233 }
3234 }
3235
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003236 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3237 if (memcmp(h, ";st=", 4) == 0) {
3238 int i;
3239 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003240 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003241 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3242 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003243 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003244 break;
3245 }
3246 }
3247 break;
3248 }
3249 }
3250
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003251 appctx->ctx.stats.scope_str = 0;
3252 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003253 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3254 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3255 int itx = 0;
3256 const char *h2;
3257 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3258 const char *err;
3259
3260 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3261 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003262 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003263 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3264 itx++;
3265 h++;
3266 }
3267
3268 if (itx > STAT_SCOPE_TXT_MAXLEN)
3269 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003270 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003272 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003273 memcpy(scope_txt, h2, itx);
3274 scope_txt[itx] = '\0';
3275 err = invalid_char(scope_txt);
3276 if (err) {
3277 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003278 appctx->ctx.stats.scope_str = 0;
3279 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003280 }
3281 break;
3282 }
3283 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003284
3285 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003286 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003287 int ret = 1;
3288
3289 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003290 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 +01003291 ret = acl_pass(ret);
3292 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3293 ret = !ret;
3294 }
3295
3296 if (ret) {
3297 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003299 break;
3300 }
3301 }
3302
3303 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003304 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003305 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003306 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003307 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3308 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003309 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003310 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003311 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003312 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3313 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003314 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003315 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003316 else {
3317 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003319 }
3320
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003321 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003322 return 1;
3323}
3324
Lukas Tribus67db8df2013-06-23 17:37:13 +02003325/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3326 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3327 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003328void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003329{
3330#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003331 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003332 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3333#endif
3334#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003335 if (from->ss_family == AF_INET6) {
3336 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003337 /* v4-mapped addresses need IP_TOS */
3338 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3339 else
3340 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3341 }
3342#endif
3343}
3344
Willy Tarreau87b09662015-04-03 00:22:06 +02003345int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003346 const char* name, unsigned int name_len,
3347 const char *str, struct my_regex *re,
3348 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003349{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003350 struct hdr_ctx ctx;
3351 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003352 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003353 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3354 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003355 struct chunk *output = get_trash_chunk();
3356
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003357 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003358
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003359 /* Choose the header browsing function. */
3360 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003361 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003362 http_find_hdr_func = http_find_header2;
3363 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003364 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003365 http_find_hdr_func = http_find_full_header2;
3366 break;
3367 default: /* impossible */
3368 return -1;
3369 }
3370
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003371 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3372 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003373 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003374 char *val = ctx.line + ctx.val;
3375 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003376
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003377 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3378 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003379
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003380 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003381 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003382 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003383
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003384 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003385
3386 hdr->len += delta;
3387 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003388
3389 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003390 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003391 }
3392
3393 return 0;
3394}
3395
Willy Tarreau87b09662015-04-03 00:22:06 +02003396static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003397 const char* name, unsigned int name_len,
3398 struct list *fmt, struct my_regex *re,
3399 int action)
3400{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003401 struct chunk *replace;
3402 int ret = -1;
3403
3404 replace = alloc_trash_chunk();
3405 if (!replace)
3406 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003407
3408 replace->len = build_logline(s, replace->str, replace->size, fmt);
3409 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003410 goto leave;
3411
3412 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003413
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003414 leave:
3415 free_trash_chunk(replace);
3416 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003417}
3418
Willy Tarreau87b09662015-04-03 00:22:06 +02003419/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003420 * transaction <txn>. Returns the verdict of the first rule that prevents
3421 * further processing of the request (auth, deny, ...), and defaults to
3422 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3423 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003424 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3425 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3426 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003427 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003428enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003429http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003430{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003431 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003432 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003433 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003434 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003435 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003436 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003437 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003438 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003439
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003440 /* If "the current_rule_list" match the executed rule list, we are in
3441 * resume condition. If a resume is needed it is always in the action
3442 * and never in the ACL or converters. In this case, we initialise the
3443 * current rule, and go to the action execution point.
3444 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003445 if (s->current_rule) {
3446 rule = s->current_rule;
3447 s->current_rule = NULL;
3448 if (s->current_rule_list == rules)
3449 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003450 }
3451 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003452
Willy Tarreauff011f22011-01-06 17:51:27 +01003453 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003454
Willy Tarreau96257ec2012-12-27 10:46:37 +01003455 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003456 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003457 int ret;
3458
Willy Tarreau192252e2015-04-04 01:47:55 +02003459 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003460 ret = acl_pass(ret);
3461
Willy Tarreauff011f22011-01-06 17:51:27 +01003462 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003463 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003464
3465 if (!ret) /* condition not matched */
3466 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003467 }
3468
Willy Tarreauacc98002015-09-27 23:34:39 +02003469 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003470resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003471 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003472 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003473 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003474
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003475 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003476 if (deny_status)
3477 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003478 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003479
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003480 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003481 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003482 if (deny_status)
3483 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003484 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003485
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003486 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003487 /* Auth might be performed on regular http-req rules as well as on stats */
3488 auth_realm = rule->arg.auth.realm;
3489 if (!auth_realm) {
3490 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3491 auth_realm = STATS_DEFAULT_REALM;
3492 else
3493 auth_realm = px->id;
3494 }
3495 /* send 401/407 depending on whether we use a proxy or not. We still
3496 * count one error, because normal browsing won't significantly
3497 * increase the counter but brute force attempts will.
3498 */
3499 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3500 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003501 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003502 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003503 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003505 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003506 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3507 return HTTP_RULE_RES_BADREQ;
3508 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003509
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003510 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003511 s->task->nice = rule->arg.nice;
3512 break;
3513
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003514 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003515 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02003516 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003517 break;
3518
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003519 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003520#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003521 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02003522 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003523#endif
3524 break;
3525
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003526 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003527 s->logs.level = rule->arg.loglevel;
3528 break;
3529
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003530 case ACT_HTTP_REPLACE_HDR:
3531 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003532 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3533 rule->arg.hdr_add.name_len,
3534 &rule->arg.hdr_add.fmt,
3535 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003536 return HTTP_RULE_RES_BADREQ;
3537 break;
3538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003540 ctx.idx = 0;
3541 /* remove all occurrences of the header */
3542 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3543 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3544 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003545 }
Willy Tarreau85603282015-01-21 20:39:27 +01003546 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003548 case ACT_HTTP_SET_HDR:
3549 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003550 /* The scope of the trash buffer must be limited to this function. The
3551 * build_logline() function can execute a lot of other function which
3552 * can use the trash buffer. So for limiting the scope of this global
3553 * buffer, we build first the header value using build_logline, and
3554 * after we store the header name.
3555 */
3556 len = rule->arg.hdr_add.name_len + 2,
3557 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003558 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003559 trash.str[rule->arg.hdr_add.name_len] = ':';
3560 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3561 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003563 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003564 /* remove all occurrences of the header */
3565 ctx.idx = 0;
3566 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3567 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3568 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3569 }
3570 }
3571
Willy Tarreau96257ec2012-12-27 10:46:37 +01003572 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3573 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003575 case ACT_HTTP_DEL_ACL:
3576 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003577 struct pat_ref *ref;
3578 char *key;
3579 int len;
3580
3581 /* collect reference */
3582 ref = pat_ref_lookup(rule->arg.map.ref);
3583 if (!ref)
3584 continue;
3585
3586 /* collect key */
3587 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3588 key = trash.str;
3589 key[len] = '\0';
3590
3591 /* perform update */
3592 /* returned code: 1=ok, 0=ko */
3593 pat_ref_delete(ref, key);
3594
3595 break;
3596 }
3597
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003598 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003599 struct pat_ref *ref;
3600 char *key;
3601 struct chunk *trash_key;
3602 int len;
3603
3604 trash_key = get_trash_chunk();
3605
3606 /* collect reference */
3607 ref = pat_ref_lookup(rule->arg.map.ref);
3608 if (!ref)
3609 continue;
3610
3611 /* collect key */
3612 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3613 key = trash_key->str;
3614 key[len] = '\0';
3615
3616 /* perform update */
3617 /* add entry only if it does not already exist */
3618 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003619 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003620
3621 break;
3622 }
3623
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003624 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003625 struct pat_ref *ref;
3626 char *key, *value;
3627 struct chunk *trash_key, *trash_value;
3628 int len;
3629
3630 trash_key = get_trash_chunk();
3631 trash_value = get_trash_chunk();
3632
3633 /* collect reference */
3634 ref = pat_ref_lookup(rule->arg.map.ref);
3635 if (!ref)
3636 continue;
3637
3638 /* collect key */
3639 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3640 key = trash_key->str;
3641 key[len] = '\0';
3642
3643 /* collect value */
3644 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3645 value = trash_value->str;
3646 value[len] = '\0';
3647
3648 /* perform update */
3649 if (pat_ref_find_elt(ref, key) != NULL)
3650 /* update entry if it exists */
3651 pat_ref_set(ref, key, value, NULL);
3652 else
3653 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003654 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003655
3656 break;
3657 }
William Lallemand73025dd2014-04-24 14:38:37 +02003658
Thierry FOURNIER42148732015-09-02 17:17:33 +02003659 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003660 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3661 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003662
Willy Tarreauacc98002015-09-27 23:34:39 +02003663 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003664 case ACT_RET_ERR:
3665 case ACT_RET_CONT:
3666 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003667 case ACT_RET_STOP:
3668 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003669 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003670 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003671 return HTTP_RULE_RES_YIELD;
3672 }
William Lallemand73025dd2014-04-24 14:38:37 +02003673 break;
3674
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003675 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003676 /* Note: only the first valid tracking parameter of each
3677 * applies.
3678 */
3679
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003680 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003681 struct stktable *t;
3682 struct stksess *ts;
3683 struct stktable_key *key;
3684 void *ptr;
3685
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003686 t = rule->arg.trk_ctr.table.t;
3687 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 +02003688
3689 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003690 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003691
3692 /* let's count a new HTTP request as it's the first time we do it */
3693 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3694 if (ptr)
3695 stktable_data_cast(ptr, http_req_cnt)++;
3696
3697 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3698 if (ptr)
3699 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3700 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3701
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003702 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003703 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003704 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003705 }
3706 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003707 break;
3708
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003709 /* other flags exists, but normaly, they never be matched. */
3710 default:
3711 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003712 }
3713 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003714
3715 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003716 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003717}
3718
Willy Tarreau71241ab2012-12-27 11:30:54 +01003719
Willy Tarreau51d861a2015-05-22 17:30:48 +02003720/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3721 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3722 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3723 * is returned, the process can continue the evaluation of next rule list. If
3724 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3725 * is returned, it means the operation could not be processed and a server error
3726 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3727 * deny rule. If *YIELD is returned, the caller must call again the function
3728 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003729 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003730static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003731http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003732{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003733 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003734 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003735 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003736 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003737 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003738 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003739
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003740 /* If "the current_rule_list" match the executed rule list, we are in
3741 * resume condition. If a resume is needed it is always in the action
3742 * and never in the ACL or converters. In this case, we initialise the
3743 * current rule, and go to the action execution point.
3744 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003745 if (s->current_rule) {
3746 rule = s->current_rule;
3747 s->current_rule = NULL;
3748 if (s->current_rule_list == rules)
3749 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003750 }
3751 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003752
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003753 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003754
3755 /* check optional condition */
3756 if (rule->cond) {
3757 int ret;
3758
Willy Tarreau192252e2015-04-04 01:47:55 +02003759 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760 ret = acl_pass(ret);
3761
3762 if (rule->cond->pol == ACL_COND_UNLESS)
3763 ret = !ret;
3764
3765 if (!ret) /* condition not matched */
3766 continue;
3767 }
3768
Willy Tarreauacc98002015-09-27 23:34:39 +02003769 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003770resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003771 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003772 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003773 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003774
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003775 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003776 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003777 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003778
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003779 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003780 s->task->nice = rule->arg.nice;
3781 break;
3782
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003783 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003784 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02003785 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003786 break;
3787
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003788 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003789#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003790 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02003791 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003792#endif
3793 break;
3794
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003795 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003796 s->logs.level = rule->arg.loglevel;
3797 break;
3798
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003799 case ACT_HTTP_REPLACE_HDR:
3800 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003801 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3802 rule->arg.hdr_add.name_len,
3803 &rule->arg.hdr_add.fmt,
3804 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003805 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003806 break;
3807
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003808 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003809 ctx.idx = 0;
3810 /* remove all occurrences of the header */
3811 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3812 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3813 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3814 }
Willy Tarreau85603282015-01-21 20:39:27 +01003815 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003816
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003817 case ACT_HTTP_SET_HDR:
3818 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003819 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3820 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3821 trash.len = rule->arg.hdr_add.name_len;
3822 trash.str[trash.len++] = ':';
3823 trash.str[trash.len++] = ' ';
3824 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 +01003825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003826 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003827 /* remove all occurrences of the header */
3828 ctx.idx = 0;
3829 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3830 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3831 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3832 }
3833 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003834 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3835 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003836
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003837 case ACT_HTTP_DEL_ACL:
3838 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003839 struct pat_ref *ref;
3840 char *key;
3841 int len;
3842
3843 /* collect reference */
3844 ref = pat_ref_lookup(rule->arg.map.ref);
3845 if (!ref)
3846 continue;
3847
3848 /* collect key */
3849 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3850 key = trash.str;
3851 key[len] = '\0';
3852
3853 /* perform update */
3854 /* returned code: 1=ok, 0=ko */
3855 pat_ref_delete(ref, key);
3856
3857 break;
3858 }
3859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003860 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003861 struct pat_ref *ref;
3862 char *key;
3863 struct chunk *trash_key;
3864 int len;
3865
3866 trash_key = get_trash_chunk();
3867
3868 /* collect reference */
3869 ref = pat_ref_lookup(rule->arg.map.ref);
3870 if (!ref)
3871 continue;
3872
3873 /* collect key */
3874 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3875 key = trash_key->str;
3876 key[len] = '\0';
3877
3878 /* perform update */
3879 /* check if the entry already exists */
3880 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003881 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003882
3883 break;
3884 }
3885
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003886 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003887 struct pat_ref *ref;
3888 char *key, *value;
3889 struct chunk *trash_key, *trash_value;
3890 int len;
3891
3892 trash_key = get_trash_chunk();
3893 trash_value = get_trash_chunk();
3894
3895 /* collect reference */
3896 ref = pat_ref_lookup(rule->arg.map.ref);
3897 if (!ref)
3898 continue;
3899
3900 /* collect key */
3901 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3902 key = trash_key->str;
3903 key[len] = '\0';
3904
3905 /* collect value */
3906 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3907 value = trash_value->str;
3908 value[len] = '\0';
3909
3910 /* perform update */
3911 if (pat_ref_find_elt(ref, key) != NULL)
3912 /* update entry if it exists */
3913 pat_ref_set(ref, key, value, NULL);
3914 else
3915 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003916 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003917
3918 break;
3919 }
William Lallemand73025dd2014-04-24 14:38:37 +02003920
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003921 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003922 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3923 return HTTP_RULE_RES_BADREQ;
3924 return HTTP_RULE_RES_DONE;
3925
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003926 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3927 /* Note: only the first valid tracking parameter of each
3928 * applies.
3929 */
3930
3931 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3932 struct stktable *t;
3933 struct stksess *ts;
3934 struct stktable_key *key;
3935 void *ptr;
3936
3937 t = rule->arg.trk_ctr.table.t;
3938 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3939
3940 if (key && (ts = stktable_get_entry(t, key))) {
3941 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3942
3943 /* let's count a new HTTP request as it's the first time we do it */
3944 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3945 if (ptr)
3946 stktable_data_cast(ptr, http_req_cnt)++;
3947
3948 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3949 if (ptr)
3950 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3951 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3952
3953 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3954 if (sess->fe != s->be)
3955 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3956
3957 /* When the client triggers a 4xx from the server, it's most often due
3958 * to a missing object or permission. These events should be tracked
3959 * because if they happen often, it may indicate a brute force or a
3960 * vulnerability scan. Normally this is done when receiving the response
3961 * but here we're tracking after this ought to have been done so we have
3962 * to do it on purpose.
3963 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003964 if ((unsigned)(txn->status - 400) < 100) {
3965 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3966 if (ptr)
3967 stktable_data_cast(ptr, http_err_cnt)++;
3968
3969 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3970 if (ptr)
3971 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3972 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3973 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003974 }
3975 }
3976 break;
3977
Thierry FOURNIER42148732015-09-02 17:17:33 +02003978 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003979 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3980 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003981
Willy Tarreauacc98002015-09-27 23:34:39 +02003982 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003983 case ACT_RET_ERR:
3984 case ACT_RET_CONT:
3985 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003986 case ACT_RET_STOP:
3987 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003988 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003989 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003990 return HTTP_RULE_RES_YIELD;
3991 }
William Lallemand73025dd2014-04-24 14:38:37 +02003992 break;
3993
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003994 /* other flags exists, but normaly, they never be matched. */
3995 default:
3996 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003997 }
3998 }
3999
4000 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004001 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004002}
4003
4004
Willy Tarreau71241ab2012-12-27 11:30:54 +01004005/* Perform an HTTP redirect based on the information in <rule>. The function
4006 * returns non-zero on success, or zero in case of a, irrecoverable error such
4007 * as too large a request to build a valid response.
4008 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004009static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004010{
Willy Tarreaub329a312015-05-22 16:27:37 +02004011 struct http_msg *req = &txn->req;
4012 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004013 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004014 struct chunk *chunk;
4015 int ret = 0;
4016
4017 chunk = alloc_trash_chunk();
4018 if (!chunk)
4019 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004020
4021 /* build redirect message */
4022 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004023 case 308:
4024 msg_fmt = HTTP_308;
4025 break;
4026 case 307:
4027 msg_fmt = HTTP_307;
4028 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004029 case 303:
4030 msg_fmt = HTTP_303;
4031 break;
4032 case 301:
4033 msg_fmt = HTTP_301;
4034 break;
4035 case 302:
4036 default:
4037 msg_fmt = HTTP_302;
4038 break;
4039 }
4040
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004041 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4042 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004043
4044 switch(rule->type) {
4045 case REDIRECT_TYPE_SCHEME: {
4046 const char *path;
4047 const char *host;
4048 struct hdr_ctx ctx;
4049 int pathlen;
4050 int hostlen;
4051
4052 host = "";
4053 hostlen = 0;
4054 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004055 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004056 host = ctx.line + ctx.val;
4057 hostlen = ctx.vlen;
4058 }
4059
4060 path = http_get_path(txn);
4061 /* build message using path */
4062 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004063 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004064 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4065 int qs = 0;
4066 while (qs < pathlen) {
4067 if (path[qs] == '?') {
4068 pathlen = qs;
4069 break;
4070 }
4071 qs++;
4072 }
4073 }
4074 } else {
4075 path = "/";
4076 pathlen = 1;
4077 }
4078
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004079 if (rule->rdr_str) { /* this is an old "redirect" rule */
4080 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004081 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4082 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004083
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004084 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004085 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4086 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004087 }
4088 else {
4089 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004090 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004091
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004092 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004093 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4094 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004095 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004096 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004097 memcpy(chunk->str + chunk->len, "://", 3);
4098 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004099
4100 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004101 memcpy(chunk->str + chunk->len, host, hostlen);
4102 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004103
4104 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004105 memcpy(chunk->str + chunk->len, path, pathlen);
4106 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004107
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004108 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004109 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004111 if (chunk->len > chunk->size - 5)
4112 goto leave;
4113 chunk->str[chunk->len] = '/';
4114 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004115 }
4116
4117 break;
4118 }
4119 case REDIRECT_TYPE_PREFIX: {
4120 const char *path;
4121 int pathlen;
4122
4123 path = http_get_path(txn);
4124 /* build message using path */
4125 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004126 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004127 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4128 int qs = 0;
4129 while (qs < pathlen) {
4130 if (path[qs] == '?') {
4131 pathlen = qs;
4132 break;
4133 }
4134 qs++;
4135 }
4136 }
4137 } else {
4138 path = "/";
4139 pathlen = 1;
4140 }
4141
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004142 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004143 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4144 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004145
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004146 /* add prefix. Note that if prefix == "/", we don't want to
4147 * add anything, otherwise it makes it hard for the user to
4148 * configure a self-redirection.
4149 */
4150 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004151 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4152 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004153 }
4154 }
4155 else {
4156 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004157 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004158
4159 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004160 if (chunk->len + pathlen > chunk->size - 4)
4161 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004162 }
4163
4164 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004165 memcpy(chunk->str + chunk->len, path, pathlen);
4166 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004167
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004168 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004169 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004170 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004171 if (chunk->len > chunk->size - 5)
4172 goto leave;
4173 chunk->str[chunk->len] = '/';
4174 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175 }
4176
4177 break;
4178 }
4179 case REDIRECT_TYPE_LOCATION:
4180 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004181 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004182 if (chunk->len + rule->rdr_len > chunk->size - 4)
4183 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004184
4185 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004186 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4187 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004188 }
4189 else {
4190 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004191 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004192
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004193 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004194 if (chunk->len > chunk->size - 4)
4195 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004196 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004197 break;
4198 }
4199
4200 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004201 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4202 chunk->len += 14;
4203 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4204 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004205 }
4206
Willy Tarreau19b14122017-02-28 09:48:11 +01004207 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004208 txn->status = rule->code;
4209 /* let's log the request time */
4210 s->logs.tv_request = now;
4211
Christopher Fauletbe821b92017-03-30 11:21:53 +02004212 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004213 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4214 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4215 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004216 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004217 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004218 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4219 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004221 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4222 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004223 }
4224 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004225 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4226 chunk->len += 4;
4227 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02004228 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004229 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004230 bi_fast_delete(req->chn->buf, req->sov);
4231 req->next -= req->sov;
4232 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004233 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01004234 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004235 req->msg_state = HTTP_MSG_CLOSED;
4236 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004237 /* Trim any possible response */
4238 res->chn->buf->i = 0;
4239 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02004240 /* let the server side turn to SI_ST_CLO */
4241 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004242 } else {
4243 /* keep-alive not possible */
4244 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004245 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4246 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004248 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4249 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004250 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004251 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004252 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 }
4254
Willy Tarreaue7dff022015-04-03 01:14:29 +02004255 if (!(s->flags & SF_ERR_MASK))
4256 s->flags |= SF_ERR_LOCAL;
4257 if (!(s->flags & SF_FINST_MASK))
4258 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004259
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004260 ret = 1;
4261 leave:
4262 free_trash_chunk(chunk);
4263 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264}
4265
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004266/* This stream analyser runs all HTTP request processing which is common to
4267 * frontends and backends, which means blocking ACLs, filters, connection-close,
4268 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004269 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004270 * either needs more data or wants to immediately abort the request (eg: deny,
4271 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004272 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004273int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004274{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004275 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004276 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004277 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004278 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004279 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004280 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004281 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004282
Willy Tarreau655dce92009-11-08 13:10:58 +01004283 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004284 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004285 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004286 }
4287
Willy Tarreau87b09662015-04-03 00:22:06 +02004288 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 +02004289 now_ms, __FUNCTION__,
4290 s,
4291 req,
4292 req->rex, req->wex,
4293 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004294 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004295 req->analysers);
4296
Willy Tarreau65410832014-04-28 21:25:43 +02004297 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004298 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004299
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004300 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004301 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004302 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004303
Willy Tarreau0b748332014-04-29 00:13:29 +02004304 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004305 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4306 goto return_prx_yield;
4307
Willy Tarreau0b748332014-04-29 00:13:29 +02004308 case HTTP_RULE_RES_CONT:
4309 case HTTP_RULE_RES_STOP: /* nothing to do */
4310 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004311
Willy Tarreau0b748332014-04-29 00:13:29 +02004312 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4313 if (txn->flags & TX_CLTARPIT)
4314 goto tarpit;
4315 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004316
Willy Tarreau0b748332014-04-29 00:13:29 +02004317 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4318 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004319
Willy Tarreau0b748332014-04-29 00:13:29 +02004320 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004321 goto done;
4322
Willy Tarreau0b748332014-04-29 00:13:29 +02004323 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4324 goto return_bad_req;
4325 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004326 }
4327
Willy Tarreau52542592014-04-28 18:33:26 +02004328 /* OK at this stage, we know that the request was accepted according to
4329 * the http-request rules, we can check for the stats. Note that the
4330 * URI is detected *before* the req* rules in order not to be affected
4331 * by a possible reqrep, while they are processed *after* so that a
4332 * reqdeny can still block them. This clearly needs to change in 1.6!
4333 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004334 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004335 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004336 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004337 txn->status = 500;
4338 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004339 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340
Willy Tarreaue7dff022015-04-03 01:14:29 +02004341 if (!(s->flags & SF_ERR_MASK))
4342 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004343 goto return_prx_cond;
4344 }
4345
4346 /* parse the whole stats request and extract the relevant information */
4347 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004348 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004349 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004350
Willy Tarreau0b748332014-04-29 00:13:29 +02004351 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4352 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004353
Willy Tarreau0b748332014-04-29 00:13:29 +02004354 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4355 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004356 }
4357
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004358 /* evaluate the req* rules except reqadd */
4359 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004360 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004361 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004362
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004363 if (txn->flags & TX_CLDENY)
4364 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004365
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004366 if (txn->flags & TX_CLTARPIT) {
4367 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004368 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004369 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004370 }
Willy Tarreau06619262006-12-17 08:37:22 +01004371
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004372 /* add request headers from the rule sets in the same order */
4373 list_for_each_entry(wl, &px->req_add, list) {
4374 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004375 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004376 ret = acl_pass(ret);
4377 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4378 ret = !ret;
4379 if (!ret)
4380 continue;
4381 }
4382
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004383 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004384 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004385 }
4386
Willy Tarreau52542592014-04-28 18:33:26 +02004387
4388 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004389 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004390 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004391 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4392 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004393
Willy Tarreaue7dff022015-04-03 01:14:29 +02004394 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4395 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4396 if (!(s->flags & SF_FINST_MASK))
4397 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004398
Willy Tarreau70730dd2014-04-24 18:06:27 +02004399 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004400 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4401 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004402 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004403 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004404 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004405
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004406 /* check whether we have some ACLs set to redirect this request */
4407 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004408 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004409 int ret;
4410
Willy Tarreau192252e2015-04-04 01:47:55 +02004411 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004412 ret = acl_pass(ret);
4413 if (rule->cond->pol == ACL_COND_UNLESS)
4414 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004415 if (!ret)
4416 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004417 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004418 if (!http_apply_redirect_rule(rule, s, txn))
4419 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004420 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004421 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004422
Willy Tarreau2be39392010-01-03 17:24:51 +01004423 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4424 * If this happens, then the data will not come immediately, so we must
4425 * send all what we have without waiting. Note that due to the small gain
4426 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004427 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004428 * itself once used.
4429 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004430 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004431
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004432 done: /* done with this analyser, continue with next ones that the calling
4433 * points will have set, if any.
4434 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004435 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004436 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4437 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004438 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004439
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004440 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02004441 /* Allow cookie logging
4442 */
4443 if (s->be->cookie_name || sess->fe->capture_name)
4444 manage_client_side_cookies(s, req);
4445
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004446 /* When a connection is tarpitted, we use the tarpit timeout,
4447 * which may be the same as the connect timeout if unspecified.
4448 * If unset, then set it to zero because we really want it to
4449 * eventually expire. We build the tarpit as an analyser.
4450 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004451 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004452
4453 /* wipe the request out so that we can drop the connection early
4454 * if the client closes first.
4455 */
4456 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004457
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004458 txn->status = http_err_codes[deny_status];
4459
Christopher Faulet0184ea72017-01-05 14:06:34 +01004460 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004461 req->analysers |= AN_REQ_HTTP_TARPIT;
4462 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4463 if (!req->analyse_exp)
4464 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004465 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004466 sess->fe->fe_counters.denied_req++;
4467 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004468 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004469 if (sess->listener->counters)
4470 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004471 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004472
4473 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004474
4475 /* Allow cookie logging
4476 */
4477 if (s->be->cookie_name || sess->fe->capture_name)
4478 manage_client_side_cookies(s, req);
4479
Willy Tarreau0b748332014-04-29 00:13:29 +02004480 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004481 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004482 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004483 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004484 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004485 sess->fe->fe_counters.denied_req++;
4486 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004488 if (sess->listener->counters)
4489 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004490 goto return_prx_cond;
4491
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004492 return_bad_req:
4493 /* We centralize bad requests processing here */
4494 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4495 /* we detected a parsing error. We want to archive this request
4496 * in the dedicated proxy area for later troubleshooting.
4497 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004498 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004499 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004500
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004501 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004502 txn->req.msg_state = HTTP_MSG_ERROR;
4503 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004504 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004505
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004506 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004507 if (sess->listener->counters)
4508 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004509
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004511 if (!(s->flags & SF_ERR_MASK))
4512 s->flags |= SF_ERR_PRXCOND;
4513 if (!(s->flags & SF_FINST_MASK))
4514 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004515
Christopher Faulet0184ea72017-01-05 14:06:34 +01004516 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004517 req->analyse_exp = TICK_ETERNITY;
4518 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004519
4520 return_prx_yield:
4521 channel_dont_connect(req);
4522 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004523}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004524
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004525/* This function performs all the processing enabled for the current request.
4526 * It returns 1 if the processing can continue on next analysers, or zero if it
4527 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004528 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004529 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004530int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004531{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004532 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004533 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004534 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004535 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004536
Willy Tarreau655dce92009-11-08 13:10:58 +01004537 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004538 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004539 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004540 return 0;
4541 }
4542
Willy Tarreau87b09662015-04-03 00:22:06 +02004543 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 +02004544 now_ms, __FUNCTION__,
4545 s,
4546 req,
4547 req->rex, req->wex,
4548 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004549 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004550 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004551
Willy Tarreau59234e92008-11-30 23:51:27 +01004552 /*
4553 * Right now, we know that we have processed the entire headers
4554 * and that unwanted requests have been filtered out. We can do
4555 * whatever we want with the remaining request. Also, now we
4556 * may have separate values for ->fe, ->be.
4557 */
Willy Tarreau06619262006-12-17 08:37:22 +01004558
Willy Tarreau59234e92008-11-30 23:51:27 +01004559 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004560 * If HTTP PROXY is set we simply get remote server address parsing
4561 * incoming request. Note that this requires that a connection is
4562 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004563 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004564 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004565 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004566 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004567
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004568 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004569 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004570 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004571 txn->req.msg_state = HTTP_MSG_ERROR;
4572 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004573 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004574 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004575
Willy Tarreaue7dff022015-04-03 01:14:29 +02004576 if (!(s->flags & SF_ERR_MASK))
4577 s->flags |= SF_ERR_RESOURCE;
4578 if (!(s->flags & SF_FINST_MASK))
4579 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004580
4581 return 0;
4582 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004583
4584 path = http_get_path(txn);
4585 url2sa(req->buf->p + msg->sl.rq.u,
4586 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004587 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004588 /* if the path was found, we have to remove everything between
4589 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4590 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4591 * u_l characters by a single "/".
4592 */
4593 if (path) {
4594 char *cur_ptr = req->buf->p;
4595 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4596 int delta;
4597
4598 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4599 http_msg_move_end(&txn->req, delta);
4600 cur_end += delta;
4601 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4602 goto return_bad_req;
4603 }
4604 else {
4605 char *cur_ptr = req->buf->p;
4606 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4607 int delta;
4608
4609 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4610 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4611 http_msg_move_end(&txn->req, delta);
4612 cur_end += delta;
4613 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4614 goto return_bad_req;
4615 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004617
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004619 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004620 * Note that doing so might move headers in the request, but
4621 * the fields will stay coherent and the URI will not move.
4622 * This should only be performed in the backend.
4623 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004624 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004626
William Lallemanda73203e2012-03-12 12:48:57 +01004627 /* add unique-id if "header-unique-id" is specified */
4628
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004629 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004630 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4631 goto return_bad_req;
4632 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004633 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004634 }
William Lallemanda73203e2012-03-12 12:48:57 +01004635
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004636 if (sess->fe->header_unique_id && s->unique_id) {
4637 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004638 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004639 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004640 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004641 goto return_bad_req;
4642 }
4643
Cyril Bontéb21570a2009-11-29 20:04:48 +01004644 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004645 * 9: add X-Forwarded-For if either the frontend or the backend
4646 * asks for it.
4647 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004649 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4651 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4652 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004653 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004654 /* The header is set to be added only if none is present
4655 * and we found it, so don't do anything.
4656 */
4657 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004658 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004659 /* Add an X-Forwarded-For header unless the source IP is
4660 * in the 'except' network range.
4661 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004662 if ((!sess->fe->except_mask.s_addr ||
4663 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4664 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004666 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004668 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004669 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004670 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004671
4672 /* Note: we rely on the backend to get the header name to be used for
4673 * x-forwarded-for, because the header is really meant for the backends.
4674 * However, if the backend did not specify any option, we have to rely
4675 * on the frontend's header name.
4676 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004677 if (s->be->fwdfor_hdr_len) {
4678 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004679 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004680 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004681 len = sess->fe->fwdfor_hdr_len;
4682 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004683 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004684 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 +01004685
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004686 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004687 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004688 }
4689 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004690 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004691 /* FIXME: for the sake of completeness, we should also support
4692 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004693 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004694 int len;
4695 char pn[INET6_ADDRSTRLEN];
4696 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004697 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004698 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004699
Willy Tarreau59234e92008-11-30 23:51:27 +01004700 /* Note: we rely on the backend to get the header name to be used for
4701 * x-forwarded-for, because the header is really meant for the backends.
4702 * However, if the backend did not specify any option, we have to rely
4703 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004704 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004705 if (s->be->fwdfor_hdr_len) {
4706 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004707 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004709 len = sess->fe->fwdfor_hdr_len;
4710 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004711 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004712 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004713
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004714 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 goto return_bad_req;
4716 }
4717 }
4718
4719 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004720 * 10: add X-Original-To if either the frontend or the backend
4721 * asks for it.
4722 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004723 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004724
4725 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004726 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004727 /* Add an X-Original-To header unless the destination IP is
4728 * in the 'except' network range.
4729 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004730 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004731
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004732 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004733 ((!sess->fe->except_mask_to.s_addr ||
4734 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4735 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004736 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 (((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 +02004738 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004739 int len;
4740 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004741 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004742
4743 /* Note: we rely on the backend to get the header name to be used for
4744 * x-original-to, because the header is really meant for the backends.
4745 * However, if the backend did not specify any option, we have to rely
4746 * on the frontend's header name.
4747 */
4748 if (s->be->orgto_hdr_len) {
4749 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004750 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004751 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004752 len = sess->fe->orgto_hdr_len;
4753 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004754 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004755 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 +02004756
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004757 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004758 goto return_bad_req;
4759 }
4760 }
4761 }
4762
Willy Tarreau50fc7772012-11-11 22:19:57 +01004763 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4764 * If an "Upgrade" token is found, the header is left untouched in order not to have
4765 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4766 * "Upgrade" is present in the Connection header.
4767 */
4768 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4769 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004770 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004771 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004772 unsigned int want_flags = 0;
4773
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004774 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004775 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004777 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004778 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004779 want_flags |= TX_CON_CLO_SET;
4780 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004781 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004782 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004783 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004784 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004785 want_flags |= TX_CON_KAL_SET;
4786 }
4787
4788 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004789 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004790 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004792
Willy Tarreau522d6c02009-12-06 18:49:18 +01004793 /* If we have no server assigned yet and we're balancing on url_param
4794 * with a POST request, we may be interested in checking the body for
4795 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004796 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004797 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004798 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004799 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004800 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004801 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004802 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004803
Christopher Fauletbe821b92017-03-30 11:21:53 +02004804 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
4805 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004806#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02004807 /* We expect some data from the client. Unless we know for sure
4808 * we already have a full request, we have to re-enable quick-ack
4809 * in case we previously disabled it, otherwise we might cause
4810 * the client to delay further data.
4811 */
4812 if ((sess->listener->options & LI_O_NOQUICKACK) &&
4813 cli_conn && conn_ctrl_ready(cli_conn) &&
4814 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
4815 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02004816 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004817#endif
Willy Tarreau03945942009-12-22 16:50:27 +01004818
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 /*************************************************************
4820 * OK, that's finished for the headers. We have done what we *
4821 * could. Let's switch to the DATA state. *
4822 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 req->analyse_exp = TICK_ETERNITY;
4824 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004825
Willy Tarreau59234e92008-11-30 23:51:27 +01004826 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004827 /* OK let's go on with the BODY now */
4828 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004829
Willy Tarreau59234e92008-11-30 23:51:27 +01004830 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004831 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004832 /* we detected a parsing error. We want to archive this request
4833 * in the dedicated proxy area for later troubleshooting.
4834 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004835 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004836 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004837
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004838 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004839 txn->req.msg_state = HTTP_MSG_ERROR;
4840 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004841 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004842 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004843
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004844 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004845 if (sess->listener->counters)
4846 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004847
Willy Tarreaue7dff022015-04-03 01:14:29 +02004848 if (!(s->flags & SF_ERR_MASK))
4849 s->flags |= SF_ERR_PRXCOND;
4850 if (!(s->flags & SF_FINST_MASK))
4851 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004852 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004853}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004854
Willy Tarreau60b85b02008-11-30 23:28:40 +01004855/* This function is an analyser which processes the HTTP tarpit. It always
4856 * returns zero, at the beginning because it prevents any other processing
4857 * from occurring, and at the end because it terminates the request.
4858 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004859int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004860{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004861 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004862
4863 /* This connection is being tarpitted. The CLIENT side has
4864 * already set the connect expiration date to the right
4865 * timeout. We just have to check that the client is still
4866 * there and that the timeout has not expired.
4867 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004868 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004869 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004870 !tick_is_expired(req->analyse_exp, now_ms))
4871 return 0;
4872
4873 /* We will set the queue timer to the time spent, just for
4874 * logging purposes. We fake a 500 server error, so that the
4875 * attacker will not suspect his connection has been tarpitted.
4876 * It will not cause trouble to the logs because we can exclude
4877 * the tarpitted connections by filtering on the 'PT' status flags.
4878 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004879 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4880
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004881 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004882 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883
Christopher Faulet0184ea72017-01-05 14:06:34 +01004884 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004886
Willy Tarreaue7dff022015-04-03 01:14:29 +02004887 if (!(s->flags & SF_ERR_MASK))
4888 s->flags |= SF_ERR_PRXCOND;
4889 if (!(s->flags & SF_FINST_MASK))
4890 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004891 return 0;
4892}
4893
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004894/* This function is an analyser which waits for the HTTP request body. It waits
4895 * for either the buffer to be full, or the full advertised contents to have
4896 * reached the buffer. It must only be called after the standard HTTP request
4897 * processing has occurred, because it expects the request to be parsed and will
4898 * look for the Expect header. It may send a 100-Continue interim response. It
4899 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4900 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4901 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004902 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004903int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004904{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004905 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004906 struct http_txn *txn = s->txn;
4907 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004908
4909 /* We have to parse the HTTP request body to find any required data.
4910 * "balance url_param check_post" should have been the only way to get
4911 * into this. We were brought here after HTTP header analysis, so all
4912 * related structures are ready.
4913 */
4914
Willy Tarreau890988f2014-04-10 11:59:33 +02004915 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4916 /* This is the first call */
4917 if (msg->msg_state < HTTP_MSG_BODY)
4918 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004919
Willy Tarreau890988f2014-04-10 11:59:33 +02004920 if (msg->msg_state < HTTP_MSG_100_SENT) {
4921 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4922 * send an HTTP/1.1 100 Continue intermediate response.
4923 */
4924 if (msg->flags & HTTP_MSGF_VER_11) {
4925 struct hdr_ctx ctx;
4926 ctx.idx = 0;
4927 /* Expect is allowed in 1.1, look for it */
4928 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4929 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004930 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004931 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004932 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004933 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004934 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004935 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004936
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004937 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004938 * req->buf->p still points to the beginning of the message. We
4939 * must save the body in msg->next because it survives buffer
4940 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004941 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004942 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004943
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004944 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004945 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4946 else
4947 msg->msg_state = HTTP_MSG_DATA;
4948 }
4949
Willy Tarreau890988f2014-04-10 11:59:33 +02004950 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4951 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004952 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004953 goto missing_data;
4954
4955 /* OK we have everything we need now */
4956 goto http_end;
4957 }
4958
4959 /* OK here we're parsing a chunked-encoded message */
4960
Willy Tarreau522d6c02009-12-06 18:49:18 +01004961 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004962 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004963 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004965 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004966 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004967
Willy Tarreau115acb92009-12-26 13:56:06 +01004968 if (!ret)
4969 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004970 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004971 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004972 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004973 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004974 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004975 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004976 }
4977
Willy Tarreaud98cf932009-12-27 22:54:55 +01004978 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004979 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4980 * for at least a whole chunk or the whole content length bytes after
4981 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004982 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004983 if (msg->msg_state == HTTP_MSG_TRAILERS)
4984 goto http_end;
4985
Willy Tarreaue115b492015-05-01 23:05:14 +02004986 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004987 goto http_end;
4988
4989 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004990 /* we get here if we need to wait for more data. If the buffer is full,
4991 * we have the maximum we can expect.
4992 */
4993 if (buffer_full(req->buf, global.tune.maxrewrite))
4994 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004995
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004996 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004997 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004998 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004999
Willy Tarreaue7dff022015-04-03 01:14:29 +02005000 if (!(s->flags & SF_ERR_MASK))
5001 s->flags |= SF_ERR_CLITO;
5002 if (!(s->flags & SF_FINST_MASK))
5003 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005004 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005005 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005006
5007 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005008 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005009 /* Not enough data. We'll re-use the http-request
5010 * timeout here. Ideally, we should set the timeout
5011 * relative to the accept() date. We just set the
5012 * request timeout once at the beginning of the
5013 * request.
5014 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005015 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005016 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005017 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005018 return 0;
5019 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005020
5021 http_end:
5022 /* The situation will not evolve, so let's give up on the analysis. */
5023 s->logs.tv_request = now; /* update the request timer to reflect full request */
5024 req->analysers &= ~an_bit;
5025 req->analyse_exp = TICK_ETERNITY;
5026 return 1;
5027
5028 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005029 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005030 txn->req.msg_state = HTTP_MSG_ERROR;
5031 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005032 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005033
Willy Tarreaue7dff022015-04-03 01:14:29 +02005034 if (!(s->flags & SF_ERR_MASK))
5035 s->flags |= SF_ERR_PRXCOND;
5036 if (!(s->flags & SF_FINST_MASK))
5037 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005038
Willy Tarreau522d6c02009-12-06 18:49:18 +01005039 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005040 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005041 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005042 if (sess->listener->counters)
5043 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005044 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005045}
5046
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005047/* send a server's name with an outgoing request over an established connection.
5048 * Note: this function is designed to be called once the request has been scheduled
5049 * for being forwarded. This is the reason why it rewinds the buffer before
5050 * proceeding.
5051 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005052int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005053
5054 struct hdr_ctx ctx;
5055
Mark Lamourinec2247f02012-01-04 13:02:01 -05005056 char *hdr_name = be->server_id_hdr_name;
5057 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005058 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005059 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005060 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005061
William Lallemandd9e90662012-01-30 17:27:17 +01005062 ctx.idx = 0;
5063
Willy Tarreau211cdec2014-04-17 20:18:08 +02005064 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005065 if (old_o) {
5066 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005067 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005068 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005069 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005070 }
5071
Willy Tarreau9b28e032012-10-12 23:49:43 +02005072 old_i = chn->buf->i;
5073 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 -05005074 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005075 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005076 }
5077
5078 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005079 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005080 memcpy(hdr_val, hdr_name, hdr_name_len);
5081 hdr_val += hdr_name_len;
5082 *hdr_val++ = ':';
5083 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005084 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5085 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005086
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005087 if (old_o) {
5088 /* If this was a forwarded request, we must readjust the amount of
5089 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005090 * variations. Note that the current state is >= HTTP_MSG_BODY,
5091 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005092 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005093 old_o += chn->buf->i - old_i;
5094 b_adv(chn->buf, old_o);
5095 txn->req.next -= old_o;
5096 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005097 }
5098
Mark Lamourinec2247f02012-01-04 13:02:01 -05005099 return 0;
5100}
5101
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102/* Terminate current transaction and prepare a new one. This is very tricky
5103 * right now but it works.
5104 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005105void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005107 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005108 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005109 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005110 struct connection *srv_conn;
5111 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005112 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005113
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 /* FIXME: We need a more portable way of releasing a backend's and a
5115 * server's connections. We need a safer way to reinitialize buffer
5116 * flags. We also need a more accurate method for computing per-request
5117 * data.
5118 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005119 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120
Willy Tarreau4213a112013-12-15 10:25:42 +01005121 /* unless we're doing keep-alive, we want to quickly close the connection
5122 * to the server.
5123 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005124 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005125 !si_conn_ready(&s->si[1])) {
5126 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5127 si_shutr(&s->si[1]);
5128 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130
Willy Tarreaue7dff022015-04-03 01:14:29 +02005131 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005132 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005133 if (unlikely(s->srv_conn))
5134 sess_change_server(s, NULL);
5135 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136
5137 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005138 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139
Willy Tarreaueee5b512015-04-03 23:46:31 +02005140 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 int n;
5142
Willy Tarreaueee5b512015-04-03 23:46:31 +02005143 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 if (n < 1 || n > 5)
5145 n = 0;
5146
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005147 if (fe->mode == PR_MODE_HTTP) {
5148 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005149 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005150 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005151 (be->mode == PR_MODE_HTTP)) {
5152 be->be_counters.p.http.rsp[n]++;
5153 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005154 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 }
5156
5157 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005158 s->logs.bytes_in -= s->req.buf->i;
5159 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160
5161 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005162 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005163 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005164 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165 s->do_log(s);
5166 }
5167
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005168 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005169 stream_stop_content_counters(s);
5170 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005171
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172 s->logs.accept_date = date; /* user-visible date for logging */
5173 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005174 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5175 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 tv_zero(&s->logs.tv_request);
5177 s->logs.t_queue = -1;
5178 s->logs.t_connect = -1;
5179 s->logs.t_data = -1;
5180 s->logs.t_close = 0;
5181 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5182 s->logs.srv_queue_size = 0; /* we will get this number soon */
5183
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005184 s->logs.bytes_in = s->req.total = s->req.buf->i;
5185 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186
5187 if (s->pend_pos)
5188 pendconn_free(s->pend_pos);
5189
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005190 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005191 if (s->flags & SF_CURR_SESS) {
5192 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005193 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005195 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005196 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 }
5198
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005199 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200
Willy Tarreau4213a112013-12-15 10:25:42 +01005201 /* only release our endpoint if we don't intend to reuse the
5202 * connection.
5203 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005204 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005205 !si_conn_ready(&s->si[1])) {
5206 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005207 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005208 }
5209
Willy Tarreau350f4872014-11-28 14:42:25 +01005210 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5211 s->si[1].err_type = SI_ET_NONE;
5212 s->si[1].conn_retries = 0; /* used for logging too */
5213 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005214 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 +01005215 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);
5216 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 +02005217 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5218 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5219 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005220
Willy Tarreaueee5b512015-04-03 23:46:31 +02005221 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005223 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005224
5225 if (prev_status == 401 || prev_status == 407) {
5226 /* In HTTP keep-alive mode, if we receive a 401, we still have
5227 * a chance of being able to send the visitor again to the same
5228 * server over the same connection. This is required by some
5229 * broken protocols such as NTLM, and anyway whenever there is
5230 * an opportunity for sending the challenge to the proper place,
5231 * it's better to do it (at least it helps with debugging).
5232 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005233 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005234 if (srv_conn)
5235 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005236 }
5237
Willy Tarreau53f96852016-02-02 18:50:47 +01005238 /* Never ever allow to reuse a connection from a non-reuse backend */
5239 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5240 srv_conn->flags |= CO_FL_PRIVATE;
5241
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005242 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005243 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005245 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005246 s->req.flags |= CF_NEVER_WAIT;
5247 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005248 }
5249
Willy Tarreau714ea782015-11-25 20:11:11 +01005250 /* we're removing the analysers, we MUST re-enable events detection.
5251 * We don't enable close on the response channel since it's either
5252 * already closed, or in keep-alive with an idle connection handler.
5253 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005254 channel_auto_read(&s->req);
5255 channel_auto_close(&s->req);
5256 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005257
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005258 /* we're in keep-alive with an idle connection, monitor it if not already done */
5259 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005260 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005261 if (!srv)
5262 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005263 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005264 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005265 else if (prev_flags & TX_NOT_FIRST)
5266 /* note: we check the request, not the connection, but
5267 * this is valid for strategies SAFE and AGGR, and in
5268 * case of ALWS, we don't care anyway.
5269 */
5270 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005271 else
5272 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005273 }
Christopher Faulete6006242017-03-10 11:52:44 +01005274 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5275 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276}
5277
5278
5279/* This function updates the request state machine according to the response
5280 * state machine and buffer flags. It returns 1 if it changes anything (flag
5281 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5282 * it is only used to find when a request/response couple is complete. Both
5283 * this function and its equivalent should loop until both return zero. It
5284 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5285 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005286int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005287{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005288 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005289 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005290 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 unsigned int old_state = txn->req.msg_state;
5292
Christopher Faulet4be98032017-07-18 10:48:24 +02005293 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294 return 0;
5295
5296 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005297 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005298 * We can shut the read side unless we want to abort_on_close,
5299 * or we have a POST request. The issue with POST requests is
5300 * that some browsers still send a CRLF after the request, and
5301 * this CRLF must be read so that it does not remain in the kernel
5302 * buffers, otherwise a close could cause an RST on some systems
5303 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005304 * Note that if we're using keep-alive on the client side, we'd
5305 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005306 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005307 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005308 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005309 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5310 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5311 !(s->be->options & PR_O_ABRT_CLOSE) &&
5312 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314
Willy Tarreau40f151a2012-12-20 12:10:09 +01005315 /* if the server closes the connection, we want to immediately react
5316 * and close the socket to save packets and syscalls.
5317 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005318 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005319
Willy Tarreau7f876a12015-11-18 11:59:55 +01005320 /* In any case we've finished parsing the request so we must
5321 * disable Nagle when sending data because 1) we're not going
5322 * to shut this side, and 2) the server is waiting for us to
5323 * send pending data.
5324 */
5325 chn->flags |= CF_NEVER_WAIT;
5326
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5328 goto wait_other_side;
5329
5330 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5331 /* The server has not finished to respond, so we
5332 * don't want to move in order not to upset it.
5333 */
5334 goto wait_other_side;
5335 }
5336
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337 /* When we get here, it means that both the request and the
5338 * response have finished receiving. Depending on the connection
5339 * mode, we'll have to wait for the last bytes to leave in either
5340 * direction, and sometimes for a close to be effective.
5341 */
5342
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005343 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5344 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005345 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5346 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005347 }
5348 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5349 /* Option forceclose is set, or either side wants to close,
5350 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005351 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005352 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005353 *
5354 * However, there is an exception if the response
5355 * length is undefined. In this case, we need to wait
5356 * the close from the server. The response will be
5357 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005358 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005359 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5360 txn->rsp.msg_state != HTTP_MSG_CLOSED)
5361 goto check_channel_flags;
5362
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005363 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5364 channel_shutr_now(chn);
5365 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005366 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005367 }
5368 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005369 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5370 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005371 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005372 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5373 channel_auto_read(chn);
5374 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005375 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005376 }
5377
Christopher Faulet4be98032017-07-18 10:48:24 +02005378 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 }
5380
5381 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5382 http_msg_closing:
5383 /* nothing else to forward, just waiting for the output buffer
5384 * to be empty and for the shutw_now to take effect.
5385 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005386 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005387 txn->req.msg_state = HTTP_MSG_CLOSED;
5388 goto http_msg_closed;
5389 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005390 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005391 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005393 }
Christopher Faulet56d26092017-07-20 11:05:10 +02005394 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 }
5396
5397 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5398 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005399 /* see above in MSG_DONE why we only do this in these states */
5400 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5401 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5402 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005403 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 goto wait_other_side;
5405 }
5406
Christopher Faulet4be98032017-07-18 10:48:24 +02005407 check_channel_flags:
5408 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5409 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5410 /* if we've just closed an output, let's switch */
5411 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
5412 txn->req.msg_state = HTTP_MSG_CLOSING;
5413 goto http_msg_closing;
5414 }
5415
5416
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005418 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419}
5420
5421
5422/* This function updates the response state machine according to the request
5423 * state machine and buffer flags. It returns 1 if it changes anything (flag
5424 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5425 * it is only used to find when a request/response couple is complete. Both
5426 * this function and its equivalent should loop until both return zero. It
5427 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5428 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005429int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005431 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005432 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005433 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005434 unsigned int old_state = txn->rsp.msg_state;
5435
Christopher Faulet4be98032017-07-18 10:48:24 +02005436 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 return 0;
5438
5439 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5440 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005441 * still monitor the server connection for a possible close
5442 * while the request is being uploaded, so we don't disable
5443 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005444 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005445 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005446
5447 if (txn->req.msg_state == HTTP_MSG_ERROR)
5448 goto wait_other_side;
5449
5450 if (txn->req.msg_state < HTTP_MSG_DONE) {
5451 /* The client seems to still be sending data, probably
5452 * because we got an error response during an upload.
5453 * We have the choice of either breaking the connection
5454 * or letting it pass through. Let's do the later.
5455 */
5456 goto wait_other_side;
5457 }
5458
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 /* When we get here, it means that both the request and the
5460 * response have finished receiving. Depending on the connection
5461 * mode, we'll have to wait for the last bytes to leave in either
5462 * direction, and sometimes for a close to be effective.
5463 */
5464
5465 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5466 /* Server-close mode : shut read and wait for the request
5467 * side to close its output buffer. The caller will detect
5468 * when we're in DONE and the other is in CLOSED and will
5469 * catch that for the final cleanup.
5470 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005471 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5472 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005474 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5475 /* Option forceclose is set, or either side wants to close,
5476 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005477 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005478 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005479 *
5480 * However, there is an exception if the response length
5481 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005483 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
5484 channel_auto_read(chn);
5485 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5486 chn->flags |= CF_NEVER_WAIT;
5487 }
5488 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005489 channel_shutr_now(chn);
5490 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005491 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 }
5493 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005494 /* The last possible modes are keep-alive and tunnel. Tunnel will
5495 * need to forward remaining data. Keep-alive will need to monitor
5496 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005497 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005498 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005499 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005500 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5501 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 }
5503
Christopher Faulet4be98032017-07-18 10:48:24 +02005504 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005505 }
5506
5507 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5508 http_msg_closing:
5509 /* nothing else to forward, just waiting for the output buffer
5510 * to be empty and for the shutw_now to take effect.
5511 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005512 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005513 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5514 goto http_msg_closed;
5515 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005516 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02005517 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005518 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005519 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005520 if (objt_server(s->target))
5521 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005522 }
Christopher Faulet56d26092017-07-20 11:05:10 +02005523 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 }
5525
5526 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5527 http_msg_closed:
5528 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005529 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 channel_auto_close(chn);
5531 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005532 goto wait_other_side;
5533 }
5534
Christopher Faulet4be98032017-07-18 10:48:24 +02005535 check_channel_flags:
5536 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5537 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5538 /* if we've just closed an output, let's switch */
5539 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5540 goto http_msg_closing;
5541 }
5542
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005544 /* We force the response to leave immediately if we're waiting for the
5545 * other side, since there is no pending shutdown to push it out.
5546 */
5547 if (!channel_is_empty(chn))
5548 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005549 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550}
5551
5552
Christopher Faulet894da4c2017-07-18 11:29:07 +02005553/* Resync the request and response state machines. */
5554void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005555{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005556 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005557#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 int old_req_state = txn->req.msg_state;
5559 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005560#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 http_sync_req_state(s);
5563 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 if (!http_sync_res_state(s))
5565 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 if (!http_sync_req_state(s))
5567 break;
5568 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005569
Christopher Faulet894da4c2017-07-18 11:29:07 +02005570 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
5571 "req->analysers=0x%08x res->analysers=0x%08x\n",
5572 now_ms, __FUNCTION__, s,
5573 http_msg_state_str(old_req_state), http_msg_state_str(old_res_state),
5574 http_msg_state_str(txn->req.msg_state), http_msg_state_str(txn->rsp.msg_state),
5575 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02005576
5577
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578 /* OK, both state machines agree on a compatible state.
5579 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5581 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02005582 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
5583 * means we must abort the request.
5584 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
5585 * corresponding channel.
5586 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
5587 * on the response with server-close mode means we've completed one
5588 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02005590 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
5591 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005592 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005593 channel_auto_close(&s->req);
5594 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005595 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 channel_auto_close(&s->res);
5597 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02005599 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
5600 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005601 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005602 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005603 channel_auto_close(&s->res);
5604 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005605 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005606 channel_abort(&s->req);
5607 channel_auto_close(&s->req);
5608 channel_auto_read(&s->req);
5609 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005610 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02005611 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5612 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5613 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5614 s->req.analysers &= AN_REQ_FLT_END;
5615 if (HAS_REQ_DATA_FILTERS(s))
5616 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
5617 }
5618 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5619 s->res.analysers &= AN_RES_FLT_END;
5620 if (HAS_RSP_DATA_FILTERS(s))
5621 s->res.analysers |= AN_RES_FLT_XFER_DATA;
5622 }
5623 channel_auto_close(&s->req);
5624 channel_auto_read(&s->req);
5625 channel_auto_close(&s->res);
5626 channel_auto_read(&s->res);
5627 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005628 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5629 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005630 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005631 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5632 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5633 /* server-close/keep-alive: terminate this transaction,
5634 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005635 * a fresh-new transaction, but only once we're sure there's
5636 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02005637 * another request. They must not hold any pending output data
5638 * and the response buffer must realigned
5639 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01005640 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02005641 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005642 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02005643 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005644 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02005645 else {
5646 s->req.analysers = AN_REQ_FLT_END;
5647 s->res.analysers = AN_RES_FLT_END;
5648 txn->flags |= TX_WAIT_CLEANUP;
5649 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005650 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005651}
5652
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653/* This function is an analyser which forwards request body (including chunk
5654 * sizes if any). It is called as soon as we must forward, even if we forward
5655 * zero byte. The only situation where it must not be called is when we're in
5656 * tunnel mode and we want to forward till the close. It's used both to forward
5657 * remaining data and to resync after end of body. It expects the msg_state to
5658 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005659 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005660 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005661 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005663int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005664{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005665 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005666 struct http_txn *txn = s->txn;
5667 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005668 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669
Christopher Faulet814d2702017-03-30 11:33:44 +02005670 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5671 now_ms, __FUNCTION__,
5672 s,
5673 req,
5674 req->rex, req->wex,
5675 req->flags,
5676 req->buf->i,
5677 req->analysers);
5678
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005679 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5680 return 0;
5681
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005682 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005683 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005684 /* Output closed while we were sending data. We must abort and
5685 * wake the other side up.
5686 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005687 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005688 msg->msg_state = HTTP_MSG_ERROR;
5689 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005690 return 1;
5691 }
5692
Willy Tarreaud98cf932009-12-27 22:54:55 +01005693 /* Note that we don't have to send 100-continue back because we don't
5694 * need the data to complete our job, and it's up to the server to
5695 * decide whether to return 100, 417 or anything else in return of
5696 * an "Expect: 100-continue" header.
5697 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005698 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5700 ? HTTP_MSG_CHUNK_SIZE
5701 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005702
5703 /* TODO/filters: when http-buffer-request option is set or if a
5704 * rule on url_param exists, the first chunk size could be
5705 * already parsed. In that case, msg->next is after the chunk
5706 * size (including the CRLF after the size). So this case should
5707 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005708 }
5709
Willy Tarreau7ba23542014-04-17 21:50:00 +02005710 /* Some post-connect processing might want us to refrain from starting to
5711 * forward data. Currently, the only reason for this is "balance url_param"
5712 * whichs need to parse/process the request after we've enabled forwarding.
5713 */
5714 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005715 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005716 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005717 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005719 }
5720 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5721 }
5722
Willy Tarreau80a92c02014-03-12 10:41:13 +01005723 /* in most states, we should abort in case of early close */
5724 channel_auto_close(req);
5725
Willy Tarreauefdf0942014-04-24 20:08:57 +02005726 if (req->to_forward) {
5727 /* We can't process the buffer's contents yet */
5728 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005729 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005730 }
5731
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005732 if (msg->msg_state < HTTP_MSG_DONE) {
5733 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5734 ? http_msg_forward_chunked_body(s, msg)
5735 : http_msg_forward_body(s, msg));
5736 if (!ret)
5737 goto missing_data_or_waiting;
5738 if (ret < 0)
5739 goto return_bad_req;
5740 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005741
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005742 /* other states, DONE...TUNNEL */
5743 /* we don't want to forward closes on DONE except in tunnel mode. */
5744 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5745 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005746
Christopher Faulet894da4c2017-07-18 11:29:07 +02005747 http_resync_states(s);
5748 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005749 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5750 if (req->flags & CF_SHUTW) {
5751 /* request errors are most likely due to the
5752 * server aborting the transfer. */
5753 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005754 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005756 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005757 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005758 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760 }
5761
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005762 /* If "option abortonclose" is set on the backend, we want to monitor
5763 * the client's connection and forward any shutdown notification to the
5764 * server, which will decide whether to close or to go on processing the
5765 * request. We only do that in tunnel mode, and not in other modes since
5766 * it can be abused to exhaust source ports. */
5767 if (s->be->options & PR_O_ABRT_CLOSE) {
5768 channel_auto_read(req);
5769 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5770 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5771 s->si[1].flags |= SI_FL_NOLINGER;
5772 channel_auto_close(req);
5773 }
5774 else if (s->txn->meth == HTTP_METH_POST) {
5775 /* POST requests may require to read extra CRLF sent by broken
5776 * browsers and which could cause an RST to be sent upon close
5777 * on some systems (eg: Linux). */
5778 channel_auto_read(req);
5779 }
5780 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005781
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005783 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005784 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005785 if (!(s->flags & SF_ERR_MASK))
5786 s->flags |= SF_ERR_CLICL;
5787 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005788 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005789 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005790 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005791 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 }
5793
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005794 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005795 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005796 if (objt_server(s->target))
5797 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005798
5799 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005800 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005801
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005802 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005803 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005804 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005805
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005806 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005807 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005808 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005809 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005810 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005811
Willy Tarreau5c620922011-05-11 19:56:11 +02005812 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005813 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005814 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005815 * modes are already handled by the stream sock layer. We must not do
5816 * this in content-length mode because it could present the MSG_MORE
5817 * flag with the last block of forwarded data, which would cause an
5818 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005819 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005820 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005821 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005822
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005823 return 0;
5824
Willy Tarreaud98cf932009-12-27 22:54:55 +01005825 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005826 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005827 if (sess->listener->counters)
5828 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005829
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005830 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005831 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005832 txn->req.msg_state = HTTP_MSG_ERROR;
5833 if (txn->status) {
5834 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005835 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005836 } else {
5837 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005838 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005839 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005840 req->analysers &= AN_REQ_FLT_END;
5841 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 +01005842
Willy Tarreaue7dff022015-04-03 01:14:29 +02005843 if (!(s->flags & SF_ERR_MASK))
5844 s->flags |= SF_ERR_PRXCOND;
5845 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005846 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005847 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005848 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005849 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005850 }
5851 return 0;
5852
5853 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005854 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 txn->req.msg_state = HTTP_MSG_ERROR;
5856 if (txn->status) {
5857 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005858 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 } else {
5860 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005861 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005862 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005863 req->analysers &= AN_REQ_FLT_END;
5864 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 +01005865
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005866 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005867 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005868 if (objt_server(s->target))
5869 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005870
Willy Tarreaue7dff022015-04-03 01:14:29 +02005871 if (!(s->flags & SF_ERR_MASK))
5872 s->flags |= SF_ERR_SRVCL;
5873 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005874 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005875 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005876 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005877 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005879 return 0;
5880}
5881
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005882/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5883 * processing can continue on next analysers, or zero if it either needs more
5884 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005885 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 * when it has nothing left to do, and may remove any analyser when it wants to
5887 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005888 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005889int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005890{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005891 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005892 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005894 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005895 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005896 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005897 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005898
Willy Tarreau87b09662015-04-03 00:22:06 +02005899 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 +02005900 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005902 rep,
5903 rep->rex, rep->wex,
5904 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005905 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005906 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005907
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005908 /*
5909 * Now parse the partial (or complete) lines.
5910 * We will check the response syntax, and also join multi-line
5911 * headers. An index of all the lines will be elaborated while
5912 * parsing.
5913 *
5914 * For the parsing, we use a 28 states FSM.
5915 *
5916 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005917 * rep->buf->p = beginning of response
5918 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5919 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005920 * msg->eol = end of current header or line (LF or CRLF)
5921 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005922 */
5923
Willy Tarreau628c40c2014-04-24 19:11:26 +02005924 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005925 /* There's a protected area at the end of the buffer for rewriting
5926 * purposes. We don't want to start to parse the request if the
5927 * protected area is affected, because we may have to move processed
5928 * data later, which is much more complicated.
5929 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005930 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005931 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005932 /* some data has still not left the buffer, wake us once that's done */
5933 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5934 goto abort_response;
5935 channel_dont_close(rep);
5936 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005937 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005938 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005939 }
5940
Willy Tarreau379357a2013-06-08 12:55:46 +02005941 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5942 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5943 buffer_slow_realign(rep->buf);
5944
Willy Tarreau9b28e032012-10-12 23:49:43 +02005945 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005946 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005947 }
5948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 /* 1: we might have to print this header in debug mode */
5950 if (unlikely((global.mode & MODE_DEBUG) &&
5951 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005952 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005954
Willy Tarreau9b28e032012-10-12 23:49:43 +02005955 sol = rep->buf->p;
5956 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005957 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005958
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 sol += hdr_idx_first_pos(&txn->hdr_idx);
5960 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005961
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005962 while (cur_idx) {
5963 eol = sol + txn->hdr_idx.v[cur_idx].len;
5964 debug_hdr("srvhdr", s, sol, eol);
5965 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5966 cur_idx = txn->hdr_idx.v[cur_idx].next;
5967 }
5968 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005969
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005970 /*
5971 * Now we quickly check if we have found a full valid response.
5972 * If not so, we check the FD and buffer states before leaving.
5973 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005974 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975 * responses are checked first.
5976 *
5977 * Depending on whether the client is still there or not, we
5978 * may send an error response back or not. Note that normally
5979 * we should only check for HTTP status there, and check I/O
5980 * errors somewhere else.
5981 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005982
Willy Tarreau655dce92009-11-08 13:10:58 +01005983 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984 /* Invalid response */
5985 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5986 /* we detected a parsing error. We want to archive this response
5987 * in the dedicated proxy area for later troubleshooting.
5988 */
5989 hdr_response_bad:
5990 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005991 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005992
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005993 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005994 if (objt_server(s->target)) {
5995 objt_server(s->target)->counters.failed_resp++;
5996 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005997 }
Willy Tarreau64648412010-03-05 10:41:54 +01005998 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005999 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006000 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006001 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006002 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006003 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006004 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006005
Willy Tarreaue7dff022015-04-03 01:14:29 +02006006 if (!(s->flags & SF_ERR_MASK))
6007 s->flags |= SF_ERR_PRXCOND;
6008 if (!(s->flags & SF_FINST_MASK))
6009 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006010
6011 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006012 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006013
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006014 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006015 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006016 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006017 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006018 goto hdr_response_bad;
6019 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006022 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006023 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006024 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006025 else if (txn->flags & TX_NOT_FIRST)
6026 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006027
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006028 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006029 if (objt_server(s->target)) {
6030 objt_server(s->target)->counters.failed_resp++;
6031 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006032 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006033
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006034 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006035 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006037 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006038 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006039 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006040
Willy Tarreaue7dff022015-04-03 01:14:29 +02006041 if (!(s->flags & SF_ERR_MASK))
6042 s->flags |= SF_ERR_SRVCL;
6043 if (!(s->flags & SF_FINST_MASK))
6044 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006045 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006046 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006047
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006048 /* read timeout : return a 504 to the client. */
6049 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006051 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006052
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006053 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006054 if (objt_server(s->target)) {
6055 objt_server(s->target)->counters.failed_resp++;
6056 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006057 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006058
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006059 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006060 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006061 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006062 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006063 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006064 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006065
Willy Tarreaue7dff022015-04-03 01:14:29 +02006066 if (!(s->flags & SF_ERR_MASK))
6067 s->flags |= SF_ERR_SRVTO;
6068 if (!(s->flags & SF_FINST_MASK))
6069 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070 return 0;
6071 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006072
Willy Tarreauf003d372012-11-26 13:35:37 +01006073 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006074 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006075 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006076 s->be->be_counters.cli_aborts++;
6077 if (objt_server(s->target))
6078 objt_server(s->target)->counters.cli_aborts++;
6079
Christopher Faulet0184ea72017-01-05 14:06:34 +01006080 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006081 channel_auto_close(rep);
6082
6083 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006084 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006085 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006086
Willy Tarreaue7dff022015-04-03 01:14:29 +02006087 if (!(s->flags & SF_ERR_MASK))
6088 s->flags |= SF_ERR_CLICL;
6089 if (!(s->flags & SF_FINST_MASK))
6090 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006091
Willy Tarreau87b09662015-04-03 00:22:06 +02006092 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006093 return 0;
6094 }
6095
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006096 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006097 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006098 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006099 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006100 else if (txn->flags & TX_NOT_FIRST)
6101 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006103 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006104 if (objt_server(s->target)) {
6105 objt_server(s->target)->counters.failed_resp++;
6106 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006107 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006108
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006109 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006110 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006112 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006113 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006114 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006115
Willy Tarreaue7dff022015-04-03 01:14:29 +02006116 if (!(s->flags & SF_ERR_MASK))
6117 s->flags |= SF_ERR_SRVCL;
6118 if (!(s->flags & SF_FINST_MASK))
6119 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006120 return 0;
6121 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006123 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006124 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006126 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006127 else if (txn->flags & TX_NOT_FIRST)
6128 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006129
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006130 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006131 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006132 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006133
Willy Tarreaue7dff022015-04-03 01:14:29 +02006134 if (!(s->flags & SF_ERR_MASK))
6135 s->flags |= SF_ERR_CLICL;
6136 if (!(s->flags & SF_FINST_MASK))
6137 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006138
Willy Tarreau87b09662015-04-03 00:22:06 +02006139 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006140 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006141 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006142
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006143 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006144 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006145 return 0;
6146 }
6147
6148 /* More interesting part now : we know that we have a complete
6149 * response which at least looks like HTTP. We have an indicator
6150 * of each header's length, so we can parse them quickly.
6151 */
6152
6153 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006154 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006155
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006156 /*
6157 * 1: get the status code
6158 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006159 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006160 if (n < 1 || n > 5)
6161 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006162 /* when the client triggers a 4xx from the server, it's most often due
6163 * to a missing object or permission. These events should be tracked
6164 * because if they happen often, it may indicate a brute force or a
6165 * vulnerability scan.
6166 */
6167 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006168 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006169
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006170 if (objt_server(s->target))
6171 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006172
Willy Tarreau91852eb2015-05-01 13:26:00 +02006173 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6174 * exactly one digit "." one digit. This check may be disabled using
6175 * option accept-invalid-http-response.
6176 */
6177 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6178 if (msg->sl.st.v_l != 8) {
6179 msg->err_pos = 0;
6180 goto hdr_response_bad;
6181 }
6182
6183 if (rep->buf->p[4] != '/' ||
6184 !isdigit((unsigned char)rep->buf->p[5]) ||
6185 rep->buf->p[6] != '.' ||
6186 !isdigit((unsigned char)rep->buf->p[7])) {
6187 msg->err_pos = 4;
6188 goto hdr_response_bad;
6189 }
6190 }
6191
Willy Tarreau5b154472009-12-21 20:11:07 +01006192 /* check if the response is HTTP/1.1 or above */
6193 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006194 ((rep->buf->p[5] > '1') ||
6195 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006196 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006197
6198 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006199 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 +01006200
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006201 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006202 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006203
Willy Tarreau9b28e032012-10-12 23:49:43 +02006204 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006205
Willy Tarreau39650402010-03-15 19:44:39 +01006206 /* Adjust server's health based on status code. Note: status codes 501
6207 * and 505 are triggered on demand by client request, so we must not
6208 * count them as server failures.
6209 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006210 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006211 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006212 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006213 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006214 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006215 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006216
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006217 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02006218 * We may be facing a 100-continue response, or any other informational
6219 * 1xx response which is non-final, in which case this is not the right
6220 * response, and we're waiting for the next one. Let's allow this response
6221 * to go to the client and wait for the next one. There's an exception for
6222 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02006224 if (txn->status < 200 &&
6225 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006226 hdr_idx_init(&txn->hdr_idx);
6227 msg->next -= channel_forward(rep, msg->next);
6228 msg->msg_state = HTTP_MSG_RPBEFORE;
6229 txn->status = 0;
6230 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006231 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006232 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02006233 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02006234
Willy Tarreaua14ad722017-07-07 11:36:32 +02006235 /*
6236 * 2: check for cacheability.
6237 */
6238
6239 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006240 case 200:
6241 case 203:
6242 case 206:
6243 case 300:
6244 case 301:
6245 case 410:
6246 /* RFC2616 @13.4:
6247 * "A response received with a status code of
6248 * 200, 203, 206, 300, 301 or 410 MAY be stored
6249 * by a cache (...) unless a cache-control
6250 * directive prohibits caching."
6251 *
6252 * RFC2616 @9.5: POST method :
6253 * "Responses to this method are not cacheable,
6254 * unless the response includes appropriate
6255 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006256 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006257 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006258 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006259 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6260 break;
6261 default:
6262 break;
6263 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006264
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006265 /*
6266 * 3: we may need to capture headers
6267 */
6268 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006269 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006270 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006271 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006272
Willy Tarreau557f1992015-05-01 10:05:17 +02006273 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6274 * by RFC7230#3.3.3 :
6275 *
6276 * The length of a message body is determined by one of the following
6277 * (in order of precedence):
6278 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006279 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6280 * the connection will become a tunnel immediately after the empty
6281 * line that concludes the header fields. A client MUST ignore
6282 * any Content-Length or Transfer-Encoding header fields received
6283 * in such a message. Any 101 response (Switching Protocols) is
6284 * managed in the same manner.
6285 *
6286 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006287 * (Informational), 204 (No Content), or 304 (Not Modified) status
6288 * code is always terminated by the first empty line after the
6289 * header fields, regardless of the header fields present in the
6290 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006291 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006292 * 3. If a Transfer-Encoding header field is present and the chunked
6293 * transfer coding (Section 4.1) is the final encoding, the message
6294 * body length is determined by reading and decoding the chunked
6295 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006296 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006297 * If a Transfer-Encoding header field is present in a response and
6298 * the chunked transfer coding is not the final encoding, the
6299 * message body length is determined by reading the connection until
6300 * it is closed by the server. If a Transfer-Encoding header field
6301 * is present in a request and the chunked transfer coding is not
6302 * the final encoding, the message body length cannot be determined
6303 * reliably; the server MUST respond with the 400 (Bad Request)
6304 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006305 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006306 * If a message is received with both a Transfer-Encoding and a
6307 * Content-Length header field, the Transfer-Encoding overrides the
6308 * Content-Length. Such a message might indicate an attempt to
6309 * perform request smuggling (Section 9.5) or response splitting
6310 * (Section 9.4) and ought to be handled as an error. A sender MUST
6311 * remove the received Content-Length field prior to forwarding such
6312 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006313 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006314 * 4. If a message is received without Transfer-Encoding and with
6315 * either multiple Content-Length header fields having differing
6316 * field-values or a single Content-Length header field having an
6317 * invalid value, then the message framing is invalid and the
6318 * recipient MUST treat it as an unrecoverable error. If this is a
6319 * request message, the server MUST respond with a 400 (Bad Request)
6320 * status code and then close the connection. If this is a response
6321 * message received by a proxy, the proxy MUST close the connection
6322 * to the server, discard the received response, and send a 502 (Bad
6323 * Gateway) response to the client. If this is a response message
6324 * received by a user agent, the user agent MUST close the
6325 * connection to the server and discard the received response.
6326 *
6327 * 5. If a valid Content-Length header field is present without
6328 * Transfer-Encoding, its decimal value defines the expected message
6329 * body length in octets. If the sender closes the connection or
6330 * the recipient times out before the indicated number of octets are
6331 * received, the recipient MUST consider the message to be
6332 * incomplete and close the connection.
6333 *
6334 * 6. If this is a request message and none of the above are true, then
6335 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006336 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006337 * 7. Otherwise, this is a response message without a declared message
6338 * body length, so the message body length is determined by the
6339 * number of octets received prior to the server closing the
6340 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006341 */
6342
6343 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006344 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006345 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006346 * FIXME: should we parse anyway and return an error on chunked encoding ?
6347 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006348 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6349 txn->status == 101)) {
6350 /* Either we've established an explicit tunnel, or we're
6351 * switching the protocol. In both cases, we're very unlikely
6352 * to understand the next protocols. We have to switch to tunnel
6353 * mode, so that we transfer the request and responses then let
6354 * this protocol pass unmodified. When we later implement specific
6355 * parsers for such protocols, we'll want to check the Upgrade
6356 * header which contains information about that protocol for
6357 * responses with status 101 (eg: see RFC2817 about TLS).
6358 */
6359 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6360 msg->flags |= HTTP_MSGF_XFER_LEN;
6361 goto end;
6362 }
6363
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006364 if (txn->meth == HTTP_METH_HEAD ||
6365 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006366 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006367 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006368 goto skip_content_length;
6369 }
6370
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006371 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006372 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006373 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006374 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006375 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6376 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006377 /* bad transfer-encoding (chunked followed by something else) */
6378 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006379 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006380 break;
6381 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006382 }
6383
Willy Tarreau1c913912015-04-30 10:57:51 +02006384 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006385 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006386 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006387 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6388 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6389 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006390 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006391 signed long long cl;
6392
Willy Tarreauad14f752011-09-02 20:33:27 +02006393 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006394 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006395 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006396 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006397
Willy Tarreauad14f752011-09-02 20:33:27 +02006398 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006399 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006400 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006401 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006402
Willy Tarreauad14f752011-09-02 20:33:27 +02006403 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006404 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006406 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006407
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006408 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006409 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006410 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006411 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006412
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006413 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006414 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006415 }
6416
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006417 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006418 /* Now we have to check if we need to modify the Connection header.
6419 * This is more difficult on the response than it is on the request,
6420 * because we can have two different HTTP versions and we don't know
6421 * how the client will interprete a response. For instance, let's say
6422 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6423 * HTTP/1.1 response without any header. Maybe it will bound itself to
6424 * HTTP/1.0 because it only knows about it, and will consider the lack
6425 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6426 * the lack of header as a keep-alive. Thus we will use two flags
6427 * indicating how a request MAY be understood by the client. In case
6428 * of multiple possibilities, we'll fix the header to be explicit. If
6429 * ambiguous cases such as both close and keepalive are seen, then we
6430 * will fall back to explicit close. Note that we won't take risks with
6431 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006432 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006433 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006434 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6435 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6436 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6437 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006438 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006439
Willy Tarreau70dffda2014-01-30 03:07:23 +01006440 /* this situation happens when combining pretend-keepalive with httpclose. */
6441 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006442 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006443 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006444 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6445
Willy Tarreau60466522010-01-18 19:08:45 +01006446 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006447 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006448 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6449 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006450
Willy Tarreau60466522010-01-18 19:08:45 +01006451 /* now adjust header transformations depending on current state */
6452 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6453 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6454 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006455 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006456 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006457 }
Willy Tarreau60466522010-01-18 19:08:45 +01006458 else { /* SCL / KAL */
6459 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006460 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006461 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006462 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006463
Willy Tarreau60466522010-01-18 19:08:45 +01006464 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006465 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006466
Willy Tarreau60466522010-01-18 19:08:45 +01006467 /* Some keep-alive responses are converted to Server-close if
6468 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006469 */
Willy Tarreau60466522010-01-18 19:08:45 +01006470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6471 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006472 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006473 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006474 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006475 }
6476
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006477 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006478 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006479 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006480
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006481 /* end of job, return OK */
6482 rep->analysers &= ~an_bit;
6483 rep->analyse_exp = TICK_ETERNITY;
6484 channel_auto_close(rep);
6485 return 1;
6486
6487 abort_keep_alive:
6488 /* A keep-alive request to the server failed on a network error.
6489 * The client is required to retry. We need to close without returning
6490 * any other information so that the client retries.
6491 */
6492 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006493 rep->analysers &= AN_RES_FLT_END;
6494 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006495 channel_auto_close(rep);
6496 s->logs.logwait = 0;
6497 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006498 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006499 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006500 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006501 return 0;
6502}
6503
6504/* This function performs all the processing enabled for the current response.
6505 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006506 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006507 * other functions. It works like process_request (see indications above).
6508 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006509int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006510{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006511 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006512 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006513 struct http_msg *msg = &txn->rsp;
6514 struct proxy *cur_proxy;
6515 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006516 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006517
Willy Tarreau87b09662015-04-03 00:22:06 +02006518 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 +02006519 now_ms, __FUNCTION__,
6520 s,
6521 rep,
6522 rep->rex, rep->wex,
6523 rep->flags,
6524 rep->buf->i,
6525 rep->analysers);
6526
6527 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6528 return 0;
6529
Willy Tarreau70730dd2014-04-24 18:06:27 +02006530 /* The stats applet needs to adjust the Connection header but we don't
6531 * apply any filter there.
6532 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006533 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6534 rep->analysers &= ~an_bit;
6535 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006536 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006537 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006538
Willy Tarreau58975672014-04-24 21:13:57 +02006539 /*
6540 * We will have to evaluate the filters.
6541 * As opposed to version 1.2, now they will be evaluated in the
6542 * filters order and not in the header order. This means that
6543 * each filter has to be validated among all headers.
6544 *
6545 * Filters are tried with ->be first, then with ->fe if it is
6546 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006547 *
6548 * Maybe we are in resume condiion. In this case I choose the
6549 * "struct proxy" which contains the rule list matching the resume
6550 * pointer. If none of theses "struct proxy" match, I initialise
6551 * the process with the first one.
6552 *
6553 * In fact, I check only correspondance betwwen the current list
6554 * pointer and the ->fe rule list. If it doesn't match, I initialize
6555 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006556 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006557 if (s->current_rule_list == &sess->fe->http_res_rules)
6558 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006559 else
6560 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006561 while (1) {
6562 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006563
Willy Tarreau58975672014-04-24 21:13:57 +02006564 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006565 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006566 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006567
Willy Tarreau51d861a2015-05-22 17:30:48 +02006568 if (ret == HTTP_RULE_RES_BADREQ)
6569 goto return_srv_prx_502;
6570
6571 if (ret == HTTP_RULE_RES_DONE) {
6572 rep->analysers &= ~an_bit;
6573 rep->analyse_exp = TICK_ETERNITY;
6574 return 1;
6575 }
6576 }
6577
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006578 /* we need to be called again. */
6579 if (ret == HTTP_RULE_RES_YIELD) {
6580 channel_dont_close(rep);
6581 return 0;
6582 }
6583
Willy Tarreau58975672014-04-24 21:13:57 +02006584 /* try headers filters */
6585 if (rule_set->rsp_exp != NULL) {
6586 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6587 return_bad_resp:
6588 if (objt_server(s->target)) {
6589 objt_server(s->target)->counters.failed_resp++;
6590 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006591 }
Willy Tarreau58975672014-04-24 21:13:57 +02006592 s->be->be_counters.failed_resp++;
6593 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006594 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006595 txn->status = 502;
6596 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006597 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006598 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006599 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006600 if (!(s->flags & SF_ERR_MASK))
6601 s->flags |= SF_ERR_PRXCOND;
6602 if (!(s->flags & SF_FINST_MASK))
6603 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006604 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006605 }
Willy Tarreau58975672014-04-24 21:13:57 +02006606 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006607
Willy Tarreau58975672014-04-24 21:13:57 +02006608 /* has the response been denied ? */
6609 if (txn->flags & TX_SVDENY) {
6610 if (objt_server(s->target))
6611 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006612
Willy Tarreau58975672014-04-24 21:13:57 +02006613 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006614 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006615 if (sess->listener->counters)
6616 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006617
Willy Tarreau58975672014-04-24 21:13:57 +02006618 goto return_srv_prx_502;
6619 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006620
Willy Tarreau58975672014-04-24 21:13:57 +02006621 /* add response headers from the rule sets in the same order */
6622 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006623 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006624 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006625 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006626 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006627 ret = acl_pass(ret);
6628 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6629 ret = !ret;
6630 if (!ret)
6631 continue;
6632 }
6633 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6634 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006635 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006636
Willy Tarreau58975672014-04-24 21:13:57 +02006637 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006638 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006639 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006640 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006641 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006642
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006643 /* After this point, this anayzer can't return yield, so we can
6644 * remove the bit corresponding to this analyzer from the list.
6645 *
6646 * Note that the intermediate returns and goto found previously
6647 * reset the analyzers.
6648 */
6649 rep->analysers &= ~an_bit;
6650 rep->analyse_exp = TICK_ETERNITY;
6651
Willy Tarreau58975672014-04-24 21:13:57 +02006652 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006653 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006654 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006655
Willy Tarreau58975672014-04-24 21:13:57 +02006656 /*
6657 * Now check for a server cookie.
6658 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006659 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006660 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006661
Willy Tarreau58975672014-04-24 21:13:57 +02006662 /*
6663 * Check for cache-control or pragma headers if required.
6664 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006665 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 +02006666 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006667
Willy Tarreau58975672014-04-24 21:13:57 +02006668 /*
6669 * Add server cookie in the response if needed
6670 */
6671 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6672 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006673 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006674 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6675 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6676 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6677 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6678 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006679 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006680 /* the server is known, it's not the one the client requested, or the
6681 * cookie's last seen date needs to be refreshed. We have to
6682 * insert a set-cookie here, except if we want to insert only on POST
6683 * requests and this one isn't. Note that servers which don't have cookies
6684 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685 */
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if (!objt_server(s->target)->cookie) {
6687 chunk_printf(&trash,
6688 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6689 s->be->cookie_name);
6690 }
6691 else {
6692 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693
Willy Tarreau58975672014-04-24 21:13:57 +02006694 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6695 /* emit last_date, which is mandatory */
6696 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6697 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6698 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006699
Willy Tarreau58975672014-04-24 21:13:57 +02006700 if (s->be->cookie_maxlife) {
6701 /* emit first_date, which is either the original one or
6702 * the current date.
6703 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006704 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006705 s30tob64(txn->cookie_first_date ?
6706 txn->cookie_first_date >> 2 :
6707 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006708 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006709 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006710 }
Willy Tarreau58975672014-04-24 21:13:57 +02006711 chunk_appendf(&trash, "; path=/");
6712 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (s->be->cookie_domain)
6715 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 if (s->be->ck_opts & PR_CK_HTTPONLY)
6718 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 if (s->be->ck_opts & PR_CK_SECURE)
6721 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6724 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006727 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006728 /* the server did not change, only the date was updated */
6729 txn->flags |= TX_SCK_UPDATED;
6730 else
6731 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006732
Willy Tarreau58975672014-04-24 21:13:57 +02006733 /* Here, we will tell an eventual cache on the client side that we don't
6734 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6735 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6736 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006737 */
Willy Tarreau58975672014-04-24 21:13:57 +02006738 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006741
Willy Tarreau58975672014-04-24 21:13:57 +02006742 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6743 "Cache-control: private", 22) < 0))
6744 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006745 }
Willy Tarreau58975672014-04-24 21:13:57 +02006746 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747
Willy Tarreau58975672014-04-24 21:13:57 +02006748 /*
6749 * Check if result will be cacheable with a cookie.
6750 * We'll block the response if security checks have caught
6751 * nasty things such as a cacheable cookie.
6752 */
6753 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6754 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6755 (s->be->options & PR_O_CHK_CACHE)) {
6756 /* we're in presence of a cacheable response containing
6757 * a set-cookie header. We'll block it as requested by
6758 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 */
Willy Tarreau58975672014-04-24 21:13:57 +02006760 if (objt_server(s->target))
6761 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006762
Willy Tarreau58975672014-04-24 21:13:57 +02006763 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006764 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006765 if (sess->listener->counters)
6766 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6769 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6770 send_log(s->be, LOG_ALERT,
6771 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6772 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6773 goto return_srv_prx_502;
6774 }
Willy Tarreau03945942009-12-22 16:50:27 +01006775
Willy Tarreau70730dd2014-04-24 18:06:27 +02006776 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006777 /*
6778 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6779 * If an "Upgrade" token is found, the header is left untouched in order
6780 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006781 * if anything but "Upgrade" is present in the Connection header. We don't
6782 * want to touch any 101 response either since it's switching to another
6783 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006784 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006785 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006786 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006787 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006788 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6789 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6792 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6793 /* we want a keep-alive response here. Keep-alive header
6794 * required if either side is not 1.1.
6795 */
6796 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6797 want_flags |= TX_CON_KAL_SET;
6798 }
6799 else {
6800 /* we want a close response here. Close header required if
6801 * the server is 1.1, regardless of the client.
6802 */
6803 if (msg->flags & HTTP_MSGF_VER_11)
6804 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006805 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806
Willy Tarreau58975672014-04-24 21:13:57 +02006807 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6808 http_change_connection_header(txn, msg, want_flags);
6809 }
6810
6811 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006812 /* Always enter in the body analyzer */
6813 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6814 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815
Willy Tarreau58975672014-04-24 21:13:57 +02006816 /* if the user wants to log as soon as possible, without counting
6817 * bytes from the server, then this is the right moment. We have
6818 * to temporarily assign bytes_out to log what we currently have.
6819 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006820 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006821 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6822 s->logs.bytes_out = txn->rsp.eoh;
6823 s->do_log(s);
6824 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006825 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006826 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006827}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828
Willy Tarreaud98cf932009-12-27 22:54:55 +01006829/* This function is an analyser which forwards response body (including chunk
6830 * sizes if any). It is called as soon as we must forward, even if we forward
6831 * zero byte. The only situation where it must not be called is when we're in
6832 * tunnel mode and we want to forward till the close. It's used both to forward
6833 * remaining data and to resync after end of body. It expects the msg_state to
6834 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006835 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006836 *
6837 * It is capable of compressing response data both in content-length mode and
6838 * in chunked mode. The state machines follows different flows depending on
6839 * whether content-length and chunked modes are used, since there are no
6840 * trailers in content-length :
6841 *
6842 * chk-mode cl-mode
6843 * ,----- BODY -----.
6844 * / \
6845 * V size > 0 V chk-mode
6846 * .--> SIZE -------------> DATA -------------> CRLF
6847 * | | size == 0 | last byte |
6848 * | v final crlf v inspected |
6849 * | TRAILERS -----------> DONE |
6850 * | |
6851 * `----------------------------------------------'
6852 *
6853 * Compression only happens in the DATA state, and must be flushed in final
6854 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6855 * is performed at once on final states for all bytes parsed, or when leaving
6856 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006857 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006858int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006859{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006860 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006861 struct http_txn *txn = s->txn;
6862 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006863 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006864
Christopher Faulet814d2702017-03-30 11:33:44 +02006865 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6866 now_ms, __FUNCTION__,
6867 s,
6868 res,
6869 res->rex, res->wex,
6870 res->flags,
6871 res->buf->i,
6872 res->analysers);
6873
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006874 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6875 return 0;
6876
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006877 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006878 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006879 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006880 /* Output closed while we were sending data. We must abort and
6881 * wake the other side up.
6882 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006883 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006884 msg->msg_state = HTTP_MSG_ERROR;
6885 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006886 return 1;
6887 }
6888
Willy Tarreau4fe41902010-06-07 22:27:41 +02006889 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006890 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006891
Christopher Fauletd7c91962015-04-30 11:48:27 +02006892 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006893 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6894 ? HTTP_MSG_CHUNK_SIZE
6895 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006896 }
6897
Willy Tarreauefdf0942014-04-24 20:08:57 +02006898 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006899 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006900 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006901 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006902 }
6903
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006904 if (msg->msg_state < HTTP_MSG_DONE) {
6905 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6906 ? http_msg_forward_chunked_body(s, msg)
6907 : http_msg_forward_body(s, msg));
6908 if (!ret)
6909 goto missing_data_or_waiting;
6910 if (ret < 0)
6911 goto return_bad_res;
6912 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006913
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006914 /* other states, DONE...TUNNEL */
6915 /* for keep-alive we don't want to forward closes on DONE */
6916 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6917 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6918 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006919
Christopher Faulet894da4c2017-07-18 11:29:07 +02006920 http_resync_states(s);
6921 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006922 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6923 if (res->flags & CF_SHUTW) {
6924 /* response errors are most likely due to the
6925 * client aborting the transfer. */
6926 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006927 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006928 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006929 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006930 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006931 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006932 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006933 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006934 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006935
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006936 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006937 if (res->flags & CF_SHUTW)
6938 goto aborted_xfer;
6939
6940 /* stop waiting for data if the input is closed before the end. If the
6941 * client side was already closed, it means that the client has aborted,
6942 * so we don't want to count this as a server abort. Otherwise it's a
6943 * server abort.
6944 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006945 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006946 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006947 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006948 /* If we have some pending data, we continue the processing */
6949 if (!buffer_pending(res->buf)) {
6950 if (!(s->flags & SF_ERR_MASK))
6951 s->flags |= SF_ERR_SRVCL;
6952 s->be->be_counters.srv_aborts++;
6953 if (objt_server(s->target))
6954 objt_server(s->target)->counters.srv_aborts++;
6955 goto return_bad_res_stats_ok;
6956 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006957 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006958
Willy Tarreau40dba092010-03-04 18:14:51 +01006959 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006960 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006961 goto return_bad_res;
6962
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006963 /* When TE: chunked is used, we need to get there again to parse
6964 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006965 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6966 * or if there are filters registered on the stream, we don't want to
6967 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006968 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006969 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006970 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006971 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6972 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006973 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006974
Willy Tarreau5c620922011-05-11 19:56:11 +02006975 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006976 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006977 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006978 * modes are already handled by the stream sock layer. We must not do
6979 * this in content-length mode because it could present the MSG_MORE
6980 * flag with the last block of forwarded data, which would cause an
6981 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006982 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006983 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006984 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006985
Willy Tarreau87b09662015-04-03 00:22:06 +02006986 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006987 return 0;
6988
Willy Tarreau40dba092010-03-04 18:14:51 +01006989 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006990 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006991 if (objt_server(s->target))
6992 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006993
6994 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006995 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006996 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006997 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006998 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006999 res->analysers &= AN_RES_FLT_END;
7000 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 +01007001 if (objt_server(s->target))
7002 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007003
Willy Tarreaue7dff022015-04-03 01:14:29 +02007004 if (!(s->flags & SF_ERR_MASK))
7005 s->flags |= SF_ERR_PRXCOND;
7006 if (!(s->flags & SF_FINST_MASK))
7007 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007008 return 0;
7009
7010 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007011 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007012 txn->rsp.msg_state = HTTP_MSG_ERROR;
7013 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007014 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007015 res->analysers &= AN_RES_FLT_END;
7016 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 +01007017
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007018 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007019 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007020 if (objt_server(s->target))
7021 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007022
Willy Tarreaue7dff022015-04-03 01:14:29 +02007023 if (!(s->flags & SF_ERR_MASK))
7024 s->flags |= SF_ERR_CLICL;
7025 if (!(s->flags & SF_FINST_MASK))
7026 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007027 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007028}
7029
7030
7031static inline int
7032http_msg_forward_body(struct stream *s, struct http_msg *msg)
7033{
7034 struct channel *chn = msg->chn;
7035 int ret;
7036
7037 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7038
7039 if (msg->msg_state == HTTP_MSG_ENDING)
7040 goto ending;
7041
7042 /* Neither content-length, nor transfer-encoding was found, so we must
7043 * read the body until the server connection is closed. In that case, we
7044 * eat data as they come. Of course, this happens for response only. */
7045 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7046 unsigned long long len = (chn->buf->i - msg->next);
7047 msg->chunk_len += len;
7048 msg->body_len += len;
7049 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007050 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7051 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7052 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007053 msg->next += ret;
7054 msg->chunk_len -= ret;
7055 if (msg->chunk_len) {
7056 /* input empty or output full */
7057 if (chn->buf->i > msg->next)
7058 chn->flags |= CF_WAKE_WRITE;
7059 goto missing_data_or_waiting;
7060 }
7061
Christopher Faulet1486b0a2017-07-18 11:42:08 +02007062 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
7063 * always set for a request. */
7064 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7065 /* The server still sending data that should be filtered */
7066 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
7067 goto missing_data_or_waiting;
7068 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007069
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007070 msg->msg_state = HTTP_MSG_ENDING;
7071
7072 ending:
7073 /* we may have some pending data starting at res->buf->p such as a last
7074 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007075 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7076 /* default_ret */ msg->next,
7077 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007078 b_adv(chn->buf, ret);
7079 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007080 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7081 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007082 if (msg->next)
7083 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007084
Christopher Fauletda02e172015-12-04 09:25:05 +01007085 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7086 /* default_ret */ 1,
7087 /* on_error */ goto error,
7088 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007089 msg->msg_state = HTTP_MSG_DONE;
7090 return 1;
7091
7092 missing_data_or_waiting:
7093 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007094 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7095 /* default_ret */ msg->next,
7096 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007097 b_adv(chn->buf, ret);
7098 msg->next -= ret;
7099 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7100 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007101 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007102 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007103 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007104 return 0;
7105 error:
7106 return -1;
7107}
7108
7109static inline int
7110http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7111{
7112 struct channel *chn = msg->chn;
7113 int ret;
7114
7115 /* Here we have the guarantee to be in one of the following state:
7116 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7117 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7118
7119 switch_states:
7120 switch (msg->msg_state) {
7121 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007122 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7123 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7124 /* on_error */ goto error);
7125 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007126 msg->chunk_len -= ret;
7127 if (msg->chunk_len) {
7128 /* input empty or output full */
7129 if (chn->buf->i > msg->next)
7130 chn->flags |= CF_WAKE_WRITE;
7131 goto missing_data_or_waiting;
7132 }
7133
7134 /* nothing left to forward for this chunk*/
7135 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7136 /* fall through for HTTP_MSG_CHUNK_CRLF */
7137
7138 case HTTP_MSG_CHUNK_CRLF:
7139 /* we want the CRLF after the data */
7140 ret = http_skip_chunk_crlf(msg);
7141 if (ret == 0)
7142 goto missing_data_or_waiting;
7143 if (ret < 0)
7144 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007145 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007146 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7147 /* fall through for HTTP_MSG_CHUNK_SIZE */
7148
7149 case HTTP_MSG_CHUNK_SIZE:
7150 /* read the chunk size and assign it to ->chunk_len,
7151 * then set ->next to point to the body and switch to
7152 * DATA or TRAILERS state.
7153 */
7154 ret = http_parse_chunk_size(msg);
7155 if (ret == 0)
7156 goto missing_data_or_waiting;
7157 if (ret < 0)
7158 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007159 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007160 if (msg->chunk_len) {
7161 msg->msg_state = HTTP_MSG_DATA;
7162 goto switch_states;
7163 }
7164 msg->msg_state = HTTP_MSG_TRAILERS;
7165 /* fall through for HTTP_MSG_TRAILERS */
7166
7167 case HTTP_MSG_TRAILERS:
7168 ret = http_forward_trailers(msg);
7169 if (ret < 0)
7170 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007171 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7172 /* default_ret */ 1,
7173 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007174 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007175 if (!ret)
7176 goto missing_data_or_waiting;
7177 break;
7178
7179 case HTTP_MSG_ENDING:
7180 goto ending;
7181
7182 default:
7183 /* This should no happen in this function */
7184 goto error;
7185 }
7186
7187 msg->msg_state = HTTP_MSG_ENDING;
7188 ending:
7189 /* we may have some pending data starting at res->buf->p such as a last
7190 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007191 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007192 /* default_ret */ msg->next,
7193 /* on_error */ goto error);
7194 b_adv(chn->buf, ret);
7195 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007196 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7197 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007198 if (msg->next)
7199 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007200
Christopher Fauletda02e172015-12-04 09:25:05 +01007201 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007202 /* default_ret */ 1,
7203 /* on_error */ goto error,
7204 /* on_wait */ goto waiting);
7205 msg->msg_state = HTTP_MSG_DONE;
7206 return 1;
7207
7208 missing_data_or_waiting:
7209 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007210 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007211 /* default_ret */ msg->next,
7212 /* on_error */ goto error);
7213 b_adv(chn->buf, ret);
7214 msg->next -= ret;
7215 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7216 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007217 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007218 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007219 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007220 return 0;
7221
7222 chunk_parsing_error:
7223 if (msg->err_pos >= 0) {
7224 if (chn->flags & CF_ISRESP)
7225 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7226 msg->msg_state, strm_fe(s));
7227 else
7228 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7229 msg, msg->msg_state, s->be);
7230 }
7231 error:
7232 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007233}
7234
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007236/* Iterate the same filter through all request headers.
7237 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 * Since it can manage the switch to another backend, it updates the per-proxy
7239 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007240 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007241int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 char *cur_ptr, *cur_end, *cur_next;
7244 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007245 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007247 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249 last_hdr = 0;
7250
Willy Tarreau9b28e032012-10-12 23:49:43 +02007251 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 old_idx = 0;
7253
7254 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007255 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007257 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 (exp->action == ACT_ALLOW ||
7259 exp->action == ACT_DENY ||
7260 exp->action == ACT_TARPIT))
7261 return 0;
7262
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007263 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 if (!cur_idx)
7265 break;
7266
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007267 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 cur_ptr = cur_next;
7269 cur_end = cur_ptr + cur_hdr->len;
7270 cur_next = cur_end + cur_hdr->cr + 1;
7271
7272 /* Now we have one header between cur_ptr and cur_end,
7273 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274 */
7275
Willy Tarreau15a53a42015-01-21 13:39:42 +01007276 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007279 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 last_hdr = 1;
7281 break;
7282
7283 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007284 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 break;
7287
7288 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007289 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 break;
7292
7293 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007294 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7295 if (trash.len < 0)
7296 return -1;
7297
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007298 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 /* FIXME: if the user adds a newline in the replacement, the
7300 * index will not be recalculated for now, and the new line
7301 * will not be counted as a new header.
7302 */
7303
7304 cur_end += delta;
7305 cur_next += delta;
7306 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007307 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 break;
7309
7310 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007311 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 cur_next += delta;
7313
Willy Tarreaufa355d42009-11-29 18:12:29 +01007314 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007315 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7316 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007317 cur_hdr->len = 0;
7318 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007319 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007320 break;
7321
7322 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323 }
7324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007325 /* keep the link from this header to next one in case of later
7326 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328 old_idx = cur_idx;
7329 }
7330 return 0;
7331}
7332
7333
7334/* Apply the filter to the request line.
7335 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7336 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 * Since it can manage the switch to another backend, it updates the per-proxy
7338 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007340int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007341{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 char *cur_ptr, *cur_end;
7343 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007344 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007345 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007348 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007349 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 (exp->action == ACT_ALLOW ||
7351 exp->action == ACT_DENY ||
7352 exp->action == ACT_TARPIT))
7353 return 0;
7354 else if (exp->action == ACT_REMOVE)
7355 return 0;
7356
7357 done = 0;
7358
Willy Tarreau9b28e032012-10-12 23:49:43 +02007359 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007360 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361
7362 /* Now we have the request line between cur_ptr and cur_end */
7363
Willy Tarreau15a53a42015-01-21 13:39:42 +01007364 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007368 done = 1;
7369 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007372 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007373 done = 1;
7374 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007377 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 done = 1;
7379 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007382 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7383 if (trash.len < 0)
7384 return -1;
7385
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007386 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 /* FIXME: if the user adds a newline in the replacement, the
7388 * index will not be recalculated for now, and the new line
7389 * will not be counted as a new header.
7390 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007391
Willy Tarreaufa355d42009-11-29 18:12:29 +01007392 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007394 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 HTTP_MSG_RQMETH,
7396 cur_ptr, cur_end + 1,
7397 NULL, NULL);
7398 if (unlikely(!cur_end))
7399 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007401 /* we have a full request and we know that we have either a CR
7402 * or an LF at <ptr>.
7403 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007404 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7405 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 /* there is no point trying this regex on headers */
7407 return 1;
7408 }
7409 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007410 return done;
7411}
Willy Tarreau97de6242006-12-27 17:18:38 +01007412
Willy Tarreau58f10d72006-12-04 02:26:12 +01007413
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007416 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 * unparsable request. Since it can manage the switch to another backend, it
7419 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007421int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422{
Willy Tarreau192252e2015-04-04 01:47:55 +02007423 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007424 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007425 struct hdr_exp *exp;
7426
7427 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007430 /*
7431 * The interleaving of transformations and verdicts
7432 * makes it difficult to decide to continue or stop
7433 * the evaluation.
7434 */
7435
Willy Tarreau6c123b12010-01-28 20:22:06 +01007436 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7437 break;
7438
Willy Tarreau3d300592007-03-18 18:34:41 +01007439 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007441 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007443
7444 /* if this filter had a condition, evaluate it now and skip to
7445 * next filter if the condition does not match.
7446 */
7447 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007448 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007449 ret = acl_pass(ret);
7450 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7451 ret = !ret;
7452
7453 if (!ret)
7454 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007455 }
7456
7457 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007458 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007459 if (unlikely(ret < 0))
7460 return -1;
7461
7462 if (likely(ret == 0)) {
7463 /* The filter did not match the request, it can be
7464 * iterated through all headers.
7465 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007466 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7467 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007469 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007470 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007471}
7472
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007474/* Find the end of a cookie value contained between <s> and <e>. It works the
7475 * same way as with headers above except that the semi-colon also ends a token.
7476 * See RFC2965 for more information. Note that it requires a valid header to
7477 * return a valid result.
7478 */
7479char *find_cookie_value_end(char *s, const char *e)
7480{
7481 int quoted, qdpair;
7482
7483 quoted = qdpair = 0;
7484 for (; s < e; s++) {
7485 if (qdpair) qdpair = 0;
7486 else if (quoted) {
7487 if (*s == '\\') qdpair = 1;
7488 else if (*s == '"') quoted = 0;
7489 }
7490 else if (*s == '"') quoted = 1;
7491 else if (*s == ',' || *s == ';') return s;
7492 }
7493 return s;
7494}
7495
7496/* Delete a value in a header between delimiters <from> and <next> in buffer
7497 * <buf>. The number of characters displaced is returned, and the pointer to
7498 * the first delimiter is updated if required. The function tries as much as
7499 * possible to respect the following principles :
7500 * - replace <from> delimiter by the <next> one unless <from> points to a
7501 * colon, in which case <next> is simply removed
7502 * - set exactly one space character after the new first delimiter, unless
7503 * there are not enough characters in the block being moved to do so.
7504 * - remove unneeded spaces before the previous delimiter and after the new
7505 * one.
7506 *
7507 * It is the caller's responsibility to ensure that :
7508 * - <from> points to a valid delimiter or the colon ;
7509 * - <next> points to a valid delimiter or the final CR/LF ;
7510 * - there are non-space chars before <from> ;
7511 * - there is a CR/LF at or after <next>.
7512 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007513int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514{
7515 char *prev = *from;
7516
7517 if (*prev == ':') {
7518 /* We're removing the first value, preserve the colon and add a
7519 * space if possible.
7520 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007521 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 next++;
7523 prev++;
7524 if (prev < next)
7525 *prev++ = ' ';
7526
Willy Tarreau2235b262016-11-05 15:50:20 +01007527 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 next++;
7529 } else {
7530 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007531 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 prev--;
7533 *from = prev;
7534
7535 /* copy the delimiter and if possible a space if we're
7536 * not at the end of the line.
7537 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007538 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 *prev++ = *next++;
7540 if (prev + 1 < next)
7541 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007542 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 next++;
7544 }
7545 }
7546 return buffer_replace2(buf, prev, next, NULL, 0);
7547}
7548
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007549/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007550 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 * desirable to call it only when needed. This code is quite complex because
7552 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7553 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007554 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007555void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007556{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007557 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007558 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007560 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007561 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7562 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007565 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007566 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007568 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007570 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007571
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007572 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 hdr_beg = hdr_next;
7574 hdr_end = hdr_beg + cur_hdr->len;
7575 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 /* We have one full header between hdr_beg and hdr_end, and the
7578 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 * "Cookie:" headers.
7580 */
7581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007583 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584 old_idx = cur_idx;
7585 continue;
7586 }
7587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 del_from = NULL; /* nothing to be deleted */
7589 preserve_hdr = 0; /* assume we may kill the whole header */
7590
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 /* Now look for cookies. Conforming to RFC2109, we have to support
7592 * attributes whose name begin with a '$', and associate them with
7593 * the right cookie, if we want to delete this cookie.
7594 * So there are 3 cases for each cookie read :
7595 * 1) it's a special attribute, beginning with a '$' : ignore it.
7596 * 2) it's a server id cookie that we *MAY* want to delete : save
7597 * some pointers on it (last semi-colon, beginning of cookie...)
7598 * 3) it's an application cookie : we *MAY* have to delete a previous
7599 * "special" cookie.
7600 * At the end of loop, if a "special" cookie remains, we may have to
7601 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 * *MUST* delete it.
7603 *
7604 * Note: RFC2965 is unclear about the processing of spaces around
7605 * the equal sign in the ATTR=VALUE form. A careful inspection of
7606 * the RFC explicitly allows spaces before it, and not within the
7607 * tokens (attrs or values). An inspection of RFC2109 allows that
7608 * too but section 10.1.3 lets one think that spaces may be allowed
7609 * after the equal sign too, resulting in some (rare) buggy
7610 * implementations trying to do that. So let's do what servers do.
7611 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7612 * allowed quoted strings in values, with any possible character
7613 * after a backslash, including control chars and delimitors, which
7614 * causes parsing to become ambiguous. Browsers also allow spaces
7615 * within values even without quotes.
7616 *
7617 * We have to keep multiple pointers in order to support cookie
7618 * removal at the beginning, middle or end of header without
7619 * corrupting the header. All of these headers are valid :
7620 *
7621 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7622 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7623 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7624 * | | | | | | | | |
7625 * | | | | | | | | hdr_end <--+
7626 * | | | | | | | +--> next
7627 * | | | | | | +----> val_end
7628 * | | | | | +-----------> val_beg
7629 * | | | | +--------------> equal
7630 * | | | +----------------> att_end
7631 * | | +---------------------> att_beg
7632 * | +--------------------------> prev
7633 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007634 */
7635
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7637 /* Iterate through all cookies on this line */
7638
7639 /* find att_beg */
7640 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007641 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007642 att_beg++;
7643
7644 /* find att_end : this is the first character after the last non
7645 * space before the equal. It may be equal to hdr_end.
7646 */
7647 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007648
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 while (equal < hdr_end) {
7650 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007651 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007652 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 continue;
7654 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655 }
7656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7658 * is between <att_beg> and <equal>, both may be identical.
7659 */
7660
7661 /* look for end of cookie if there is an equal sign */
7662 if (equal < hdr_end && *equal == '=') {
7663 /* look for the beginning of the value */
7664 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007665 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 val_beg++;
7667
7668 /* find the end of the value, respecting quotes */
7669 next = find_cookie_value_end(val_beg, hdr_end);
7670
7671 /* make val_end point to the first white space or delimitor after the value */
7672 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007673 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 val_end--;
7675 } else {
7676 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007677 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 /* We have nothing to do with attributes beginning with '$'. However,
7680 * they will automatically be removed if a header before them is removed,
7681 * since they're supposed to be linked together.
7682 */
7683 if (*att_beg == '$')
7684 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 /* Ignore cookies with no equal sign */
7687 if (equal == next) {
7688 /* This is not our cookie, so we must preserve it. But if we already
7689 * scheduled another cookie for removal, we cannot remove the
7690 * complete header, but we can remove the previous block itself.
7691 */
7692 preserve_hdr = 1;
7693 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007694 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 val_end += delta;
7696 next += delta;
7697 hdr_end += delta;
7698 hdr_next += delta;
7699 cur_hdr->len += delta;
7700 http_msg_move_end(&txn->req, delta);
7701 prev = del_from;
7702 del_from = NULL;
7703 }
7704 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 /* if there are spaces around the equal sign, we need to
7708 * strip them otherwise we'll get trouble for cookie captures,
7709 * or even for rewrites. Since this happens extremely rarely,
7710 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007711 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7713 int stripped_before = 0;
7714 int stripped_after = 0;
7715
7716 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 equal += stripped_before;
7719 val_beg += stripped_before;
7720 }
7721
7722 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007723 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 val_beg += stripped_after;
7725 stripped_before += stripped_after;
7726 }
7727
7728 val_end += stripped_before;
7729 next += stripped_before;
7730 hdr_end += stripped_before;
7731 hdr_next += stripped_before;
7732 cur_hdr->len += stripped_before;
7733 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 /* First, let's see if we want to capture this cookie. We check
7738 * that we don't already have a client side cookie, because we
7739 * can only capture one. Also as an optimisation, we ignore
7740 * cookies shorter than the declared name.
7741 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007742 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7743 (val_end - att_beg >= sess->fe->capture_namelen) &&
7744 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 int log_len = val_end - att_beg;
7746
7747 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7748 Alert("HTTP logging : out of memory.\n");
7749 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007750 if (log_len > sess->fe->capture_len)
7751 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007752 memcpy(txn->cli_cookie, att_beg, log_len);
7753 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756
Willy Tarreaubca99692010-10-06 19:25:55 +02007757 /* Persistence cookies in passive, rewrite or insert mode have the
7758 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007760 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007761 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007762 * For cookies in prefix mode, the form is :
7763 *
7764 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007766 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7767 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7768 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7772 * have the server ID between val_beg and delim, and the original cookie between
7773 * delim+1 and val_end. Otherwise, delim==val_end :
7774 *
7775 * Cookie: NAME=SRV; # in all but prefix modes
7776 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7777 * | || || | |+-> next
7778 * | || || | +--> val_end
7779 * | || || +---------> delim
7780 * | || |+------------> val_beg
7781 * | || +-------------> att_end = equal
7782 * | |+-----------------> att_beg
7783 * | +------------------> prev
7784 * +-------------------------> hdr_beg
7785 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007787 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 for (delim = val_beg; delim < val_end; delim++)
7789 if (*delim == COOKIE_DELIM)
7790 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007791 } else {
7792 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007794 /* Now check if the cookie contains a date field, which would
7795 * appear after a vertical bar ('|') just after the server name
7796 * and before the delimiter.
7797 */
7798 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7799 if (vbar1) {
7800 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007801 * right is the last seen date. It is a base64 encoded
7802 * 30-bit value representing the UNIX date since the
7803 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007804 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007805 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007806 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007807 if (val_end - vbar1 >= 5) {
7808 val = b64tos30(vbar1);
7809 if (val > 0)
7810 txn->cookie_last_date = val << 2;
7811 }
7812 /* look for a second vertical bar */
7813 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7814 if (vbar1 && (val_end - vbar1 > 5)) {
7815 val = b64tos30(vbar1 + 1);
7816 if (val > 0)
7817 txn->cookie_first_date = val << 2;
7818 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007819 }
7820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007821
Willy Tarreauf64d1412010-10-07 20:06:11 +02007822 /* if the cookie has an expiration date and the proxy wants to check
7823 * it, then we do that now. We first check if the cookie is too old,
7824 * then only if it has expired. We detect strict overflow because the
7825 * time resolution here is not great (4 seconds). Cookies with dates
7826 * in the future are ignored if their offset is beyond one day. This
7827 * allows an admin to fix timezone issues without expiring everyone
7828 * and at the same time avoids keeping unwanted side effects for too
7829 * long.
7830 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007831 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7832 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007833 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007834 txn->flags &= ~TX_CK_MASK;
7835 txn->flags |= TX_CK_OLD;
7836 delim = val_beg; // let's pretend we have not found the cookie
7837 txn->cookie_first_date = 0;
7838 txn->cookie_last_date = 0;
7839 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007840 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7841 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007842 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007843 txn->flags &= ~TX_CK_MASK;
7844 txn->flags |= TX_CK_EXPIRED;
7845 delim = val_beg; // let's pretend we have not found the cookie
7846 txn->cookie_first_date = 0;
7847 txn->cookie_last_date = 0;
7848 }
7849
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007850 /* Here, we'll look for the first running server which supports the cookie.
7851 * This allows to share a same cookie between several servers, for example
7852 * to dedicate backup servers to specific servers only.
7853 * However, to prevent clients from sticking to cookie-less backup server
7854 * when they have incidentely learned an empty cookie, we simply ignore
7855 * empty cookies and mark them as invalid.
7856 * The same behaviour is applied when persistence must be ignored.
7857 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007858 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007859 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 while (srv) {
7862 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7863 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007864 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007866 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007867 /* we found the server and we can use it */
7868 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007869 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007870 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007871 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 break;
7873 } else {
7874 /* we found a server, but it's down,
7875 * mark it as such and go on in case
7876 * another one is available.
7877 */
7878 txn->flags &= ~TX_CK_MASK;
7879 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007881 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007882 srv = srv->next;
7883 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007884
Willy Tarreauf64d1412010-10-07 20:06:11 +02007885 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007886 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007887 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007888 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007889 txn->flags |= TX_CK_UNUSED;
7890 else
7891 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 /* depending on the cookie mode, we may have to either :
7895 * - delete the complete cookie if we're in insert+indirect mode, so that
7896 * the server never sees it ;
7897 * - remove the server id from the cookie value, and tag the cookie as an
7898 * application cookie so that it does not get accidentely removed later,
7899 * if we're in cookie prefix mode
7900 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007901 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007902 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903
Willy Tarreau9b28e032012-10-12 23:49:43 +02007904 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 val_end += delta;
7906 next += delta;
7907 hdr_end += delta;
7908 hdr_next += delta;
7909 cur_hdr->len += delta;
7910 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007911
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 del_from = NULL;
7913 preserve_hdr = 1; /* we want to keep this cookie */
7914 }
7915 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007916 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 del_from = prev;
7918 }
7919 } else {
7920 /* This is not our cookie, so we must preserve it. But if we already
7921 * scheduled another cookie for removal, we cannot remove the
7922 * complete header, but we can remove the previous block itself.
7923 */
7924 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007926 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007927 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007928 if (att_beg >= del_from)
7929 att_beg += delta;
7930 if (att_end >= del_from)
7931 att_end += delta;
7932 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007933 val_end += delta;
7934 next += delta;
7935 hdr_end += delta;
7936 hdr_next += delta;
7937 cur_hdr->len += delta;
7938 http_msg_move_end(&txn->req, delta);
7939 prev = del_from;
7940 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007944 /* continue with next cookie on this header line */
7945 att_beg = next;
7946 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 /* There are no more cookies on this line.
7949 * We may still have one (or several) marked for deletion at the
7950 * end of the line. We must do this now in two ways :
7951 * - if some cookies must be preserved, we only delete from the
7952 * mark to the end of line ;
7953 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007956 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007957 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007958 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007959 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960 cur_hdr->len += delta;
7961 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007962 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963
7964 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007965 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7966 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007968 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007971 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007972 }
7973
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007974 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007977}
7978
7979
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980/* Iterate the same filter through all response headers contained in <rtr>.
7981 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7982 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007983int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985 char *cur_ptr, *cur_end, *cur_next;
7986 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007987 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007989 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990
7991 last_hdr = 0;
7992
Willy Tarreau9b28e032012-10-12 23:49:43 +02007993 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 old_idx = 0;
7995
7996 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007997 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007999 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 (exp->action == ACT_ALLOW ||
8001 exp->action == ACT_DENY))
8002 return 0;
8003
8004 cur_idx = txn->hdr_idx.v[old_idx].next;
8005 if (!cur_idx)
8006 break;
8007
8008 cur_hdr = &txn->hdr_idx.v[cur_idx];
8009 cur_ptr = cur_next;
8010 cur_end = cur_ptr + cur_hdr->len;
8011 cur_next = cur_end + cur_hdr->cr + 1;
8012
8013 /* Now we have one header between cur_ptr and cur_end,
8014 * and the next header starts at cur_next.
8015 */
8016
Willy Tarreau15a53a42015-01-21 13:39:42 +01008017 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008018 switch (exp->action) {
8019 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008020 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 last_hdr = 1;
8022 break;
8023
8024 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008025 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 last_hdr = 1;
8027 break;
8028
8029 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008030 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8031 if (trash.len < 0)
8032 return -1;
8033
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008034 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 /* FIXME: if the user adds a newline in the replacement, the
8036 * index will not be recalculated for now, and the new line
8037 * will not be counted as a new header.
8038 */
8039
8040 cur_end += delta;
8041 cur_next += delta;
8042 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008043 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 break;
8045
8046 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008047 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 cur_next += delta;
8049
Willy Tarreaufa355d42009-11-29 18:12:29 +01008050 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8052 txn->hdr_idx.used--;
8053 cur_hdr->len = 0;
8054 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008055 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056 break;
8057
8058 }
8059 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060
8061 /* keep the link from this header to next one in case of later
8062 * removal of next header.
8063 */
8064 old_idx = cur_idx;
8065 }
8066 return 0;
8067}
8068
8069
8070/* Apply the filter to the status line in the response buffer <rtr>.
8071 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8072 * or -1 if a replacement resulted in an invalid status line.
8073 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008074int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 char *cur_ptr, *cur_end;
8077 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008078 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008079 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080
8081
Willy Tarreau3d300592007-03-18 18:34:41 +01008082 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008084 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 (exp->action == ACT_ALLOW ||
8086 exp->action == ACT_DENY))
8087 return 0;
8088 else if (exp->action == ACT_REMOVE)
8089 return 0;
8090
8091 done = 0;
8092
Willy Tarreau9b28e032012-10-12 23:49:43 +02008093 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008094 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095
8096 /* Now we have the status line between cur_ptr and cur_end */
8097
Willy Tarreau15a53a42015-01-21 13:39:42 +01008098 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099 switch (exp->action) {
8100 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008101 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 done = 1;
8103 break;
8104
8105 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008106 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107 done = 1;
8108 break;
8109
8110 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008111 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8112 if (trash.len < 0)
8113 return -1;
8114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008115 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116 /* FIXME: if the user adds a newline in the replacement, the
8117 * index will not be recalculated for now, and the new line
8118 * will not be counted as a new header.
8119 */
8120
Willy Tarreaufa355d42009-11-29 18:12:29 +01008121 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008123 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008124 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125 cur_ptr, cur_end + 1,
8126 NULL, NULL);
8127 if (unlikely(!cur_end))
8128 return -1;
8129
8130 /* we have a full respnse and we know that we have either a CR
8131 * or an LF at <ptr>.
8132 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008133 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008134 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 /* there is no point trying this regex on headers */
8136 return 1;
8137 }
8138 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 return done;
8140}
8141
8142
8143
8144/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008145 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8147 * unparsable response.
8148 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008149int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150{
Willy Tarreau192252e2015-04-04 01:47:55 +02008151 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008152 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008153 struct hdr_exp *exp;
8154
8155 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156 int ret;
8157
8158 /*
8159 * The interleaving of transformations and verdicts
8160 * makes it difficult to decide to continue or stop
8161 * the evaluation.
8162 */
8163
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008164 if (txn->flags & TX_SVDENY)
8165 break;
8166
Willy Tarreau3d300592007-03-18 18:34:41 +01008167 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8169 exp->action == ACT_PASS)) {
8170 exp = exp->next;
8171 continue;
8172 }
8173
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008174 /* if this filter had a condition, evaluate it now and skip to
8175 * next filter if the condition does not match.
8176 */
8177 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008178 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008179 ret = acl_pass(ret);
8180 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8181 ret = !ret;
8182 if (!ret)
8183 continue;
8184 }
8185
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008187 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008188 if (unlikely(ret < 0))
8189 return -1;
8190
8191 if (likely(ret == 0)) {
8192 /* The filter did not match the response, it can be
8193 * iterated through all headers.
8194 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008195 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8196 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008198 }
8199 return 0;
8200}
8201
8202
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008204 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008205 * desirable to call it only when needed. This function is also used when we
8206 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008208void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008210 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008211 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008212 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008213 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008215 char *hdr_beg, *hdr_end, *hdr_next;
8216 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 /* Iterate through the headers.
8219 * we start with the start line.
8220 */
8221 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008222 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223
8224 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8225 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008226 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227
8228 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008229 hdr_beg = hdr_next;
8230 hdr_end = hdr_beg + cur_hdr->len;
8231 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 /* We have one full header between hdr_beg and hdr_end, and the
8234 * next header starts at hdr_next. We're only interested in
8235 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 */
8237
Willy Tarreau24581ba2010-08-31 22:39:35 +02008238 is_cookie2 = 0;
8239 prev = hdr_beg + 10;
8240 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008241 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8243 if (!val) {
8244 old_idx = cur_idx;
8245 continue;
8246 }
8247 is_cookie2 = 1;
8248 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249 }
8250
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8252 * <prev> points to the colon.
8253 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008254 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8257 * check-cache is enabled) and we are not interested in checking
8258 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008259 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008260 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 return;
8262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 /* OK so now we know we have to process this response cookie.
8264 * The format of the Set-Cookie header is slightly different
8265 * from the format of the Cookie header in that it does not
8266 * support the comma as a cookie delimiter (thus the header
8267 * cannot be folded) because the Expires attribute described in
8268 * the original Netscape's spec may contain an unquoted date
8269 * with a comma inside. We have to live with this because
8270 * many browsers don't support Max-Age and some browsers don't
8271 * support quoted strings. However the Set-Cookie2 header is
8272 * clean.
8273 *
8274 * We have to keep multiple pointers in order to support cookie
8275 * removal at the beginning, middle or end of header without
8276 * corrupting the header (in case of set-cookie2). A special
8277 * pointer, <scav> points to the beginning of the set-cookie-av
8278 * fields after the first semi-colon. The <next> pointer points
8279 * either to the end of line (set-cookie) or next unquoted comma
8280 * (set-cookie2). All of these headers are valid :
8281 *
8282 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8283 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8284 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8285 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8286 * | | | | | | | | | |
8287 * | | | | | | | | +-> next hdr_end <--+
8288 * | | | | | | | +------------> scav
8289 * | | | | | | +--------------> val_end
8290 * | | | | | +--------------------> val_beg
8291 * | | | | +----------------------> equal
8292 * | | | +------------------------> att_end
8293 * | | +----------------------------> att_beg
8294 * | +------------------------------> prev
8295 * +-----------------------------------------> hdr_beg
8296 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008297
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 for (; prev < hdr_end; prev = next) {
8299 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 /* find att_beg */
8302 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008303 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 /* find att_end : this is the first character after the last non
8307 * space before the equal. It may be equal to hdr_end.
8308 */
8309 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310
Willy Tarreau24581ba2010-08-31 22:39:35 +02008311 while (equal < hdr_end) {
8312 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8313 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008314 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008315 continue;
8316 att_end = equal;
8317 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318
Willy Tarreau24581ba2010-08-31 22:39:35 +02008319 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8320 * is between <att_beg> and <equal>, both may be identical.
8321 */
8322
8323 /* look for end of cookie if there is an equal sign */
8324 if (equal < hdr_end && *equal == '=') {
8325 /* look for the beginning of the value */
8326 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008327 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008328 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 /* find the end of the value, respecting quotes */
8331 next = find_cookie_value_end(val_beg, hdr_end);
8332
8333 /* make val_end point to the first white space or delimitor after the value */
8334 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008335 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 val_end--;
8337 } else {
8338 /* <equal> points to next comma, semi-colon or EOL */
8339 val_beg = val_end = next = equal;
8340 }
8341
8342 if (next < hdr_end) {
8343 /* Set-Cookie2 supports multiple cookies, and <next> points to
8344 * a colon or semi-colon before the end. So skip all attr-value
8345 * pairs and look for the next comma. For Set-Cookie, since
8346 * commas are permitted in values, skip to the end.
8347 */
8348 if (is_cookie2)
8349 next = find_hdr_value_end(next, hdr_end);
8350 else
8351 next = hdr_end;
8352 }
8353
8354 /* Now everything is as on the diagram above */
8355
8356 /* Ignore cookies with no equal sign */
8357 if (equal == val_end)
8358 continue;
8359
8360 /* If there are spaces around the equal sign, we need to
8361 * strip them otherwise we'll get trouble for cookie captures,
8362 * or even for rewrites. Since this happens extremely rarely,
8363 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008364 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8366 int stripped_before = 0;
8367 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008368
Willy Tarreau24581ba2010-08-31 22:39:35 +02008369 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008370 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 equal += stripped_before;
8372 val_beg += stripped_before;
8373 }
8374
8375 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008376 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008377 val_beg += stripped_after;
8378 stripped_before += stripped_after;
8379 }
8380
8381 val_end += stripped_before;
8382 next += stripped_before;
8383 hdr_end += stripped_before;
8384 hdr_next += stripped_before;
8385 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008386 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 }
8388
8389 /* First, let's see if we want to capture this cookie. We check
8390 * that we don't already have a server side cookie, because we
8391 * can only capture one. Also as an optimisation, we ignore
8392 * cookies shorter than the declared name.
8393 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008394 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008395 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008396 (val_end - att_beg >= sess->fe->capture_namelen) &&
8397 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008398 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008399 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008400 Alert("HTTP logging : out of memory.\n");
8401 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008402 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008403 if (log_len > sess->fe->capture_len)
8404 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008405 memcpy(txn->srv_cookie, att_beg, log_len);
8406 txn->srv_cookie[log_len] = 0;
8407 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008408 }
8409
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008410 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008411 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008412 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008413 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8414 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008415 /* assume passive cookie by default */
8416 txn->flags &= ~TX_SCK_MASK;
8417 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418
8419 /* If the cookie is in insert mode on a known server, we'll delete
8420 * this occurrence because we'll insert another one later.
8421 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008422 * a direct access.
8423 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008425 /* The "preserve" flag was set, we don't want to touch the
8426 * server's cookie.
8427 */
8428 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008429 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008430 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008431 /* this cookie must be deleted */
8432 if (*prev == ':' && next == hdr_end) {
8433 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008434 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008435 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8436 txn->hdr_idx.used--;
8437 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008438 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008439 hdr_next += delta;
8440 http_msg_move_end(&txn->rsp, delta);
8441 /* note: while both invalid now, <next> and <hdr_end>
8442 * are still equal, so the for() will stop as expected.
8443 */
8444 } else {
8445 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008446 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008447 next = prev;
8448 hdr_end += delta;
8449 hdr_next += delta;
8450 cur_hdr->len += delta;
8451 http_msg_move_end(&txn->rsp, delta);
8452 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008453 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008454 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008455 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008456 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008457 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008458 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 * with this server since we know it.
8460 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008461 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008462 next += delta;
8463 hdr_end += delta;
8464 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008466 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467
Willy Tarreauf1348312010-10-07 15:54:11 +02008468 txn->flags &= ~TX_SCK_MASK;
8469 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008471 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008472 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008473 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008475 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008476 next += delta;
8477 hdr_end += delta;
8478 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008480 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481
Willy Tarreau827aee92011-03-10 16:55:02 +01008482 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008483 txn->flags &= ~TX_SCK_MASK;
8484 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 }
8486 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 /* that's done for this cookie, check the next one on the same
8488 * line when next != hdr_end (only if is_cookie2).
8489 */
8490 }
8491 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008493 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494}
8495
8496
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008499 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008500void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008502 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 char *p1, *p2;
8504
8505 char *cur_ptr, *cur_end, *cur_next;
8506 int cur_idx;
8507
Willy Tarreau5df51872007-11-25 16:20:08 +01008508 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509 return;
8510
8511 /* Iterate through the headers.
8512 * we start with the start line.
8513 */
8514 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008515 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516
8517 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8518 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008519 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520
8521 cur_hdr = &txn->hdr_idx.v[cur_idx];
8522 cur_ptr = cur_next;
8523 cur_end = cur_ptr + cur_hdr->len;
8524 cur_next = cur_end + cur_hdr->cr + 1;
8525
8526 /* We have one full header between cur_ptr and cur_end, and the
8527 * next header starts at cur_next. We're only interested in
8528 * "Cookie:" headers.
8529 */
8530
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008531 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8532 if (val) {
8533 if ((cur_end - (cur_ptr + val) >= 8) &&
8534 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8535 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8536 return;
8537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538 }
8539
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008540 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8541 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542 continue;
8543
8544 /* OK, right now we know we have a cache-control header at cur_ptr */
8545
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008546 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008547
8548 if (p1 >= cur_end) /* no more info */
8549 continue;
8550
8551 /* p1 is at the beginning of the value */
8552 p2 = p1;
8553
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008554 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008555 p2++;
8556
8557 /* we have a complete value between p1 and p2 */
8558 if (p2 < cur_end && *p2 == '=') {
8559 /* we have something of the form no-cache="set-cookie" */
8560 if ((cur_end - p1 >= 21) &&
8561 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8562 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008563 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 continue;
8565 }
8566
8567 /* OK, so we know that either p2 points to the end of string or to a comma */
8568 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008569 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008570 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8571 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8572 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008573 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 return;
8575 }
8576
8577 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008578 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008579 continue;
8580 }
8581 }
8582}
8583
Willy Tarreau58f10d72006-12-04 02:26:12 +01008584
Willy Tarreaub2513902006-12-17 14:52:38 +01008585/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008586 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008587 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008588 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008589 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008590 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008591 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008592 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008593 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008594int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008595{
8596 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008597 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008598 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008599
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008600 if (!uri_auth)
8601 return 0;
8602
Cyril Bonté70be45d2010-10-12 00:14:35 +02008603 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008604 return 0;
8605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008606 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008607 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008608 return 0;
8609
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008610 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008611 return 0;
8612
Willy Tarreaub2513902006-12-17 14:52:38 +01008613 return 1;
8614}
8615
Willy Tarreau4076a152009-04-02 15:18:36 +02008616/*
8617 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008618 * By default it tries to report the error position as msg->err_pos. However if
8619 * this one is not set, it will then report msg->next, which is the last known
8620 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008621 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008622 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008623void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008624 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008625 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008626{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008627 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008628 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008629 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008630
Willy Tarreauf3764b72016-03-31 13:45:10 +02008631 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008632 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008633 len1 = MIN(len1, es->len);
8634 len2 = es->len - len1; /* remaining data if buffer wraps */
8635
Willy Tarreauf3764b72016-03-31 13:45:10 +02008636 if (!es->buf)
8637 es->buf = malloc(global.tune.bufsize);
8638
8639 if (es->buf) {
8640 memcpy(es->buf, chn->buf->p, len1);
8641 if (len2)
8642 memcpy(es->buf + len1, chn->buf->data, len2);
8643 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008644
Willy Tarreau4076a152009-04-02 15:18:36 +02008645 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008646 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008647 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008648 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008649
Willy Tarreau4076a152009-04-02 15:18:36 +02008650 es->when = date; // user-visible date
8651 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008652 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008653 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008654 if (objt_conn(sess->origin))
8655 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008656 else
8657 memset(&es->src, 0, sizeof(es->src));
8658
Willy Tarreau078272e2010-12-12 12:46:33 +01008659 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008660 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008661 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008662 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008663 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008664 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008665 es->b_out = chn->buf->o;
8666 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008667 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008668 es->m_clen = msg->chunk_len;
8669 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008670}
Willy Tarreaub2513902006-12-17 14:52:38 +01008671
Willy Tarreau294c4732011-12-16 21:35:50 +01008672/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8673 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8674 * performed over the whole headers. Otherwise it must contain a valid header
8675 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8676 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8677 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8678 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008679 * -1. The value fetch stops at commas, so this function is suited for use with
8680 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008681 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008682 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008683unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008684 struct hdr_idx *idx, int occ,
8685 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008686{
Willy Tarreau294c4732011-12-16 21:35:50 +01008687 struct hdr_ctx local_ctx;
8688 char *ptr_hist[MAX_HDR_HISTORY];
8689 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008690 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008691 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008692
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 if (!ctx) {
8694 local_ctx.idx = 0;
8695 ctx = &local_ctx;
8696 }
8697
Willy Tarreaubce70882009-09-07 11:51:47 +02008698 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008699 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008700 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008701 occ--;
8702 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008703 *vptr = ctx->line + ctx->val;
8704 *vlen = ctx->vlen;
8705 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008706 }
8707 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008708 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 }
8710
8711 /* negative occurrence, we scan all the list then walk back */
8712 if (-occ > MAX_HDR_HISTORY)
8713 return 0;
8714
Willy Tarreau294c4732011-12-16 21:35:50 +01008715 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008716 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008717 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8718 len_hist[hist_ptr] = ctx->vlen;
8719 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 hist_ptr = 0;
8721 found++;
8722 }
8723 if (-occ > found)
8724 return 0;
8725 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008726 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8727 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8728 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008729 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008730 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 if (hist_ptr >= MAX_HDR_HISTORY)
8732 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008733 *vptr = ptr_hist[hist_ptr];
8734 *vlen = len_hist[hist_ptr];
8735 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008736}
8737
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008738/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8739 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8740 * performed over the whole headers. Otherwise it must contain a valid header
8741 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8742 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8743 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8744 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8745 * -1. This function differs from http_get_hdr() in that it only returns full
8746 * line header values and does not stop at commas.
8747 * The return value is 0 if nothing was found, or non-zero otherwise.
8748 */
8749unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8750 struct hdr_idx *idx, int occ,
8751 struct hdr_ctx *ctx, char **vptr, int *vlen)
8752{
8753 struct hdr_ctx local_ctx;
8754 char *ptr_hist[MAX_HDR_HISTORY];
8755 int len_hist[MAX_HDR_HISTORY];
8756 unsigned int hist_ptr;
8757 int found;
8758
8759 if (!ctx) {
8760 local_ctx.idx = 0;
8761 ctx = &local_ctx;
8762 }
8763
8764 if (occ >= 0) {
8765 /* search from the beginning */
8766 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8767 occ--;
8768 if (occ <= 0) {
8769 *vptr = ctx->line + ctx->val;
8770 *vlen = ctx->vlen;
8771 return 1;
8772 }
8773 }
8774 return 0;
8775 }
8776
8777 /* negative occurrence, we scan all the list then walk back */
8778 if (-occ > MAX_HDR_HISTORY)
8779 return 0;
8780
8781 found = hist_ptr = 0;
8782 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8783 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8784 len_hist[hist_ptr] = ctx->vlen;
8785 if (++hist_ptr >= MAX_HDR_HISTORY)
8786 hist_ptr = 0;
8787 found++;
8788 }
8789 if (-occ > found)
8790 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008791
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008792 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008793 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8794 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8795 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008796 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008797 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008798 if (hist_ptr >= MAX_HDR_HISTORY)
8799 hist_ptr -= MAX_HDR_HISTORY;
8800 *vptr = ptr_hist[hist_ptr];
8801 *vlen = len_hist[hist_ptr];
8802 return 1;
8803}
8804
Willy Tarreaubaaee002006-06-26 02:48:02 +02008805/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008806 * Print a debug line with a header. Always stop at the first CR or LF char,
8807 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8808 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008809 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008810void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008811{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008812 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008813 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008814
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008815 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008816 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008817 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
8818 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008819
8820 for (max = 0; start + max < end; max++)
8821 if (start[max] == '\r' || start[max] == '\n')
8822 break;
8823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008824 UBOUND(max, trash.size - trash.len - 3);
8825 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8826 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008827 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008828}
8829
Willy Tarreaueee5b512015-04-03 23:46:31 +02008830
8831/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8832 * The hdr_idx is allocated as well. In case of allocation failure, everything
8833 * allocated is freed and NULL is returned. Otherwise the new transaction is
8834 * assigned to the stream and returned.
8835 */
8836struct http_txn *http_alloc_txn(struct stream *s)
8837{
8838 struct http_txn *txn = s->txn;
8839
8840 if (txn)
8841 return txn;
8842
8843 txn = pool_alloc2(pool2_http_txn);
8844 if (!txn)
8845 return txn;
8846
8847 txn->hdr_idx.size = global.tune.max_http_hdr;
8848 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8849 if (!txn->hdr_idx.v) {
8850 pool_free2(pool2_http_txn, txn);
8851 return NULL;
8852 }
8853
8854 s->txn = txn;
8855 return txn;
8856}
8857
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008858void http_txn_reset_req(struct http_txn *txn)
8859{
8860 txn->req.flags = 0;
8861 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8862 txn->req.next = 0;
8863 txn->req.chunk_len = 0LL;
8864 txn->req.body_len = 0LL;
8865 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8866}
8867
8868void http_txn_reset_res(struct http_txn *txn)
8869{
8870 txn->rsp.flags = 0;
8871 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8872 txn->rsp.next = 0;
8873 txn->rsp.chunk_len = 0LL;
8874 txn->rsp.body_len = 0LL;
8875 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8876}
8877
Willy Tarreau0937bc42009-12-22 15:03:09 +01008878/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008879 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880 * the required fields are properly allocated and that we only need to (re)init
8881 * them. This should be used before processing any new request.
8882 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008883void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008884{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008885 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008886 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008887
8888 txn->flags = 0;
8889 txn->status = -1;
8890
Willy Tarreauf64d1412010-10-07 20:06:11 +02008891 txn->cookie_first_date = 0;
8892 txn->cookie_last_date = 0;
8893
Willy Tarreaueee5b512015-04-03 23:46:31 +02008894 txn->srv_cookie = NULL;
8895 txn->cli_cookie = NULL;
8896 txn->uri = NULL;
8897
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008898 http_txn_reset_req(txn);
8899 http_txn_reset_res(txn);
8900
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008901 txn->req.chn = &s->req;
8902 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008903
8904 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008905
8906 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8907 if (fe->options2 & PR_O2_REQBUG_OK)
8908 txn->req.err_pos = -1; /* let buggy requests pass */
8909
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910 if (txn->hdr_idx.v)
8911 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008912
8913 vars_init(&s->vars_txn, SCOPE_TXN);
8914 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915}
8916
8917/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008918void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008920 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008921 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922
8923 /* these ones will have been dynamically allocated */
8924 pool_free2(pool2_requri, txn->uri);
8925 pool_free2(pool2_capture, txn->cli_cookie);
8926 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008927 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008928
William Lallemanda73203e2012-03-12 12:48:57 +01008929 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008930 txn->uri = NULL;
8931 txn->srv_cookie = NULL;
8932 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008933
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008934 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008935 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008936 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008937 pool_free2(h->pool, s->req_cap[h->index]);
8938 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008939 }
8940
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008941 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008942 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008943 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008944 pool_free2(h->pool, s->res_cap[h->index]);
8945 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008946 }
8947
Willy Tarreau6204cd92016-03-10 16:33:04 +01008948 vars_prune(&s->vars_txn, s->sess, s);
8949 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008950}
8951
8952/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008953void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008954{
8955 http_end_txn(s);
8956 http_init_txn(s);
8957
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008958 /* reinitialise the current rule list pointer to NULL. We are sure that
8959 * any rulelist match the NULL pointer.
8960 */
8961 s->current_rule_list = NULL;
8962
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008963 s->be = strm_fe(s);
8964 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008965 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008966 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008967 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008968 /* re-init store persistence */
8969 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008970 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008971
Willy Tarreau0937bc42009-12-22 15:03:09 +01008972 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008974 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008975
Willy Tarreau739cfba2010-01-25 23:11:14 +01008976 /* We must trim any excess data from the response buffer, because we
8977 * may have blocked an invalid response from a server that we don't
8978 * want to accidentely forward once we disable the analysers, nor do
8979 * we want those data to come along with next response. A typical
8980 * example of such data would be from a buggy server responding to
8981 * a HEAD with some data, or sending more than the advertised
8982 * content-length.
8983 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008984 if (unlikely(s->res.buf->i))
8985 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008986
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008987 /* Now we can realign the response buffer */
8988 buffer_realign(s->res.buf);
8989
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008990 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008991 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008993 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008994 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008996 s->req.rex = TICK_ETERNITY;
8997 s->req.wex = TICK_ETERNITY;
8998 s->req.analyse_exp = TICK_ETERNITY;
8999 s->res.rex = TICK_ETERNITY;
9000 s->res.wex = TICK_ETERNITY;
9001 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01009002 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009003}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009004
Sasha Pachev218f0642014-06-16 12:05:59 -06009005void free_http_res_rules(struct list *r)
9006{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009007 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009008
9009 list_for_each_entry_safe(pr, tr, r, list) {
9010 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009011 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009012 free(pr);
9013 }
9014}
9015
9016void free_http_req_rules(struct list *r)
9017{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009018 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009019
9020 list_for_each_entry_safe(pr, tr, r, list) {
9021 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009022 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009023 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009024
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009025 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009026 free(pr);
9027 }
9028}
9029
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009030/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009031struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009032{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009033 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009034 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009035 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009036 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009037
Vincent Bernat02779b62016-04-03 13:48:43 +02009038 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009039 if (!rule) {
9040 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009041 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 }
9043
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009044 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009045 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009046 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009047 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009048 int code;
9049 int hc;
9050
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009051 if (!strcmp(args[0], "tarpit")) {
9052 rule->action = ACT_HTTP_REQ_TARPIT;
9053 rule->deny_status = HTTP_ERR_500;
9054 }
9055 else {
9056 rule->action = ACT_ACTION_DENY;
9057 rule->deny_status = HTTP_ERR_403;
9058 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009060 if (strcmp(args[cur_arg], "deny_status") == 0) {
9061 cur_arg++;
9062 if (!args[cur_arg]) {
9063 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9064 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9065 goto out_err;
9066 }
9067
9068 code = atol(args[cur_arg]);
9069 cur_arg++;
9070 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9071 if (http_err_codes[hc] == code) {
9072 rule->deny_status = hc;
9073 break;
9074 }
9075 }
9076
9077 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009078 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9079 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009080 }
9081 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009082 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009083 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009084 cur_arg = 1;
9085
9086 while(*args[cur_arg]) {
9087 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009088 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009089 cur_arg+=2;
9090 continue;
9091 } else
9092 break;
9093 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009094 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009095 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009096 cur_arg = 1;
9097
9098 if (!*args[cur_arg] ||
9099 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9100 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9101 file, linenum, args[0]);
9102 goto out_err;
9103 }
9104 rule->arg.nice = atoi(args[cur_arg]);
9105 if (rule->arg.nice < -1024)
9106 rule->arg.nice = -1024;
9107 else if (rule->arg.nice > 1024)
9108 rule->arg.nice = 1024;
9109 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009110 } else if (!strcmp(args[0], "set-tos")) {
9111#ifdef IP_TOS
9112 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009113 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009114 cur_arg = 1;
9115
9116 if (!*args[cur_arg] ||
9117 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9118 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9119 file, linenum, args[0]);
9120 goto out_err;
9121 }
9122
9123 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9124 if (err && *err != '\0') {
9125 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9126 file, linenum, err, args[0]);
9127 goto out_err;
9128 }
9129 cur_arg++;
9130#else
9131 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9132 goto out_err;
9133#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009134 } else if (!strcmp(args[0], "set-mark")) {
9135#ifdef SO_MARK
9136 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009137 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009138 cur_arg = 1;
9139
9140 if (!*args[cur_arg] ||
9141 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9142 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9143 file, linenum, args[0]);
9144 goto out_err;
9145 }
9146
9147 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9148 if (err && *err != '\0') {
9149 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9150 file, linenum, err, args[0]);
9151 goto out_err;
9152 }
9153 cur_arg++;
9154 global.last_checks |= LSTCHK_NETADM;
9155#else
9156 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9157 goto out_err;
9158#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009159 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009160 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009161 cur_arg = 1;
9162
9163 if (!*args[cur_arg] ||
9164 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9165 bad_log_level:
9166 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9167 file, linenum, args[0]);
9168 goto out_err;
9169 }
9170 if (strcmp(args[cur_arg], "silent") == 0)
9171 rule->arg.loglevel = -1;
9172 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9173 goto bad_log_level;
9174 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009175 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009176 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009177 cur_arg = 1;
9178
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009179 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9180 (*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 +01009181 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9182 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009183 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009184 }
9185
9186 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9187 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9188 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009189
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009190 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009191 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009192 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 +01009193 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9194 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9195 file, linenum, args[0], error);
9196 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009197 goto out_err;
9198 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009199 free(proxy->conf.lfs_file);
9200 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9201 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009202 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009203 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009204 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009205 cur_arg = 1;
9206
9207 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009208 (*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 -06009209 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9210 file, linenum, args[0]);
9211 goto out_err;
9212 }
9213
9214 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9215 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9216 LIST_INIT(&rule->arg.hdr_add.fmt);
9217
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009218 error = NULL;
9219 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9220 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9221 args[cur_arg + 1], error);
9222 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009223 goto out_err;
9224 }
9225
9226 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009227 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009228 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 +01009229 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9230 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9231 file, linenum, args[0], error);
9232 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009233 goto out_err;
9234 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009235
9236 free(proxy->conf.lfs_file);
9237 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9238 proxy->conf.lfs_line = proxy->conf.args.line;
9239 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009240 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009241 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009242 cur_arg = 1;
9243
9244 if (!*args[cur_arg] ||
9245 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9246 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\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
9254 proxy->conf.args.ctx = ARGC_HRQ;
9255 free(proxy->conf.lfs_file);
9256 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9257 proxy->conf.lfs_line = proxy->conf.args.line;
9258 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009259 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9260 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009261 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009262 struct sample_expr *expr;
9263 unsigned int where;
9264 char *err = NULL;
9265
9266 cur_arg = 1;
9267 proxy->conf.args.ctx = ARGC_TRK;
9268
9269 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9270 if (!expr) {
9271 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9272 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9273 free(err);
9274 goto out_err;
9275 }
9276
9277 where = 0;
9278 if (proxy->cap & PR_CAP_FE)
9279 where |= SMP_VAL_FE_HRQ_HDR;
9280 if (proxy->cap & PR_CAP_BE)
9281 where |= SMP_VAL_BE_HRQ_HDR;
9282
9283 if (!(expr->fetch->val & where)) {
9284 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9285 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9286 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9287 args[cur_arg-1], sample_src_names(expr->fetch->use));
9288 free(expr);
9289 goto out_err;
9290 }
9291
9292 if (strcmp(args[cur_arg], "table") == 0) {
9293 cur_arg++;
9294 if (!args[cur_arg]) {
9295 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9296 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9297 free(expr);
9298 goto out_err;
9299 }
9300 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009301 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009302 cur_arg++;
9303 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009304 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009305 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009306 } else if (strcmp(args[0], "redirect") == 0) {
9307 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009308 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009309
Willy Tarreaube4653b2015-05-28 15:26:58 +02009310 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009311 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9312 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9313 goto out_err;
9314 }
9315
9316 /* this redirect rule might already contain a parsed condition which
9317 * we'll pass to the http-request rule.
9318 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009319 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009320 rule->arg.redir = redir;
9321 rule->cond = redir->cond;
9322 redir->cond = NULL;
9323 cur_arg = 2;
9324 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009325 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9326 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009327 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009328 /*
9329 * '+ 8' for 'add-acl('
9330 * '- 9' for 'add-acl(' + trailing ')'
9331 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009332 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009333
9334 cur_arg = 1;
9335
9336 if (!*args[cur_arg] ||
9337 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9338 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9339 file, linenum, args[0]);
9340 goto out_err;
9341 }
9342
9343 LIST_INIT(&rule->arg.map.key);
9344 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009345 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009346 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009347 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9348 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9349 file, linenum, args[0], error);
9350 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009351 goto out_err;
9352 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009353 free(proxy->conf.lfs_file);
9354 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9355 proxy->conf.lfs_line = proxy->conf.args.line;
9356 cur_arg += 1;
9357 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9358 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009359 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009360 /*
9361 * '+ 8' for 'del-acl('
9362 * '- 9' for 'del-acl(' + trailing ')'
9363 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009364 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009365
9366 cur_arg = 1;
9367
9368 if (!*args[cur_arg] ||
9369 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9370 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9371 file, linenum, args[0]);
9372 goto out_err;
9373 }
9374
9375 LIST_INIT(&rule->arg.map.key);
9376 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009377 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009378 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009379 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9380 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9381 file, linenum, args[0], error);
9382 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009383 goto out_err;
9384 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009385 free(proxy->conf.lfs_file);
9386 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9387 proxy->conf.lfs_line = proxy->conf.args.line;
9388 cur_arg += 1;
9389 } else if (strncmp(args[0], "del-map", 7) == 0) {
9390 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009391 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009392 /*
9393 * '+ 8' for 'del-map('
9394 * '- 9' for 'del-map(' + trailing ')'
9395 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009396 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009397
9398 cur_arg = 1;
9399
9400 if (!*args[cur_arg] ||
9401 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9402 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9403 file, linenum, args[0]);
9404 goto out_err;
9405 }
9406
9407 LIST_INIT(&rule->arg.map.key);
9408 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009409 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009410 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009411 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9412 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9413 file, linenum, args[0], error);
9414 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009415 goto out_err;
9416 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009417 free(proxy->conf.lfs_file);
9418 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9419 proxy->conf.lfs_line = proxy->conf.args.line;
9420 cur_arg += 1;
9421 } else if (strncmp(args[0], "set-map", 7) == 0) {
9422 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009423 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009424 /*
9425 * '+ 8' for 'set-map('
9426 * '- 9' for 'set-map(' + trailing ')'
9427 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009428 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009429
9430 cur_arg = 1;
9431
9432 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9433 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9434 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9435 file, linenum, args[0]);
9436 goto out_err;
9437 }
9438
9439 LIST_INIT(&rule->arg.map.key);
9440 LIST_INIT(&rule->arg.map.value);
9441 proxy->conf.args.ctx = ARGC_HRQ;
9442
9443 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009444 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009445 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009446 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9447 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9448 file, linenum, args[0], error);
9449 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009450 goto out_err;
9451 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009452
9453 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009454 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009455 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 +01009456 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9457 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9458 file, linenum, args[0], error);
9459 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009460 goto out_err;
9461 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009462 free(proxy->conf.lfs_file);
9463 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9464 proxy->conf.lfs_line = proxy->conf.args.line;
9465
9466 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009467 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9468 char *errmsg = NULL;
9469 cur_arg = 1;
9470 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009471 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009472 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009473 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009474 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9475 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9476 free(errmsg);
9477 goto out_err;
9478 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009479 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009480 action_build_list(&http_req_keywords.list, &trash);
9481 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9482 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009483 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009484 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009485 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009486 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009487 }
9488
9489 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9490 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009491 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009492
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009493 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9494 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9495 file, linenum, args[0], errmsg);
9496 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009497 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009498 }
9499 rule->cond = cond;
9500 }
9501 else if (*args[cur_arg]) {
9502 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9503 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9504 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009505 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009506 }
9507
9508 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009509 out_err:
9510 free(rule);
9511 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009512}
9513
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009514/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009515struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009516{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009517 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009518 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009519 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009520 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009521
9522 rule = calloc(1, sizeof(*rule));
9523 if (!rule) {
9524 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9525 goto out_err;
9526 }
9527
9528 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009529 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009530 cur_arg = 1;
9531 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009532 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009533 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009534 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009535 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009536 cur_arg = 1;
9537
9538 if (!*args[cur_arg] ||
9539 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9540 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9541 file, linenum, args[0]);
9542 goto out_err;
9543 }
9544 rule->arg.nice = atoi(args[cur_arg]);
9545 if (rule->arg.nice < -1024)
9546 rule->arg.nice = -1024;
9547 else if (rule->arg.nice > 1024)
9548 rule->arg.nice = 1024;
9549 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009550 } else if (!strcmp(args[0], "set-tos")) {
9551#ifdef IP_TOS
9552 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009553 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009554 cur_arg = 1;
9555
9556 if (!*args[cur_arg] ||
9557 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9558 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9559 file, linenum, args[0]);
9560 goto out_err;
9561 }
9562
9563 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9564 if (err && *err != '\0') {
9565 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9566 file, linenum, err, args[0]);
9567 goto out_err;
9568 }
9569 cur_arg++;
9570#else
9571 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9572 goto out_err;
9573#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009574 } else if (!strcmp(args[0], "set-mark")) {
9575#ifdef SO_MARK
9576 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009577 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009578 cur_arg = 1;
9579
9580 if (!*args[cur_arg] ||
9581 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9582 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9583 file, linenum, args[0]);
9584 goto out_err;
9585 }
9586
9587 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9588 if (err && *err != '\0') {
9589 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9590 file, linenum, err, args[0]);
9591 goto out_err;
9592 }
9593 cur_arg++;
9594 global.last_checks |= LSTCHK_NETADM;
9595#else
9596 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9597 goto out_err;
9598#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009599 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009600 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009601 cur_arg = 1;
9602
9603 if (!*args[cur_arg] ||
9604 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9605 bad_log_level:
9606 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9607 file, linenum, args[0]);
9608 goto out_err;
9609 }
9610 if (strcmp(args[cur_arg], "silent") == 0)
9611 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009612 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009613 goto bad_log_level;
9614 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009615 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009616 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009617 cur_arg = 1;
9618
9619 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9620 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9621 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9622 file, linenum, args[0]);
9623 goto out_err;
9624 }
9625
9626 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9627 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9628 LIST_INIT(&rule->arg.hdr_add.fmt);
9629
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009630 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009631 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009632 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 +01009633 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9634 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9635 file, linenum, args[0], error);
9636 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009637 goto out_err;
9638 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009639 free(proxy->conf.lfs_file);
9640 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9641 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009642 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009643 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009644 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009645 cur_arg = 1;
9646
9647 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009648 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9649 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009650 file, linenum, args[0]);
9651 goto out_err;
9652 }
9653
9654 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9655 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9656 LIST_INIT(&rule->arg.hdr_add.fmt);
9657
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009658 error = NULL;
9659 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9660 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9661 args[cur_arg + 1], error);
9662 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009663 goto out_err;
9664 }
9665
9666 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009667 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009668 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 +01009669 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9670 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9671 file, linenum, args[0], error);
9672 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009673 goto out_err;
9674 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009675
9676 free(proxy->conf.lfs_file);
9677 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9678 proxy->conf.lfs_line = proxy->conf.args.line;
9679 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009680 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009681 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009682 cur_arg = 1;
9683
9684 if (!*args[cur_arg] ||
9685 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9686 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9687 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
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694 proxy->conf.args.ctx = ARGC_HRS;
9695 free(proxy->conf.lfs_file);
9696 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9697 proxy->conf.lfs_line = proxy->conf.args.line;
9698 cur_arg += 1;
9699 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9700 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009701 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009702 /*
9703 * '+ 8' for 'add-acl('
9704 * '- 9' for 'add-acl(' + trailing ')'
9705 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009706 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009707
9708 cur_arg = 1;
9709
9710 if (!*args[cur_arg] ||
9711 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9712 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9713 file, linenum, args[0]);
9714 goto out_err;
9715 }
9716
9717 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009718 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009719 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009720 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009721 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9722 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9723 file, linenum, args[0], error);
9724 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009725 goto out_err;
9726 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009727 free(proxy->conf.lfs_file);
9728 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9729 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009730
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009731 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009732 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9733 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009734 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009735 /*
9736 * '+ 8' for 'del-acl('
9737 * '- 9' for 'del-acl(' + trailing ')'
9738 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009739 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009740
9741 cur_arg = 1;
9742
9743 if (!*args[cur_arg] ||
9744 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9745 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9746 file, linenum, args[0]);
9747 goto out_err;
9748 }
9749
9750 LIST_INIT(&rule->arg.map.key);
9751 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009752 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009753 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009754 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9755 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9756 file, linenum, args[0], error);
9757 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009758 goto out_err;
9759 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009760 free(proxy->conf.lfs_file);
9761 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9762 proxy->conf.lfs_line = proxy->conf.args.line;
9763 cur_arg += 1;
9764 } else if (strncmp(args[0], "del-map", 7) == 0) {
9765 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009766 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009767 /*
9768 * '+ 8' for 'del-map('
9769 * '- 9' for 'del-map(' + trailing ')'
9770 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009771 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009772
9773 cur_arg = 1;
9774
9775 if (!*args[cur_arg] ||
9776 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9777 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9778 file, linenum, args[0]);
9779 goto out_err;
9780 }
9781
9782 LIST_INIT(&rule->arg.map.key);
9783 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009784 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009785 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009786 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9787 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9788 file, linenum, args[0], error);
9789 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009790 goto out_err;
9791 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009792 free(proxy->conf.lfs_file);
9793 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9794 proxy->conf.lfs_line = proxy->conf.args.line;
9795 cur_arg += 1;
9796 } else if (strncmp(args[0], "set-map", 7) == 0) {
9797 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009798 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009799 /*
9800 * '+ 8' for 'set-map('
9801 * '- 9' for 'set-map(' + trailing ')'
9802 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009803 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009804
9805 cur_arg = 1;
9806
9807 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9808 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9809 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9810 file, linenum, args[0]);
9811 goto out_err;
9812 }
9813
9814 LIST_INIT(&rule->arg.map.key);
9815 LIST_INIT(&rule->arg.map.value);
9816
9817 proxy->conf.args.ctx = ARGC_HRS;
9818
9819 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009820 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009821 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009822 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9823 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9824 file, linenum, args[0], error);
9825 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009826 goto out_err;
9827 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009828
9829 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009830 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009831 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 +01009832 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9833 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9834 file, linenum, args[0], error);
9835 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009836 goto out_err;
9837 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009838
9839 free(proxy->conf.lfs_file);
9840 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9841 proxy->conf.lfs_line = proxy->conf.args.line;
9842
9843 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009844 } else if (strcmp(args[0], "redirect") == 0) {
9845 struct redirect_rule *redir;
9846 char *errmsg = NULL;
9847
9848 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9849 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9850 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9851 goto out_err;
9852 }
9853
9854 /* this redirect rule might already contain a parsed condition which
9855 * we'll pass to the http-request rule.
9856 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009857 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009858 rule->arg.redir = redir;
9859 rule->cond = redir->cond;
9860 redir->cond = NULL;
9861 cur_arg = 2;
9862 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009863 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9864 args[0][9] == '\0' && args[0][8] >= '0' &&
9865 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9866 struct sample_expr *expr;
9867 unsigned int where;
9868 char *err = NULL;
9869
9870 cur_arg = 1;
9871 proxy->conf.args.ctx = ARGC_TRK;
9872
9873 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9874 if (!expr) {
9875 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9876 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9877 free(err);
9878 goto out_err;
9879 }
9880
9881 where = 0;
9882 if (proxy->cap & PR_CAP_FE)
9883 where |= SMP_VAL_FE_HRS_HDR;
9884 if (proxy->cap & PR_CAP_BE)
9885 where |= SMP_VAL_BE_HRS_HDR;
9886
9887 if (!(expr->fetch->val & where)) {
9888 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9889 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9890 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9891 args[cur_arg-1], sample_src_names(expr->fetch->use));
9892 free(expr);
9893 goto out_err;
9894 }
9895
9896 if (strcmp(args[cur_arg], "table") == 0) {
9897 cur_arg++;
9898 if (!args[cur_arg]) {
9899 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9900 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9901 free(expr);
9902 goto out_err;
9903 }
9904 /* we copy the table name for now, it will be resolved later */
9905 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9906 cur_arg++;
9907 }
9908 rule->arg.trk_ctr.expr = expr;
9909 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009910 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9911 char *errmsg = NULL;
9912 cur_arg = 1;
9913 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009914 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009915 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009916 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009917 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9918 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9919 free(errmsg);
9920 goto out_err;
9921 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009922 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009923 action_build_list(&http_res_keywords.list, &trash);
9924 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9925 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009926 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009927 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009928 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009929 goto out_err;
9930 }
9931
9932 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9933 struct acl_cond *cond;
9934 char *errmsg = NULL;
9935
9936 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9937 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9938 file, linenum, args[0], errmsg);
9939 free(errmsg);
9940 goto out_err;
9941 }
9942 rule->cond = cond;
9943 }
9944 else if (*args[cur_arg]) {
9945 Alert("parsing [%s:%d]: 'http-response %s' expects"
9946 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9947 file, linenum, args[0], args[cur_arg]);
9948 goto out_err;
9949 }
9950
9951 return rule;
9952 out_err:
9953 free(rule);
9954 return NULL;
9955}
9956
Willy Tarreau4baae242012-12-27 12:00:31 +01009957/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009958 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009959 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9960 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009961 */
9962struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009963 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009964{
9965 struct redirect_rule *rule;
9966 int cur_arg;
9967 int type = REDIRECT_TYPE_NONE;
9968 int code = 302;
9969 const char *destination = NULL;
9970 const char *cookie = NULL;
9971 int cookie_set = 0;
9972 unsigned int flags = REDIRECT_FLAG_NONE;
9973 struct acl_cond *cond = NULL;
9974
9975 cur_arg = 0;
9976 while (*(args[cur_arg])) {
9977 if (strcmp(args[cur_arg], "location") == 0) {
9978 if (!*args[cur_arg + 1])
9979 goto missing_arg;
9980
9981 type = REDIRECT_TYPE_LOCATION;
9982 cur_arg++;
9983 destination = args[cur_arg];
9984 }
9985 else if (strcmp(args[cur_arg], "prefix") == 0) {
9986 if (!*args[cur_arg + 1])
9987 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009988 type = REDIRECT_TYPE_PREFIX;
9989 cur_arg++;
9990 destination = args[cur_arg];
9991 }
9992 else if (strcmp(args[cur_arg], "scheme") == 0) {
9993 if (!*args[cur_arg + 1])
9994 goto missing_arg;
9995
9996 type = REDIRECT_TYPE_SCHEME;
9997 cur_arg++;
9998 destination = args[cur_arg];
9999 }
10000 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10001 if (!*args[cur_arg + 1])
10002 goto missing_arg;
10003
10004 cur_arg++;
10005 cookie = args[cur_arg];
10006 cookie_set = 1;
10007 }
10008 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10009 if (!*args[cur_arg + 1])
10010 goto missing_arg;
10011
10012 cur_arg++;
10013 cookie = args[cur_arg];
10014 cookie_set = 0;
10015 }
10016 else if (strcmp(args[cur_arg], "code") == 0) {
10017 if (!*args[cur_arg + 1])
10018 goto missing_arg;
10019
10020 cur_arg++;
10021 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010022 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010023 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010024 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010025 args[cur_arg - 1], args[cur_arg]);
10026 return NULL;
10027 }
10028 }
10029 else if (!strcmp(args[cur_arg],"drop-query")) {
10030 flags |= REDIRECT_FLAG_DROP_QS;
10031 }
10032 else if (!strcmp(args[cur_arg],"append-slash")) {
10033 flags |= REDIRECT_FLAG_APPEND_SLASH;
10034 }
10035 else if (strcmp(args[cur_arg], "if") == 0 ||
10036 strcmp(args[cur_arg], "unless") == 0) {
10037 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10038 if (!cond) {
10039 memprintf(errmsg, "error in condition: %s", *errmsg);
10040 return NULL;
10041 }
10042 break;
10043 }
10044 else {
10045 memprintf(errmsg,
10046 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10047 args[cur_arg]);
10048 return NULL;
10049 }
10050 cur_arg++;
10051 }
10052
10053 if (type == REDIRECT_TYPE_NONE) {
10054 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10055 return NULL;
10056 }
10057
Willy Tarreaube4653b2015-05-28 15:26:58 +020010058 if (dir && type != REDIRECT_TYPE_LOCATION) {
10059 memprintf(errmsg, "response only supports redirect type 'location'");
10060 return NULL;
10061 }
10062
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010063 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010064 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010065 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010066
10067 if (!use_fmt) {
10068 /* old-style static redirect rule */
10069 rule->rdr_str = strdup(destination);
10070 rule->rdr_len = strlen(destination);
10071 }
10072 else {
10073 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010074
10075 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10076 * if prefix == "/", we don't want to add anything, otherwise it
10077 * makes it hard for the user to configure a self-redirection.
10078 */
Godbachd9722032014-12-18 15:44:58 +080010079 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010080 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010081 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10082 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 +010010083 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10084 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010085 return NULL;
10086 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010087 free(curproxy->conf.lfs_file);
10088 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10089 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010090 }
10091 }
10092
Willy Tarreau4baae242012-12-27 12:00:31 +010010093 if (cookie) {
10094 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10095 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10096 */
10097 rule->cookie_len = strlen(cookie);
10098 if (cookie_set) {
10099 rule->cookie_str = malloc(rule->cookie_len + 10);
10100 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10101 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10102 rule->cookie_len += 9;
10103 } else {
10104 rule->cookie_str = malloc(rule->cookie_len + 21);
10105 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10106 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10107 rule->cookie_len += 20;
10108 }
10109 }
10110 rule->type = type;
10111 rule->code = code;
10112 rule->flags = flags;
10113 LIST_INIT(&rule->list);
10114 return rule;
10115
10116 missing_arg:
10117 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10118 return NULL;
10119}
10120
Willy Tarreau8797c062007-05-07 00:55:35 +020010121/************************************************************************/
10122/* The code below is dedicated to ACL parsing and matching */
10123/************************************************************************/
10124
10125
Willy Tarreau14174bc2012-04-16 14:34:04 +020010126/* This function ensures that the prerequisites for an L7 fetch are ready,
10127 * which means that a request or response is ready. If some data is missing,
10128 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010129 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10130 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010131 *
10132 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010133 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10134 * decide whether or not an HTTP message is present ;
10135 * 0 if the requested data cannot be fetched or if it is certain that
10136 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010137 * 1 if an HTTP message is ready
10138 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010139int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010140 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010141{
Willy Tarreau192252e2015-04-04 01:47:55 +020010142 struct http_txn *txn;
10143 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144
Willy Tarreaube508f12016-03-10 11:47:01 +010010145 /* Note: it is possible that <s> is NULL when called before stream
10146 * initialization (eg: tcp-request connection), so this function is the
10147 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010148 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010149 if (!s)
10150 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010151
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010152 if (!s->txn) {
10153 if (unlikely(!http_alloc_txn(s)))
10154 return 0; /* not enough memory */
10155 http_init_txn(s);
10156 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010157 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010158 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159
10160 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010161 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010162
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010163 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010164 /* If the buffer does not leave enough free space at the end,
10165 * we must first realign it.
10166 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010167 if (s->req.buf->p > s->req.buf->data &&
10168 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10169 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010170
Willy Tarreau14174bc2012-04-16 14:34:04 +020010171 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010172 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010173 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174
10175 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010176 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010177 http_msg_analyzer(msg, &txn->hdr_idx);
10178
10179 /* Still no valid request ? */
10180 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010181 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010182 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010183 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010184 }
10185 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010186 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010187 return 0;
10188 }
10189
10190 /* OK we just got a valid HTTP request. We have some minor
10191 * preparation to perform so that further checks can rely
10192 * on HTTP tests.
10193 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010194
10195 /* If the request was parsed but was too large, we must absolutely
10196 * return an error so that it is not processed. At the moment this
10197 * cannot happen, but if the parsers are to change in the future,
10198 * we want this check to be maintained.
10199 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010200 if (unlikely(s->req.buf->i + s->req.buf->p >
10201 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010202 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010203 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010204 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010205 return 1;
10206 }
10207
Willy Tarreau9b28e032012-10-12 23:49:43 +020010208 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010209 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010210 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010211
Willy Tarreau506d0502013-07-06 13:29:24 +020010212 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10213 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010214 }
10215
Willy Tarreau506d0502013-07-06 13:29:24 +020010216 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010217 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010218 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010219
10220 /* otherwise everything's ready for the request */
10221 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010222 else {
10223 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010224 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10225 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010226 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010227 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010228 }
10229
10230 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010231 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010232 return 1;
10233}
Willy Tarreau8797c062007-05-07 00:55:35 +020010234
Willy Tarreau8797c062007-05-07 00:55:35 +020010235/* 1. Check on METHOD
10236 * We use the pre-parsed method if it is known, and store its number as an
10237 * integer. If it is unknown, we use the pointer and the length.
10238 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010239static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010240{
10241 int len, meth;
10242
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010243 len = strlen(text);
10244 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010245
10246 pattern->val.i = meth;
10247 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010248 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010249 pattern->len = len;
10250 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010251 else {
10252 pattern->ptr.str = NULL;
10253 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010254 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010255 return 1;
10256}
10257
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010258/* This function fetches the method of current HTTP request and stores
10259 * it in the global pattern struct as a chunk. There are two possibilities :
10260 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10261 * in <len> and <ptr> is NULL ;
10262 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10263 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010264 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010265 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010266static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010267smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010268{
10269 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010270 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010271
Willy Tarreau24e32d82012-04-23 23:55:44 +020010272 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010273
Willy Tarreaube508f12016-03-10 11:47:01 +010010274 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010275 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010276 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010277 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010279 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10280 /* ensure the indexes are not affected */
10281 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010282 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010283 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10284 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010285 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010286 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010287 return 1;
10288}
10289
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010290/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010291static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010292{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010293 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010294 struct pattern_list *lst;
10295 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010296
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010297 list_for_each_entry(lst, &expr->patterns, list) {
10298 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010300 /* well-known method */
10301 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010302 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010303 return pattern;
10304 else
10305 continue;
10306 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010307
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010308 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010309 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010310 continue;
10311
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010312 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010313 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10314 (!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 +010010315 return pattern;
10316 }
10317 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318}
10319
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010320static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010321smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010322{
Willy Tarreaube508f12016-03-10 11:47:01 +010010323 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010324 char *ptr;
10325 int len;
10326
Willy Tarreauc0239e02012-04-16 14:42:55 +020010327 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010328
Willy Tarreaube508f12016-03-10 11:47:01 +010010329 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010330 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010331 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010332
10333 while ((len-- > 0) && (*ptr++ != '/'));
10334 if (len <= 0)
10335 return 0;
10336
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010337 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 smp->data.u.str.str = ptr;
10339 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010340
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010341 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010342 return 1;
10343}
10344
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010345static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010346smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010347{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010348 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010349 char *ptr;
10350 int len;
10351
Willy Tarreauc0239e02012-04-16 14:42:55 +020010352 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010353
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010354 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010355 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10356 return 0;
10357
Willy Tarreau8797c062007-05-07 00:55:35 +020010358 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010359 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010360
10361 while ((len-- > 0) && (*ptr++ != '/'));
10362 if (len <= 0)
10363 return 0;
10364
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010365 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010366 smp->data.u.str.str = ptr;
10367 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010368
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010369 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010370 return 1;
10371}
10372
10373/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010374static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010375smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010376{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010377 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010378 char *ptr;
10379 int len;
10380
Willy Tarreauc0239e02012-04-16 14:42:55 +020010381 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010382
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010383 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010384 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10385 return 0;
10386
Willy Tarreau8797c062007-05-07 00:55:35 +020010387 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010388 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010390 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010391 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010392 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010393 return 1;
10394}
10395
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010396static int
10397smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10398{
10399 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10400 return 0;
10401
10402 if (!smp->strm->unique_id) {
10403 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10404 return 0;
10405 smp->strm->unique_id[0] = '\0';
10406 }
10407 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10408 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10409
10410 smp->data.type = SMP_T_STR;
10411 smp->data.u.str.str = smp->strm->unique_id;
10412 smp->flags = SMP_F_CONST;
10413 return 1;
10414}
10415
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020010416/* Returns a string block containing all headers including the
10417 * empty line wich separes headers from the body. This is useful
10418 * form some headers analysis.
10419 */
10420static int
10421smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
10422{
10423 struct http_msg *msg;
10424 struct hdr_idx *idx;
10425 struct http_txn *txn;
10426
10427 CHECK_HTTP_MESSAGE_FIRST();
10428
10429 txn = smp->strm->txn;
10430 idx = &txn->hdr_idx;
10431 msg = &txn->req;
10432
10433 smp->data.type = SMP_T_STR;
10434 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
10435 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
10436 (msg->chn->buf->p[msg->eoh] == '\r');
10437
10438 return 1;
10439}
10440
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020010441/* Returns the header request in a length/value encoded format.
10442 * This is useful for exchanges with the SPOE.
10443 *
10444 * A "length value" is a multibyte code encoding numbers. It uses the
10445 * SPOE format. The encoding is the following:
10446 *
10447 * Each couple "header name" / "header value" is composed
10448 * like this:
10449 * "length value" "header name bytes"
10450 * "length value" "header value bytes"
10451 * When the last header is reached, the header name and the header
10452 * value are empty. Their length are 0
10453 */
10454static int
10455smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
10456{
10457 struct http_msg *msg;
10458 struct chunk *temp;
10459 struct hdr_idx *idx;
10460 const char *cur_ptr, *cur_next, *p;
10461 int old_idx, cur_idx;
10462 struct hdr_idx_elem *cur_hdr;
10463 const char *hn, *hv;
10464 int hnl, hvl;
10465 int ret;
10466 struct http_txn *txn;
10467 char *buf;
10468 char *end;
10469
10470 CHECK_HTTP_MESSAGE_FIRST();
10471
10472 temp = get_trash_chunk();
10473 buf = temp->str;
10474 end = temp->str + temp->size;
10475
10476 txn = smp->strm->txn;
10477 idx = &txn->hdr_idx;
10478 msg = &txn->req;
10479
10480 /* Build array of headers. */
10481 old_idx = 0;
10482 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
10483 while (1) {
10484 cur_idx = idx->v[old_idx].next;
10485 if (!cur_idx)
10486 break;
10487 old_idx = cur_idx;
10488
10489 cur_hdr = &idx->v[cur_idx];
10490 cur_ptr = cur_next;
10491 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
10492
10493 /* Now we have one full header at cur_ptr of len cur_hdr->len,
10494 * and the next header starts at cur_next. We'll check
10495 * this header in the list as well as against the default
10496 * rule.
10497 */
10498
10499 /* look for ': *'. */
10500 hn = cur_ptr;
10501 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
10502 if (p >= cur_ptr+cur_hdr->len)
10503 continue;
10504 hnl = p - hn;
10505 p++;
10506 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
10507 p++;
10508 if (p >= cur_ptr + cur_hdr->len)
10509 continue;
10510 hv = p;
10511 hvl = cur_ptr + cur_hdr->len-p;
10512
10513 /* encode the header name. */
10514 ret = encode_varint(hnl, &buf, end);
10515 if (ret == -1)
10516 return 0;
10517 if (buf + hnl > end)
10518 return 0;
10519 memcpy(buf, hn, hnl);
10520 buf += hnl;
10521
10522 /* encode and copy the value. */
10523 ret = encode_varint(hvl, &buf, end);
10524 if (ret == -1)
10525 return 0;
10526 if (buf + hvl > end)
10527 return 0;
10528 memcpy(buf, hv, hvl);
10529 buf += hvl;
10530 }
10531
10532 /* encode the end of the header list with empty
10533 * header name and header value.
10534 */
10535 ret = encode_varint(0, &buf, end);
10536 if (ret == -1)
10537 return 0;
10538 ret = encode_varint(0, &buf, end);
10539 if (ret == -1)
10540 return 0;
10541
10542 /* Initialise sample data which will be filled. */
10543 smp->data.type = SMP_T_BIN;
10544 smp->data.u.str.str = temp->str;
10545 smp->data.u.str.len = buf - temp->str;
10546 smp->data.u.str.size = temp->size;
10547
10548 return 1;
10549}
10550
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010551/* returns the longest available part of the body. This requires that the body
10552 * has been waited for using http-buffer-request.
10553 */
10554static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010555smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010556{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010557 struct http_msg *msg;
10558 unsigned long len;
10559 unsigned long block1;
10560 char *body;
10561 struct chunk *temp;
10562
10563 CHECK_HTTP_MESSAGE_FIRST();
10564
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010565 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010566 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010567 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010568 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010569
10570 len = http_body_bytes(msg);
10571 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10572
10573 block1 = len;
10574 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10575 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10576
10577 if (block1 == len) {
10578 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010579 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010580 smp->data.u.str.str = body;
10581 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010582 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10583 }
10584 else {
10585 /* buffer is wrapped, we need to defragment it */
10586 temp = get_trash_chunk();
10587 memcpy(temp->str, body, block1);
10588 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010589 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010590 smp->data.u.str.str = temp->str;
10591 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010592 smp->flags = SMP_F_VOL_TEST;
10593 }
10594 return 1;
10595}
10596
10597
10598/* returns the available length of the body. This requires that the body
10599 * has been waited for using http-buffer-request.
10600 */
10601static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010602smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010603{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010604 struct http_msg *msg;
10605
10606 CHECK_HTTP_MESSAGE_FIRST();
10607
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010608 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010609 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010610 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010611 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010612
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010613 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010614 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010615
10616 smp->flags = SMP_F_VOL_TEST;
10617 return 1;
10618}
10619
10620
10621/* returns the advertised length of the body, or the advertised size of the
10622 * chunks available in the buffer. This requires that the body has been waited
10623 * for using http-buffer-request.
10624 */
10625static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010626smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010627{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010628 struct http_msg *msg;
10629
10630 CHECK_HTTP_MESSAGE_FIRST();
10631
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010632 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010633 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010634 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010635 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010636
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010637 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010638 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010639
10640 smp->flags = SMP_F_VOL_TEST;
10641 return 1;
10642}
10643
10644
Willy Tarreau8797c062007-05-07 00:55:35 +020010645/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010646static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010647smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010648{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010649 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010650
Willy Tarreauc0239e02012-04-16 14:42:55 +020010651 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010653 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010654 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010655 smp->data.u.str.len = txn->req.sl.rq.u_l;
10656 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010657 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010658 return 1;
10659}
10660
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010663{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010664 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010665 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010666
Willy Tarreauc0239e02012-04-16 14:42:55 +020010667 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010668
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010669 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010670 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 +020010671 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010672 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010673
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010674 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010675 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010676 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010677 return 1;
10678}
10679
10680static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010681smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010682{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010683 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010684 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010685
Willy Tarreauc0239e02012-04-16 14:42:55 +020010686 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010687
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010688 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010689 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 +020010690 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10691 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010692
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010693 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010694 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010695 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010696 return 1;
10697}
10698
Willy Tarreau185b5c42012-04-26 15:11:51 +020010699/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10700 * Accepts an optional argument of type string containing the header field name,
10701 * and an optional argument of type signed or unsigned integer to request an
10702 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010703 * headers are considered from the first one. It does not stop on commas and
10704 * returns full lines instead (useful for User-Agent or Date for example).
10705 */
10706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010708{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010709 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010710 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010711 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010712 int occ = 0;
10713 const char *name_str = NULL;
10714 int name_len = 0;
10715
10716 if (!ctx) {
10717 /* first call */
10718 ctx = &static_hdr_ctx;
10719 ctx->idx = 0;
10720 smp->ctx.a[0] = ctx;
10721 }
10722
10723 if (args) {
10724 if (args[0].type != ARGT_STR)
10725 return 0;
10726 name_str = args[0].data.str.str;
10727 name_len = args[0].data.str.len;
10728
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010729 if (args[1].type == ARGT_SINT)
10730 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010731 }
10732
10733 CHECK_HTTP_MESSAGE_FIRST();
10734
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010735 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010736 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 +020010737
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010738 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10739 /* search for header from the beginning */
10740 ctx->idx = 0;
10741
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010742 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010743 /* no explicit occurrence and single fetch => last header by default */
10744 occ = -1;
10745
10746 if (!occ)
10747 /* prepare to report multiple occurrences for ACL fetches */
10748 smp->flags |= SMP_F_NOT_LAST;
10749
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010750 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010751 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010752 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 +020010753 return 1;
10754
10755 smp->flags &= ~SMP_F_NOT_LAST;
10756 return 0;
10757}
10758
10759/* 6. Check on HTTP header count. The number of occurrences is returned.
10760 * Accepts exactly 1 argument of type string. It does not stop on commas and
10761 * returns full lines instead (useful for User-Agent or Date for example).
10762 */
10763static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010764smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010765{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010766 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010767 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010768 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010769 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010770 const char *name = NULL;
10771 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010772
Willy Tarreau601a4d12015-04-01 19:16:09 +020010773 if (args && args->type == ARGT_STR) {
10774 name = args->data.str.str;
10775 len = args->data.str.len;
10776 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010777
10778 CHECK_HTTP_MESSAGE_FIRST();
10779
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010780 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010781 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 +020010782
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010783 ctx.idx = 0;
10784 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010785 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010786 cnt++;
10787
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010788 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010789 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010790 smp->flags = SMP_F_VOL_HDR;
10791 return 1;
10792}
10793
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010795smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010796{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010797 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010798 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010799 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010800 struct chunk *temp;
10801 char del = ',';
10802
10803 if (args && args->type == ARGT_STR)
10804 del = *args[0].data.str.str;
10805
10806 CHECK_HTTP_MESSAGE_FIRST();
10807
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010808 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010809 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 +020010810
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010811 temp = get_trash_chunk();
10812
10813 ctx.idx = 0;
10814 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10815 if (temp->len)
10816 temp->str[temp->len++] = del;
10817 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10818 temp->len += ctx.del;
10819 }
10820
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010821 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010822 smp->data.u.str.str = temp->str;
10823 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010824 smp->flags = SMP_F_VOL_HDR;
10825 return 1;
10826}
10827
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010828/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10829 * Accepts an optional argument of type string containing the header field name,
10830 * and an optional argument of type signed or unsigned integer to request an
10831 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010832 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010833 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010834static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010835smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010836{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010837 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010838 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010839 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010840 int occ = 0;
10841 const char *name_str = NULL;
10842 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010843
Willy Tarreaua890d072013-04-02 12:01:06 +020010844 if (!ctx) {
10845 /* first call */
10846 ctx = &static_hdr_ctx;
10847 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010848 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010849 }
10850
Willy Tarreau185b5c42012-04-26 15:11:51 +020010851 if (args) {
10852 if (args[0].type != ARGT_STR)
10853 return 0;
10854 name_str = args[0].data.str.str;
10855 name_len = args[0].data.str.len;
10856
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010857 if (args[1].type == ARGT_SINT)
10858 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010859 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010860
Willy Tarreaue333ec92012-04-16 16:26:40 +020010861 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010862
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010863 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010864 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 +020010865
Willy Tarreau185b5c42012-04-26 15:11:51 +020010866 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010867 /* search for header from the beginning */
10868 ctx->idx = 0;
10869
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010870 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010871 /* no explicit occurrence and single fetch => last header by default */
10872 occ = -1;
10873
10874 if (!occ)
10875 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010876 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010877
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010878 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010879 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010880 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 +020010881 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010882
Willy Tarreau37406352012-04-23 16:16:37 +020010883 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010884 return 0;
10885}
10886
Willy Tarreauc11416f2007-06-17 16:58:38 +020010887/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010888 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010889 */
10890static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010891smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010892{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010893 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010894 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010895 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010896 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010897 const char *name = NULL;
10898 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010899
Willy Tarreau601a4d12015-04-01 19:16:09 +020010900 if (args && args->type == ARGT_STR) {
10901 name = args->data.str.str;
10902 len = args->data.str.len;
10903 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010904
Willy Tarreaue333ec92012-04-16 16:26:40 +020010905 CHECK_HTTP_MESSAGE_FIRST();
10906
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010907 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010908 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 +020010909
Willy Tarreau33a7e692007-06-10 19:45:56 +020010910 ctx.idx = 0;
10911 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010912 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010913 cnt++;
10914
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010915 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010916 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010917 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010918 return 1;
10919}
10920
Willy Tarreau185b5c42012-04-26 15:11:51 +020010921/* Fetch an HTTP header's integer value. The integer value is returned. It
10922 * takes a mandatory argument of type string and an optional one of type int
10923 * to designate a specific occurrence. It returns an unsigned integer, which
10924 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010925 */
10926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010927smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010928{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010929 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010930
Willy Tarreauf853c462012-04-23 18:53:56 +020010931 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010932 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010933 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010934 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010935
Willy Tarreaud53e2422012-04-16 17:21:11 +020010936 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010937}
10938
Cyril Bonté69fa9922012-10-25 00:01:06 +020010939/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10940 * and an optional one of type int to designate a specific occurrence.
10941 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010942 */
10943static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010944smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010945{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010946 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010947
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010948 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010949 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010950 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010951 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010952 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010953 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010954 if (smp->data.u.str.len < temp->size - 1) {
10955 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10956 temp->str[smp->data.u.str.len] = '\0';
10957 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010958 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010959 break;
10960 }
10961 }
10962 }
10963
Willy Tarreaud53e2422012-04-16 17:21:11 +020010964 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010965 if (!(smp->flags & SMP_F_NOT_LAST))
10966 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010967 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010968 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010969}
10970
Willy Tarreau737b0c12007-06-10 21:28:46 +020010971/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10972 * the first '/' after the possible hostname, and ends before the possible '?'.
10973 */
10974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010975smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010976{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010977 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010978 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010979
Willy Tarreauc0239e02012-04-16 14:42:55 +020010980 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010981
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010982 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010983 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010984 ptr = http_get_path(txn);
10985 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010986 return 0;
10987
10988 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010989 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010990 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010991
10992 while (ptr < end && *ptr != '?')
10993 ptr++;
10994
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010995 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010996 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010997 return 1;
10998}
10999
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011000/* This produces a concatenation of the first occurrence of the Host header
11001 * followed by the path component if it begins with a slash ('/'). This means
11002 * that '*' will not be added, resulting in exactly the first Host entry.
11003 * If no Host header is found, then the path is returned as-is. The returned
11004 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010011005 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011006 */
11007static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011008smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011009{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011010 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011011 char *ptr, *end, *beg;
11012 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011013 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011014
11015 CHECK_HTTP_MESSAGE_FIRST();
11016
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011017 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011018 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011019 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011020 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011021
11022 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020011023 temp = get_trash_chunk();
11024 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011025 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011026 smp->data.u.str.str = temp->str;
11027 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011028
11029 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011030 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011031 beg = http_get_path(txn);
11032 if (!beg)
11033 beg = end;
11034
11035 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11036
11037 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011038 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
11039 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011040 }
11041
11042 smp->flags = SMP_F_VOL_1ST;
11043 return 1;
11044}
11045
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011046/* This produces a 32-bit hash of the concatenation of the first occurrence of
11047 * the Host header followed by the path component if it begins with a slash ('/').
11048 * This means that '*' will not be added, resulting in exactly the first Host
11049 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011050 * is hashed using the path hash followed by a full avalanche hash and provides a
11051 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011052 * high-traffic sites without having to store whole paths.
11053 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020011054int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011055smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011056{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011057 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011058 struct hdr_ctx ctx;
11059 unsigned int hash = 0;
11060 char *ptr, *beg, *end;
11061 int len;
11062
11063 CHECK_HTTP_MESSAGE_FIRST();
11064
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011065 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011066 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011067 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011068 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11069 ptr = ctx.line + ctx.val;
11070 len = ctx.vlen;
11071 while (len--)
11072 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11073 }
11074
11075 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011076 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011077 beg = http_get_path(txn);
11078 if (!beg)
11079 beg = end;
11080
11081 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
11082
11083 if (beg < ptr && *beg == '/') {
11084 while (beg < ptr)
11085 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11086 }
11087 hash = full_hash(hash);
11088
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011089 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011090 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010011091 smp->flags = SMP_F_VOL_1ST;
11092 return 1;
11093}
11094
Willy Tarreau4a550602012-12-09 14:53:32 +010011095/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011096 * path as returned by smp_fetch_base32(). The idea is to have per-source and
11097 * per-path counters. The result is a binary block from 8 to 20 bytes depending
11098 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010011099 * that in environments where IPv6 is insignificant, truncating the output to
11100 * 8 bytes would still work.
11101 */
11102static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011103smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010011104{
Willy Tarreau47ca5452012-12-23 20:22:19 +010011105 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011106 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011107
11108 if (!cli_conn)
11109 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010011110
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011111 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010011112 return 0;
11113
Willy Tarreau47ca5452012-12-23 20:22:19 +010011114 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011115 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011116 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011117
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011118 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011119 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011120 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011121 temp->len += 4;
11122 break;
11123 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011124 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011125 temp->len += 16;
11126 break;
11127 default:
11128 return 0;
11129 }
11130
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011131 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011132 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011133 return 1;
11134}
11135
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011136/* Extracts the query string, which comes after the question mark '?'. If no
11137 * question mark is found, nothing is returned. Otherwise it returns a sample
11138 * of type string carrying the whole query string.
11139 */
11140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011141smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011142{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011143 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011144 char *ptr, *end;
11145
11146 CHECK_HTTP_MESSAGE_FIRST();
11147
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011148 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011149 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11150 end = ptr + txn->req.sl.rq.u_l;
11151
11152 /* look up the '?' */
11153 do {
11154 if (ptr == end)
11155 return 0;
11156 } while (*ptr++ != '?');
11157
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011158 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011159 smp->data.u.str.str = ptr;
11160 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011161 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11162 return 1;
11163}
11164
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011165static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011166smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011167{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011168 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11169 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11170 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011171
Willy Tarreau24e32d82012-04-23 23:55:44 +020011172 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011173
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011174 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011175 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011176 return 1;
11177}
11178
Willy Tarreau7f18e522010-10-22 20:04:13 +020011179/* return a valid test if the current request is the first one on the connection */
11180static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011181smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011182{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011183 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011184 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011185 return 1;
11186}
11187
Willy Tarreau34db1082012-04-19 17:16:54 +020011188/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011189static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011190smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011191{
11192
Willy Tarreau24e32d82012-04-23 23:55:44 +020011193 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011194 return 0;
11195
Willy Tarreauc0239e02012-04-16 14:42:55 +020011196 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011197
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011198 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011199 return 0;
11200
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011201 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011202 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011203 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011204 return 1;
11205}
Willy Tarreau8797c062007-05-07 00:55:35 +020011206
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011207/* Accepts exactly 1 argument of type userlist */
11208static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011209smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011210{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011211 if (!args || args->type != ARGT_USR)
11212 return 0;
11213
11214 CHECK_HTTP_MESSAGE_FIRST();
11215
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011216 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011217 return 0;
11218
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011219 /* if the user does not belong to the userlist or has a wrong password,
11220 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011221 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011222 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011223 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11224 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011225 return 0;
11226
11227 /* pat_match_auth() will need the user list */
11228 smp->ctx.a[0] = args->data.usr;
11229
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011230 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011231 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011232 smp->data.u.str.str = smp->strm->txn->auth.user;
11233 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011234
11235 return 1;
11236}
11237
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011238/* Try to find the next occurrence of a cookie name in a cookie header value.
11239 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11240 * the cookie value is returned into *value and *value_l, and the function
11241 * returns a pointer to the next pointer to search from if the value was found.
11242 * Otherwise if the cookie was not found, NULL is returned and neither value
11243 * nor value_l are touched. The input <hdr> string should first point to the
11244 * header's value, and the <hdr_end> pointer must point to the first character
11245 * not part of the value. <list> must be non-zero if value may represent a list
11246 * of values (cookie headers). This makes it faster to abort parsing when no
11247 * list is expected.
11248 */
David Carlier4686f792015-09-25 14:10:50 +010011249char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011250extract_cookie_value(char *hdr, const char *hdr_end,
11251 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011252 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011253{
11254 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11255 char *next;
11256
11257 /* we search at least a cookie name followed by an equal, and more
11258 * generally something like this :
11259 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11260 */
11261 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11262 /* Iterate through all cookies on this line */
11263
Willy Tarreau2235b262016-11-05 15:50:20 +010011264 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011265 att_beg++;
11266
11267 /* find att_end : this is the first character after the last non
11268 * space before the equal. It may be equal to hdr_end.
11269 */
11270 equal = att_end = att_beg;
11271
11272 while (equal < hdr_end) {
11273 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11274 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011275 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011276 continue;
11277 att_end = equal;
11278 }
11279
11280 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11281 * is between <att_beg> and <equal>, both may be identical.
11282 */
11283
11284 /* look for end of cookie if there is an equal sign */
11285 if (equal < hdr_end && *equal == '=') {
11286 /* look for the beginning of the value */
11287 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011288 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011289 val_beg++;
11290
11291 /* find the end of the value, respecting quotes */
11292 next = find_cookie_value_end(val_beg, hdr_end);
11293
11294 /* make val_end point to the first white space or delimitor after the value */
11295 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011296 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011297 val_end--;
11298 } else {
11299 val_beg = val_end = next = equal;
11300 }
11301
11302 /* We have nothing to do with attributes beginning with '$'. However,
11303 * they will automatically be removed if a header before them is removed,
11304 * since they're supposed to be linked together.
11305 */
11306 if (*att_beg == '$')
11307 continue;
11308
11309 /* Ignore cookies with no equal sign */
11310 if (equal == next)
11311 continue;
11312
11313 /* Now we have the cookie name between att_beg and att_end, and
11314 * its value between val_beg and val_end.
11315 */
11316
11317 if (att_end - att_beg == cookie_name_l &&
11318 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11319 /* let's return this value and indicate where to go on from */
11320 *value = val_beg;
11321 *value_l = val_end - val_beg;
11322 return next + 1;
11323 }
11324
11325 /* Set-Cookie headers only have the name in the first attr=value part */
11326 if (!list)
11327 break;
11328 }
11329
11330 return NULL;
11331}
11332
William Lallemanda43ba4e2014-01-28 18:14:25 +010011333/* Fetch a captured HTTP request header. The index is the position of
11334 * the "capture" option in the configuration file
11335 */
11336static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011337smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011338{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011339 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011340 int idx;
11341
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011342 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011343 return 0;
11344
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011345 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011346
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011347 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 +010011348 return 0;
11349
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011350 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011351 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011352 smp->data.u.str.str = smp->strm->req_cap[idx];
11353 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011354
11355 return 1;
11356}
11357
11358/* Fetch a captured HTTP response header. The index is the position of
11359 * the "capture" option in the configuration file
11360 */
11361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011362smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011363{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011364 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011365 int idx;
11366
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011367 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011368 return 0;
11369
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011370 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011371
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011372 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 +010011373 return 0;
11374
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011375 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011376 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011377 smp->data.u.str.str = smp->strm->res_cap[idx];
11378 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011379
11380 return 1;
11381}
11382
William Lallemand65ad6e12014-01-31 15:08:02 +010011383/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011385smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011386{
11387 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011388 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011389 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011390
Willy Tarreau15e91e12015-04-04 00:52:09 +020011391 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011392 return 0;
11393
William Lallemand96a77852014-02-05 00:30:02 +010011394 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011395
William Lallemand96a77852014-02-05 00:30:02 +010011396 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11397 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011398
William Lallemand96a77852014-02-05 00:30:02 +010011399 temp = get_trash_chunk();
11400 temp->str = txn->uri;
11401 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011403 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011404 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011405
11406 return 1;
11407
11408}
11409
11410/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11411static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011412smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011413{
11414 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011415 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011416 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011417
Willy Tarreau15e91e12015-04-04 00:52:09 +020011418 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011419 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011420
William Lallemand65ad6e12014-01-31 15:08:02 +010011421 ptr = txn->uri;
11422
11423 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11424 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011425
William Lallemand65ad6e12014-01-31 15:08:02 +010011426 if (!*ptr)
11427 return 0;
11428
11429 ptr++; /* skip the space */
11430
11431 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011432 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011433 if (!ptr)
11434 return 0;
11435 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11436 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011437
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011438 smp->data.u.str = *temp;
11439 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011440 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011441 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011442
11443 return 1;
11444}
11445
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011446/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11447 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11448 */
11449static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011450smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011451{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011452 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011453
Willy Tarreau15e91e12015-04-04 00:52:09 +020011454 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011455 return 0;
11456
11457 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011458 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011459 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011460 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011461
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011462 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011463 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011464 smp->flags = SMP_F_CONST;
11465 return 1;
11466
11467}
11468
11469/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11470 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11471 */
11472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011473smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011474{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011475 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011476
Willy Tarreau15e91e12015-04-04 00:52:09 +020011477 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011478 return 0;
11479
11480 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011481 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011482 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011483 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011484
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011485 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011486 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011487 smp->flags = SMP_F_CONST;
11488 return 1;
11489
11490}
11491
William Lallemand65ad6e12014-01-31 15:08:02 +010011492
Willy Tarreaue333ec92012-04-16 16:26:40 +020011493/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011494 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011495 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011496 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011497 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011498 * Accepts exactly 1 argument of type string. If the input options indicate
11499 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011500 * The returned sample is of type CSTR. Can be used to parse cookies in other
11501 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011502 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011503int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011504{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011505 struct http_txn *txn;
11506 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011507 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011508 const struct http_msg *msg;
11509 const char *hdr_name;
11510 int hdr_name_len;
11511 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011512 int occ = 0;
11513 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011514
Willy Tarreau24e32d82012-04-23 23:55:44 +020011515 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011516 return 0;
11517
Willy Tarreaua890d072013-04-02 12:01:06 +020011518 if (!ctx) {
11519 /* first call */
11520 ctx = &static_hdr_ctx;
11521 ctx->idx = 0;
11522 smp->ctx.a[2] = ctx;
11523 }
11524
Willy Tarreaue333ec92012-04-16 16:26:40 +020011525 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011526
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011527 txn = smp->strm->txn;
11528 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011529
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011530 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011531 msg = &txn->req;
11532 hdr_name = "Cookie";
11533 hdr_name_len = 6;
11534 } else {
11535 msg = &txn->rsp;
11536 hdr_name = "Set-Cookie";
11537 hdr_name_len = 10;
11538 }
11539
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011540 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011541 /* no explicit occurrence and single fetch => last cookie by default */
11542 occ = -1;
11543
11544 /* OK so basically here, either we want only one value and it's the
11545 * last one, or we want to iterate over all of them and we fetch the
11546 * next one.
11547 */
11548
Willy Tarreau9b28e032012-10-12 23:49:43 +020011549 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011550 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011551 /* search for the header from the beginning, we must first initialize
11552 * the search parameters.
11553 */
Willy Tarreau37406352012-04-23 16:16:37 +020011554 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011555 ctx->idx = 0;
11556 }
11557
Willy Tarreau28376d62012-04-26 21:26:10 +020011558 smp->flags |= SMP_F_VOL_HDR;
11559
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011560 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011561 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11562 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011563 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11564 goto out;
11565
Willy Tarreau24e32d82012-04-23 23:55:44 +020011566 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011567 continue;
11568
Willy Tarreau37406352012-04-23 16:16:37 +020011569 smp->ctx.a[0] = ctx->line + ctx->val;
11570 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011571 }
11572
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011573 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011574 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011575 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011576 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011577 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011578 &smp->data.u.str.str,
11579 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011580 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011581 found = 1;
11582 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011583 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011584 smp->flags |= SMP_F_NOT_LAST;
11585 return 1;
11586 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011587 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011588 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011589 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011590 /* all cookie headers and values were scanned. If we're looking for the
11591 * last occurrence, we may return it now.
11592 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011593 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011594 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011595 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011596}
11597
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011598/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011599 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011600 * multiple cookies may be parsed on the same line. The returned sample is of
11601 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011602 */
11603static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011604smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011605{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011606 struct http_txn *txn;
11607 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011608 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011609 const struct http_msg *msg;
11610 const char *hdr_name;
11611 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011612 int cnt;
11613 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011614 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011615
Willy Tarreau24e32d82012-04-23 23:55:44 +020011616 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011617 return 0;
11618
Willy Tarreaue333ec92012-04-16 16:26:40 +020011619 CHECK_HTTP_MESSAGE_FIRST();
11620
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011621 txn = smp->strm->txn;
11622 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011623
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011624 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011625 msg = &txn->req;
11626 hdr_name = "Cookie";
11627 hdr_name_len = 6;
11628 } else {
11629 msg = &txn->rsp;
11630 hdr_name = "Set-Cookie";
11631 hdr_name_len = 10;
11632 }
11633
Willy Tarreau9b28e032012-10-12 23:49:43 +020011634 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011635 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011636 ctx.idx = 0;
11637 cnt = 0;
11638
11639 while (1) {
11640 /* Note: val_beg == NULL every time we need to fetch a new header */
11641 if (!val_beg) {
11642 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11643 break;
11644
Willy Tarreau24e32d82012-04-23 23:55:44 +020011645 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011646 continue;
11647
11648 val_beg = ctx.line + ctx.val;
11649 val_end = val_beg + ctx.vlen;
11650 }
11651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011652 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011653 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011654 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011655 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011656 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011657 &smp->data.u.str.str,
11658 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011659 cnt++;
11660 }
11661 }
11662
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011663 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011664 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011665 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011666 return 1;
11667}
11668
Willy Tarreau51539362012-05-08 12:46:28 +020011669/* Fetch an cookie's integer value. The integer value is returned. It
11670 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11671 */
11672static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011673smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011674{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011675 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011676
11677 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011678 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011679 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011680 }
11681
11682 return ret;
11683}
11684
Willy Tarreau8797c062007-05-07 00:55:35 +020011685/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011686/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011687/************************************************************************/
11688
David Cournapeau16023ee2010-12-23 20:55:41 +090011689/*
11690 * Given a path string and its length, find the position of beginning of the
11691 * query string. Returns NULL if no query string is found in the path.
11692 *
11693 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11694 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011695 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011696 */
bedis4c75cca2012-10-05 08:38:24 +020011697static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011698{
11699 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011700
bedis4c75cca2012-10-05 08:38:24 +020011701 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011702 return p ? p + 1 : NULL;
11703}
11704
bedis4c75cca2012-10-05 08:38:24 +020011705static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011706{
bedis4c75cca2012-10-05 08:38:24 +020011707 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011708}
11709
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011710/* after increasing a pointer value, it can exceed the first buffer
11711 * size. This function transform the value of <ptr> according with
11712 * the expected position. <chunks> is an array of the one or two
11713 * avalaible chunks. The first value is the start of the first chunk,
11714 * the second value if the end+1 of the first chunks. The third value
11715 * is NULL or the start of the second chunk and the fourth value is
11716 * the end+1 of the second chunk. The function returns 1 if does a
11717 * wrap, else returns 0.
11718 */
11719static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11720{
11721 if (*ptr < chunks[1])
11722 return 0;
11723 if (!chunks[2])
11724 return 0;
11725 *ptr = chunks[2] + ( *ptr - chunks[1] );
11726 return 1;
11727}
11728
David Cournapeau16023ee2010-12-23 20:55:41 +090011729/*
11730 * Given a url parameter, find the starting position of the first occurence,
11731 * or NULL if the parameter is not found.
11732 *
11733 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11734 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011735 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011736 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 * or the second chunk. The caller must be check the position before using the
11738 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011739 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011740static const char *
11741find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011742 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011743 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011744{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 const char *pos, *last, *equal;
11746 const char **bufs = chunks;
11747 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011748
David Cournapeau16023ee2010-12-23 20:55:41 +090011749
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011750 pos = bufs[0];
11751 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011752 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011753 /* Check the equal. */
11754 equal = pos + url_param_name_l;
11755 if (fix_pointer_if_wrap(chunks, &equal)) {
11756 if (equal >= chunks[3])
11757 return NULL;
11758 } else {
11759 if (equal >= chunks[1])
11760 return NULL;
11761 }
11762 if (*equal == '=') {
11763 if (pos + url_param_name_l > last) {
11764 /* process wrap case, we detect a wrap. In this case, the
11765 * comparison is performed in two parts.
11766 */
11767
11768 /* This is the end, we dont have any other chunk. */
11769 if (bufs != chunks || !bufs[2])
11770 return NULL;
11771
11772 /* Compute the length of each part of the comparison. */
11773 l1 = last - pos;
11774 l2 = url_param_name_l - l1;
11775
11776 /* The second buffer is too short to contain the compared string. */
11777 if (bufs[2] + l2 > bufs[3])
11778 return NULL;
11779
11780 if (memcmp(pos, url_param_name, l1) == 0 &&
11781 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11782 return pos;
11783
11784 /* Perform wrapping and jump the string who fail the comparison. */
11785 bufs += 2;
11786 pos = bufs[0] + l2;
11787 last = bufs[1];
11788
11789 } else {
11790 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011791 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11792 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011793 pos += url_param_name_l + 1;
11794 if (fix_pointer_if_wrap(chunks, &pos))
11795 last = bufs[2];
11796 }
11797 }
11798
11799 while (1) {
11800 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011801 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011802 pos++;
11803 if (pos < last)
11804 break;
11805 /* process buffer wrapping. */
11806 if (bufs != chunks || !bufs[2])
11807 return NULL;
11808 bufs += 2;
11809 pos = bufs[0];
11810 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011811 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011812 pos++;
11813 }
11814 return NULL;
11815}
11816
11817/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011818 * Given a url parameter name and a query string, find the next value.
11819 * An empty url_param_name matches the first available parameter.
11820 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11821 * respectively provide a pointer to the value and its end.
11822 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011823 */
11824static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011826 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011827 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011828{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011829 const char *arg_start, *qs_end;
11830 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011831
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011832 arg_start = chunks[0];
11833 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011834 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011835 /* Looks for an argument name. */
11836 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011837 url_param_name, url_param_name_l,
11838 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011839 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011840 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011841 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011842 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011843 if (!arg_start)
11844 return 0;
11845
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011846 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011847 while (1) {
11848 /* looks for the first argument. */
11849 value_start = memchr(arg_start, '=', qs_end - arg_start);
11850 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011851 /* Check for wrapping. */
11852 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011853 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011854 chunks[2]) {
11855 arg_start = chunks[2];
11856 qs_end = chunks[3];
11857 continue;
11858 }
11859 return 0;
11860 }
11861 break;
11862 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011863 value_start++;
11864 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011865 else {
11866 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011867 value_start = arg_start + url_param_name_l + 1;
11868
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011869 /* Check for pointer wrapping. */
11870 if (fix_pointer_if_wrap(chunks, &value_start)) {
11871 /* Update the end pointer. */
11872 qs_end = chunks[3];
11873
11874 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011875 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011876 return 0;
11877 }
11878 }
11879
David Cournapeau16023ee2010-12-23 20:55:41 +090011880 value_end = value_start;
11881
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011882 while (1) {
11883 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11884 value_end++;
11885 if (value_end < qs_end)
11886 break;
11887 /* process buffer wrapping. */
11888 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011889 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011890 chunks[2]) {
11891 value_end = chunks[2];
11892 qs_end = chunks[3];
11893 continue;
11894 }
11895 break;
11896 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011897
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011898 *vstart = value_start;
11899 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011900 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011901}
11902
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011903/* This scans a URL-encoded query string. It takes an optionally wrapping
11904 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11905 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11906 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011907 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011908static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011909smp_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 +090011910{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011911 const char *vstart, *vend;
11912 struct chunk *temp;
11913 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011914
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011915 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011916 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011917 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011918 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011919 return 0;
11920
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011921 /* Create sample. If the value is contiguous, return the pointer as CONST,
11922 * if the value is wrapped, copy-it in a buffer.
11923 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011924 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011925 if (chunks[2] &&
11926 vstart >= chunks[0] && vstart <= chunks[1] &&
11927 vend >= chunks[2] && vend <= chunks[3]) {
11928 /* Wrapped case. */
11929 temp = get_trash_chunk();
11930 memcpy(temp->str, vstart, chunks[1] - vstart);
11931 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011932 smp->data.u.str.str = temp->str;
11933 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011934 } else {
11935 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011936 smp->data.u.str.str = (char *)vstart;
11937 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011938 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11939 }
11940
11941 /* Update context, check wrapping. */
11942 chunks[0] = vend;
11943 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11944 chunks[1] = chunks[3];
11945 chunks[2] = NULL;
11946 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011947
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011948 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011949 smp->flags |= SMP_F_NOT_LAST;
11950
David Cournapeau16023ee2010-12-23 20:55:41 +090011951 return 1;
11952}
11953
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011954/* This function iterates over each parameter of the query string. It uses
11955 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011956 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11957 * An optional parameter name is passed in args[0], otherwise any parameter is
11958 * considered. It supports an optional delimiter argument for the beginning of
11959 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011960 */
11961static int
11962smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11963{
11964 struct http_msg *msg;
11965 char delim = '?';
11966 const char *name;
11967 int name_len;
11968
Dragan Dosen26f77e52015-05-25 10:02:11 +020011969 if (!args ||
11970 (args[0].type && args[0].type != ARGT_STR) ||
11971 (args[1].type && args[1].type != ARGT_STR))
11972 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011973
Dragan Dosen26f77e52015-05-25 10:02:11 +020011974 name = "";
11975 name_len = 0;
11976 if (args->type == ARGT_STR) {
11977 name = args->data.str.str;
11978 name_len = args->data.str.len;
11979 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011980
Dragan Dosen26f77e52015-05-25 10:02:11 +020011981 if (args[1].type)
11982 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011983
Dragan Dosen26f77e52015-05-25 10:02:11 +020011984 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011985 CHECK_HTTP_MESSAGE_FIRST();
11986
11987 msg = &smp->strm->txn->req;
11988
11989 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11990 msg->sl.rq.u_l, delim);
11991 if (!smp->ctx.a[0])
11992 return 0;
11993
11994 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011995
11996 /* Assume that the context is filled with NULL pointer
11997 * before the first call.
11998 * smp->ctx.a[2] = NULL;
11999 * smp->ctx.a[3] = NULL;
12000 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020012001 }
12002
12003 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
12004}
12005
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012006/* This function iterates over each parameter of the body. This requires
12007 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012008 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
12009 * contigous part of the body, and optionally ctx->a[2..3] to reference the
12010 * optional second part if the body wraps at the end of the buffer. An optional
12011 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012012 */
12013static int
12014smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
12015{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012016 struct http_msg *msg;
12017 unsigned long len;
12018 unsigned long block1;
12019 char *body;
12020 const char *name;
12021 int name_len;
12022
12023 if (!args || (args[0].type && args[0].type != ARGT_STR))
12024 return 0;
12025
12026 name = "";
12027 name_len = 0;
12028 if (args[0].type == ARGT_STR) {
12029 name = args[0].data.str.str;
12030 name_len = args[0].data.str.len;
12031 }
12032
12033 if (!smp->ctx.a[0]) { // first call, find the query string
12034 CHECK_HTTP_MESSAGE_FIRST();
12035
12036 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010012037 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012038 else
Willy Tarreaube508f12016-03-10 11:47:01 +010012039 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012040
12041 len = http_body_bytes(msg);
12042 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
12043
12044 block1 = len;
12045 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
12046 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
12047
12048 if (block1 == len) {
12049 /* buffer is not wrapped (or empty) */
12050 smp->ctx.a[0] = body;
12051 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012052
12053 /* Assume that the context is filled with NULL pointer
12054 * before the first call.
12055 * smp->ctx.a[2] = NULL;
12056 * smp->ctx.a[3] = NULL;
12057 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012058 }
12059 else {
12060 /* buffer is wrapped, we need to defragment it */
12061 smp->ctx.a[0] = body;
12062 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020012063 smp->ctx.a[2] = msg->chn->buf->data;
12064 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012065 }
12066 }
12067 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
12068}
12069
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012070/* Return the signed integer value for the specified url parameter (see url_param
12071 * above).
12072 */
12073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012074smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012075{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012076 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012077
12078 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012079 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012080 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020012081 }
12082
12083 return ret;
12084}
12085
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012086/* This produces a 32-bit hash of the concatenation of the first occurrence of
12087 * the Host header followed by the path component if it begins with a slash ('/').
12088 * This means that '*' will not be added, resulting in exactly the first Host
12089 * entry. If no Host header is found, then the path is used. The resulting value
12090 * is hashed using the url hash followed by a full avalanche hash and provides a
12091 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
12092 * high-traffic sites without having to store whole paths.
12093 * this differs from the base32 functions in that it includes the url parameters
12094 * as well as the path
12095 */
12096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012097smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012098{
Willy Tarreau15e91e12015-04-04 00:52:09 +020012099 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012100 struct hdr_ctx ctx;
12101 unsigned int hash = 0;
12102 char *ptr, *beg, *end;
12103 int len;
12104
12105 CHECK_HTTP_MESSAGE_FIRST();
12106
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012107 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012108 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020012109 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012110 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
12111 ptr = ctx.line + ctx.val;
12112 len = ctx.vlen;
12113 while (len--)
12114 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12115 }
12116
12117 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012118 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 +000012119 beg = http_get_path(txn);
12120 if (!beg)
12121 beg = end;
12122
12123 for (ptr = beg; ptr < end ; ptr++);
12124
12125 if (beg < ptr && *beg == '/') {
12126 while (beg < ptr)
12127 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12128 }
12129 hash = full_hash(hash);
12130
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012131 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012132 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012133 smp->flags = SMP_F_VOL_1ST;
12134 return 1;
12135}
12136
12137/* This concatenates the source address with the 32-bit hash of the Host and
12138 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12139 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12140 * on the source address length. The URL hash is stored before the address so
12141 * that in environments where IPv6 is insignificant, truncating the output to
12142 * 8 bytes would still work.
12143 */
12144static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012145smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012146{
12147 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012148 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012149
Dragan Dosendb5af612016-06-16 11:23:01 +020012150 if (!cli_conn)
12151 return 0;
12152
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012153 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012154 return 0;
12155
12156 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012157 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12158 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012159
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012160 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012161 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012162 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012163 temp->len += 4;
12164 break;
12165 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012166 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012167 temp->len += 16;
12168 break;
12169 default:
12170 return 0;
12171 }
12172
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012173 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012174 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012175 return 1;
12176}
12177
Willy Tarreau185b5c42012-04-26 15:11:51 +020012178/* This function is used to validate the arguments passed to any "hdr" fetch
12179 * keyword. These keywords support an optional positive or negative occurrence
12180 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12181 * is assumed that the types are already the correct ones. Returns 0 on error,
12182 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12183 * error message in case of error, that the caller is responsible for freeing.
12184 * The initial location must either be freeable or NULL.
12185 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012186int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012187{
12188 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012189 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012190 return 0;
12191 }
12192 return 1;
12193}
12194
Willy Tarreau276fae92013-07-25 14:36:01 +020012195/* takes an UINT value on input supposed to represent the time since EPOCH,
12196 * adds an optional offset found in args[0] and emits a string representing
12197 * the date in RFC-1123/5322 format.
12198 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012199static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012200{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012201 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012202 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12203 struct chunk *temp;
12204 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012205 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012206 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012207
12208 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012209 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012210 curr_date += args[0].data.sint;
12211
12212 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012213 if (!tm)
12214 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012215
12216 temp = get_trash_chunk();
12217 temp->len = snprintf(temp->str, temp->size - temp->len,
12218 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12219 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12220 tm->tm_hour, tm->tm_min, tm->tm_sec);
12221
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012222 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012223 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012224 return 1;
12225}
12226
Thierry FOURNIERad903512014-04-11 17:51:01 +020012227/* Match language range with language tag. RFC2616 14.4:
12228 *
12229 * A language-range matches a language-tag if it exactly equals
12230 * the tag, or if it exactly equals a prefix of the tag such
12231 * that the first tag character following the prefix is "-".
12232 *
12233 * Return 1 if the strings match, else return 0.
12234 */
12235static inline int language_range_match(const char *range, int range_len,
12236 const char *tag, int tag_len)
12237{
12238 const char *end = range + range_len;
12239 const char *tend = tag + tag_len;
12240 while (range < end) {
12241 if (*range == '-' && tag == tend)
12242 return 1;
12243 if (*range != *tag || tag == tend)
12244 return 0;
12245 range++;
12246 tag++;
12247 }
12248 /* Return true only if the last char of the tag is matched. */
12249 return tag == tend;
12250}
12251
12252/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012253static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012254{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012255 const char *al = smp->data.u.str.str;
12256 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012257 const char *token;
12258 int toklen;
12259 int qvalue;
12260 const char *str;
12261 const char *w;
12262 int best_q = 0;
12263
12264 /* Set the constant to the sample, because the output of the
12265 * function will be peek in the constant configuration string.
12266 */
12267 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012268 smp->data.u.str.size = 0;
12269 smp->data.u.str.str = "";
12270 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012271
12272 /* Parse the accept language */
12273 while (1) {
12274
12275 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012276 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012277 al++;
12278 if (al >= end)
12279 break;
12280
12281 /* Start of the fisrt word. */
12282 token = al;
12283
12284 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012285 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012286 al++;
12287 if (al == token)
12288 goto expect_comma;
12289
12290 /* Length of the token. */
12291 toklen = al - token;
12292 qvalue = 1000;
12293
12294 /* Check if the token exists in the list. If the token not exists,
12295 * jump to the next token.
12296 */
12297 str = args[0].data.str.str;
12298 w = str;
12299 while (1) {
12300 if (*str == ';' || *str == '\0') {
12301 if (language_range_match(token, toklen, w, str-w))
12302 goto look_for_q;
12303 if (*str == '\0')
12304 goto expect_comma;
12305 w = str + 1;
12306 }
12307 str++;
12308 }
12309 goto expect_comma;
12310
12311look_for_q:
12312
12313 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012314 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012315 al++;
12316 if (al >= end)
12317 goto process_value;
12318
12319 /* If ',' is found, process the result */
12320 if (*al == ',')
12321 goto process_value;
12322
12323 /* If the character is different from ';', look
12324 * for the end of the header part in best effort.
12325 */
12326 if (*al != ';')
12327 goto expect_comma;
12328
12329 /* Assumes that the char is ';', now expect "q=". */
12330 al++;
12331
12332 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012333 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012334 al++;
12335 if (al >= end)
12336 goto process_value;
12337
12338 /* Expect 'q'. If no 'q', continue in best effort */
12339 if (*al != 'q')
12340 goto process_value;
12341 al++;
12342
12343 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012344 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012345 al++;
12346 if (al >= end)
12347 goto process_value;
12348
12349 /* Expect '='. If no '=', continue in best effort */
12350 if (*al != '=')
12351 goto process_value;
12352 al++;
12353
12354 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012355 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012356 al++;
12357 if (al >= end)
12358 goto process_value;
12359
12360 /* Parse the q value. */
12361 qvalue = parse_qvalue(al, &al);
12362
12363process_value:
12364
12365 /* If the new q value is the best q value, then store the associated
12366 * language in the response. If qvalue is the biggest value (1000),
12367 * break the process.
12368 */
12369 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012370 smp->data.u.str.str = (char *)w;
12371 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012372 if (qvalue >= 1000)
12373 break;
12374 best_q = qvalue;
12375 }
12376
12377expect_comma:
12378
12379 /* Expect comma or end. If the end is detected, quit the loop. */
12380 while (al < end && *al != ',')
12381 al++;
12382 if (al >= end)
12383 break;
12384
12385 /* Comma is found, jump it and restart the analyzer. */
12386 al++;
12387 }
12388
12389 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012390 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12391 smp->data.u.str.str = args[1].data.str.str;
12392 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012393 }
12394
12395 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012396 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012397}
12398
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012399/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012400static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012401{
12402 /* If the constant flag is set or if not size is avalaible at
12403 * the end of the buffer, copy the string in other buffer
12404 * before decoding.
12405 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012406 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012407 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012408 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12409 smp->data.u.str.str = str->str;
12410 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012411 smp->flags &= ~SMP_F_CONST;
12412 }
12413
12414 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012415 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12416 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020012417 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012418}
12419
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012420static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12421{
12422 struct proxy *fe = strm_fe(smp->strm);
12423 int idx, i;
12424 struct cap_hdr *hdr;
12425 int len;
12426
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012427 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012428 return 0;
12429
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012430 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012431
12432 /* Check the availibity of the capture id. */
12433 if (idx > fe->nb_req_cap - 1)
12434 return 0;
12435
12436 /* Look for the original configuration. */
12437 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12438 hdr != NULL && i != idx ;
12439 i--, hdr = hdr->next);
12440 if (!hdr)
12441 return 0;
12442
12443 /* check for the memory allocation */
12444 if (smp->strm->req_cap[hdr->index] == NULL)
12445 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12446 if (smp->strm->req_cap[hdr->index] == NULL)
12447 return 0;
12448
12449 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012450 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012451 if (len > hdr->len)
12452 len = hdr->len;
12453
12454 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012455 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012456 smp->strm->req_cap[idx][len] = '\0';
12457
12458 return 1;
12459}
12460
12461static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12462{
12463 struct proxy *fe = strm_fe(smp->strm);
12464 int idx, i;
12465 struct cap_hdr *hdr;
12466 int len;
12467
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012468 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012469 return 0;
12470
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012471 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012472
12473 /* Check the availibity of the capture id. */
12474 if (idx > fe->nb_rsp_cap - 1)
12475 return 0;
12476
12477 /* Look for the original configuration. */
12478 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12479 hdr != NULL && i != idx ;
12480 i--, hdr = hdr->next);
12481 if (!hdr)
12482 return 0;
12483
12484 /* check for the memory allocation */
12485 if (smp->strm->res_cap[hdr->index] == NULL)
12486 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12487 if (smp->strm->res_cap[hdr->index] == NULL)
12488 return 0;
12489
12490 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012491 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012492 if (len > hdr->len)
12493 len = hdr->len;
12494
12495 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012496 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012497 smp->strm->res_cap[idx][len] = '\0';
12498
12499 return 1;
12500}
12501
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012502/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012503 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012504 * the relevant part of the request line accordingly. Then it updates various
12505 * pointers to the next elements which were moved, and the total buffer length.
12506 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012507 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12508 * error, though this can be revisited when this code is finally exploited.
12509 *
12510 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12511 * query string and 3 to replace uri.
12512 *
12513 * In query string case, the mark question '?' must be set at the start of the
12514 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012515 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012516int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012517 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012518{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012519 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012520 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012521 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012522 int delta;
12523
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012524 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012525 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012526 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012527 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12528
12529 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012530 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012531 txn->req.sl.rq.m_l += delta;
12532 txn->req.sl.rq.u += delta;
12533 txn->req.sl.rq.v += delta;
12534 break;
12535
12536 case 1: // path
12537 cur_ptr = http_get_path(txn);
12538 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012539 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012540
12541 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012542 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 +010012543 cur_end++;
12544
12545 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012546 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012547 txn->req.sl.rq.u_l += delta;
12548 txn->req.sl.rq.v += delta;
12549 break;
12550
12551 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012552 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012553 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012554 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12555 while (cur_ptr < cur_end && *cur_ptr != '?')
12556 cur_ptr++;
12557
12558 /* skip the question mark or indicate that we must insert it
12559 * (but only if the format string is not empty then).
12560 */
12561 if (cur_ptr < cur_end)
12562 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012563 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012564 offset = 0;
12565
12566 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012567 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012568 txn->req.sl.rq.u_l += delta;
12569 txn->req.sl.rq.v += delta;
12570 break;
12571
12572 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012573 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012574 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12575
12576 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012577 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012578 txn->req.sl.rq.u_l += delta;
12579 txn->req.sl.rq.v += delta;
12580 break;
12581
12582 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012583 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012584 }
12585
12586 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012587 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012588 txn->req.sl.rq.l += delta;
12589 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012590 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012591 return 0;
12592}
12593
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012594/* This function replace the HTTP status code and the associated message. The
12595 * variable <status> contains the new status code. This function never fails.
12596 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012597void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012598{
12599 struct http_txn *txn = s->txn;
12600 char *cur_ptr, *cur_end;
12601 int delta;
12602 char *res;
12603 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012604 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012605 int msg_len;
12606
12607 chunk_reset(&trash);
12608
12609 res = ultoa_o(status, trash.str, trash.size);
12610 c_l = res - trash.str;
12611
12612 trash.str[c_l] = ' ';
12613 trash.len = c_l + 1;
12614
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012615 /* Do we have a custom reason format string? */
12616 if (msg == NULL)
12617 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012618 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012619 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12620 trash.len += msg_len;
12621
12622 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12623 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12624
12625 /* commit changes and adjust message */
12626 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12627
12628 /* adjust res line offsets and lengths */
12629 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12630 txn->rsp.sl.st.c_l = c_l;
12631 txn->rsp.sl.st.r_l = msg_len;
12632
12633 delta = trash.len - (cur_end - cur_ptr);
12634 txn->rsp.sl.st.l += delta;
12635 txn->hdr_idx.v[0].len += delta;
12636 http_msg_move_end(&txn->rsp, delta);
12637}
12638
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012639/* This function executes one of the set-{method,path,query,uri} actions. It
12640 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012641 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012642 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012643 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12644 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012645 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012646enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012647 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012648{
12649 chunk_reset(&trash);
12650
12651 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012652 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012653 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012654 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012655
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012656 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012657 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012658}
12659
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012660/* This function is just a compliant action wrapper for "set-status". */
12661enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012662 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012663{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012664 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012665 return ACT_RET_CONT;
12666}
12667
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012668/* parse an http-request action among :
12669 * set-method
12670 * set-path
12671 * set-query
12672 * set-uri
12673 *
12674 * All of them accept a single argument of type string representing a log-format.
12675 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12676 * 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 +020012677 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012678 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012679enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12680 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012681{
12682 int cur_arg = *orig_arg;
12683
Thierry FOURNIER42148732015-09-02 17:17:33 +020012684 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012685
12686 switch (args[0][4]) {
12687 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012688 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012689 rule->action_ptr = http_action_set_req_line;
12690 break;
12691 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012692 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012693 rule->action_ptr = http_action_set_req_line;
12694 break;
12695 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012696 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012697 rule->action_ptr = http_action_set_req_line;
12698 break;
12699 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012700 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012701 rule->action_ptr = http_action_set_req_line;
12702 break;
12703 default:
12704 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012705 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012706 }
12707
12708 if (!*args[cur_arg] ||
12709 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12710 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012711 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012712 }
12713
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012714 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012715 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012716 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012717 (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 +010012718 return ACT_RET_PRS_ERR;
12719 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012720
12721 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012722 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012723}
12724
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012725/* parse set-status action:
12726 * This action accepts a single argument of type int representing
12727 * an http status code. It returns ACT_RET_PRS_OK on success,
12728 * ACT_RET_PRS_ERR on error.
12729 */
12730enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12731 struct act_rule *rule, char **err)
12732{
12733 char *error;
12734
Thierry FOURNIER42148732015-09-02 17:17:33 +020012735 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012736 rule->action_ptr = action_http_set_status;
12737
12738 /* Check if an argument is available */
12739 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012740 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012741 return ACT_RET_PRS_ERR;
12742 }
12743
12744 /* convert status code as integer */
12745 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12746 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12747 memprintf(err, "expects an integer status code between 100 and 999");
12748 return ACT_RET_PRS_ERR;
12749 }
12750
12751 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012752
12753 /* set custom reason string */
12754 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12755 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12756 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12757 (*orig_arg)++;
12758 rule->arg.status.reason = strdup(args[*orig_arg]);
12759 (*orig_arg)++;
12760 }
12761
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012762 return ACT_RET_PRS_OK;
12763}
12764
Willy Tarreaua9083d02015-05-08 15:27:59 +020012765/* This function executes the "capture" action. It executes a fetch expression,
12766 * turns the result into a string and puts it in a capture slot. It always
12767 * returns 1. If an error occurs the action is cancelled, but the rule
12768 * processing continues.
12769 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012770enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012771 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012772{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012773 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012774 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012775 char **cap = s->req_cap;
12776 int len;
12777
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012778 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 +020012779 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012780 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012781
12782 if (cap[h->index] == NULL)
12783 cap[h->index] = pool_alloc2(h->pool);
12784
12785 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012786 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012787
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012788 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012789 if (len > h->len)
12790 len = h->len;
12791
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012792 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012793 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012794 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012795}
12796
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012797/* This function executes the "capture" action and store the result in a
12798 * capture slot if exists. It executes a fetch expression, turns the result
12799 * into a string and puts it in a capture slot. It always returns 1. If an
12800 * error occurs the action is cancelled, but the rule processing continues.
12801 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012802enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012803 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012804{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012805 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012806 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012807 char **cap = s->req_cap;
12808 struct proxy *fe = strm_fe(s);
12809 int len;
12810 int i;
12811
12812 /* Look for the original configuration. */
12813 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012814 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012815 i--, h = h->next);
12816 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012817 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012818
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012819 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 +020012820 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012821 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012822
12823 if (cap[h->index] == NULL)
12824 cap[h->index] = pool_alloc2(h->pool);
12825
12826 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012827 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012828
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012829 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012830 if (len > h->len)
12831 len = h->len;
12832
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012833 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012834 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012835 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012836}
12837
Willy Tarreaua9083d02015-05-08 15:27:59 +020012838/* parse an "http-request capture" action. It takes a single argument which is
12839 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012840 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012841 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012842 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012843enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12844 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012845{
12846 struct sample_expr *expr;
12847 struct cap_hdr *hdr;
12848 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012849 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012850
12851 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12852 if (strcmp(args[cur_arg], "if") == 0 ||
12853 strcmp(args[cur_arg], "unless") == 0)
12854 break;
12855
12856 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012857 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012858 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012859 }
12860
Willy Tarreaua9083d02015-05-08 15:27:59 +020012861 cur_arg = *orig_arg;
12862 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12863 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012864 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012865
12866 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12867 memprintf(err,
12868 "fetch method '%s' extracts information from '%s', none of which is available here",
12869 args[cur_arg-1], sample_src_names(expr->fetch->use));
12870 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012871 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012872 }
12873
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012874 if (!args[cur_arg] || !*args[cur_arg]) {
12875 memprintf(err, "expects 'len or 'id'");
12876 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012877 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012878 }
12879
Willy Tarreaua9083d02015-05-08 15:27:59 +020012880 if (strcmp(args[cur_arg], "len") == 0) {
12881 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012882
12883 if (!(px->cap & PR_CAP_FE)) {
12884 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012885 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012886 }
12887
12888 proxy->conf.args.ctx = ARGC_CAP;
12889
Willy Tarreaua9083d02015-05-08 15:27:59 +020012890 if (!args[cur_arg]) {
12891 memprintf(err, "missing length value");
12892 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012893 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012894 }
12895 /* we copy the table name for now, it will be resolved later */
12896 len = atoi(args[cur_arg]);
12897 if (len <= 0) {
12898 memprintf(err, "length must be > 0");
12899 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012900 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012901 }
12902 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012903
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012904 if (!len) {
12905 memprintf(err, "a positive 'len' argument is mandatory");
12906 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012907 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012908 }
12909
Vincent Bernat02779b62016-04-03 13:48:43 +020012910 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012911 hdr->next = px->req_cap;
12912 hdr->name = NULL; /* not a header capture */
12913 hdr->namelen = 0;
12914 hdr->len = len;
12915 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12916 hdr->index = px->nb_req_cap++;
12917
12918 px->req_cap = hdr;
12919 px->to_log |= LW_REQHDR;
12920
Thierry FOURNIER42148732015-09-02 17:17:33 +020012921 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012922 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012923 rule->arg.cap.expr = expr;
12924 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012925 }
12926
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012927 else if (strcmp(args[cur_arg], "id") == 0) {
12928 int id;
12929 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012930
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012931 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012932
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012933 if (!args[cur_arg]) {
12934 memprintf(err, "missing id value");
12935 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012936 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012937 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012938
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012939 id = strtol(args[cur_arg], &error, 10);
12940 if (*error != '\0') {
12941 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12942 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012943 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012944 }
12945 cur_arg++;
12946
12947 proxy->conf.args.ctx = ARGC_CAP;
12948
Thierry FOURNIER42148732015-09-02 17:17:33 +020012949 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012950 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012951 rule->arg.capid.expr = expr;
12952 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012953 }
12954
12955 else {
12956 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12957 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012958 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012959 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012960
12961 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012962 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012963}
12964
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012965/* This function executes the "capture" action and store the result in a
12966 * capture slot if exists. It executes a fetch expression, turns the result
12967 * into a string and puts it in a capture slot. It always returns 1. If an
12968 * error occurs the action is cancelled, but the rule processing continues.
12969 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012970enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012971 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012972{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012973 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012974 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012975 char **cap = s->res_cap;
12976 struct proxy *fe = strm_fe(s);
12977 int len;
12978 int i;
12979
12980 /* Look for the original configuration. */
12981 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012982 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012983 i--, h = h->next);
12984 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012985 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012986
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012987 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 +020012988 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012989 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012990
12991 if (cap[h->index] == NULL)
12992 cap[h->index] = pool_alloc2(h->pool);
12993
12994 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012995 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012996
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012997 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012998 if (len > h->len)
12999 len = h->len;
13000
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020013001 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013002 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020013003 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013004}
13005
13006/* parse an "http-response capture" action. It takes a single argument which is
13007 * a sample fetch expression. It stores the expression into arg->act.p[0] and
13008 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013009 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013010 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013011enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
13012 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013013{
13014 struct sample_expr *expr;
13015 int cur_arg;
13016 int id;
13017 char *error;
13018
13019 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
13020 if (strcmp(args[cur_arg], "if") == 0 ||
13021 strcmp(args[cur_arg], "unless") == 0)
13022 break;
13023
13024 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013025 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013026 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013027 }
13028
13029 cur_arg = *orig_arg;
13030 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
13031 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013032 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013033
13034 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
13035 memprintf(err,
13036 "fetch method '%s' extracts information from '%s', none of which is available here",
13037 args[cur_arg-1], sample_src_names(expr->fetch->use));
13038 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013039 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013040 }
13041
13042 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020013043 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013044 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013045 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013046 }
13047
13048 if (strcmp(args[cur_arg], "id") != 0) {
13049 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
13050 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013051 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013052 }
13053
13054 cur_arg++;
13055
13056 if (!args[cur_arg]) {
13057 memprintf(err, "missing id value");
13058 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013059 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013060 }
13061
13062 id = strtol(args[cur_arg], &error, 10);
13063 if (*error != '\0') {
13064 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
13065 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013066 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013067 }
13068 cur_arg++;
13069
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013070 proxy->conf.args.ctx = ARGC_CAP;
13071
Thierry FOURNIER42148732015-09-02 17:17:33 +020013072 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013073 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020013074 rule->arg.capid.expr = expr;
13075 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013076
13077 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020013078 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013079}
13080
William Lallemand73025dd2014-04-24 14:38:37 +020013081/*
13082 * Return the struct http_req_action_kw associated to a keyword.
13083 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013084struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013085{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013086 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013087}
13088
13089/*
13090 * Return the struct http_res_action_kw associated to a keyword.
13091 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013092struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020013093{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020013094 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020013095}
13096
Willy Tarreau12207b32016-11-22 19:48:51 +010013097
13098/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
13099 * now.
13100 */
13101static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
13102{
13103 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
13104 return 1;
13105
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013106 if (*args[2]) {
13107 struct proxy *px;
13108
13109 px = proxy_find_by_name(args[2], 0, 0);
13110 if (px)
13111 appctx->ctx.errors.iid = px->uuid;
13112 else
13113 appctx->ctx.errors.iid = atoi(args[2]);
13114
13115 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020013116 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013117 appctx->ctx.cli.msg = "No such proxy.\n";
13118 appctx->st0 = CLI_ST_PRINT;
13119 return 1;
13120 }
13121 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013122 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013123 appctx->ctx.errors.iid = -1; // dump all proxies
13124
Willy Tarreau35069f82016-11-25 09:16:37 +010013125 appctx->ctx.errors.flag = 0;
13126 if (strcmp(args[3], "request") == 0)
13127 appctx->ctx.errors.flag |= 4; // ignore response
13128 else if (strcmp(args[3], "response") == 0)
13129 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013130 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013131 return 0;
13132}
13133
13134/* This function dumps all captured errors onto the stream interface's
13135 * read buffer. It returns 0 if the output buffer is full and it needs
13136 * to be called again, otherwise non-zero.
13137 */
13138static int cli_io_handler_show_errors(struct appctx *appctx)
13139{
13140 struct stream_interface *si = appctx->owner;
13141 extern const char *monthname[12];
13142
13143 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13144 return 1;
13145
13146 chunk_reset(&trash);
13147
13148 if (!appctx->ctx.errors.px) {
13149 /* the function had not been called yet, let's prepare the
13150 * buffer for a response.
13151 */
13152 struct tm tm;
13153
13154 get_localtime(date.tv_sec, &tm);
13155 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13156 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13157 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13158 error_snapshot_id);
13159
Willy Tarreau06d80a92017-10-19 14:32:15 +020013160 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013161 /* Socket buffer full. Let's try again later from the same point */
13162 si_applet_cant_put(si);
13163 return 0;
13164 }
13165
13166 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013167 appctx->ctx.errors.bol = 0;
13168 appctx->ctx.errors.ptr = -1;
13169 }
13170
13171 /* we have two inner loops here, one for the proxy, the other one for
13172 * the buffer.
13173 */
13174 while (appctx->ctx.errors.px) {
13175 struct error_snapshot *es;
13176
Willy Tarreau35069f82016-11-25 09:16:37 +010013177 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013178 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013179 if (appctx->ctx.errors.flag & 2) // skip req
13180 goto next;
13181 }
13182 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013183 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013184 if (appctx->ctx.errors.flag & 4) // skip resp
13185 goto next;
13186 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013187
13188 if (!es->when.tv_sec)
13189 goto next;
13190
13191 if (appctx->ctx.errors.iid >= 0 &&
13192 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13193 es->oe->uuid != appctx->ctx.errors.iid)
13194 goto next;
13195
13196 if (appctx->ctx.errors.ptr < 0) {
13197 /* just print headers now */
13198
13199 char pn[INET6_ADDRSTRLEN];
13200 struct tm tm;
13201 int port;
13202
13203 get_localtime(es->when.tv_sec, &tm);
13204 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13205 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13206 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13207
13208 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13209 case AF_INET:
13210 case AF_INET6:
13211 port = get_host_port(&es->src);
13212 break;
13213 default:
13214 port = 0;
13215 }
13216
Willy Tarreau35069f82016-11-25 09:16:37 +010013217 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013218 case 0:
13219 chunk_appendf(&trash,
13220 " frontend %s (#%d): invalid request\n"
13221 " backend %s (#%d)",
13222 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13223 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13224 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13225 break;
13226 case 1:
13227 chunk_appendf(&trash,
13228 " backend %s (#%d): invalid response\n"
13229 " frontend %s (#%d)",
13230 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13231 es->oe->id, es->oe->uuid);
13232 break;
13233 }
13234
13235 chunk_appendf(&trash,
13236 ", server %s (#%d), event #%u\n"
13237 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013238 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013239 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13240 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13241 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13242 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13243 es->ev_id,
13244 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013245 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013246 es->m_clen, es->m_blen,
13247 es->b_flags, es->b_out, es->b_tot,
13248 es->len, es->b_wrap, es->pos);
13249
Willy Tarreau06d80a92017-10-19 14:32:15 +020013250 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013251 /* Socket buffer full. Let's try again later from the same point */
13252 si_applet_cant_put(si);
13253 return 0;
13254 }
13255 appctx->ctx.errors.ptr = 0;
13256 appctx->ctx.errors.sid = es->sid;
13257 }
13258
13259 if (appctx->ctx.errors.sid != es->sid) {
13260 /* the snapshot changed while we were dumping it */
13261 chunk_appendf(&trash,
13262 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020013263 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013264 si_applet_cant_put(si);
13265 return 0;
13266 }
13267 goto next;
13268 }
13269
13270 /* OK, ptr >= 0, so we have to dump the current line */
13271 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13272 int newptr;
13273 int newline;
13274
13275 newline = appctx->ctx.errors.bol;
13276 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13277 if (newptr == appctx->ctx.errors.ptr)
13278 return 0;
13279
Willy Tarreau06d80a92017-10-19 14:32:15 +020013280 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013281 /* Socket buffer full. Let's try again later from the same point */
13282 si_applet_cant_put(si);
13283 return 0;
13284 }
13285 appctx->ctx.errors.ptr = newptr;
13286 appctx->ctx.errors.bol = newline;
13287 };
13288 next:
13289 appctx->ctx.errors.bol = 0;
13290 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013291 appctx->ctx.errors.flag ^= 1;
13292 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013293 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013294 }
13295
13296 /* dump complete */
13297 return 1;
13298}
13299
13300/* register cli keywords */
13301static struct cli_kw_list cli_kws = {{ },{
13302 { { "show", "errors", NULL },
13303 "show errors : report last request and response errors for each proxy",
13304 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13305 },
13306 {{},}
13307}};
13308
Willy Tarreau4a568972010-05-12 08:08:50 +020013309/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013310/* All supported ACL keywords must be declared here. */
13311/************************************************************************/
13312
13313/* Note: must not be declared <const> as its list will be overwritten.
13314 * Please take care of keeping this list alphabetically sorted.
13315 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013316static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013317 { "base", "base", PAT_MATCH_STR },
13318 { "base_beg", "base", PAT_MATCH_BEG },
13319 { "base_dir", "base", PAT_MATCH_DIR },
13320 { "base_dom", "base", PAT_MATCH_DOM },
13321 { "base_end", "base", PAT_MATCH_END },
13322 { "base_len", "base", PAT_MATCH_LEN },
13323 { "base_reg", "base", PAT_MATCH_REG },
13324 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013325
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013326 { "cook", "req.cook", PAT_MATCH_STR },
13327 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13328 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13329 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13330 { "cook_end", "req.cook", PAT_MATCH_END },
13331 { "cook_len", "req.cook", PAT_MATCH_LEN },
13332 { "cook_reg", "req.cook", PAT_MATCH_REG },
13333 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013334
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013335 { "hdr", "req.hdr", PAT_MATCH_STR },
13336 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13337 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13338 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13339 { "hdr_end", "req.hdr", PAT_MATCH_END },
13340 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13341 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13342 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013343
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013344 /* these two declarations uses strings with list storage (in place
13345 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13346 * and delete functions are relative to the list management. The parse
13347 * and match method are related to the corresponding fetch methods. This
13348 * is very particular ACL declaration mode.
13349 */
13350 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13351 { "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 +020013352
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013353 { "path", "path", PAT_MATCH_STR },
13354 { "path_beg", "path", PAT_MATCH_BEG },
13355 { "path_dir", "path", PAT_MATCH_DIR },
13356 { "path_dom", "path", PAT_MATCH_DOM },
13357 { "path_end", "path", PAT_MATCH_END },
13358 { "path_len", "path", PAT_MATCH_LEN },
13359 { "path_reg", "path", PAT_MATCH_REG },
13360 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013361
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013362 { "req_ver", "req.ver", PAT_MATCH_STR },
13363 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013364
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013365 { "scook", "res.cook", PAT_MATCH_STR },
13366 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13367 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13368 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13369 { "scook_end", "res.cook", PAT_MATCH_END },
13370 { "scook_len", "res.cook", PAT_MATCH_LEN },
13371 { "scook_reg", "res.cook", PAT_MATCH_REG },
13372 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013373
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013374 { "shdr", "res.hdr", PAT_MATCH_STR },
13375 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13376 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13377 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13378 { "shdr_end", "res.hdr", PAT_MATCH_END },
13379 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13380 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13381 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013382
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013383 { "url", "url", PAT_MATCH_STR },
13384 { "url_beg", "url", PAT_MATCH_BEG },
13385 { "url_dir", "url", PAT_MATCH_DIR },
13386 { "url_dom", "url", PAT_MATCH_DOM },
13387 { "url_end", "url", PAT_MATCH_END },
13388 { "url_len", "url", PAT_MATCH_LEN },
13389 { "url_reg", "url", PAT_MATCH_REG },
13390 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013391
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013392 { "urlp", "urlp", PAT_MATCH_STR },
13393 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13394 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13395 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13396 { "urlp_end", "urlp", PAT_MATCH_END },
13397 { "urlp_len", "urlp", PAT_MATCH_LEN },
13398 { "urlp_reg", "urlp", PAT_MATCH_REG },
13399 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013400
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013401 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013402}};
13403
13404/************************************************************************/
13405/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013406/************************************************************************/
13407/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013408static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013409 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013410 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013411 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13412
Willy Tarreau87b09662015-04-03 00:22:06 +020013413 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013414 { "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 +020013415
13416 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013417 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13418 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13419 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013420
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013421 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13422 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013423
Willy Tarreau409bcde2013-01-08 00:31:00 +010013424 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13425 * are only here to match the ACL's name, are request-only and are used
13426 * for ACL compatibility only.
13427 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013428 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13429 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013430 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13431 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013432
13433 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13434 * only here to match the ACL's name, are request-only and are used for
13435 * ACL compatibility only.
13436 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013437 { "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 +020013438 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013439 { "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 +020013440 { "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 +010013441
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013442 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013443 { "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 +010013444 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013445 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013446 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013447 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013448
13449 /* HTTP protocol on the request path */
13450 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013451 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013452
13453 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013454 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13455 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013456
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013457 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013458 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13459 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013460 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013461
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020013462 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020013463 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13464
Willy Tarreau18ed2562013-01-14 15:56:36 +010013465 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013466 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13467 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013468
Willy Tarreau18ed2562013-01-14 15:56:36 +010013469 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013470 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013471 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13472 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013473
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013474 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013475 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013476 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013477 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013478 { "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 +010013479 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013480 { "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 +010013481
13482 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013483 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013484 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13485 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013486
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013487 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013488 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013489 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013490 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013491 { "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 +010013492 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013493 { "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 +010013494
Willy Tarreau409bcde2013-01-08 00:31:00 +010013495 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013496 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013497 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13498 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013499 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013500
13501 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013502 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013503 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013504 { "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 +020013505 { "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 +010013506
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013507 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013508 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013509 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013510 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013511 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013512 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013513 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013514 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13515 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013516 { "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 +010013517 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013518}};
13519
Willy Tarreau8797c062007-05-07 00:55:35 +020013520
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013521/************************************************************************/
13522/* All supported converter keywords must be declared here. */
13523/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013524/* Note: must not be declared <const> as its list will be overwritten */
13525static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013526 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013527 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013528 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13529 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013530 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013531 { NULL, NULL, 0, 0, 0 },
13532}};
13533
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013534
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013535/************************************************************************/
13536/* All supported http-request action keywords must be declared here. */
13537/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013538struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013539 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013540 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013541 { "set-method", parse_set_req_line },
13542 { "set-path", parse_set_req_line },
13543 { "set-query", parse_set_req_line },
13544 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013545 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013546 }
13547};
13548
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013549struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013550 .kw = {
13551 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013552 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013553 { NULL, NULL }
13554 }
13555};
13556
Willy Tarreau8797c062007-05-07 00:55:35 +020013557__attribute__((constructor))
13558static void __http_protocol_init(void)
13559{
13560 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013561 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013562 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013563 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013564 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013565 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013566}
13567
13568
Willy Tarreau58f10d72006-12-04 02:26:12 +010013569/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013570 * Local variables:
13571 * c-indent-level: 8
13572 * c-basic-offset: 8
13573 * End:
13574 */