blob: 24d034ae03a8870dc3f6654a5a2c6b76efc12f77 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020063#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020067#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020069#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010070#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020072#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau522d6c02009-12-06 18:49:18 +010075const char HTTP_100[] =
76 "HTTP/1.1 100 Continue\r\n\r\n";
77
78const struct chunk http_100_chunk = {
79 .str = (char *)&HTTP_100,
80 .len = sizeof(HTTP_100)-1
81};
82
Willy Tarreaua9679ac2010-01-03 17:32:57 +010083/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400102
103/* same as 302 except that the browser MUST retry with the same method */
104const char *HTTP_307 =
105 "HTTP/1.1 307 Temporary Redirect\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
110/* same as 301 except that the browser MUST retry with the same method */
111const char *HTTP_308 =
112 "HTTP/1.1 308 Permanent Redirect\r\n"
113 "Content-length: 0\r\n"
114 "Location: "; /* not terminated since it will be concatenated with the URL */
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
117const char *HTTP_401_fmt =
118 "HTTP/1.0 401 Unauthorized\r\n"
119 "Cache-Control: no-cache\r\n"
120 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200121 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
123 "\r\n"
124 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
125
Willy Tarreau844a7e72010-01-31 21:46:18 +0100126const char *HTTP_407_fmt =
127 "HTTP/1.0 407 Unauthorized\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
132 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800133 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135
136const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200137 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_400] = 400,
139 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400140 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_500] = 500,
144 [HTTP_ERR_502] = 502,
145 [HTTP_ERR_503] = 503,
146 [HTTP_ERR_504] = 504,
147};
148
Willy Tarreau80587432006-12-24 17:47:20 +0100149static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200150 [HTTP_ERR_200] =
151 "HTTP/1.0 200 OK\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
157
Willy Tarreau0f772532006-12-23 20:51:41 +0100158 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100159 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
165
166 [HTTP_ERR_403] =
167 "HTTP/1.0 403 Forbidden\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
173
CJ Ess108b1dd2015-04-07 12:03:37 -0400174 [HTTP_ERR_405] =
175 "HTTP/1.0 405 Method Not Allowed\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
181
Willy Tarreau0f772532006-12-23 20:51:41 +0100182 [HTTP_ERR_408] =
183 "HTTP/1.0 408 Request Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
189
CJ Ess108b1dd2015-04-07 12:03:37 -0400190 [HTTP_ERR_429] =
191 "HTTP/1.0 429 Too Many Requests\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
197
Willy Tarreau0f772532006-12-23 20:51:41 +0100198 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200199 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200204 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100205
206 [HTTP_ERR_502] =
207 "HTTP/1.0 502 Bad Gateway\r\n"
208 "Cache-Control: no-cache\r\n"
209 "Connection: close\r\n"
210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
213
214 [HTTP_ERR_503] =
215 "HTTP/1.0 503 Service Unavailable\r\n"
216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
221
222 [HTTP_ERR_504] =
223 "HTTP/1.0 504 Gateway Time-out\r\n"
224 "Cache-Control: no-cache\r\n"
225 "Connection: close\r\n"
226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
229
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232/* status codes available for the stats admin page (strictly 4 chars length) */
233const char *stat_status_codes[STAT_STATUS_SIZE] = {
234 [STAT_STATUS_DENY] = "DENY",
235 [STAT_STATUS_DONE] = "DONE",
236 [STAT_STATUS_ERRP] = "ERRP",
237 [STAT_STATUS_EXCD] = "EXCD",
238 [STAT_STATUS_NONE] = "NONE",
239 [STAT_STATUS_PART] = "PART",
240 [STAT_STATUS_UNKN] = "UNKN",
241};
242
243
William Lallemand73025dd2014-04-24 14:38:37 +0200244/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_req_keywords.list)
247};
248
249/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200250struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200251 .list = LIST_HEAD_INIT(http_res_keywords.list)
252};
253
Willy Tarreau80587432006-12-24 17:47:20 +0100254/* We must put the messages here since GCC cannot initialize consts depending
255 * on strlen().
256 */
257struct chunk http_err_chunks[HTTP_ERR_SIZE];
258
Willy Tarreaua890d072013-04-02 12:01:06 +0200259/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
260static struct hdr_ctx static_hdr_ctx;
261
Willy Tarreau42250582007-04-01 01:30:43 +0200262#define FD_SETS_ARE_BITFIELDS
263#ifdef FD_SETS_ARE_BITFIELDS
264/*
265 * This map is used with all the FD_* macros to check whether a particular bit
266 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
267 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
268 * byte should be encoded. Be careful to always pass bytes from 0 to 255
269 * exclusively to the macros.
270 */
271fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
272fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100273fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200274
275#else
276#error "Check if your OS uses bitfields for fd_sets"
277#endif
278
Willy Tarreau87b09662015-04-03 00:22:06 +0200279static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200280
David Carlier7365f7d2016-04-04 11:54:42 +0100281static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
282static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100283
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200284/* This function returns a reason associated with the HTTP status.
285 * This function never fails, a message is always returned.
286 */
287const char *get_reason(unsigned int status)
288{
289 switch (status) {
290 case 100: return "Continue";
291 case 101: return "Switching Protocols";
292 case 102: return "Processing";
293 case 200: return "OK";
294 case 201: return "Created";
295 case 202: return "Accepted";
296 case 203: return "Non-Authoritative Information";
297 case 204: return "No Content";
298 case 205: return "Reset Content";
299 case 206: return "Partial Content";
300 case 207: return "Multi-Status";
301 case 210: return "Content Different";
302 case 226: return "IM Used";
303 case 300: return "Multiple Choices";
304 case 301: return "Moved Permanently";
305 case 302: return "Moved Temporarily";
306 case 303: return "See Other";
307 case 304: return "Not Modified";
308 case 305: return "Use Proxy";
309 case 307: return "Temporary Redirect";
310 case 308: return "Permanent Redirect";
311 case 310: return "Too many Redirects";
312 case 400: return "Bad Request";
313 case 401: return "Unauthorized";
314 case 402: return "Payment Required";
315 case 403: return "Forbidden";
316 case 404: return "Not Found";
317 case 405: return "Method Not Allowed";
318 case 406: return "Not Acceptable";
319 case 407: return "Proxy Authentication Required";
320 case 408: return "Request Time-out";
321 case 409: return "Conflict";
322 case 410: return "Gone";
323 case 411: return "Length Required";
324 case 412: return "Precondition Failed";
325 case 413: return "Request Entity Too Large";
326 case 414: return "Request-URI Too Long";
327 case 415: return "Unsupported Media Type";
328 case 416: return "Requested range unsatisfiable";
329 case 417: return "Expectation failed";
330 case 418: return "I'm a teapot";
331 case 422: return "Unprocessable entity";
332 case 423: return "Locked";
333 case 424: return "Method failure";
334 case 425: return "Unordered Collection";
335 case 426: return "Upgrade Required";
336 case 428: return "Precondition Required";
337 case 429: return "Too Many Requests";
338 case 431: return "Request Header Fields Too Large";
339 case 449: return "Retry With";
340 case 450: return "Blocked by Windows Parental Controls";
341 case 451: return "Unavailable For Legal Reasons";
342 case 456: return "Unrecoverable Error";
343 case 499: return "client has closed connection";
344 case 500: return "Internal Server Error";
345 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200346 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 503: return "Service Unavailable";
348 case 504: return "Gateway Time-out";
349 case 505: return "HTTP Version not supported";
350 case 506: return "Variant also negociate";
351 case 507: return "Insufficient storage";
352 case 508: return "Loop detected";
353 case 509: return "Bandwidth Limit Exceeded";
354 case 510: return "Not extended";
355 case 511: return "Network authentication required";
356 case 520: return "Web server is returning an unknown error";
357 default:
358 switch (status) {
359 case 100 ... 199: return "Informational";
360 case 200 ... 299: return "Success";
361 case 300 ... 399: return "Redirection";
362 case 400 ... 499: return "Client Error";
363 case 500 ... 599: return "Server Error";
364 default: return "Other";
365 }
366 }
367}
368
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200369/* This function returns HTTP_ERR_<num> (enum) matching http status code.
370 * Returned value should match codes from http_err_codes.
371 */
372static const int http_get_status_idx(unsigned int status)
373{
374 switch (status) {
375 case 200: return HTTP_ERR_200;
376 case 400: return HTTP_ERR_400;
377 case 403: return HTTP_ERR_403;
378 case 405: return HTTP_ERR_405;
379 case 408: return HTTP_ERR_408;
380 case 429: return HTTP_ERR_429;
381 case 500: return HTTP_ERR_500;
382 case 502: return HTTP_ERR_502;
383 case 503: return HTTP_ERR_503;
384 case 504: return HTTP_ERR_504;
385 default: return HTTP_ERR_500;
386 }
387}
388
Willy Tarreau80587432006-12-24 17:47:20 +0100389void init_proto_http()
390{
Willy Tarreau42250582007-04-01 01:30:43 +0200391 int i;
392 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100393 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200394
Willy Tarreau80587432006-12-24 17:47:20 +0100395 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
396 if (!http_err_msgs[msg]) {
397 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
398 abort();
399 }
400
401 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
402 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
403 }
Willy Tarreau42250582007-04-01 01:30:43 +0200404
405 /* initialize the log header encoding map : '{|}"#' should be encoded with
406 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
407 * URL encoding only requires '"', '#' to be encoded as well as non-
408 * printable characters above.
409 */
410 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
411 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100412 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200413 for (i = 0; i < 32; i++) {
414 FD_SET(i, hdr_encode_map);
415 FD_SET(i, url_encode_map);
416 }
417 for (i = 127; i < 256; i++) {
418 FD_SET(i, hdr_encode_map);
419 FD_SET(i, url_encode_map);
420 }
421
422 tmp = "\"#{|}";
423 while (*tmp) {
424 FD_SET(*tmp, hdr_encode_map);
425 tmp++;
426 }
427
428 tmp = "\"#";
429 while (*tmp) {
430 FD_SET(*tmp, url_encode_map);
431 tmp++;
432 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200433
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100434 /* initialize the http header encoding map. The draft httpbis define the
435 * header content as:
436 *
437 * HTTP-message = start-line
438 * *( header-field CRLF )
439 * CRLF
440 * [ message-body ]
441 * header-field = field-name ":" OWS field-value OWS
442 * field-value = *( field-content / obs-fold )
443 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
444 * obs-fold = CRLF 1*( SP / HTAB )
445 * field-vchar = VCHAR / obs-text
446 * VCHAR = %x21-7E
447 * obs-text = %x80-FF
448 *
449 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
450 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
451 * "obs-fold" is volontary forgotten because haproxy remove this.
452 */
453 memset(http_encode_map, 0, sizeof(http_encode_map));
454 for (i = 0x00; i <= 0x08; i++)
455 FD_SET(i, http_encode_map);
456 for (i = 0x0a; i <= 0x1f; i++)
457 FD_SET(i, http_encode_map);
458 FD_SET(0x7f, http_encode_map);
459
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200460 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200461 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200462 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100463 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
Willy Tarreau53b6c742006-12-17 13:37:46 +0100466/*
467 * We have 26 list of methods (1 per first letter), each of which can have
468 * up to 3 entries (2 valid, 1 null).
469 */
470struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100471 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 int len;
473 const char text[8];
474};
475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100476const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477 ['C' - 'A'] = {
478 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
479 },
480 ['D' - 'A'] = {
481 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
482 },
483 ['G' - 'A'] = {
484 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
485 },
486 ['H' - 'A'] = {
487 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
488 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200489 ['O' - 'A'] = {
490 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
491 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100492 ['P' - 'A'] = {
493 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
494 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
495 },
496 ['T' - 'A'] = {
497 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
498 },
499 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200500 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 */
502};
503
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100505 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
506 [HTTP_METH_GET] = { "GET", 3 },
507 [HTTP_METH_HEAD] = { "HEAD", 4 },
508 [HTTP_METH_POST] = { "POST", 4 },
509 [HTTP_METH_PUT] = { "PUT", 3 },
510 [HTTP_METH_DELETE] = { "DELETE", 6 },
511 [HTTP_METH_TRACE] = { "TRACE", 5 },
512 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
513};
514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100515/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200516 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100517 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
518 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
519 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
520 * digit. Note: please do not overwrite values in assignment since gcc-2.95
521 * will not handle them correctly. It's worth noting that chars 128..255 are
522 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100523 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100524const unsigned char http_char_classes[256] = {
525 [ 0] = HTTP_FLG_CTL,
526 [ 1] = HTTP_FLG_CTL,
527 [ 2] = HTTP_FLG_CTL,
528 [ 3] = HTTP_FLG_CTL,
529 [ 4] = HTTP_FLG_CTL,
530 [ 5] = HTTP_FLG_CTL,
531 [ 6] = HTTP_FLG_CTL,
532 [ 7] = HTTP_FLG_CTL,
533 [ 8] = HTTP_FLG_CTL,
534 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
535 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
536 [ 11] = HTTP_FLG_CTL,
537 [ 12] = HTTP_FLG_CTL,
538 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
539 [ 14] = HTTP_FLG_CTL,
540 [ 15] = HTTP_FLG_CTL,
541 [ 16] = HTTP_FLG_CTL,
542 [ 17] = HTTP_FLG_CTL,
543 [ 18] = HTTP_FLG_CTL,
544 [ 19] = HTTP_FLG_CTL,
545 [ 20] = HTTP_FLG_CTL,
546 [ 21] = HTTP_FLG_CTL,
547 [ 22] = HTTP_FLG_CTL,
548 [ 23] = HTTP_FLG_CTL,
549 [ 24] = HTTP_FLG_CTL,
550 [ 25] = HTTP_FLG_CTL,
551 [ 26] = HTTP_FLG_CTL,
552 [ 27] = HTTP_FLG_CTL,
553 [ 28] = HTTP_FLG_CTL,
554 [ 29] = HTTP_FLG_CTL,
555 [ 30] = HTTP_FLG_CTL,
556 [ 31] = HTTP_FLG_CTL,
557 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
558 ['!'] = HTTP_FLG_TOK,
559 ['"'] = HTTP_FLG_SEP,
560 ['#'] = HTTP_FLG_TOK,
561 ['$'] = HTTP_FLG_TOK,
562 ['%'] = HTTP_FLG_TOK,
563 ['&'] = HTTP_FLG_TOK,
564 [ 39] = HTTP_FLG_TOK,
565 ['('] = HTTP_FLG_SEP,
566 [')'] = HTTP_FLG_SEP,
567 ['*'] = HTTP_FLG_TOK,
568 ['+'] = HTTP_FLG_TOK,
569 [','] = HTTP_FLG_SEP,
570 ['-'] = HTTP_FLG_TOK,
571 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
572 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
573 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
574 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
575 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
576 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
577 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
579 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
580 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
581 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
582 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
583 [':'] = HTTP_FLG_SEP,
584 [';'] = HTTP_FLG_SEP,
585 ['<'] = HTTP_FLG_SEP,
586 ['='] = HTTP_FLG_SEP,
587 ['>'] = HTTP_FLG_SEP,
588 ['?'] = HTTP_FLG_SEP,
589 ['@'] = HTTP_FLG_SEP,
590 ['A'] = HTTP_FLG_TOK,
591 ['B'] = HTTP_FLG_TOK,
592 ['C'] = HTTP_FLG_TOK,
593 ['D'] = HTTP_FLG_TOK,
594 ['E'] = HTTP_FLG_TOK,
595 ['F'] = HTTP_FLG_TOK,
596 ['G'] = HTTP_FLG_TOK,
597 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
598 ['I'] = HTTP_FLG_TOK,
599 ['J'] = HTTP_FLG_TOK,
600 ['K'] = HTTP_FLG_TOK,
601 ['L'] = HTTP_FLG_TOK,
602 ['M'] = HTTP_FLG_TOK,
603 ['N'] = HTTP_FLG_TOK,
604 ['O'] = HTTP_FLG_TOK,
605 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
606 ['Q'] = HTTP_FLG_TOK,
607 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
608 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
609 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
610 ['U'] = HTTP_FLG_TOK,
611 ['V'] = HTTP_FLG_TOK,
612 ['W'] = HTTP_FLG_TOK,
613 ['X'] = HTTP_FLG_TOK,
614 ['Y'] = HTTP_FLG_TOK,
615 ['Z'] = HTTP_FLG_TOK,
616 ['['] = HTTP_FLG_SEP,
617 [ 92] = HTTP_FLG_SEP,
618 [']'] = HTTP_FLG_SEP,
619 ['^'] = HTTP_FLG_TOK,
620 ['_'] = HTTP_FLG_TOK,
621 ['`'] = HTTP_FLG_TOK,
622 ['a'] = HTTP_FLG_TOK,
623 ['b'] = HTTP_FLG_TOK,
624 ['c'] = HTTP_FLG_TOK,
625 ['d'] = HTTP_FLG_TOK,
626 ['e'] = HTTP_FLG_TOK,
627 ['f'] = HTTP_FLG_TOK,
628 ['g'] = HTTP_FLG_TOK,
629 ['h'] = HTTP_FLG_TOK,
630 ['i'] = HTTP_FLG_TOK,
631 ['j'] = HTTP_FLG_TOK,
632 ['k'] = HTTP_FLG_TOK,
633 ['l'] = HTTP_FLG_TOK,
634 ['m'] = HTTP_FLG_TOK,
635 ['n'] = HTTP_FLG_TOK,
636 ['o'] = HTTP_FLG_TOK,
637 ['p'] = HTTP_FLG_TOK,
638 ['q'] = HTTP_FLG_TOK,
639 ['r'] = HTTP_FLG_TOK,
640 ['s'] = HTTP_FLG_TOK,
641 ['t'] = HTTP_FLG_TOK,
642 ['u'] = HTTP_FLG_TOK,
643 ['v'] = HTTP_FLG_TOK,
644 ['w'] = HTTP_FLG_TOK,
645 ['x'] = HTTP_FLG_TOK,
646 ['y'] = HTTP_FLG_TOK,
647 ['z'] = HTTP_FLG_TOK,
648 ['{'] = HTTP_FLG_SEP,
649 ['|'] = HTTP_FLG_TOK,
650 ['}'] = HTTP_FLG_SEP,
651 ['~'] = HTTP_FLG_TOK,
652 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653};
654
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100655/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100656 * Adds a header and its CRLF at the tail of the message's buffer, just before
657 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100658 * The header is also automatically added to the index <hdr_idx>, and the end
659 * of headers is automatically adjusted. The number of bytes added is returned
660 * on success, otherwise <0 is returned indicating an error.
661 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100662int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100663{
664 int bytes, len;
665
666 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200667 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100668 if (!bytes)
669 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100670 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100671 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
672}
673
674/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100675 * Adds a header and its CRLF at the tail of the message's buffer, just before
676 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100677 * the buffer is only opened and the space reserved, but nothing is copied.
678 * The header is also automatically added to the index <hdr_idx>, and the end
679 * of headers is automatically adjusted. The number of bytes added is returned
680 * on success, otherwise <0 is returned indicating an error.
681 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682int http_header_add_tail2(struct http_msg *msg,
683 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100684{
685 int bytes;
686
Willy Tarreau9b28e032012-10-12 23:49:43 +0200687 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100688 if (!bytes)
689 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100690 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100691 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
692}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693
694/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100695 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
696 * If so, returns the position of the first non-space character relative to
697 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
698 * to return a pointer to the place after the first space. Returns 0 if the
699 * header name does not match. Checks are case-insensitive.
700 */
701int http_header_match2(const char *hdr, const char *end,
702 const char *name, int len)
703{
704 const char *val;
705
706 if (hdr + len >= end)
707 return 0;
708 if (hdr[len] != ':')
709 return 0;
710 if (strncasecmp(hdr, name, len) != 0)
711 return 0;
712 val = hdr + len + 1;
713 while (val < end && HTTP_IS_SPHT(*val))
714 val++;
715 if ((val >= end) && (len + 2 <= end - hdr))
716 return len + 2; /* we may replace starting from second space */
717 return val - hdr;
718}
719
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200720/* Find the first or next occurrence of header <name> in message buffer <sol>
721 * using headers index <idx>, and return it in the <ctx> structure. This
722 * structure holds everything necessary to use the header and find next
723 * occurrence. If its <idx> member is 0, the header is searched from the
724 * beginning. Otherwise, the next occurrence is returned. The function returns
725 * 1 when it finds a value, and 0 when there is no more. It is very similar to
726 * http_find_header2() except that it is designed to work with full-line headers
727 * whose comma is not a delimiter but is part of the syntax. As a special case,
728 * if ctx->val is NULL when searching for a new values of a header, the current
729 * header is rescanned. This allows rescanning after a header deletion.
730 */
731int http_find_full_header2(const char *name, int len,
732 char *sol, struct hdr_idx *idx,
733 struct hdr_ctx *ctx)
734{
735 char *eol, *sov;
736 int cur_idx, old_idx;
737
738 cur_idx = ctx->idx;
739 if (cur_idx) {
740 /* We have previously returned a header, let's search another one */
741 sol = ctx->line;
742 eol = sol + idx->v[cur_idx].len;
743 goto next_hdr;
744 }
745
746 /* first request for this header */
747 sol += hdr_idx_first_pos(idx);
748 old_idx = 0;
749 cur_idx = hdr_idx_first_idx(idx);
750 while (cur_idx) {
751 eol = sol + idx->v[cur_idx].len;
752
753 if (len == 0) {
754 /* No argument was passed, we want any header.
755 * To achieve this, we simply build a fake request. */
756 while (sol + len < eol && sol[len] != ':')
757 len++;
758 name = sol;
759 }
760
761 if ((len < eol - sol) &&
762 (sol[len] == ':') &&
763 (strncasecmp(sol, name, len) == 0)) {
764 ctx->del = len;
765 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100766 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200767 sov++;
768
769 ctx->line = sol;
770 ctx->prev = old_idx;
771 ctx->idx = cur_idx;
772 ctx->val = sov - sol;
773 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100774 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200775 eol--;
776 ctx->tws++;
777 }
778 ctx->vlen = eol - sov;
779 return 1;
780 }
781 next_hdr:
782 sol = eol + idx->v[cur_idx].cr + 1;
783 old_idx = cur_idx;
784 cur_idx = idx->v[cur_idx].next;
785 }
786 return 0;
787}
788
Willy Tarreauc90dc232015-02-20 13:51:36 +0100789/* Find the first or next header field in message buffer <sol> using headers
790 * index <idx>, and return it in the <ctx> structure. This structure holds
791 * everything necessary to use the header and find next occurrence. If its
792 * <idx> member is 0, the first header is retrieved. Otherwise, the next
793 * occurrence is returned. The function returns 1 when it finds a value, and
794 * 0 when there is no more. It is equivalent to http_find_full_header2() with
795 * no header name.
796 */
797int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
798{
799 char *eol, *sov;
800 int cur_idx, old_idx;
801 int len;
802
803 cur_idx = ctx->idx;
804 if (cur_idx) {
805 /* We have previously returned a header, let's search another one */
806 sol = ctx->line;
807 eol = sol + idx->v[cur_idx].len;
808 goto next_hdr;
809 }
810
811 /* first request for this header */
812 sol += hdr_idx_first_pos(idx);
813 old_idx = 0;
814 cur_idx = hdr_idx_first_idx(idx);
815 while (cur_idx) {
816 eol = sol + idx->v[cur_idx].len;
817
818 len = 0;
819 while (1) {
820 if (len >= eol - sol)
821 goto next_hdr;
822 if (sol[len] == ':')
823 break;
824 len++;
825 }
826
827 ctx->del = len;
828 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100829 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100830 sov++;
831
832 ctx->line = sol;
833 ctx->prev = old_idx;
834 ctx->idx = cur_idx;
835 ctx->val = sov - sol;
836 ctx->tws = 0;
837
Willy Tarreau2235b262016-11-05 15:50:20 +0100838 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100839 eol--;
840 ctx->tws++;
841 }
842 ctx->vlen = eol - sov;
843 return 1;
844
845 next_hdr:
846 sol = eol + idx->v[cur_idx].cr + 1;
847 old_idx = cur_idx;
848 cur_idx = idx->v[cur_idx].next;
849 }
850 return 0;
851}
852
Willy Tarreau68085d82010-01-18 14:54:04 +0100853/* Find the end of the header value contained between <s> and <e>. See RFC2616,
854 * par 2.2 for more information. Note that it requires a valid header to return
855 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200856 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100857char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858{
859 int quoted, qdpair;
860
861 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100862
863#if defined(__x86_64__) || \
864 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
865 defined(__ARM_ARCH_7A__)
866 /* speedup: skip everything not a comma nor a double quote */
867 for (; s <= e - sizeof(int); s += sizeof(int)) {
868 unsigned int c = *(int *)s; // comma
869 unsigned int q = c; // quote
870
871 c ^= 0x2c2c2c2c; // contains one zero on a comma
872 q ^= 0x22222222; // contains one zero on a quote
873
874 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
875 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
876
877 if ((c | q) & 0x80808080)
878 break; // found a comma or a quote
879 }
880#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200881 for (; s < e; s++) {
882 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200883 else if (quoted) {
884 if (*s == '\\') qdpair = 1;
885 else if (*s == '"') quoted = 0;
886 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200887 else if (*s == '"') quoted = 1;
888 else if (*s == ',') return s;
889 }
890 return s;
891}
892
893/* Find the first or next occurrence of header <name> in message buffer <sol>
894 * using headers index <idx>, and return it in the <ctx> structure. This
895 * structure holds everything necessary to use the header and find next
896 * occurrence. If its <idx> member is 0, the header is searched from the
897 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 * 1 when it finds a value, and 0 when there is no more. It is designed to work
899 * with headers defined as comma-separated lists. As a special case, if ctx->val
900 * is NULL when searching for a new values of a header, the current header is
901 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200902 */
903int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200905 struct hdr_ctx *ctx)
906{
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 char *eol, *sov;
908 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909
Willy Tarreau68085d82010-01-18 14:54:04 +0100910 cur_idx = ctx->idx;
911 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200912 /* We have previously returned a value, let's search
913 * another one on the same line.
914 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200915 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200916 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100917 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 eol = sol + idx->v[cur_idx].len;
919
920 if (sov >= eol)
921 /* no more values in this header */
922 goto next_hdr;
923
Willy Tarreau68085d82010-01-18 14:54:04 +0100924 /* values remaining for this header, skip the comma but save it
925 * for later use (eg: for header deletion).
926 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200927 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100928 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 sov++;
930
931 goto return_hdr;
932 }
933
934 /* first request for this header */
935 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100936 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200938 while (cur_idx) {
939 eol = sol + idx->v[cur_idx].len;
940
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200941 if (len == 0) {
942 /* No argument was passed, we want any header.
943 * To achieve this, we simply build a fake request. */
944 while (sol + len < eol && sol[len] != ':')
945 len++;
946 name = sol;
947 }
948
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 if ((len < eol - sol) &&
950 (sol[len] == ':') &&
951 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100952 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200953 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100954 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100956
Willy Tarreau33a7e692007-06-10 19:45:56 +0200957 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100958 ctx->prev = old_idx;
959 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200960 ctx->idx = cur_idx;
961 ctx->val = sov - sol;
962
963 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200964 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100965 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200966 eol--;
967 ctx->tws++;
968 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200969 ctx->vlen = eol - sov;
970 return 1;
971 }
972 next_hdr:
973 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100974 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200975 cur_idx = idx->v[cur_idx].next;
976 }
977 return 0;
978}
979
980int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100981 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200982 struct hdr_ctx *ctx)
983{
984 return http_find_header2(name, strlen(name), sol, idx, ctx);
985}
986
Willy Tarreau68085d82010-01-18 14:54:04 +0100987/* Remove one value of a header. This only works on a <ctx> returned by one of
988 * the http_find_header functions. The value is removed, as well as surrounding
989 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100990 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100991 * message <msg>. The new index is returned. If it is zero, it means there is
992 * no more header, so any processing may stop. The ctx is always left in a form
993 * that can be handled by http_find_header2() to find next occurrence.
994 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100995int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100996{
997 int cur_idx = ctx->idx;
998 char *sol = ctx->line;
999 struct hdr_idx_elem *hdr;
1000 int delta, skip_comma;
1001
1002 if (!cur_idx)
1003 return 0;
1004
1005 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001006 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +01001007 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +01001009 http_msg_move_end(msg, delta);
1010 idx->used--;
1011 hdr->len = 0; /* unused entry */
1012 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +01001013 if (idx->tail == ctx->idx)
1014 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +01001016 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +01001017 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001018 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001019 return ctx->idx;
1020 }
1021
1022 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001023 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1024 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001025 */
1026
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001027 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001028 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001029 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001030 NULL, 0);
1031 hdr->len += delta;
1032 http_msg_move_end(msg, delta);
1033 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001034 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001035 return ctx->idx;
1036}
1037
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001038/* This function handles a server error at the stream interface level. The
1039 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +01001040 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001041 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001042 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001044static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +01001045 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046{
Willy Tarreau2019f952017-03-14 11:07:31 +01001047 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001048 channel_auto_read(si_oc(si));
1049 channel_abort(si_oc(si));
1050 channel_auto_close(si_oc(si));
1051 channel_erase(si_oc(si));
1052 channel_auto_close(si_ic(si));
1053 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001054 if (msg)
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001055 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001056 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001057 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001059 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060}
1061
Willy Tarreau87b09662015-04-03 00:22:06 +02001062/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001063 * and message.
1064 */
1065
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001066struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +01001067{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001068 const int msgnum = http_get_status_idx(s->txn->status);
1069
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001070 if (s->be->errmsg[msgnum].str)
1071 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001072 else if (strm_fe(s)->errmsg[msgnum].str)
1073 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001074 else
1075 return &http_err_chunks[msgnum];
1076}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001078void
1079http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1080{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001081 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001082 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001083 stream_int_retnclose(&s->si[0], msg);
1084}
1085
Willy Tarreau53b6c742006-12-17 13:37:46 +01001086/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001087 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1088 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001089 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001090enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001091{
1092 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001094
1095 m = ((unsigned)*str - 'A');
1096
1097 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 for (h = http_methods[m]; h->len > 0; h++) {
1099 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001100 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001102 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001103 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001104 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001105 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001106}
1107
Willy Tarreau21d2af32008-02-14 20:25:24 +01001108/* Parse the URI from the given transaction (which is assumed to be in request
1109 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1110 * It is returned otherwise.
1111 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001112char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001113{
1114 char *ptr, *end;
1115
Willy Tarreau9b28e032012-10-12 23:49:43 +02001116 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001117 end = ptr + txn->req.sl.rq.u_l;
1118
1119 if (ptr >= end)
1120 return NULL;
1121
1122 /* RFC2616, par. 5.1.2 :
1123 * Request-URI = "*" | absuri | abspath | authority
1124 */
1125
1126 if (*ptr == '*')
1127 return NULL;
1128
1129 if (isalpha((unsigned char)*ptr)) {
1130 /* this is a scheme as described by RFC3986, par. 3.1 */
1131 ptr++;
1132 while (ptr < end &&
1133 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1134 ptr++;
1135 /* skip '://' */
1136 if (ptr == end || *ptr++ != ':')
1137 return NULL;
1138 if (ptr == end || *ptr++ != '/')
1139 return NULL;
1140 if (ptr == end || *ptr++ != '/')
1141 return NULL;
1142 }
1143 /* skip [user[:passwd]@]host[:[port]] */
1144
1145 while (ptr < end && *ptr != '/')
1146 ptr++;
1147
1148 if (ptr == end)
1149 return NULL;
1150
1151 /* OK, we got the '/' ! */
1152 return ptr;
1153}
1154
William Lallemand65ad6e12014-01-31 15:08:02 +01001155/* Parse the URI from the given string and look for the "/" beginning the PATH.
1156 * If not found, return NULL. It is returned otherwise.
1157 */
1158static char *
1159http_get_path_from_string(char *str)
1160{
1161 char *ptr = str;
1162
1163 /* RFC2616, par. 5.1.2 :
1164 * Request-URI = "*" | absuri | abspath | authority
1165 */
1166
1167 if (*ptr == '*')
1168 return NULL;
1169
1170 if (isalpha((unsigned char)*ptr)) {
1171 /* this is a scheme as described by RFC3986, par. 3.1 */
1172 ptr++;
1173 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1174 ptr++;
1175 /* skip '://' */
1176 if (*ptr == '\0' || *ptr++ != ':')
1177 return NULL;
1178 if (*ptr == '\0' || *ptr++ != '/')
1179 return NULL;
1180 if (*ptr == '\0' || *ptr++ != '/')
1181 return NULL;
1182 }
1183 /* skip [user[:passwd]@]host[:[port]] */
1184
1185 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1186 ptr++;
1187
1188 if (*ptr == '\0' || *ptr == ' ')
1189 return NULL;
1190
1191 /* OK, we got the '/' ! */
1192 return ptr;
1193}
1194
Willy Tarreau71241ab2012-12-27 11:30:54 +01001195/* Returns a 302 for a redirectable request that reaches a server working in
1196 * in redirect mode. This may only be called just after the stream interface
1197 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1198 * follow normal proxy processing. NOTE: this function is designed to support
1199 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001201void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001202{
1203 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001204 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001205 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001206 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001207
1208 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001209 trash.len = strlen(HTTP_302);
1210 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001211
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001212 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001213
Willy Tarreauefb453c2008-10-26 20:49:47 +01001214 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001215 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001216 return;
1217
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001218 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001219 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001220 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1221 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001222 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001223
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001224 /* 3: add the request URI. Since it was already forwarded, we need
1225 * to temporarily rewind the buffer.
1226 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001227 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001228 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001229
Willy Tarreauefb453c2008-10-26 20:49:47 +01001230 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001231 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 +02001232
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001233 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001234
Willy Tarreauefb453c2008-10-26 20:49:47 +01001235 if (!path)
1236 return;
1237
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001238 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001239 return;
1240
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001241 memcpy(trash.str + trash.len, path, len);
1242 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001243
1244 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001245 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1246 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001247 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001248 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1249 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001250 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001251
1252 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001253 si_shutr(si);
1254 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001255 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001256 si->state = SI_ST_CLO;
1257
1258 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001259 txn->status = 302;
1260 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001261
1262 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001263 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001264 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001265}
1266
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001267/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001268 * that the server side is closed. Note that err_type is actually a
1269 * bitmask, where almost only aborts may be cumulated with other
1270 * values. We consider that aborted operations are more important
1271 * than timeouts or errors due to the fact that nobody else in the
1272 * logs might explain incomplete retries. All others should avoid
1273 * being cumulated. It should normally not be possible to have multiple
1274 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001275 * Note that connection errors appearing on the second request of a keep-alive
1276 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001277 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001278void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001279{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001280 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001281
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001282 /* set s->txn->status for http_error_message(s) */
1283 s->txn->status = 503;
1284
Willy Tarreauefb453c2008-10-26 20:49:47 +01001285 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001286 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001287 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001288 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001289 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001290 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001291 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001292 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001293 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001294 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001295 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001296 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001297 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001298 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001299 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001300 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001301 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001302 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001303 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001304 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001305 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001306 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001307 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001308 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001309 http_error_message(s));
1310 else { /* SI_ET_CONN_OTHER and others */
1311 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001312 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001313 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001314 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001315}
1316
Willy Tarreau42250582007-04-01 01:30:43 +02001317extern const char sess_term_cond[8];
1318extern const char sess_fin_state[8];
1319extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001320struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001321struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001322struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001323struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324
Willy Tarreau117f59e2007-03-04 18:17:17 +01001325/*
1326 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001327 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001328 */
1329void capture_headers(char *som, struct hdr_idx *idx,
1330 char **cap, struct cap_hdr *cap_hdr)
1331{
1332 char *eol, *sol, *col, *sov;
1333 int cur_idx;
1334 struct cap_hdr *h;
1335 int len;
1336
1337 sol = som + hdr_idx_first_pos(idx);
1338 cur_idx = hdr_idx_first_idx(idx);
1339
1340 while (cur_idx) {
1341 eol = sol + idx->v[cur_idx].len;
1342
1343 col = sol;
1344 while (col < eol && *col != ':')
1345 col++;
1346
1347 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001348 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001349 sov++;
1350
1351 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001352 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001353 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1354 if (cap[h->index] == NULL)
1355 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001356 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001357
1358 if (cap[h->index] == NULL) {
1359 Alert("HTTP capture : out of memory.\n");
1360 continue;
1361 }
1362
1363 len = eol - sov;
1364 if (len > h->len)
1365 len = h->len;
1366
1367 memcpy(cap[h->index], sov, len);
1368 cap[h->index][len]=0;
1369 }
1370 }
1371 sol = eol + idx->v[cur_idx].cr + 1;
1372 cur_idx = idx->v[cur_idx].next;
1373 }
1374}
1375
1376
Willy Tarreau42250582007-04-01 01:30:43 +02001377/* either we find an LF at <ptr> or we jump to <bad>.
1378 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001379#define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0)
Willy Tarreau42250582007-04-01 01:30:43 +02001380
1381/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1382 * otherwise to <http_msg_ood> with <state> set to <st>.
1383 */
1384#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1385 ptr++; \
1386 if (likely(ptr < end)) \
1387 goto good; \
1388 else { \
1389 state = (st); \
1390 goto http_msg_ood; \
1391 } \
1392 } while (0)
1393
1394
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001396 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001397 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1398 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1399 * will give undefined results.
1400 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1401 * and that msg->sol points to the beginning of the response.
1402 * If a complete line is found (which implies that at least one CR or LF is
1403 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1404 * returned indicating an incomplete line (which does not mean that parts have
1405 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1406 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1407 * upon next call.
1408 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001409 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1411 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001412 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001414const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001415 enum ht_state state, const char *ptr, const char *end,
1416 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001417{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001418 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001419
Willy Tarreau8973c702007-01-21 23:58:29 +01001420 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001423 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1425
1426 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001427 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1429 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001430 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001431 state = HTTP_MSG_ERROR;
1432 break;
1433
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001435 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001437 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 goto http_msg_rpcode;
1439 }
1440 if (likely(HTTP_IS_SPHT(*ptr)))
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1442 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001443 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001444 state = HTTP_MSG_ERROR;
1445 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001446
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001448 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 if (likely(!HTTP_IS_LWS(*ptr)))
1450 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1451
1452 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001453 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001454 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1455 }
1456
1457 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001458 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001459 http_msg_rsp_reason:
1460 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001461 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001462 msg->sl.st.r_l = 0;
1463 goto http_msg_rpline_eol;
1464
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001468 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 goto http_msg_rpreason;
1470 }
1471 if (likely(HTTP_IS_SPHT(*ptr)))
1472 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1473 /* so it's a CR/LF, so there is no reason phrase */
1474 goto http_msg_rsp_reason;
1475
Willy Tarreau8973c702007-01-21 23:58:29 +01001476 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001477 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001478 if (likely(!HTTP_IS_CRLF(*ptr)))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001480 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 http_msg_rpline_eol:
1482 /* We have seen the end of line. Note that we do not
1483 * necessarily have the \n yet, but at least we know that we
1484 * have EITHER \r OR \n, otherwise the response would not be
1485 * complete. We can then record the response length and return
1486 * to the caller which will be able to register it.
1487 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001488 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001489 return ptr;
1490
Willy Tarreau8973c702007-01-21 23:58:29 +01001491 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001492#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001493 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1494 exit(1);
1495#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001496 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 }
1498
1499 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001500 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 if (ret_state)
1502 *ret_state = state;
1503 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001504 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001506}
1507
Willy Tarreau8973c702007-01-21 23:58:29 +01001508/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 * This function parses a request line between <ptr> and <end>, starting with
1510 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1511 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1512 * will give undefined results.
1513 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1514 * and that msg->sol points to the beginning of the request.
1515 * If a complete line is found (which implies that at least one CR or LF is
1516 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1517 * returned indicating an incomplete line (which does not mean that parts have
1518 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1519 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1520 * upon next call.
1521 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001522 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1524 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001525 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001526 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001527const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001528 enum ht_state state, const char *ptr, const char *end,
1529 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001530{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001531 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001535 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 if (likely(HTTP_IS_TOKEN(*ptr)))
1537 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001540 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1542 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 if (likely(HTTP_IS_CRLF(*ptr))) {
1545 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001546 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001548 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001550 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001552 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 msg->sl.rq.v_l = 0;
1554 goto http_msg_rqline_eol;
1555 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001556 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001557 state = HTTP_MSG_ERROR;
1558 break;
1559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001561 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001563 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 goto http_msg_rquri;
1565 }
1566 if (likely(HTTP_IS_SPHT(*ptr)))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1568 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1569 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001573#if defined(__x86_64__) || \
1574 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1575 defined(__ARM_ARCH_7A__)
1576 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1577 while (ptr <= end - sizeof(int)) {
1578 int x = *(int *)ptr - 0x21212121;
1579 if (x & 0x80808080)
1580 break;
1581
1582 x -= 0x5e5e5e5e;
1583 if (!(x & 0x80808080))
1584 break;
1585
1586 ptr += sizeof(int);
1587 }
1588#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001589 if (ptr >= end) {
1590 state = HTTP_MSG_RQURI;
1591 goto http_msg_ood;
1592 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001593 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001594 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001595 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001598 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001602 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001603 /* non-ASCII chars are forbidden unless option
1604 * accept-invalid-http-request is enabled in the frontend.
1605 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001606 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001607 if (msg->err_pos < -1)
1608 goto invalid_char;
1609 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001610 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001611 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1612 }
1613
1614 if (likely(HTTP_IS_CRLF(*ptr))) {
1615 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1616 goto http_msg_req09_uri_e;
1617 }
1618
1619 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001620 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001621 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001622 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001623 state = HTTP_MSG_ERROR;
1624 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001627 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001629 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 goto http_msg_rqver;
1631 }
1632 if (likely(HTTP_IS_SPHT(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1634 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1635 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001639 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001641
1642 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001643 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001644 http_msg_rqline_eol:
1645 /* We have seen the end of line. Note that we do not
1646 * necessarily have the \n yet, but at least we know that we
1647 * have EITHER \r OR \n, otherwise the request would not be
1648 * complete. We can then record the request length and return
1649 * to the caller which will be able to register it.
1650 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001651 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001652 return ptr;
1653 }
1654
1655 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001656 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001657 state = HTTP_MSG_ERROR;
1658 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001661#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1663 exit(1);
1664#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001665 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001669 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 if (ret_state)
1671 *ret_state = state;
1672 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001673 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001677/*
1678 * Returns the data from Authorization header. Function may be called more
1679 * than once so data is stored in txn->auth_data. When no header is found
1680 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001681 * searching again for something we are unable to find anyway. However, if
1682 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001684 */
1685
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001686/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1687 * set according to global.tune.bufsize.
1688 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001689char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001690
1691int
Willy Tarreau87b09662015-04-03 00:22:06 +02001692get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001693{
1694
Willy Tarreaueee5b512015-04-03 23:46:31 +02001695 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001696 struct chunk auth_method;
1697 struct hdr_ctx ctx;
1698 char *h, *p;
1699 int len;
1700
1701#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001702 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001703#endif
1704
1705 if (txn->auth.method == HTTP_AUTH_WRONG)
1706 return 0;
1707
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001708 txn->auth.method = HTTP_AUTH_WRONG;
1709
1710 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001711
1712 if (txn->flags & TX_USE_PX_CONN) {
1713 h = "Proxy-Authorization";
1714 len = strlen(h);
1715 } else {
1716 h = "Authorization";
1717 len = strlen(h);
1718 }
1719
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001720 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001721 return 0;
1722
1723 h = ctx.line + ctx.val;
1724
1725 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001726 len = p - h;
1727 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001728 return 0;
1729
David Carlier7365f7d2016-04-04 11:54:42 +01001730 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1731 return 0;
1732
Willy Tarreau5c557d12016-03-13 08:17:02 +01001733 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001734
1735 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1736
1737 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001738 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001739
1740 if (len < 0)
1741 return 0;
1742
1743
1744 get_http_auth_buff[len] = '\0';
1745
1746 p = strchr(get_http_auth_buff, ':');
1747
1748 if (!p)
1749 return 0;
1750
1751 txn->auth.user = get_http_auth_buff;
1752 *p = '\0';
1753 txn->auth.pass = p+1;
1754
1755 txn->auth.method = HTTP_AUTH_BASIC;
1756 return 1;
1757 }
1758
1759 return 0;
1760}
1761
Willy Tarreau58f10d72006-12-04 02:26:12 +01001762
Willy Tarreau8973c702007-01-21 23:58:29 +01001763/*
1764 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001765 * depending on the initial msg->msg_state. The caller is responsible for
1766 * ensuring that the message does not wrap. The function can be preempted
1767 * everywhere when data are missing and recalled at the exact same location
1768 * with no information loss. The message may even be realigned between two
1769 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001770 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001771 * fields. Note that msg->sol will be initialized after completing the first
1772 * state, so that none of the msg pointers has to be initialized prior to the
1773 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001775void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776{
Willy Tarreau3770f232013-12-07 00:01:53 +01001777 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001779 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001781 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001782 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001783 ptr = buf->p + msg->next;
1784 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 if (unlikely(ptr >= end))
1787 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 /*
1791 * First, states that are specific to the response only.
1792 * We check them first so that request and headers are
1793 * closer to each other (accessed more often).
1794 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001795 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001796 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001797 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001798 /* we have a start of message, but we have to check
1799 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001800 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001801 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001802 if (unlikely(ptr != buf->p)) {
1803 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001804 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001805 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001806 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001807 }
Willy Tarreau26927362012-05-18 23:22:52 +02001808 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001809 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001810 hdr_idx_init(idx);
1811 state = HTTP_MSG_RPVER;
1812 goto http_msg_rpver;
1813 }
1814
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001815 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1816 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001817 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001818 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001819
1820 if (unlikely(*ptr == '\n'))
1821 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1822 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1823 /* stop here */
1824
Willy Tarreau8973c702007-01-21 23:58:29 +01001825 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001826 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001827 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001828 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1829 /* stop here */
1830
Willy Tarreau8973c702007-01-21 23:58:29 +01001831 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001832 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001833 case HTTP_MSG_RPVER_SP:
1834 case HTTP_MSG_RPCODE:
1835 case HTTP_MSG_RPCODE_SP:
1836 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001837 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001838 state, ptr, end,
1839 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001840 if (unlikely(!ptr))
1841 return;
1842
1843 /* we have a full response and we know that we have either a CR
1844 * or an LF at <ptr>.
1845 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001846 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1847
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001849 if (likely(*ptr == '\r'))
1850 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1851 goto http_msg_rpline_end;
1852
Willy Tarreau8973c702007-01-21 23:58:29 +01001853 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001855 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001856 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001857 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1858 /* stop here */
1859
1860 /*
1861 * Second, states that are specific to the request only
1862 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001864 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001866 /* we have a start of message, but we have to check
1867 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001868 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001869 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001870 if (likely(ptr != buf->p)) {
1871 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001872 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001873 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001874 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 }
Willy Tarreau26927362012-05-18 23:22:52 +02001876 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001877 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001878 state = HTTP_MSG_RQMETH;
1879 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001881
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001882 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1883 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001885 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 if (unlikely(*ptr == '\n'))
1888 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1889 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001890 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001893 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001894 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001896 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001899 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 case HTTP_MSG_RQMETH_SP:
1901 case HTTP_MSG_RQURI:
1902 case HTTP_MSG_RQURI_SP:
1903 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001904 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001905 state, ptr, end,
1906 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 if (unlikely(!ptr))
1908 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 /* we have a full request and we know that we have either a CR
1911 * or an LF at <ptr>.
1912 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001913 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001914
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 if (likely(*ptr == '\r'))
1917 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001921 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 /* check for HTTP/0.9 request : no version information available.
1923 * msg->sol must point to the first of CR or LF.
1924 */
1925 if (unlikely(msg->sl.rq.v_l == 0))
1926 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001927
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001928 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001929 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001930 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001931
Willy Tarreau8973c702007-01-21 23:58:29 +01001932 /*
1933 * Common states below
1934 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001935 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001936 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001938 if (likely(!HTTP_IS_CRLF(*ptr))) {
1939 goto http_msg_hdr_name;
1940 }
1941
1942 if (likely(*ptr == '\r'))
1943 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1944 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001947 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001948 /* assumes msg->sol points to the first char */
1949 if (likely(HTTP_IS_TOKEN(*ptr)))
1950 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001951
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001952 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001954
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001955 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1956 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001957 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001958 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001959
1960 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001962
1963 /* and we still accept this non-token character */
1964 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001967 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001968 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969 if (likely(HTTP_IS_SPHT(*ptr)))
1970 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001973 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001974
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001975 if (likely(!HTTP_IS_CRLF(*ptr))) {
1976 goto http_msg_hdr_val;
1977 }
1978
1979 if (likely(*ptr == '\r'))
1980 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1981 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001984 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001985 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001986 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001987
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001988 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001989 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 if (likely(HTTP_IS_SPHT(*ptr))) {
1991 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 for (; buf->p + msg->sov < ptr; msg->sov++)
1993 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001994 goto http_msg_hdr_l1_sp;
1995 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001996 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001997 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001998 goto http_msg_complete_header;
1999
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002000 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002001 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002002 /* assumes msg->sol points to the first char, and msg->sov
2003 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002005
2006 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
2007 * and lower. In fact since most of the time is spent in the loop, we
2008 * also remove the sign bit test so that bytes 0x8e..0x0d break the
2009 * loop, but we don't care since they're very rare in header values.
2010 */
2011#if defined(__x86_64__)
2012 while (ptr <= end - sizeof(long)) {
2013 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
2014 goto http_msg_hdr_val2;
2015 ptr += sizeof(long);
2016 }
2017#endif
2018#if defined(__x86_64__) || \
2019 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
2020 defined(__ARM_ARCH_7A__)
2021 while (ptr <= end - sizeof(int)) {
2022 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
2023 goto http_msg_hdr_val2;
2024 ptr += sizeof(int);
2025 }
2026#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002027 if (ptr >= end) {
2028 state = HTTP_MSG_HDR_VAL;
2029 goto http_msg_ood;
2030 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002031 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002033 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002036 /* Note: we could also copy eol into ->eoh so that we have the
2037 * real header end in case it ends with lots of LWS, but is this
2038 * really needed ?
2039 */
2040 if (likely(*ptr == '\r'))
2041 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2042 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002045 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002046 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002047 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002049 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002050 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002051 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2052 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002053 for (; buf->p + msg->eol < ptr; msg->eol++)
2054 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002055 goto http_msg_hdr_val;
2056 }
2057 http_msg_complete_header:
2058 /*
2059 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002060 * Assumes msg->sol points to the first char, msg->sov points
2061 * to the first character of the value and msg->eol to the
2062 * first CR or LF so we know how the line ends. We insert last
2063 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002064 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002065 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002066 idx, idx->tail) < 0)) {
2067 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002068 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002069 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002072 if (likely(!HTTP_IS_CRLF(*ptr))) {
2073 goto http_msg_hdr_name;
2074 }
2075
2076 if (likely(*ptr == '\r'))
2077 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2078 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002079
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002080 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002081 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002082 /* Assumes msg->sol points to the first of either CR or LF.
2083 * Sets ->sov and ->next to the total header length, ->eoh to
2084 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2085 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002086 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002087 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002089 msg->eoh = msg->sol;
2090 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002091 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002092 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002093 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002094
2095 case HTTP_MSG_ERROR:
2096 /* this may only happen if we call http_msg_analyser() twice with an error */
2097 break;
2098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002099 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002100#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002101 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2102 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002103#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002104 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002105 }
2106 http_msg_ood:
2107 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002108 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002109 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002110 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002112 http_msg_invalid:
2113 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002114 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002115 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002117 return;
2118}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002119
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002120/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2121 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2122 * nothing is done and 1 is returned.
2123 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002124static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002125{
2126 int delta;
2127 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002128 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002129
2130 if (msg->sl.rq.v_l != 0)
2131 return 1;
2132
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002133 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2134 if (txn->meth != HTTP_METH_GET)
2135 return 0;
2136
Willy Tarreau9b28e032012-10-12 23:49:43 +02002137 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002138
2139 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002140 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2141 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002142 }
2143 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002144 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002145 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002146 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002147 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002148 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002149 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002150 NULL, NULL);
2151 if (unlikely(!cur_end))
2152 return 0;
2153
2154 /* we have a full HTTP/1.0 request now and we know that
2155 * we have either a CR or an LF at <ptr>.
2156 */
2157 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2158 return 1;
2159}
2160
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002161/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002162 * and "keep-alive" values. If we already know that some headers may safely
2163 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002164 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2165 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002166 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002167 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2168 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2169 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002170 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002171 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002172void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002173{
Willy Tarreau5b154472009-12-21 20:11:07 +01002174 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002175 const char *hdr_val = "Connection";
2176 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002177
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002178 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002179 return;
2180
Willy Tarreau88d349d2010-01-25 12:15:43 +01002181 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2182 hdr_val = "Proxy-Connection";
2183 hdr_len = 16;
2184 }
2185
Willy Tarreau5b154472009-12-21 20:11:07 +01002186 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002187 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002188 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002189 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2190 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002191 if (to_del & 2)
2192 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002193 else
2194 txn->flags |= TX_CON_KAL_SET;
2195 }
2196 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2197 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002198 if (to_del & 1)
2199 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002200 else
2201 txn->flags |= TX_CON_CLO_SET;
2202 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002203 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2204 txn->flags |= TX_HDR_CONN_UPG;
2205 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002206 }
2207
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002208 txn->flags |= TX_HDR_CONN_PRS;
2209 return;
2210}
Willy Tarreau5b154472009-12-21 20:11:07 +01002211
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002212/* Apply desired changes on the Connection: header. Values may be removed and/or
2213 * added depending on the <wanted> flags, which are exclusively composed of
2214 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2215 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2216 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002217void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002218{
2219 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002220 const char *hdr_val = "Connection";
2221 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002222
2223 ctx.idx = 0;
2224
Willy Tarreau88d349d2010-01-25 12:15:43 +01002225
2226 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2227 hdr_val = "Proxy-Connection";
2228 hdr_len = 16;
2229 }
2230
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002231 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002232 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002233 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2234 if (wanted & TX_CON_KAL_SET)
2235 txn->flags |= TX_CON_KAL_SET;
2236 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002237 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002238 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002239 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2240 if (wanted & TX_CON_CLO_SET)
2241 txn->flags |= TX_CON_CLO_SET;
2242 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002243 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002244 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002245 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002246
2247 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2248 return;
2249
2250 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2251 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002252 hdr_val = "Connection: close";
2253 hdr_len = 17;
2254 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2255 hdr_val = "Proxy-Connection: close";
2256 hdr_len = 23;
2257 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002258 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002259 }
2260
2261 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2262 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002263 hdr_val = "Connection: keep-alive";
2264 hdr_len = 22;
2265 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2266 hdr_val = "Proxy-Connection: keep-alive";
2267 hdr_len = 28;
2268 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002269 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002270 }
2271 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002272}
2273
Christopher Faulet113f7de2015-12-14 14:52:13 +01002274/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2275 * point to the first byte of data after the chunk size, so that we know we can
2276 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2277 * forming the chunk size. That way it is always possible to differentiate
2278 * between the start of the body and the start of the data. Return the number
2279 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2280 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002281 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002282static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002283{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002284 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002285 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002286 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002287 const char *end = buf->data + buf->size;
2288 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002289 unsigned int chunk = 0;
2290
2291 /* The chunk size is in the following form, though we are only
2292 * interested in the size and CRLF :
2293 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2294 */
2295 while (1) {
2296 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002297 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002298 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002299 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002300 if (c < 0) /* not a hex digit anymore */
2301 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002302 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002303 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002304 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002305 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002306 chunk = (chunk << 4) + c;
2307 }
2308
Willy Tarreaud98cf932009-12-27 22:54:55 +01002309 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002310 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002311 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002312
Willy Tarreau2235b262016-11-05 15:50:20 +01002313 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002314 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002315 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002316 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002317 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002318 }
2319
Willy Tarreaud98cf932009-12-27 22:54:55 +01002320 /* Up to there, we know that at least one byte is present at *ptr. Check
2321 * for the end of chunk size.
2322 */
2323 while (1) {
2324 if (likely(HTTP_IS_CRLF(*ptr))) {
2325 /* we now have a CR or an LF at ptr */
2326 if (likely(*ptr == '\r')) {
2327 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002328 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002329 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002330 return 0;
2331 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002332
Willy Tarreaud98cf932009-12-27 22:54:55 +01002333 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002334 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002335 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002336 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002337 /* done */
2338 break;
2339 }
2340 else if (*ptr == ';') {
2341 /* chunk extension, ends at next CRLF */
2342 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002343 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002344 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002345 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002346
2347 while (!HTTP_IS_CRLF(*ptr)) {
2348 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002349 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002350 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002351 return 0;
2352 }
2353 /* we have a CRLF now, loop above */
2354 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002355 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002356 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002357 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002358 }
2359
Christopher Faulet113f7de2015-12-14 14:52:13 +01002360 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2361 * or may not be present. We save the number of bytes parsed into
2362 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002363 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002364 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002365 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002366 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002367 msg->chunk_len = chunk;
2368 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002369 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002370 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002371 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002372 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002373}
2374
Christopher Faulet113f7de2015-12-14 14:52:13 +01002375/* This function skips trailers in the buffer associated with HTTP message
2376 * <msg>. The first visited position is msg->next. If the end of the trailers is
2377 * found, the function returns >0. So, the caller can automatically schedul it
2378 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2379 * data are available, the function does not change anything except maybe
2380 * msg->sol if it could parse some lines, and returns zero. If a parse error
2381 * is encountered, the function returns < 0 and does not change anything except
2382 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2383 * state before calling this function, which implies that all non-trailers data
2384 * have already been scheduled for forwarding, and that msg->next exactly
2385 * matches the length of trailers already parsed and not forwarded. It is also
2386 * important to note that this function is designed to be able to parse wrapped
2387 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002388 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002389static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002390{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002391 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002392
Christopher Faulet113f7de2015-12-14 14:52:13 +01002393 /* we have msg->next which points to next line. Look for CRLF. But
2394 * first, we reset msg->sol */
2395 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002396 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002397 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002398 const char *start = b_ptr(buf, msg->next + msg->sol);
2399 const char *stop = bi_end(buf);
2400 const char *ptr = start;
2401 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002402
2403 /* scan current line and stop at LF or CRLF */
2404 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002405 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002406 return 0;
2407
2408 if (*ptr == '\n') {
2409 if (!p1)
2410 p1 = ptr;
2411 p2 = ptr;
2412 break;
2413 }
2414
2415 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002416 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002417 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002418 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002419 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002420 p1 = ptr;
2421 }
2422
2423 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002424 if (ptr >= buf->data + buf->size)
2425 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002426 }
2427
2428 /* after LF; point to beginning of next line */
2429 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002430 if (p2 >= buf->data + buf->size)
2431 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002432
Christopher Faulet2fb28802015-12-01 10:40:57 +01002433 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002434 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002435 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002436 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002437
Christopher Fauletd7c91962015-04-30 11:48:27 +02002438 /* LF/CRLF at beginning of line => end of trailers at p2.
2439 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002440 * from this message. */
2441 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002442 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002443
Willy Tarreaud98cf932009-12-27 22:54:55 +01002444 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002445 }
2446}
2447
Christopher Faulet113f7de2015-12-14 14:52:13 +01002448/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2449 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2450 * in order to include this part into the next forwarding phase. Note that the
2451 * caller must ensure that ->p points to the first byte to parse. It returns
2452 * the number of bytes parsed on success, so the caller can set msg_state to
2453 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2454 * change anything and returns zero. If a parse error is encountered, the
2455 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2456 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002457 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002458static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002459{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002460 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002461 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002462 int bytes;
2463
2464 /* NB: we'll check data availabilty at the end. It's not a
2465 * problem because whatever we match first will be checked
2466 * against the correct length.
2467 */
2468 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002469 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002470 if (*ptr == '\r') {
2471 bytes++;
2472 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002473 if (ptr >= buf->data + buf->size)
2474 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002475 }
2476
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002477 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002478 return 0;
2479
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002480 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002481 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002482 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002483 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002484 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002485}
Willy Tarreau5b154472009-12-21 20:11:07 +01002486
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002487/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2488 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2489 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2490 * Unparsable qvalues return 1000 as "q=1.000".
2491 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002492int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002493{
2494 int q = 1000;
2495
Willy Tarreau506c69a2014-07-08 00:59:48 +02002496 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002497 goto out;
2498 q = (*qvalue++ - '0') * 1000;
2499
2500 if (*qvalue++ != '.')
2501 goto out;
2502
Willy Tarreau506c69a2014-07-08 00:59:48 +02002503 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002504 goto out;
2505 q += (*qvalue++ - '0') * 100;
2506
Willy Tarreau506c69a2014-07-08 00:59:48 +02002507 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002508 goto out;
2509 q += (*qvalue++ - '0') * 10;
2510
Willy Tarreau506c69a2014-07-08 00:59:48 +02002511 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002512 goto out;
2513 q += (*qvalue++ - '0') * 1;
2514 out:
2515 if (q > 1000)
2516 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002517 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002518 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002519 return q;
2520}
William Lallemand82fe75c2012-10-23 10:25:10 +02002521
Willy Tarreau87b09662015-04-03 00:22:06 +02002522void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002523{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002524 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002525 int tmp = TX_CON_WANT_KAL;
2526
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002527 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2528 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002529 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2530 tmp = TX_CON_WANT_TUN;
2531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2534 tmp = TX_CON_WANT_TUN;
2535 }
2536
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002537 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002538 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2539 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002540 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2542 tmp = TX_CON_WANT_CLO;
2543 else
2544 tmp = TX_CON_WANT_SCL;
2545 }
2546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002547 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002548 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2549 tmp = TX_CON_WANT_CLO;
2550
2551 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2552 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2553
2554 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2555 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2556 /* parse the Connection header and possibly clean it */
2557 int to_del = 0;
2558 if ((msg->flags & HTTP_MSGF_VER_11) ||
2559 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002560 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002561 to_del |= 2; /* remove "keep-alive" */
2562 if (!(msg->flags & HTTP_MSGF_VER_11))
2563 to_del |= 1; /* remove "close" */
2564 http_parse_connection_header(txn, msg, to_del);
2565 }
2566
2567 /* check if client or config asks for explicit close in KAL/SCL */
2568 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2569 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2570 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2571 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2572 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002573 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002574 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2575}
William Lallemand82fe75c2012-10-23 10:25:10 +02002576
Willy Tarreaud787e662009-07-07 10:14:51 +02002577/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2578 * processing can continue on next analysers, or zero if it either needs more
2579 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002580 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002581 * when it has nothing left to do, and may remove any analyser when it wants to
2582 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002583 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002584int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002585{
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /*
2587 * We will parse the partial (or complete) lines.
2588 * We will check the request syntax, and also join multi-line
2589 * headers. An index of all the lines will be elaborated while
2590 * parsing.
2591 *
2592 * For the parsing, we use a 28 states FSM.
2593 *
2594 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002595 * req->buf->p = beginning of request
2596 * req->buf->p + msg->eoh = end of processed headers / start of current one
2597 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002598 * msg->eol = end of current header or line (LF or CRLF)
2599 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002600 *
2601 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002602 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002603 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2604 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002606
Willy Tarreau59234e92008-11-30 23:51:27 +01002607 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002608 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002609 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002611 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002612
Willy Tarreau87b09662015-04-03 00:22:06 +02002613 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 +01002614 now_ms, __FUNCTION__,
2615 s,
2616 req,
2617 req->rex, req->wex,
2618 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002619 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002620 req->analysers);
2621
Willy Tarreau52a0c602009-08-16 22:45:38 +02002622 /* we're speaking HTTP here, so let's speak HTTP to the client */
2623 s->srv_error = http_return_srv_error;
2624
Willy Tarreau83e3af02009-12-28 17:39:57 +01002625 /* There's a protected area at the end of the buffer for rewriting
2626 * purposes. We don't want to start to parse the request if the
2627 * protected area is affected, because we may have to move processed
2628 * data later, which is much more complicated.
2629 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002631
2632 /* This point is executed when some data is avalaible for analysis,
2633 * so we log the end of the idle time. */
2634 if (s->logs.t_idle == -1)
2635 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2636
Willy Tarreau379357a2013-06-08 12:55:46 +02002637 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002638 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002639 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002640 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002641 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002642 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002643 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002644 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002645 return 0;
2646 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002647 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2648 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2649 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002650 }
2651
Willy Tarreau9b28e032012-10-12 23:49:43 +02002652 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002653 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002654 }
2655
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 /* 1: we might have to print this header in debug mode */
2657 if (unlikely((global.mode & MODE_DEBUG) &&
2658 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002659 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002663 /* this is a bit complex : in case of error on the request line,
2664 * we know that rq.l is still zero, so we display only the part
2665 * up to the end of the line (truncated by debug_hdr).
2666 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002667 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 sol += hdr_idx_first_pos(&txn->hdr_idx);
2671 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002672
Willy Tarreau59234e92008-11-30 23:51:27 +01002673 while (cur_idx) {
2674 eol = sol + txn->hdr_idx.v[cur_idx].len;
2675 debug_hdr("clihdr", s, sol, eol);
2676 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2677 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002678 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 }
2680
Willy Tarreau58f10d72006-12-04 02:26:12 +01002681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 /*
2683 * Now we quickly check if we have found a full valid request.
2684 * If not so, we check the FD and buffer states before leaving.
2685 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002686 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002687 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002688 * on a keep-alive stream, if we encounter and error, close, t/o,
2689 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002690 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002691 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002692 * Last, we may increase some tracked counters' http request errors on
2693 * the cases that are deliberately the client's fault. For instance,
2694 * a timeout or connection reset is not counted as an error. However
2695 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002697
Willy Tarreau655dce92009-11-08 13:10:58 +01002698 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002699 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002700 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002701 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002702 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002703 stream_inc_http_req_ctr(s);
2704 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002705 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002708
Willy Tarreau59234e92008-11-30 23:51:27 +01002709 /* 1: Since we are in header mode, if there's no space
2710 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002711 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 * must terminate it now.
2713 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002714 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 /* FIXME: check if URI is set and return Status
2716 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002717 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002718 stream_inc_http_req_ctr(s);
2719 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002720 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002721 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002722 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 goto return_bad_req;
2724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002725
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002727 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002728 if (!(s->flags & SF_ERR_MASK))
2729 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002730
Willy Tarreaufcffa692010-01-10 14:21:19 +01002731 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002732 goto failed_keep_alive;
2733
Willy Tarreau0f228a02015-05-01 15:37:53 +02002734 if (sess->fe->options & PR_O_IGNORE_PRB)
2735 goto failed_keep_alive;
2736
Willy Tarreau59234e92008-11-30 23:51:27 +01002737 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002738 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002739 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002740 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002741 }
2742
Willy Tarreaudc979f22012-12-04 10:39:01 +01002743 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002744 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002746 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002747 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002748 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002749 proxy_inc_fe_req_ctr(sess->fe);
2750 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002751 if (sess->listener->counters)
2752 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreaue7dff022015-04-03 01:14:29 +02002754 if (!(s->flags & SF_FINST_MASK))
2755 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 return 0;
2757 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002758
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002760 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002761 if (!(s->flags & SF_ERR_MASK))
2762 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002763
Willy Tarreaufcffa692010-01-10 14:21:19 +01002764 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002765 goto failed_keep_alive;
2766
Willy Tarreau0f228a02015-05-01 15:37:53 +02002767 if (sess->fe->options & PR_O_IGNORE_PRB)
2768 goto failed_keep_alive;
2769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002771 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002772 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002773 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002774 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002776 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002777 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002778 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002779 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002780
Willy Tarreau87b09662015-04-03 00:22:06 +02002781 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002782 proxy_inc_fe_req_ctr(sess->fe);
2783 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002784 if (sess->listener->counters)
2785 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002786
Willy Tarreaue7dff022015-04-03 01:14:29 +02002787 if (!(s->flags & SF_FINST_MASK))
2788 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 return 0;
2790 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002791
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002793 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002794 if (!(s->flags & SF_ERR_MASK))
2795 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002796
Willy Tarreaufcffa692010-01-10 14:21:19 +01002797 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002798 goto failed_keep_alive;
2799
Willy Tarreau0f228a02015-05-01 15:37:53 +02002800 if (sess->fe->options & PR_O_IGNORE_PRB)
2801 goto failed_keep_alive;
2802
Willy Tarreau4076a152009-04-02 15:18:36 +02002803 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002804 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002806 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002808 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002809 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002810 stream_inc_http_err_ctr(s);
2811 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002812 proxy_inc_fe_req_ctr(sess->fe);
2813 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002814 if (sess->listener->counters)
2815 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002816
Willy Tarreaue7dff022015-04-03 01:14:29 +02002817 if (!(s->flags & SF_FINST_MASK))
2818 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002819 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002820 }
2821
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002822 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002823 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002824 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002825#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002826 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2827 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002828 /* We need more data, we have to re-enable quick-ack in case we
2829 * previously disabled it, otherwise we might cause the client
2830 * to delay next data.
2831 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002832 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002833 }
2834#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002835
Willy Tarreaufcffa692010-01-10 14:21:19 +01002836 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2837 /* If the client starts to talk, let's fall back to
2838 * request timeout processing.
2839 */
2840 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002841 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002842 }
2843
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002845 if (!tick_isset(req->analyse_exp)) {
2846 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2847 (txn->flags & TX_WAIT_NEXT_RQ) &&
2848 tick_isset(s->be->timeout.httpka))
2849 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2850 else
2851 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2852 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002853
Willy Tarreau59234e92008-11-30 23:51:27 +01002854 /* we're not ready yet */
2855 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002856
2857 failed_keep_alive:
2858 /* Here we process low-level errors for keep-alive requests. In
2859 * short, if the request is not the first one and it experiences
2860 * a timeout, read error or shutdown, we just silently close so
2861 * that the client can try again.
2862 */
2863 txn->status = 0;
2864 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002865 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002866 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002867 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002868 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002869 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002870 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002872
Willy Tarreaud787e662009-07-07 10:14:51 +02002873 /* OK now we have a complete HTTP request with indexed headers. Let's
2874 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002875 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002876 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002877 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002878 * byte after the last LF. msg->sov points to the first byte of data.
2879 * msg->eol cannot be trusted because it may have been left uninitialized
2880 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002881 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002882
Willy Tarreau87b09662015-04-03 00:22:06 +02002883 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002884 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002885
Willy Tarreaub16a5742010-01-10 14:46:16 +01002886 if (txn->flags & TX_WAIT_NEXT_RQ) {
2887 /* kill the pending keep-alive timeout */
2888 txn->flags &= ~TX_WAIT_NEXT_RQ;
2889 req->analyse_exp = TICK_ETERNITY;
2890 }
2891
2892
Willy Tarreaud787e662009-07-07 10:14:51 +02002893 /* Maybe we found in invalid header name while we were configured not
2894 * to block on that, so we have to capture it now.
2895 */
2896 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002897 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002898
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 /*
2900 * 1: identify the method
2901 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002902 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002903
2904 /* we can make use of server redirect on GET and HEAD */
2905 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002906 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002907
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 /*
2909 * 2: check if the URI matches the monitor_uri.
2910 * We have to do this for every request which gets in, because
2911 * the monitor-uri is defined by the frontend.
2912 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002913 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2914 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002915 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002916 sess->fe->monitor_uri,
2917 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002918 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002920 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002922
Willy Tarreaue7dff022015-04-03 01:14:29 +02002923 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002924 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002925
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002927 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002928 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002929
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 ret = acl_pass(ret);
2931 if (cond->pol == ACL_COND_UNLESS)
2932 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002933
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 if (ret) {
2935 /* we fail this request, let's return 503 service unavail */
2936 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002937 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002938 if (!(s->flags & SF_ERR_MASK))
2939 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002941 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002942 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002943
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 /* nothing to fail, let's reply normaly */
2945 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002946 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002947 if (!(s->flags & SF_ERR_MASK))
2948 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 goto return_prx_cond;
2950 }
2951
2952 /*
2953 * 3: Maybe we have to copy the original REQURI for the logs ?
2954 * Note: we cannot log anymore if the request has been
2955 * classified as invalid.
2956 */
2957 if (unlikely(s->logs.logwait & LW_REQ)) {
2958 /* we have a complete HTTP request that we must log */
2959 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2960 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002961
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 if (urilen >= REQURI_LEN)
2963 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002964 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002966
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002967 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002968 s->do_log(s);
2969 } else {
2970 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002971 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 }
Willy Tarreau06619262006-12-17 08:37:22 +01002973
Willy Tarreau91852eb2015-05-01 13:26:00 +02002974 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2975 * exactly one digit "." one digit. This check may be disabled using
2976 * option accept-invalid-http-request.
2977 */
2978 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2979 if (msg->sl.rq.v_l != 8) {
2980 msg->err_pos = msg->sl.rq.v;
2981 goto return_bad_req;
2982 }
2983
2984 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2985 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2986 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2987 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2988 msg->err_pos = msg->sl.rq.v + 4;
2989 goto return_bad_req;
2990 }
2991 }
Willy Tarreau13317662015-05-01 13:47:08 +02002992 else {
2993 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2994 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2995 goto return_bad_req;
2996 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002997
Willy Tarreau5b154472009-12-21 20:11:07 +01002998 /* ... and check if the request is HTTP/1.1 or above */
2999 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003000 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3001 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3002 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003003 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003004
3005 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003006 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 +01003007
Willy Tarreau88d349d2010-01-25 12:15:43 +01003008 /* if the frontend has "option http-use-proxy-header", we'll check if
3009 * we have what looks like a proxied connection instead of a connection,
3010 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3011 * Note that this is *not* RFC-compliant, however browsers and proxies
3012 * happen to do that despite being non-standard :-(
3013 * We consider that a request not beginning with either '/' or '*' is
3014 * a proxied connection, which covers both "scheme://location" and
3015 * CONNECT ip:port.
3016 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003017 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003018 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003019 txn->flags |= TX_USE_PX_CONN;
3020
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003021 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003022 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003023
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003025 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003026 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003027 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003028
Willy Tarreau557f1992015-05-01 10:05:17 +02003029 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3030 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003031 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003032 * The length of a message body is determined by one of the following
3033 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003034 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003035 * 1. Any response to a HEAD request and any response with a 1xx
3036 * (Informational), 204 (No Content), or 304 (Not Modified) status
3037 * code is always terminated by the first empty line after the
3038 * header fields, regardless of the header fields present in the
3039 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003040 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003041 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3042 * the connection will become a tunnel immediately after the empty
3043 * line that concludes the header fields. A client MUST ignore any
3044 * Content-Length or Transfer-Encoding header fields received in
3045 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003046 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003047 * 3. If a Transfer-Encoding header field is present and the chunked
3048 * transfer coding (Section 4.1) is the final encoding, the message
3049 * body length is determined by reading and decoding the chunked
3050 * data until the transfer coding indicates the data is complete.
3051 *
3052 * If a Transfer-Encoding header field is present in a response and
3053 * the chunked transfer coding is not the final encoding, the
3054 * message body length is determined by reading the connection until
3055 * it is closed by the server. If a Transfer-Encoding header field
3056 * is present in a request and the chunked transfer coding is not
3057 * the final encoding, the message body length cannot be determined
3058 * reliably; the server MUST respond with the 400 (Bad Request)
3059 * status code and then close the connection.
3060 *
3061 * If a message is received with both a Transfer-Encoding and a
3062 * Content-Length header field, the Transfer-Encoding overrides the
3063 * Content-Length. Such a message might indicate an attempt to
3064 * perform request smuggling (Section 9.5) or response splitting
3065 * (Section 9.4) and ought to be handled as an error. A sender MUST
3066 * remove the received Content-Length field prior to forwarding such
3067 * a message downstream.
3068 *
3069 * 4. If a message is received without Transfer-Encoding and with
3070 * either multiple Content-Length header fields having differing
3071 * field-values or a single Content-Length header field having an
3072 * invalid value, then the message framing is invalid and the
3073 * recipient MUST treat it as an unrecoverable error. If this is a
3074 * request message, the server MUST respond with a 400 (Bad Request)
3075 * status code and then close the connection. If this is a response
3076 * message received by a proxy, the proxy MUST close the connection
3077 * to the server, discard the received response, and send a 502 (Bad
3078 * Gateway) response to the client. If this is a response message
3079 * received by a user agent, the user agent MUST close the
3080 * connection to the server and discard the received response.
3081 *
3082 * 5. If a valid Content-Length header field is present without
3083 * Transfer-Encoding, its decimal value defines the expected message
3084 * body length in octets. If the sender closes the connection or
3085 * the recipient times out before the indicated number of octets are
3086 * received, the recipient MUST consider the message to be
3087 * incomplete and close the connection.
3088 *
3089 * 6. If this is a request message and none of the above are true, then
3090 * the message body length is zero (no message body is present).
3091 *
3092 * 7. Otherwise, this is a response message without a declared message
3093 * body length, so the message body length is determined by the
3094 * number of octets received prior to the server closing the
3095 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003096 */
3097
Willy Tarreau32b47f42009-10-18 20:55:02 +02003098 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003099 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003100 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003101 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02003102 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003103 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003104 /* chunked not last, return badreq */
3105 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003106 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 }
3108
Willy Tarreau1c913912015-04-30 10:57:51 +02003109 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003110 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003111 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3112 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3113 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3114 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003115 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003116 signed long long cl;
3117
Willy Tarreauad14f752011-09-02 20:33:27 +02003118 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003119 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003120 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003121 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003122
Willy Tarreauad14f752011-09-02 20:33:27 +02003123 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003124 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003125 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003126 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003127
Willy Tarreauad14f752011-09-02 20:33:27 +02003128 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003129 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003130 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003131 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003132
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003133 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003134 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003135 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003136 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003137
Christopher Fauletbe821b92017-03-30 11:21:53 +02003138 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003139 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003140 }
3141
Willy Tarreau34dfc602015-05-01 10:09:49 +02003142 /* even bodyless requests have a known length */
3143 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003144
Willy Tarreau179085c2014-04-28 16:48:56 +02003145 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3146 * only change if both the request and the config reference something else.
3147 * Option httpclose by itself sets tunnel mode where headers are mangled.
3148 * However, if another mode is set, it will affect it (eg: server-close/
3149 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3150 * if FE and BE have the same settings (common). The method consists in
3151 * checking if options changed between the two calls (implying that either
3152 * one is non-null, or one of them is non-null and we are there for the first
3153 * time.
3154 */
3155 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003156 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003157 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003158
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003159 /* we may have to wait for the request's body */
3160 if ((s->be->options & PR_O_WREQ_BODY) &&
3161 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3162 req->analysers |= AN_REQ_HTTP_BODY;
3163
Willy Tarreaud787e662009-07-07 10:14:51 +02003164 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003165 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003166 req->analyse_exp = TICK_ETERNITY;
3167 return 1;
3168
3169 return_bad_req:
3170 /* We centralize bad requests processing here */
3171 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3172 /* we detected a parsing error. We want to archive this request
3173 * in the dedicated proxy area for later troubleshooting.
3174 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003175 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003176 }
3177
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003178 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003179 txn->req.msg_state = HTTP_MSG_ERROR;
3180 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003181 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003182
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003183 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003184 if (sess->listener->counters)
3185 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003186
3187 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003188 if (!(s->flags & SF_ERR_MASK))
3189 s->flags |= SF_ERR_PRXCOND;
3190 if (!(s->flags & SF_FINST_MASK))
3191 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003192
Christopher Faulet0184ea72017-01-05 14:06:34 +01003193 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003194 req->analyse_exp = TICK_ETERNITY;
3195 return 0;
3196}
3197
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003198
Willy Tarreau347a35d2013-11-22 17:51:09 +01003199/* This function prepares an applet to handle the stats. It can deal with the
3200 * "100-continue" expectation, check that admin rules are met for POST requests,
3201 * and program a response message if something was unexpected. It cannot fail
3202 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003203 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003204 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003205 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003206 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003207int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003208{
3209 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003210 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003211 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003212 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003213 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003214 struct uri_auth *uri_auth = s->be->uri_auth;
3215 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003217
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003218 appctx = si_appctx(si);
3219 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3220 appctx->st1 = appctx->st2 = 0;
3221 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3222 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003223 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003224 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003225
3226 uri = msg->chn->buf->p + msg->sl.rq.u;
3227 lookup = uri + uri_auth->uri_len;
3228
3229 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3230 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003231 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003232 break;
3233 }
3234 }
3235
3236 if (uri_auth->refresh) {
3237 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3238 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003239 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003240 break;
3241 }
3242 }
3243 }
3244
3245 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3246 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003247 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003248 break;
3249 }
3250 }
3251
Willy Tarreau1e62df92016-01-11 18:57:53 +01003252 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3253 if (memcmp(h, ";typed", 6) == 0) {
3254 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3255 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3256 break;
3257 }
3258 }
3259
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003260 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3261 if (memcmp(h, ";st=", 4) == 0) {
3262 int i;
3263 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003264 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003265 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3266 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003267 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003268 break;
3269 }
3270 }
3271 break;
3272 }
3273 }
3274
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 appctx->ctx.stats.scope_str = 0;
3276 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003277 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3278 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3279 int itx = 0;
3280 const char *h2;
3281 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3282 const char *err;
3283
3284 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3285 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003286 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003287 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3288 itx++;
3289 h++;
3290 }
3291
3292 if (itx > STAT_SCOPE_TXT_MAXLEN)
3293 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003294 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003296 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003297 memcpy(scope_txt, h2, itx);
3298 scope_txt[itx] = '\0';
3299 err = invalid_char(scope_txt);
3300 if (err) {
3301 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003302 appctx->ctx.stats.scope_str = 0;
3303 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003304 }
3305 break;
3306 }
3307 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003308
3309 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003310 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003311 int ret = 1;
3312
3313 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003314 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 +01003315 ret = acl_pass(ret);
3316 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3317 ret = !ret;
3318 }
3319
3320 if (ret) {
3321 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003322 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003323 break;
3324 }
3325 }
3326
3327 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003328 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003330 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003331 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3332 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003333 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003334 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003335 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003336 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3337 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003338 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003339 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003340 else {
3341 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003342 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003343 }
3344
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003345 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003346 return 1;
3347}
3348
Lukas Tribus67db8df2013-06-23 17:37:13 +02003349/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3350 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3351 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003352void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003353{
3354#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003355 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003356 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3357#endif
3358#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003359 if (from->ss_family == AF_INET6) {
3360 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003361 /* v4-mapped addresses need IP_TOS */
3362 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3363 else
3364 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3365 }
3366#endif
3367}
3368
Willy Tarreau87b09662015-04-03 00:22:06 +02003369int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003370 const char* name, unsigned int name_len,
3371 const char *str, struct my_regex *re,
3372 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003373{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003374 struct hdr_ctx ctx;
3375 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003376 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003377 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3378 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003379 struct chunk *output = get_trash_chunk();
3380
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003381 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003382
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003383 /* Choose the header browsing function. */
3384 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003385 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003386 http_find_hdr_func = http_find_header2;
3387 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003388 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003389 http_find_hdr_func = http_find_full_header2;
3390 break;
3391 default: /* impossible */
3392 return -1;
3393 }
3394
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003395 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3396 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003397 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003398 char *val = ctx.line + ctx.val;
3399 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003400
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003401 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3402 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003403
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003404 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003405 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003406 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003407
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003408 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003409
3410 hdr->len += delta;
3411 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003412
3413 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003414 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003415 }
3416
3417 return 0;
3418}
3419
Willy Tarreau87b09662015-04-03 00:22:06 +02003420static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003421 const char* name, unsigned int name_len,
3422 struct list *fmt, struct my_regex *re,
3423 int action)
3424{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003425 struct chunk *replace;
3426 int ret = -1;
3427
3428 replace = alloc_trash_chunk();
3429 if (!replace)
3430 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003431
3432 replace->len = build_logline(s, replace->str, replace->size, fmt);
3433 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003434 goto leave;
3435
3436 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003437
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003438 leave:
3439 free_trash_chunk(replace);
3440 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003441}
3442
Willy Tarreau87b09662015-04-03 00:22:06 +02003443/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003444 * transaction <txn>. Returns the verdict of the first rule that prevents
3445 * further processing of the request (auth, deny, ...), and defaults to
3446 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3447 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003448 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3449 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3450 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003451 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003452enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003453http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003454{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003455 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003456 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003457 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003458 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003459 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003460 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003461 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003462 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003463
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003464 /* If "the current_rule_list" match the executed rule list, we are in
3465 * resume condition. If a resume is needed it is always in the action
3466 * and never in the ACL or converters. In this case, we initialise the
3467 * current rule, and go to the action execution point.
3468 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003469 if (s->current_rule) {
3470 rule = s->current_rule;
3471 s->current_rule = NULL;
3472 if (s->current_rule_list == rules)
3473 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003474 }
3475 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003476
Willy Tarreauff011f22011-01-06 17:51:27 +01003477 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003478
Willy Tarreau96257ec2012-12-27 10:46:37 +01003479 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003480 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003481 int ret;
3482
Willy Tarreau192252e2015-04-04 01:47:55 +02003483 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003484 ret = acl_pass(ret);
3485
Willy Tarreauff011f22011-01-06 17:51:27 +01003486 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003487 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003488
3489 if (!ret) /* condition not matched */
3490 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003491 }
3492
Willy Tarreauacc98002015-09-27 23:34:39 +02003493 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003494resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003495 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003496 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003497 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003498
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003499 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003500 if (deny_status)
3501 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003502 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003503
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003504 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003505 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003506 if (deny_status)
3507 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003508 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003509
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003510 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003511 /* Auth might be performed on regular http-req rules as well as on stats */
3512 auth_realm = rule->arg.auth.realm;
3513 if (!auth_realm) {
3514 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3515 auth_realm = STATS_DEFAULT_REALM;
3516 else
3517 auth_realm = px->id;
3518 }
3519 /* send 401/407 depending on whether we use a proxy or not. We still
3520 * count one error, because normal browsing won't significantly
3521 * increase the counter but brute force attempts will.
3522 */
3523 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3524 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003525 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003526 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003527 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003529 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003530 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3531 return HTTP_RULE_RES_BADREQ;
3532 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003535 s->task->nice = rule->arg.nice;
3536 break;
3537
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003538 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003539 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003540 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003541 break;
3542
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003544#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003545 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003546 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003547#endif
3548 break;
3549
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003550 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003551 s->logs.level = rule->arg.loglevel;
3552 break;
3553
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003554 case ACT_HTTP_REPLACE_HDR:
3555 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003556 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3557 rule->arg.hdr_add.name_len,
3558 &rule->arg.hdr_add.fmt,
3559 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003560 return HTTP_RULE_RES_BADREQ;
3561 break;
3562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003563 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003564 ctx.idx = 0;
3565 /* remove all occurrences of the header */
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);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003569 }
Willy Tarreau85603282015-01-21 20:39:27 +01003570 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003572 case ACT_HTTP_SET_HDR:
3573 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003574 /* The scope of the trash buffer must be limited to this function. The
3575 * build_logline() function can execute a lot of other function which
3576 * can use the trash buffer. So for limiting the scope of this global
3577 * buffer, we build first the header value using build_logline, and
3578 * after we store the header name.
3579 */
3580 len = rule->arg.hdr_add.name_len + 2,
3581 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003582 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003583 trash.str[rule->arg.hdr_add.name_len] = ':';
3584 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3585 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003586
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003587 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003588 /* remove all occurrences of the header */
3589 ctx.idx = 0;
3590 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3591 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3592 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3593 }
3594 }
3595
Willy Tarreau96257ec2012-12-27 10:46:37 +01003596 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3597 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003598
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003599 case ACT_HTTP_DEL_ACL:
3600 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003601 struct pat_ref *ref;
3602 char *key;
3603 int len;
3604
3605 /* collect reference */
3606 ref = pat_ref_lookup(rule->arg.map.ref);
3607 if (!ref)
3608 continue;
3609
3610 /* collect key */
3611 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3612 key = trash.str;
3613 key[len] = '\0';
3614
3615 /* perform update */
3616 /* returned code: 1=ok, 0=ko */
3617 pat_ref_delete(ref, key);
3618
3619 break;
3620 }
3621
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003622 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003623 struct pat_ref *ref;
3624 char *key;
3625 struct chunk *trash_key;
3626 int len;
3627
3628 trash_key = get_trash_chunk();
3629
3630 /* collect reference */
3631 ref = pat_ref_lookup(rule->arg.map.ref);
3632 if (!ref)
3633 continue;
3634
3635 /* collect key */
3636 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3637 key = trash_key->str;
3638 key[len] = '\0';
3639
3640 /* perform update */
3641 /* add entry only if it does not already exist */
3642 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003643 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003644
3645 break;
3646 }
3647
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003648 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003649 struct pat_ref *ref;
3650 char *key, *value;
3651 struct chunk *trash_key, *trash_value;
3652 int len;
3653
3654 trash_key = get_trash_chunk();
3655 trash_value = get_trash_chunk();
3656
3657 /* collect reference */
3658 ref = pat_ref_lookup(rule->arg.map.ref);
3659 if (!ref)
3660 continue;
3661
3662 /* collect key */
3663 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3664 key = trash_key->str;
3665 key[len] = '\0';
3666
3667 /* collect value */
3668 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3669 value = trash_value->str;
3670 value[len] = '\0';
3671
3672 /* perform update */
3673 if (pat_ref_find_elt(ref, key) != NULL)
3674 /* update entry if it exists */
3675 pat_ref_set(ref, key, value, NULL);
3676 else
3677 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003678 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003679
3680 break;
3681 }
William Lallemand73025dd2014-04-24 14:38:37 +02003682
Thierry FOURNIER42148732015-09-02 17:17:33 +02003683 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003684 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3685 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003686
Willy Tarreauacc98002015-09-27 23:34:39 +02003687 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003688 case ACT_RET_ERR:
3689 case ACT_RET_CONT:
3690 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003691 case ACT_RET_STOP:
3692 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003693 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003694 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003695 return HTTP_RULE_RES_YIELD;
3696 }
William Lallemand73025dd2014-04-24 14:38:37 +02003697 break;
3698
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003699 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003700 /* Note: only the first valid tracking parameter of each
3701 * applies.
3702 */
3703
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003704 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003705 struct stktable *t;
3706 struct stksess *ts;
3707 struct stktable_key *key;
3708 void *ptr;
3709
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003710 t = rule->arg.trk_ctr.table.t;
3711 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 +02003712
3713 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003714 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003715
3716 /* let's count a new HTTP request as it's the first time we do it */
3717 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3718 if (ptr)
3719 stktable_data_cast(ptr, http_req_cnt)++;
3720
3721 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3722 if (ptr)
3723 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3724 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3725
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003726 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003727 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003728 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003729 }
3730 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003731 break;
3732
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003733 /* other flags exists, but normaly, they never be matched. */
3734 default:
3735 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003736 }
3737 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003738
3739 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003740 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003741}
3742
Willy Tarreau71241ab2012-12-27 11:30:54 +01003743
Willy Tarreau51d861a2015-05-22 17:30:48 +02003744/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3745 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3746 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3747 * is returned, the process can continue the evaluation of next rule list. If
3748 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3749 * is returned, it means the operation could not be processed and a server error
3750 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3751 * deny rule. If *YIELD is returned, the caller must call again the function
3752 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003753 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003754static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003755http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003756{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003757 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003758 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003759 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003760 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003761 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003762 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003763
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003764 /* If "the current_rule_list" match the executed rule list, we are in
3765 * resume condition. If a resume is needed it is always in the action
3766 * and never in the ACL or converters. In this case, we initialise the
3767 * current rule, and go to the action execution point.
3768 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003769 if (s->current_rule) {
3770 rule = s->current_rule;
3771 s->current_rule = NULL;
3772 if (s->current_rule_list == rules)
3773 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003774 }
3775 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003776
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003777 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003778
3779 /* check optional condition */
3780 if (rule->cond) {
3781 int ret;
3782
Willy Tarreau192252e2015-04-04 01:47:55 +02003783 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003784 ret = acl_pass(ret);
3785
3786 if (rule->cond->pol == ACL_COND_UNLESS)
3787 ret = !ret;
3788
3789 if (!ret) /* condition not matched */
3790 continue;
3791 }
3792
Willy Tarreauacc98002015-09-27 23:34:39 +02003793 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003794resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003796 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003797 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003798
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003799 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003800 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003801 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003803 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003804 s->task->nice = rule->arg.nice;
3805 break;
3806
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003807 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003808 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003809 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003810 break;
3811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003812 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003813#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003814 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003816#endif
3817 break;
3818
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003819 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003820 s->logs.level = rule->arg.loglevel;
3821 break;
3822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003823 case ACT_HTTP_REPLACE_HDR:
3824 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003825 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3826 rule->arg.hdr_add.name_len,
3827 &rule->arg.hdr_add.fmt,
3828 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003829 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003830 break;
3831
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003832 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003833 ctx.idx = 0;
3834 /* remove all occurrences of the header */
3835 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3836 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3837 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3838 }
Willy Tarreau85603282015-01-21 20:39:27 +01003839 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003840
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003841 case ACT_HTTP_SET_HDR:
3842 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3844 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3845 trash.len = rule->arg.hdr_add.name_len;
3846 trash.str[trash.len++] = ':';
3847 trash.str[trash.len++] = ' ';
3848 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 +01003849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003851 /* remove all occurrences of the header */
3852 ctx.idx = 0;
3853 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3854 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3855 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3856 }
3857 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003858 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3859 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003860
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003861 case ACT_HTTP_DEL_ACL:
3862 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003863 struct pat_ref *ref;
3864 char *key;
3865 int len;
3866
3867 /* collect reference */
3868 ref = pat_ref_lookup(rule->arg.map.ref);
3869 if (!ref)
3870 continue;
3871
3872 /* collect key */
3873 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3874 key = trash.str;
3875 key[len] = '\0';
3876
3877 /* perform update */
3878 /* returned code: 1=ok, 0=ko */
3879 pat_ref_delete(ref, key);
3880
3881 break;
3882 }
3883
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003884 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003885 struct pat_ref *ref;
3886 char *key;
3887 struct chunk *trash_key;
3888 int len;
3889
3890 trash_key = get_trash_chunk();
3891
3892 /* collect reference */
3893 ref = pat_ref_lookup(rule->arg.map.ref);
3894 if (!ref)
3895 continue;
3896
3897 /* collect key */
3898 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3899 key = trash_key->str;
3900 key[len] = '\0';
3901
3902 /* perform update */
3903 /* check if the entry already exists */
3904 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003905 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003906
3907 break;
3908 }
3909
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003910 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003911 struct pat_ref *ref;
3912 char *key, *value;
3913 struct chunk *trash_key, *trash_value;
3914 int len;
3915
3916 trash_key = get_trash_chunk();
3917 trash_value = get_trash_chunk();
3918
3919 /* collect reference */
3920 ref = pat_ref_lookup(rule->arg.map.ref);
3921 if (!ref)
3922 continue;
3923
3924 /* collect key */
3925 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3926 key = trash_key->str;
3927 key[len] = '\0';
3928
3929 /* collect value */
3930 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3931 value = trash_value->str;
3932 value[len] = '\0';
3933
3934 /* perform update */
3935 if (pat_ref_find_elt(ref, key) != NULL)
3936 /* update entry if it exists */
3937 pat_ref_set(ref, key, value, NULL);
3938 else
3939 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003940 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003941
3942 break;
3943 }
William Lallemand73025dd2014-04-24 14:38:37 +02003944
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003945 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003946 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3947 return HTTP_RULE_RES_BADREQ;
3948 return HTTP_RULE_RES_DONE;
3949
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003950 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3951 /* Note: only the first valid tracking parameter of each
3952 * applies.
3953 */
3954
3955 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3956 struct stktable *t;
3957 struct stksess *ts;
3958 struct stktable_key *key;
3959 void *ptr;
3960
3961 t = rule->arg.trk_ctr.table.t;
3962 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3963
3964 if (key && (ts = stktable_get_entry(t, key))) {
3965 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3966
3967 /* let's count a new HTTP request as it's the first time we do it */
3968 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3969 if (ptr)
3970 stktable_data_cast(ptr, http_req_cnt)++;
3971
3972 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3973 if (ptr)
3974 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3975 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3976
3977 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3978 if (sess->fe != s->be)
3979 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3980
3981 /* When the client triggers a 4xx from the server, it's most often due
3982 * to a missing object or permission. These events should be tracked
3983 * because if they happen often, it may indicate a brute force or a
3984 * vulnerability scan. Normally this is done when receiving the response
3985 * but here we're tracking after this ought to have been done so we have
3986 * to do it on purpose.
3987 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003988 if ((unsigned)(txn->status - 400) < 100) {
3989 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3990 if (ptr)
3991 stktable_data_cast(ptr, http_err_cnt)++;
3992
3993 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3994 if (ptr)
3995 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3996 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3997 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003998 }
3999 }
4000 break;
4001
Thierry FOURNIER42148732015-09-02 17:17:33 +02004002 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004003 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4004 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004005
Willy Tarreauacc98002015-09-27 23:34:39 +02004006 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004007 case ACT_RET_ERR:
4008 case ACT_RET_CONT:
4009 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004010 case ACT_RET_STOP:
4011 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004012 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004013 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004014 return HTTP_RULE_RES_YIELD;
4015 }
William Lallemand73025dd2014-04-24 14:38:37 +02004016 break;
4017
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004018 /* other flags exists, but normaly, they never be matched. */
4019 default:
4020 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004021 }
4022 }
4023
4024 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004025 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004026}
4027
4028
Willy Tarreau71241ab2012-12-27 11:30:54 +01004029/* Perform an HTTP redirect based on the information in <rule>. The function
4030 * returns non-zero on success, or zero in case of a, irrecoverable error such
4031 * as too large a request to build a valid response.
4032 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004033static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004034{
Willy Tarreaub329a312015-05-22 16:27:37 +02004035 struct http_msg *req = &txn->req;
4036 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004037 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004038 struct chunk *chunk;
4039 int ret = 0;
4040
4041 chunk = alloc_trash_chunk();
4042 if (!chunk)
4043 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004044
4045 /* build redirect message */
4046 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004047 case 308:
4048 msg_fmt = HTTP_308;
4049 break;
4050 case 307:
4051 msg_fmt = HTTP_307;
4052 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 case 303:
4054 msg_fmt = HTTP_303;
4055 break;
4056 case 301:
4057 msg_fmt = HTTP_301;
4058 break;
4059 case 302:
4060 default:
4061 msg_fmt = HTTP_302;
4062 break;
4063 }
4064
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004065 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4066 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004067
4068 switch(rule->type) {
4069 case REDIRECT_TYPE_SCHEME: {
4070 const char *path;
4071 const char *host;
4072 struct hdr_ctx ctx;
4073 int pathlen;
4074 int hostlen;
4075
4076 host = "";
4077 hostlen = 0;
4078 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004079 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004080 host = ctx.line + ctx.val;
4081 hostlen = ctx.vlen;
4082 }
4083
4084 path = http_get_path(txn);
4085 /* build message using path */
4086 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004087 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004088 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4089 int qs = 0;
4090 while (qs < pathlen) {
4091 if (path[qs] == '?') {
4092 pathlen = qs;
4093 break;
4094 }
4095 qs++;
4096 }
4097 }
4098 } else {
4099 path = "/";
4100 pathlen = 1;
4101 }
4102
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004103 if (rule->rdr_str) { /* this is an old "redirect" rule */
4104 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004105 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4106 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004107
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004108 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004109 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4110 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004111 }
4112 else {
4113 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004114 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004115
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004116 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004117 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4118 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004119 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004120 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004121 memcpy(chunk->str + chunk->len, "://", 3);
4122 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004123
4124 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004125 memcpy(chunk->str + chunk->len, host, hostlen);
4126 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004127
4128 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004129 memcpy(chunk->str + chunk->len, path, pathlen);
4130 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004132 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004133 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004134 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004135 if (chunk->len > chunk->size - 5)
4136 goto leave;
4137 chunk->str[chunk->len] = '/';
4138 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004139 }
4140
4141 break;
4142 }
4143 case REDIRECT_TYPE_PREFIX: {
4144 const char *path;
4145 int pathlen;
4146
4147 path = http_get_path(txn);
4148 /* build message using path */
4149 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004150 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004151 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4152 int qs = 0;
4153 while (qs < pathlen) {
4154 if (path[qs] == '?') {
4155 pathlen = qs;
4156 break;
4157 }
4158 qs++;
4159 }
4160 }
4161 } else {
4162 path = "/";
4163 pathlen = 1;
4164 }
4165
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004166 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004167 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4168 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004170 /* add prefix. Note that if prefix == "/", we don't want to
4171 * add anything, otherwise it makes it hard for the user to
4172 * configure a self-redirection.
4173 */
4174 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004175 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4176 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004177 }
4178 }
4179 else {
4180 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004181 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004182
4183 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004184 if (chunk->len + pathlen > chunk->size - 4)
4185 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004186 }
4187
4188 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004189 memcpy(chunk->str + chunk->len, path, pathlen);
4190 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004192 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004193 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004194 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004195 if (chunk->len > chunk->size - 5)
4196 goto leave;
4197 chunk->str[chunk->len] = '/';
4198 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004199 }
4200
4201 break;
4202 }
4203 case REDIRECT_TYPE_LOCATION:
4204 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004205 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004206 if (chunk->len + rule->rdr_len > chunk->size - 4)
4207 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004208
4209 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004210 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4211 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004212 }
4213 else {
4214 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004215 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004216
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004217 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004218 if (chunk->len > chunk->size - 4)
4219 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004220 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004221 break;
4222 }
4223
4224 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004225 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4226 chunk->len += 14;
4227 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4228 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004229 }
4230
Willy Tarreau19b14122017-02-28 09:48:11 +01004231 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004232 txn->status = rule->code;
4233 /* let's log the request time */
4234 s->logs.tv_request = now;
4235
Christopher Fauletbe821b92017-03-30 11:21:53 +02004236 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004237 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4239 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004240 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004241 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004242 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4243 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004244 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004245 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4246 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 }
4248 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004249 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4250 chunk->len += 4;
4251 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4252 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004254 bi_fast_delete(req->chn->buf, req->sov);
4255 req->next -= req->sov;
4256 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004257 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01004258 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004259 req->msg_state = HTTP_MSG_CLOSED;
4260 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004261 /* Trim any possible response */
4262 res->chn->buf->i = 0;
4263 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264 } else {
4265 /* keep-alive not possible */
4266 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004267 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4268 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004270 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4271 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004272 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004273 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004274 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004275 }
4276
Willy Tarreaue7dff022015-04-03 01:14:29 +02004277 if (!(s->flags & SF_ERR_MASK))
4278 s->flags |= SF_ERR_LOCAL;
4279 if (!(s->flags & SF_FINST_MASK))
4280 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004281
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004282 ret = 1;
4283 leave:
4284 free_trash_chunk(chunk);
4285 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004286}
4287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004288/* This stream analyser runs all HTTP request processing which is common to
4289 * frontends and backends, which means blocking ACLs, filters, connection-close,
4290 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004291 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 * either needs more data or wants to immediately abort the request (eg: deny,
4293 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004295int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004296{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004297 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004298 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004299 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004301 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004302 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004303 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004304
Willy Tarreau655dce92009-11-08 13:10:58 +01004305 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004306 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004307 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004308 }
4309
Willy Tarreau87b09662015-04-03 00:22:06 +02004310 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 +02004311 now_ms, __FUNCTION__,
4312 s,
4313 req,
4314 req->rex, req->wex,
4315 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004316 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004317 req->analysers);
4318
Willy Tarreau65410832014-04-28 21:25:43 +02004319 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004320 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004321
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004322 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004323 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004324 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004325
Willy Tarreau0b748332014-04-29 00:13:29 +02004326 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004327 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4328 goto return_prx_yield;
4329
Willy Tarreau0b748332014-04-29 00:13:29 +02004330 case HTTP_RULE_RES_CONT:
4331 case HTTP_RULE_RES_STOP: /* nothing to do */
4332 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004333
Willy Tarreau0b748332014-04-29 00:13:29 +02004334 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4335 if (txn->flags & TX_CLTARPIT)
4336 goto tarpit;
4337 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004338
Willy Tarreau0b748332014-04-29 00:13:29 +02004339 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4340 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004341
Willy Tarreau0b748332014-04-29 00:13:29 +02004342 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004343 goto done;
4344
Willy Tarreau0b748332014-04-29 00:13:29 +02004345 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4346 goto return_bad_req;
4347 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004348 }
4349
Willy Tarreau52542592014-04-28 18:33:26 +02004350 /* OK at this stage, we know that the request was accepted according to
4351 * the http-request rules, we can check for the stats. Note that the
4352 * URI is detected *before* the req* rules in order not to be affected
4353 * by a possible reqrep, while they are processed *after* so that a
4354 * reqdeny can still block them. This clearly needs to change in 1.6!
4355 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004356 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004357 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004358 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004359 txn->status = 500;
4360 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004361 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004362
Willy Tarreaue7dff022015-04-03 01:14:29 +02004363 if (!(s->flags & SF_ERR_MASK))
4364 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004365 goto return_prx_cond;
4366 }
4367
4368 /* parse the whole stats request and extract the relevant information */
4369 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004370 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004371 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004372
Willy Tarreau0b748332014-04-29 00:13:29 +02004373 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4374 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004375
Willy Tarreau0b748332014-04-29 00:13:29 +02004376 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4377 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004378 }
4379
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004380 /* evaluate the req* rules except reqadd */
4381 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004382 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004383 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004384
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004385 if (txn->flags & TX_CLDENY)
4386 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004387
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004388 if (txn->flags & TX_CLTARPIT) {
4389 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004390 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004391 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004392 }
Willy Tarreau06619262006-12-17 08:37:22 +01004393
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004394 /* add request headers from the rule sets in the same order */
4395 list_for_each_entry(wl, &px->req_add, list) {
4396 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004397 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004398 ret = acl_pass(ret);
4399 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4400 ret = !ret;
4401 if (!ret)
4402 continue;
4403 }
4404
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004405 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004406 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004407 }
4408
Willy Tarreau52542592014-04-28 18:33:26 +02004409
4410 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004411 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004412 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004413 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4414 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004415
Willy Tarreaue7dff022015-04-03 01:14:29 +02004416 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4417 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4418 if (!(s->flags & SF_FINST_MASK))
4419 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004420
Willy Tarreau70730dd2014-04-24 18:06:27 +02004421 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004422 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4423 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004424 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004425 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004426 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004427
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004428 /* check whether we have some ACLs set to redirect this request */
4429 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004430 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004431 int ret;
4432
Willy Tarreau192252e2015-04-04 01:47:55 +02004433 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004434 ret = acl_pass(ret);
4435 if (rule->cond->pol == ACL_COND_UNLESS)
4436 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004437 if (!ret)
4438 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004439 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004440 if (!http_apply_redirect_rule(rule, s, txn))
4441 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004442 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004443 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004444
Willy Tarreau2be39392010-01-03 17:24:51 +01004445 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4446 * If this happens, then the data will not come immediately, so we must
4447 * send all what we have without waiting. Note that due to the small gain
4448 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004449 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004450 * itself once used.
4451 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004452 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004453
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004454 done: /* done with this analyser, continue with next ones that the calling
4455 * points will have set, if any.
4456 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004457 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004458 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4459 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004460 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004461
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004462 tarpit:
4463 /* When a connection is tarpitted, we use the tarpit timeout,
4464 * which may be the same as the connect timeout if unspecified.
4465 * If unset, then set it to zero because we really want it to
4466 * eventually expire. We build the tarpit as an analyser.
4467 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004468 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004469
4470 /* wipe the request out so that we can drop the connection early
4471 * if the client closes first.
4472 */
4473 channel_dont_connect(req);
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
Jarno Huuskonen800d1762017-03-06 14:56:36 +02004480 txn->status = http_err_codes[deny_status];
4481
Christopher Faulet0184ea72017-01-05 14:06:34 +01004482 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 req->analysers |= AN_REQ_HTTP_TARPIT;
4484 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4485 if (!req->analyse_exp)
4486 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004487 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004488 sess->fe->fe_counters.denied_req++;
4489 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004490 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004491 if (sess->listener->counters)
4492 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004493 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004494
4495 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004496
4497 /* Allow cookie logging
4498 */
4499 if (s->be->cookie_name || sess->fe->capture_name)
4500 manage_client_side_cookies(s, req);
4501
Willy Tarreau0b748332014-04-29 00:13:29 +02004502 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004503 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004504 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004505 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02004506 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004507 sess->fe->fe_counters.denied_req++;
4508 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004509 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004510 if (sess->listener->counters)
4511 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004512 goto return_prx_cond;
4513
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004514 return_bad_req:
4515 /* We centralize bad requests processing here */
4516 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4517 /* we detected a parsing error. We want to archive this request
4518 * in the dedicated proxy area for later troubleshooting.
4519 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004520 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004521 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004522
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004523 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 txn->req.msg_state = HTTP_MSG_ERROR;
4525 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004526 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004527
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004528 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004529 if (sess->listener->counters)
4530 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004531
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004532 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004533 if (!(s->flags & SF_ERR_MASK))
4534 s->flags |= SF_ERR_PRXCOND;
4535 if (!(s->flags & SF_FINST_MASK))
4536 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004537
Christopher Faulet0184ea72017-01-05 14:06:34 +01004538 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539 req->analyse_exp = TICK_ETERNITY;
4540 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004541
4542 return_prx_yield:
4543 channel_dont_connect(req);
4544 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004546
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004547/* This function performs all the processing enabled for the current request.
4548 * It returns 1 if the processing can continue on next analysers, or zero if it
4549 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004550 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004551 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004552int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004553{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004554 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004555 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004556 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004557 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004558
Willy Tarreau655dce92009-11-08 13:10:58 +01004559 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004560 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004561 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004562 return 0;
4563 }
4564
Willy Tarreau87b09662015-04-03 00:22:06 +02004565 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 +02004566 now_ms, __FUNCTION__,
4567 s,
4568 req,
4569 req->rex, req->wex,
4570 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004571 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004572 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004573
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 /*
4575 * Right now, we know that we have processed the entire headers
4576 * and that unwanted requests have been filtered out. We can do
4577 * whatever we want with the remaining request. Also, now we
4578 * may have separate values for ->fe, ->be.
4579 */
Willy Tarreau06619262006-12-17 08:37:22 +01004580
Willy Tarreau59234e92008-11-30 23:51:27 +01004581 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004582 * If HTTP PROXY is set we simply get remote server address parsing
4583 * incoming request. Note that this requires that a connection is
4584 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004585 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004586 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004587 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004588 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004589
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004590 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004591 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004592 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004593 txn->req.msg_state = HTTP_MSG_ERROR;
4594 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004595 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004596 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004597
Willy Tarreaue7dff022015-04-03 01:14:29 +02004598 if (!(s->flags & SF_ERR_MASK))
4599 s->flags |= SF_ERR_RESOURCE;
4600 if (!(s->flags & SF_FINST_MASK))
4601 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004602
4603 return 0;
4604 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004605
4606 path = http_get_path(txn);
4607 url2sa(req->buf->p + msg->sl.rq.u,
4608 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004609 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004610 /* if the path was found, we have to remove everything between
4611 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4612 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4613 * u_l characters by a single "/".
4614 */
4615 if (path) {
4616 char *cur_ptr = req->buf->p;
4617 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4618 int delta;
4619
4620 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4621 http_msg_move_end(&txn->req, delta);
4622 cur_end += delta;
4623 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4624 goto return_bad_req;
4625 }
4626 else {
4627 char *cur_ptr = req->buf->p;
4628 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4629 int delta;
4630
4631 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4632 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4633 http_msg_move_end(&txn->req, delta);
4634 cur_end += delta;
4635 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4636 goto return_bad_req;
4637 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004638 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004639
Willy Tarreau59234e92008-11-30 23:51:27 +01004640 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004641 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004642 * Note that doing so might move headers in the request, but
4643 * the fields will stay coherent and the URI will not move.
4644 * This should only be performed in the backend.
4645 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004646 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004647 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004648
William Lallemanda73203e2012-03-12 12:48:57 +01004649 /* add unique-id if "header-unique-id" is specified */
4650
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004651 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004652 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4653 goto return_bad_req;
4654 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004655 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004656 }
William Lallemanda73203e2012-03-12 12:48:57 +01004657
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004658 if (sess->fe->header_unique_id && s->unique_id) {
4659 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004660 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004661 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004662 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004663 goto return_bad_req;
4664 }
4665
Cyril Bontéb21570a2009-11-29 20:04:48 +01004666 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 * 9: add X-Forwarded-For if either the frontend or the backend
4668 * asks for it.
4669 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004670 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004671 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004672 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4673 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4674 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004675 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004676 /* The header is set to be added only if none is present
4677 * and we found it, so don't do anything.
4678 */
4679 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004680 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004681 /* Add an X-Forwarded-For header unless the source IP is
4682 * in the 'except' network range.
4683 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004684 if ((!sess->fe->except_mask.s_addr ||
4685 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4686 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004687 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004689 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004690 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004691 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004692 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004693
4694 /* Note: we rely on the backend to get the header name to be used for
4695 * x-forwarded-for, because the header is really meant for the backends.
4696 * However, if the backend did not specify any option, we have to rely
4697 * on the frontend's header name.
4698 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004699 if (s->be->fwdfor_hdr_len) {
4700 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004701 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004702 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004703 len = sess->fe->fwdfor_hdr_len;
4704 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004705 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004706 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 +01004707
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004708 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004709 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004710 }
4711 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004712 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 /* FIXME: for the sake of completeness, we should also support
4714 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004715 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 int len;
4717 char pn[INET6_ADDRSTRLEN];
4718 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004719 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004720 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004721
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 /* Note: we rely on the backend to get the header name to be used for
4723 * x-forwarded-for, because the header is really meant for the backends.
4724 * However, if the backend did not specify any option, we have to rely
4725 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004726 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004727 if (s->be->fwdfor_hdr_len) {
4728 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004729 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004730 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004731 len = sess->fe->fwdfor_hdr_len;
4732 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004733 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004734 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004735
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004736 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004737 goto return_bad_req;
4738 }
4739 }
4740
4741 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004742 * 10: add X-Original-To if either the frontend or the backend
4743 * asks for it.
4744 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004745 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004746
4747 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004748 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004749 /* Add an X-Original-To header unless the destination IP is
4750 * in the 'except' network range.
4751 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004752 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004753
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004754 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004755 ((!sess->fe->except_mask_to.s_addr ||
4756 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4757 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004758 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004759 (((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 +02004760 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004761 int len;
4762 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004763 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004764
4765 /* Note: we rely on the backend to get the header name to be used for
4766 * x-original-to, because the header is really meant for the backends.
4767 * However, if the backend did not specify any option, we have to rely
4768 * on the frontend's header name.
4769 */
4770 if (s->be->orgto_hdr_len) {
4771 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004772 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004773 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004774 len = sess->fe->orgto_hdr_len;
4775 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004776 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004777 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 +02004778
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004779 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004780 goto return_bad_req;
4781 }
4782 }
4783 }
4784
Willy Tarreau50fc7772012-11-11 22:19:57 +01004785 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4786 * If an "Upgrade" token is found, the header is left untouched in order not to have
4787 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4788 * "Upgrade" is present in the Connection header.
4789 */
4790 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4791 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004792 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004793 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004794 unsigned int want_flags = 0;
4795
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004796 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004797 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004798 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004799 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004800 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004801 want_flags |= TX_CON_CLO_SET;
4802 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004803 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004804 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004805 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004806 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004807 want_flags |= TX_CON_KAL_SET;
4808 }
4809
4810 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004811 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004812 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004813
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004814
Willy Tarreau522d6c02009-12-06 18:49:18 +01004815 /* If we have no server assigned yet and we're balancing on url_param
4816 * with a POST request, we may be interested in checking the body for
4817 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004818 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004819 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004820 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004821 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004822 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004824 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004825
Christopher Fauletbe821b92017-03-30 11:21:53 +02004826 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
4827 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004828#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02004829 /* We expect some data from the client. Unless we know for sure
4830 * we already have a full request, we have to re-enable quick-ack
4831 * in case we previously disabled it, otherwise we might cause
4832 * the client to delay further data.
4833 */
4834 if ((sess->listener->options & LI_O_NOQUICKACK) &&
4835 cli_conn && conn_ctrl_ready(cli_conn) &&
4836 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
4837 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
4838 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004839#endif
Willy Tarreau03945942009-12-22 16:50:27 +01004840
Willy Tarreau59234e92008-11-30 23:51:27 +01004841 /*************************************************************
4842 * OK, that's finished for the headers. We have done what we *
4843 * could. Let's switch to the DATA state. *
4844 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004845 req->analyse_exp = TICK_ETERNITY;
4846 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004847
Willy Tarreau59234e92008-11-30 23:51:27 +01004848 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004849 /* OK let's go on with the BODY now */
4850 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004851
Willy Tarreau59234e92008-11-30 23:51:27 +01004852 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004853 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004854 /* we detected a parsing error. We want to archive this request
4855 * in the dedicated proxy area for later troubleshooting.
4856 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004857 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004858 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004859
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004860 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004861 txn->req.msg_state = HTTP_MSG_ERROR;
4862 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004863 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004864 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004865
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004866 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004867 if (sess->listener->counters)
4868 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004869
Willy Tarreaue7dff022015-04-03 01:14:29 +02004870 if (!(s->flags & SF_ERR_MASK))
4871 s->flags |= SF_ERR_PRXCOND;
4872 if (!(s->flags & SF_FINST_MASK))
4873 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004874 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004875}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004876
Willy Tarreau60b85b02008-11-30 23:28:40 +01004877/* This function is an analyser which processes the HTTP tarpit. It always
4878 * returns zero, at the beginning because it prevents any other processing
4879 * from occurring, and at the end because it terminates the request.
4880 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004881int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004882{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004883 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004884
4885 /* This connection is being tarpitted. The CLIENT side has
4886 * already set the connect expiration date to the right
4887 * timeout. We just have to check that the client is still
4888 * there and that the timeout has not expired.
4889 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004890 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004891 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004892 !tick_is_expired(req->analyse_exp, now_ms))
4893 return 0;
4894
4895 /* We will set the queue timer to the time spent, just for
4896 * logging purposes. We fake a 500 server error, so that the
4897 * attacker will not suspect his connection has been tarpitted.
4898 * It will not cause trouble to the logs because we can exclude
4899 * the tarpitted connections by filtering on the 'PT' status flags.
4900 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004901 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4902
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004903 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004904 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004905
Christopher Faulet0184ea72017-01-05 14:06:34 +01004906 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004907 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004908
Willy Tarreaue7dff022015-04-03 01:14:29 +02004909 if (!(s->flags & SF_ERR_MASK))
4910 s->flags |= SF_ERR_PRXCOND;
4911 if (!(s->flags & SF_FINST_MASK))
4912 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004913 return 0;
4914}
4915
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004916/* This function is an analyser which waits for the HTTP request body. It waits
4917 * for either the buffer to be full, or the full advertised contents to have
4918 * reached the buffer. It must only be called after the standard HTTP request
4919 * processing has occurred, because it expects the request to be parsed and will
4920 * look for the Expect header. It may send a 100-Continue interim response. It
4921 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4922 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4923 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004924 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004925int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004926{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004927 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004928 struct http_txn *txn = s->txn;
4929 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004930
4931 /* We have to parse the HTTP request body to find any required data.
4932 * "balance url_param check_post" should have been the only way to get
4933 * into this. We were brought here after HTTP header analysis, so all
4934 * related structures are ready.
4935 */
4936
Willy Tarreau890988f2014-04-10 11:59:33 +02004937 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4938 /* This is the first call */
4939 if (msg->msg_state < HTTP_MSG_BODY)
4940 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004941
Willy Tarreau890988f2014-04-10 11:59:33 +02004942 if (msg->msg_state < HTTP_MSG_100_SENT) {
4943 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4944 * send an HTTP/1.1 100 Continue intermediate response.
4945 */
4946 if (msg->flags & HTTP_MSGF_VER_11) {
4947 struct hdr_ctx ctx;
4948 ctx.idx = 0;
4949 /* Expect is allowed in 1.1, look for it */
4950 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4951 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004952 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004953 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004954 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004955 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004956 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004958
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004959 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004960 * req->buf->p still points to the beginning of the message. We
4961 * must save the body in msg->next because it survives buffer
4962 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004964 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004965
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004966 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004967 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4968 else
4969 msg->msg_state = HTTP_MSG_DATA;
4970 }
4971
Willy Tarreau890988f2014-04-10 11:59:33 +02004972 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4973 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004974 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004975 goto missing_data;
4976
4977 /* OK we have everything we need now */
4978 goto http_end;
4979 }
4980
4981 /* OK here we're parsing a chunked-encoded message */
4982
Willy Tarreau522d6c02009-12-06 18:49:18 +01004983 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004984 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004985 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004986 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004987 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004988 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004989
Willy Tarreau115acb92009-12-26 13:56:06 +01004990 if (!ret)
4991 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004992 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004993 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004994 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004995 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004996 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004997 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004998 }
4999
Willy Tarreaud98cf932009-12-27 22:54:55 +01005000 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005001 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5002 * for at least a whole chunk or the whole content length bytes after
5003 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005004 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005005 if (msg->msg_state == HTTP_MSG_TRAILERS)
5006 goto http_end;
5007
Willy Tarreaue115b492015-05-01 23:05:14 +02005008 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005009 goto http_end;
5010
5011 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005012 /* we get here if we need to wait for more data. If the buffer is full,
5013 * we have the maximum we can expect.
5014 */
5015 if (buffer_full(req->buf, global.tune.maxrewrite))
5016 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005018 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005019 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005020 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005021
Willy Tarreaue7dff022015-04-03 01:14:29 +02005022 if (!(s->flags & SF_ERR_MASK))
5023 s->flags |= SF_ERR_CLITO;
5024 if (!(s->flags & SF_FINST_MASK))
5025 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005026 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005027 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005028
5029 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005030 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005031 /* Not enough data. We'll re-use the http-request
5032 * timeout here. Ideally, we should set the timeout
5033 * relative to the accept() date. We just set the
5034 * request timeout once at the beginning of the
5035 * request.
5036 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005037 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005038 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005039 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005040 return 0;
5041 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005042
5043 http_end:
5044 /* The situation will not evolve, so let's give up on the analysis. */
5045 s->logs.tv_request = now; /* update the request timer to reflect full request */
5046 req->analysers &= ~an_bit;
5047 req->analyse_exp = TICK_ETERNITY;
5048 return 1;
5049
5050 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005051 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005052 txn->req.msg_state = HTTP_MSG_ERROR;
5053 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005054 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005055
Willy Tarreaue7dff022015-04-03 01:14:29 +02005056 if (!(s->flags & SF_ERR_MASK))
5057 s->flags |= SF_ERR_PRXCOND;
5058 if (!(s->flags & SF_FINST_MASK))
5059 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005060
Willy Tarreau522d6c02009-12-06 18:49:18 +01005061 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005062 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005063 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005064 if (sess->listener->counters)
5065 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005066 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005067}
5068
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005069/* send a server's name with an outgoing request over an established connection.
5070 * Note: this function is designed to be called once the request has been scheduled
5071 * for being forwarded. This is the reason why it rewinds the buffer before
5072 * proceeding.
5073 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005074int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005075
5076 struct hdr_ctx ctx;
5077
Mark Lamourinec2247f02012-01-04 13:02:01 -05005078 char *hdr_name = be->server_id_hdr_name;
5079 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005080 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005082 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005083
William Lallemandd9e90662012-01-30 17:27:17 +01005084 ctx.idx = 0;
5085
Willy Tarreau211cdec2014-04-17 20:18:08 +02005086 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005087 if (old_o) {
5088 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005089 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005090 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005091 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005092 }
5093
Willy Tarreau9b28e032012-10-12 23:49:43 +02005094 old_i = chn->buf->i;
5095 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 -05005096 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005097 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005098 }
5099
5100 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005101 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005102 memcpy(hdr_val, hdr_name, hdr_name_len);
5103 hdr_val += hdr_name_len;
5104 *hdr_val++ = ':';
5105 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005106 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5107 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005108
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005109 if (old_o) {
5110 /* If this was a forwarded request, we must readjust the amount of
5111 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005112 * variations. Note that the current state is >= HTTP_MSG_BODY,
5113 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005114 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005115 old_o += chn->buf->i - old_i;
5116 b_adv(chn->buf, old_o);
5117 txn->req.next -= old_o;
5118 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005119 }
5120
Mark Lamourinec2247f02012-01-04 13:02:01 -05005121 return 0;
5122}
5123
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124/* Terminate current transaction and prepare a new one. This is very tricky
5125 * right now but it works.
5126 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005127void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005129 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005130 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005131 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005132 struct connection *srv_conn;
5133 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005134 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005135
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136 /* FIXME: We need a more portable way of releasing a backend's and a
5137 * server's connections. We need a safer way to reinitialize buffer
5138 * flags. We also need a more accurate method for computing per-request
5139 * data.
5140 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005141 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005142
Willy Tarreau4213a112013-12-15 10:25:42 +01005143 /* unless we're doing keep-alive, we want to quickly close the connection
5144 * to the server.
5145 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005146 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005147 !si_conn_ready(&s->si[1])) {
5148 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5149 si_shutr(&s->si[1]);
5150 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005151 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152
Willy Tarreaue7dff022015-04-03 01:14:29 +02005153 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005154 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005155 if (unlikely(s->srv_conn))
5156 sess_change_server(s, NULL);
5157 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158
5159 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005160 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161
Willy Tarreaueee5b512015-04-03 23:46:31 +02005162 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163 int n;
5164
Willy Tarreaueee5b512015-04-03 23:46:31 +02005165 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 if (n < 1 || n > 5)
5167 n = 0;
5168
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005169 if (fe->mode == PR_MODE_HTTP) {
5170 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005171 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005172 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005173 (be->mode == PR_MODE_HTTP)) {
5174 be->be_counters.p.http.rsp[n]++;
5175 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005176 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 }
5178
5179 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005180 s->logs.bytes_in -= s->req.buf->i;
5181 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182
5183 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005184 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005185 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005186 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005187 s->do_log(s);
5188 }
5189
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005190 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005191 stream_stop_content_counters(s);
5192 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005193
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 s->logs.accept_date = date; /* user-visible date for logging */
5195 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005196 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5197 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 tv_zero(&s->logs.tv_request);
5199 s->logs.t_queue = -1;
5200 s->logs.t_connect = -1;
5201 s->logs.t_data = -1;
5202 s->logs.t_close = 0;
5203 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5204 s->logs.srv_queue_size = 0; /* we will get this number soon */
5205
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005206 s->logs.bytes_in = s->req.total = s->req.buf->i;
5207 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208
5209 if (s->pend_pos)
5210 pendconn_free(s->pend_pos);
5211
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005212 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005213 if (s->flags & SF_CURR_SESS) {
5214 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005215 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005217 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005218 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 }
5220
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005221 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222
Willy Tarreau4213a112013-12-15 10:25:42 +01005223 /* only release our endpoint if we don't intend to reuse the
5224 * connection.
5225 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005226 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005227 !si_conn_ready(&s->si[1])) {
5228 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005229 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005230 }
5231
Willy Tarreau350f4872014-11-28 14:42:25 +01005232 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5233 s->si[1].err_type = SI_ET_NONE;
5234 s->si[1].conn_retries = 0; /* used for logging too */
5235 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005236 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 +01005237 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);
5238 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 +02005239 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5240 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5241 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005242
Willy Tarreaueee5b512015-04-03 23:46:31 +02005243 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005245 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005246
5247 if (prev_status == 401 || prev_status == 407) {
5248 /* In HTTP keep-alive mode, if we receive a 401, we still have
5249 * a chance of being able to send the visitor again to the same
5250 * server over the same connection. This is required by some
5251 * broken protocols such as NTLM, and anyway whenever there is
5252 * an opportunity for sending the challenge to the proper place,
5253 * it's better to do it (at least it helps with debugging).
5254 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005255 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005256 if (srv_conn)
5257 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005258 }
5259
Willy Tarreau53f96852016-02-02 18:50:47 +01005260 /* Never ever allow to reuse a connection from a non-reuse backend */
5261 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5262 srv_conn->flags |= CO_FL_PRIVATE;
5263
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005264 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005265 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005267 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005268 s->req.flags |= CF_NEVER_WAIT;
5269 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005270 }
5271
Willy Tarreau714ea782015-11-25 20:11:11 +01005272 /* we're removing the analysers, we MUST re-enable events detection.
5273 * We don't enable close on the response channel since it's either
5274 * already closed, or in keep-alive with an idle connection handler.
5275 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005276 channel_auto_read(&s->req);
5277 channel_auto_close(&s->req);
5278 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005280 /* we're in keep-alive with an idle connection, monitor it if not already done */
5281 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005282 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005283 if (!srv)
5284 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005285 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005286 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005287 else if (prev_flags & TX_NOT_FIRST)
5288 /* note: we check the request, not the connection, but
5289 * this is valid for strategies SAFE and AGGR, and in
5290 * case of ALWS, we don't care anyway.
5291 */
5292 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005293 else
5294 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005295 }
Christopher Faulete6006242017-03-10 11:52:44 +01005296 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5297 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298}
5299
5300
5301/* This function updates the request state machine according to the response
5302 * state machine and buffer flags. It returns 1 if it changes anything (flag
5303 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5304 * it is only used to find when a request/response couple is complete. Both
5305 * this function and its equivalent should loop until both return zero. It
5306 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5307 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005308int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005310 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005311 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005312 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 unsigned int old_state = txn->req.msg_state;
5314
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5316 return 0;
5317
5318 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005319 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005320 * We can shut the read side unless we want to abort_on_close,
5321 * or we have a POST request. The issue with POST requests is
5322 * that some browsers still send a CRLF after the request, and
5323 * this CRLF must be read so that it does not remain in the kernel
5324 * buffers, otherwise a close could cause an RST on some systems
5325 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005326 * Note that if we're using keep-alive on the client side, we'd
5327 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005328 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005329 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005330 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005331 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5332 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5333 !(s->be->options & PR_O_ABRT_CLOSE) &&
5334 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336
Willy Tarreau40f151a2012-12-20 12:10:09 +01005337 /* if the server closes the connection, we want to immediately react
5338 * and close the socket to save packets and syscalls.
5339 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005340 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005341
Willy Tarreau7f876a12015-11-18 11:59:55 +01005342 /* In any case we've finished parsing the request so we must
5343 * disable Nagle when sending data because 1) we're not going
5344 * to shut this side, and 2) the server is waiting for us to
5345 * send pending data.
5346 */
5347 chn->flags |= CF_NEVER_WAIT;
5348
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5350 goto wait_other_side;
5351
5352 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5353 /* The server has not finished to respond, so we
5354 * don't want to move in order not to upset it.
5355 */
5356 goto wait_other_side;
5357 }
5358
5359 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5360 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005361 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005362 txn->req.msg_state = HTTP_MSG_TUNNEL;
5363 goto wait_other_side;
5364 }
5365
5366 /* When we get here, it means that both the request and the
5367 * response have finished receiving. Depending on the connection
5368 * mode, we'll have to wait for the last bytes to leave in either
5369 * direction, and sometimes for a close to be effective.
5370 */
5371
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005372 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5373 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005374 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5375 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005376 }
5377 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5378 /* Option forceclose is set, or either side wants to close,
5379 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005380 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005381 * once both states are CLOSED.
5382 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005383 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5384 channel_shutr_now(chn);
5385 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005386 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387 }
5388 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005389 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5390 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005391 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005392 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5393 channel_auto_read(chn);
5394 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005395 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 }
5397
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005398 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005400 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005401
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005402 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 txn->req.msg_state = HTTP_MSG_CLOSING;
5404 goto http_msg_closing;
5405 }
5406 else {
5407 txn->req.msg_state = HTTP_MSG_CLOSED;
5408 goto http_msg_closed;
5409 }
5410 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005411 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005412 }
5413
5414 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5415 http_msg_closing:
5416 /* nothing else to forward, just waiting for the output buffer
5417 * to be empty and for the shutw_now to take effect.
5418 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005419 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420 txn->req.msg_state = HTTP_MSG_CLOSED;
5421 goto http_msg_closed;
5422 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005423 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005424 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 txn->req.msg_state = HTTP_MSG_ERROR;
5426 goto wait_other_side;
5427 }
5428 }
5429
5430 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5431 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005432 /* see above in MSG_DONE why we only do this in these states */
5433 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5434 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5435 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005436 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 goto wait_other_side;
5438 }
5439
5440 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005441 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442}
5443
5444
5445/* This function updates the response state machine according to the request
5446 * state machine and buffer flags. It returns 1 if it changes anything (flag
5447 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5448 * it is only used to find when a request/response couple is complete. Both
5449 * this function and its equivalent should loop until both return zero. It
5450 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005452int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005454 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005455 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005456 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 unsigned int old_state = txn->rsp.msg_state;
5458
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5460 return 0;
5461
5462 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5463 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005464 * still monitor the server connection for a possible close
5465 * while the request is being uploaded, so we don't disable
5466 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469
5470 if (txn->req.msg_state == HTTP_MSG_ERROR)
5471 goto wait_other_side;
5472
5473 if (txn->req.msg_state < HTTP_MSG_DONE) {
5474 /* The client seems to still be sending data, probably
5475 * because we got an error response during an upload.
5476 * We have the choice of either breaking the connection
5477 * or letting it pass through. Let's do the later.
5478 */
5479 goto wait_other_side;
5480 }
5481
5482 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5483 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005484 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005485 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005486 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005487 goto wait_other_side;
5488 }
5489
5490 /* When we get here, it means that both the request and the
5491 * response have finished receiving. Depending on the connection
5492 * mode, we'll have to wait for the last bytes to leave in either
5493 * direction, and sometimes for a close to be effective.
5494 */
5495
5496 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5497 /* Server-close mode : shut read and wait for the request
5498 * side to close its output buffer. The caller will detect
5499 * when we're in DONE and the other is in CLOSED and will
5500 * catch that for the final cleanup.
5501 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005502 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5503 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005505 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5506 /* Option forceclose is set, or either side wants to close,
5507 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005508 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005509 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005511 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5512 channel_shutr_now(chn);
5513 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 }
5516 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005517 /* The last possible modes are keep-alive and tunnel. Tunnel will
5518 * need to forward remaining data. Keep-alive will need to monitor
5519 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005520 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005521 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005522 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005523 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5524 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005525 }
5526
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005527 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005529 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005530 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5531 goto http_msg_closing;
5532 }
5533 else {
5534 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5535 goto http_msg_closed;
5536 }
5537 }
5538 goto wait_other_side;
5539 }
5540
5541 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5542 http_msg_closing:
5543 /* nothing else to forward, just waiting for the output buffer
5544 * to be empty and for the shutw_now to take effect.
5545 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005546 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5548 goto http_msg_closed;
5549 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005550 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005551 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005553 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005554 if (objt_server(s->target))
5555 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 goto wait_other_side;
5557 }
5558 }
5559
5560 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5561 http_msg_closed:
5562 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005563 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005564 channel_auto_close(chn);
5565 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 goto wait_other_side;
5567 }
5568
5569 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005570 /* We force the response to leave immediately if we're waiting for the
5571 * other side, since there is no pending shutdown to push it out.
5572 */
5573 if (!channel_is_empty(chn))
5574 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005575 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576}
5577
5578
5579/* Resync the request and response state machines. Return 1 if either state
5580 * changes.
5581 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005582int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005583{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005584 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 int old_req_state = txn->req.msg_state;
5586 int old_res_state = txn->rsp.msg_state;
5587
Willy Tarreau610ecce2010-01-04 21:15:02 +01005588 http_sync_req_state(s);
5589 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005590 if (!http_sync_res_state(s))
5591 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005592 if (!http_sync_req_state(s))
5593 break;
5594 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005595
Christopher Faulet814d2702017-03-30 11:33:44 +02005596 DPRINTF(stderr,"[%u] %s: stream=%p old=%d,%d cur=%d,%d\n",
5597 now_ms, __FUNCTION__,
5598 s,
5599 old_req_state, old_res_state,
5600 txn->req.msg_state, txn->rsp.msg_state);
5601
5602
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 /* OK, both state machines agree on a compatible state.
5604 * There are a few cases we're interested in :
5605 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5606 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5607 * directions, so let's simply disable both analysers.
5608 * - HTTP_MSG_CLOSED on the response only means we must abort the
5609 * request.
5610 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5611 * with server-close mode means we've completed one request and we
5612 * must re-initialize the server connection.
5613 */
5614
5615 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5616 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5617 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5618 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005619 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005620 channel_auto_close(&s->req);
5621 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005622 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005623 channel_auto_close(&s->res);
5624 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005625 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005626 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005627 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005628 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005630 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005631 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005632 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005633 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005634 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005635 channel_auto_close(&s->res);
5636 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005637 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005638 channel_abort(&s->req);
5639 channel_auto_close(&s->req);
5640 channel_auto_read(&s->req);
5641 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005642 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005643 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5644 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005645 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005646 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5647 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5648 /* server-close/keep-alive: terminate this transaction,
5649 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005650 * a fresh-new transaction, but only once we're sure there's
5651 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02005652 * another request. They must not hold any pending output data
5653 * and the response buffer must realigned
5654 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01005655 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005656 if (s->req.buf->o)
5657 s->req.flags |= CF_WAKE_WRITE;
Christopher Fauletc0c672a2017-03-28 11:51:33 +02005658 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005659 s->res.flags |= CF_WAKE_WRITE;
Christopher Faulete6006242017-03-10 11:52:44 +01005660 else {
5661 s->req.analysers = AN_REQ_FLT_END;
5662 s->res.analysers = AN_RES_FLT_END;
5663 txn->flags |= TX_WAIT_CLEANUP;
5664 return 1;
5665 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005666 }
5667
Willy Tarreau610ecce2010-01-04 21:15:02 +01005668 return txn->req.msg_state != old_req_state ||
5669 txn->rsp.msg_state != old_res_state;
5670}
5671
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672/* This function is an analyser which forwards request body (including chunk
5673 * sizes if any). It is called as soon as we must forward, even if we forward
5674 * zero byte. The only situation where it must not be called is when we're in
5675 * tunnel mode and we want to forward till the close. It's used both to forward
5676 * remaining data and to resync after end of body. It expects the msg_state to
5677 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005678 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005679 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005680 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005682int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005684 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005685 struct http_txn *txn = s->txn;
5686 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005687 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005688
Christopher Faulet814d2702017-03-30 11:33:44 +02005689 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5690 now_ms, __FUNCTION__,
5691 s,
5692 req,
5693 req->rex, req->wex,
5694 req->flags,
5695 req->buf->i,
5696 req->analysers);
5697
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005698 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5699 return 0;
5700
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005701 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005702 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005703 /* Output closed while we were sending data. We must abort and
5704 * wake the other side up.
5705 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005706 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005707 msg->msg_state = HTTP_MSG_ERROR;
5708 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005709 return 1;
5710 }
5711
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712 /* Note that we don't have to send 100-continue back because we don't
5713 * need the data to complete our job, and it's up to the server to
5714 * decide whether to return 100, 417 or anything else in return of
5715 * an "Expect: 100-continue" header.
5716 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005717 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5719 ? HTTP_MSG_CHUNK_SIZE
5720 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005721
5722 /* TODO/filters: when http-buffer-request option is set or if a
5723 * rule on url_param exists, the first chunk size could be
5724 * already parsed. In that case, msg->next is after the chunk
5725 * size (including the CRLF after the size). So this case should
5726 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005727 }
5728
Willy Tarreau7ba23542014-04-17 21:50:00 +02005729 /* Some post-connect processing might want us to refrain from starting to
5730 * forward data. Currently, the only reason for this is "balance url_param"
5731 * whichs need to parse/process the request after we've enabled forwarding.
5732 */
5733 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005734 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005735 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005736 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005737 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005738 }
5739 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5740 }
5741
Willy Tarreau80a92c02014-03-12 10:41:13 +01005742 /* in most states, we should abort in case of early close */
5743 channel_auto_close(req);
5744
Willy Tarreauefdf0942014-04-24 20:08:57 +02005745 if (req->to_forward) {
5746 /* We can't process the buffer's contents yet */
5747 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005748 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005749 }
5750
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751 if (msg->msg_state < HTTP_MSG_DONE) {
5752 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5753 ? http_msg_forward_chunked_body(s, msg)
5754 : http_msg_forward_body(s, msg));
5755 if (!ret)
5756 goto missing_data_or_waiting;
5757 if (ret < 0)
5758 goto return_bad_req;
5759 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005760
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005761 /* other states, DONE...TUNNEL */
5762 /* we don't want to forward closes on DONE except in tunnel mode. */
5763 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5764 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005765
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005766 if (http_resync_states(s)) {
5767 /* some state changes occurred, maybe the analyser
5768 * was disabled too. */
5769 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5770 if (req->flags & CF_SHUTW) {
5771 /* request errors are most likely due to the
5772 * server aborting the transfer. */
5773 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005774 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005775 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005776 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005777 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005778 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005779 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005780 }
5781
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 /* If "option abortonclose" is set on the backend, we want to monitor
5783 * the client's connection and forward any shutdown notification to the
5784 * server, which will decide whether to close or to go on processing the
5785 * request. We only do that in tunnel mode, and not in other modes since
5786 * it can be abused to exhaust source ports. */
5787 if (s->be->options & PR_O_ABRT_CLOSE) {
5788 channel_auto_read(req);
5789 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5790 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5791 s->si[1].flags |= SI_FL_NOLINGER;
5792 channel_auto_close(req);
5793 }
5794 else if (s->txn->meth == HTTP_METH_POST) {
5795 /* POST requests may require to read extra CRLF sent by broken
5796 * browsers and which could cause an RST to be sent upon close
5797 * on some systems (eg: Linux). */
5798 channel_auto_read(req);
5799 }
5800 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005801
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005802 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005803 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005804 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005805 if (!(s->flags & SF_ERR_MASK))
5806 s->flags |= SF_ERR_CLICL;
5807 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005808 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005809 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005810 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005811 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005812 }
5813
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005814 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005815 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005816 if (objt_server(s->target))
5817 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005818
5819 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005820 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005821
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005822 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005823 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005824 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005825
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005826 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005827 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005828 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005829 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005830 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005831
Willy Tarreau5c620922011-05-11 19:56:11 +02005832 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005833 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005834 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005835 * modes are already handled by the stream sock layer. We must not do
5836 * this in content-length mode because it could present the MSG_MORE
5837 * flag with the last block of forwarded data, which would cause an
5838 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005839 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005840 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005841 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005842
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005843 return 0;
5844
Willy Tarreaud98cf932009-12-27 22:54:55 +01005845 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005846 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005847 if (sess->listener->counters)
5848 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005849
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005850 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005851 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852 txn->req.msg_state = HTTP_MSG_ERROR;
5853 if (txn->status) {
5854 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005855 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005856 } else {
5857 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005858 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005860 req->analysers &= AN_REQ_FLT_END;
5861 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 +01005862
Willy Tarreaue7dff022015-04-03 01:14:29 +02005863 if (!(s->flags & SF_ERR_MASK))
5864 s->flags |= SF_ERR_PRXCOND;
5865 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005866 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005867 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005868 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005869 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005870 }
5871 return 0;
5872
5873 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005874 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005875 txn->req.msg_state = HTTP_MSG_ERROR;
5876 if (txn->status) {
5877 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005878 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005879 } else {
5880 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005881 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005883 req->analysers &= AN_REQ_FLT_END;
5884 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 +01005885
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005886 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005887 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005888 if (objt_server(s->target))
5889 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005890
Willy Tarreaue7dff022015-04-03 01:14:29 +02005891 if (!(s->flags & SF_ERR_MASK))
5892 s->flags |= SF_ERR_SRVCL;
5893 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005894 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005895 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005896 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005897 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005898 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005899 return 0;
5900}
5901
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5903 * processing can continue on next analysers, or zero if it either needs more
5904 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005905 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005906 * when it has nothing left to do, and may remove any analyser when it wants to
5907 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005908 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005909int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005910{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005911 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005912 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005914 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005915 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005917 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005918
Willy Tarreau87b09662015-04-03 00:22:06 +02005919 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 +02005920 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005921 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005922 rep,
5923 rep->rex, rep->wex,
5924 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005925 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005926 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005927
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928 /*
5929 * Now parse the partial (or complete) lines.
5930 * We will check the response syntax, and also join multi-line
5931 * headers. An index of all the lines will be elaborated while
5932 * parsing.
5933 *
5934 * For the parsing, we use a 28 states FSM.
5935 *
5936 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005937 * rep->buf->p = beginning of response
5938 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5939 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005940 * msg->eol = end of current header or line (LF or CRLF)
5941 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005942 */
5943
Willy Tarreau628c40c2014-04-24 19:11:26 +02005944 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005945 /* There's a protected area at the end of the buffer for rewriting
5946 * purposes. We don't want to start to parse the request if the
5947 * protected area is affected, because we may have to move processed
5948 * data later, which is much more complicated.
5949 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005950 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005951 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005952 /* some data has still not left the buffer, wake us once that's done */
5953 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5954 goto abort_response;
5955 channel_dont_close(rep);
5956 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005957 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005958 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005959 }
5960
Willy Tarreau379357a2013-06-08 12:55:46 +02005961 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5962 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5963 buffer_slow_realign(rep->buf);
5964
Willy Tarreau9b28e032012-10-12 23:49:43 +02005965 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005966 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005967 }
5968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 /* 1: we might have to print this header in debug mode */
5970 if (unlikely((global.mode & MODE_DEBUG) &&
5971 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005972 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005974
Willy Tarreau9b28e032012-10-12 23:49:43 +02005975 sol = rep->buf->p;
5976 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005978
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005979 sol += hdr_idx_first_pos(&txn->hdr_idx);
5980 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 while (cur_idx) {
5983 eol = sol + txn->hdr_idx.v[cur_idx].len;
5984 debug_hdr("srvhdr", s, sol, eol);
5985 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5986 cur_idx = txn->hdr_idx.v[cur_idx].next;
5987 }
5988 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005989
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 /*
5991 * Now we quickly check if we have found a full valid response.
5992 * If not so, we check the FD and buffer states before leaving.
5993 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005994 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 * responses are checked first.
5996 *
5997 * Depending on whether the client is still there or not, we
5998 * may send an error response back or not. Note that normally
5999 * we should only check for HTTP status there, and check I/O
6000 * errors somewhere else.
6001 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006002
Willy Tarreau655dce92009-11-08 13:10:58 +01006003 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006004 /* Invalid response */
6005 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6006 /* we detected a parsing error. We want to archive this response
6007 * in the dedicated proxy area for later troubleshooting.
6008 */
6009 hdr_response_bad:
6010 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006011 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006013 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006014 if (objt_server(s->target)) {
6015 objt_server(s->target)->counters.failed_resp++;
6016 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006017 }
Willy Tarreau64648412010-03-05 10:41:54 +01006018 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006019 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006020 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006022 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006023 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006024 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025
Willy Tarreaue7dff022015-04-03 01:14:29 +02006026 if (!(s->flags & SF_ERR_MASK))
6027 s->flags |= SF_ERR_PRXCOND;
6028 if (!(s->flags & SF_FINST_MASK))
6029 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006030
6031 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006032 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006034 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006035 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006036 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006037 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 goto hdr_response_bad;
6039 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006041 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006042 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006044 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006045 else if (txn->flags & TX_NOT_FIRST)
6046 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006047
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006048 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006049 if (objt_server(s->target)) {
6050 objt_server(s->target)->counters.failed_resp++;
6051 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006052 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006053
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006054 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006055 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006057 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006058 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006059 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02006060
Willy Tarreaue7dff022015-04-03 01:14:29 +02006061 if (!(s->flags & SF_ERR_MASK))
6062 s->flags |= SF_ERR_SRVCL;
6063 if (!(s->flags & SF_FINST_MASK))
6064 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006065 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006067
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006068 /* read timeout : return a 504 to the client. */
6069 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006070 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006071 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006073 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006074 if (objt_server(s->target)) {
6075 objt_server(s->target)->counters.failed_resp++;
6076 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006077 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006078
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006079 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006080 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006082 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006083 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006084 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02006085
Willy Tarreaue7dff022015-04-03 01:14:29 +02006086 if (!(s->flags & SF_ERR_MASK))
6087 s->flags |= SF_ERR_SRVTO;
6088 if (!(s->flags & SF_FINST_MASK))
6089 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 return 0;
6091 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006092
Willy Tarreauf003d372012-11-26 13:35:37 +01006093 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006094 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006095 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006096 s->be->be_counters.cli_aborts++;
6097 if (objt_server(s->target))
6098 objt_server(s->target)->counters.cli_aborts++;
6099
Christopher Faulet0184ea72017-01-05 14:06:34 +01006100 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006101 channel_auto_close(rep);
6102
6103 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006104 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006105 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01006106
Willy Tarreaue7dff022015-04-03 01:14:29 +02006107 if (!(s->flags & SF_ERR_MASK))
6108 s->flags |= SF_ERR_CLICL;
6109 if (!(s->flags & SF_FINST_MASK))
6110 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006111
Willy Tarreau87b09662015-04-03 00:22:06 +02006112 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006113 return 0;
6114 }
6115
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006116 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006117 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006118 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006119 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006120 else if (txn->flags & TX_NOT_FIRST)
6121 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006122
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006123 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006124 if (objt_server(s->target)) {
6125 objt_server(s->target)->counters.failed_resp++;
6126 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006127 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006128
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006129 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006130 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006131 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006132 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006133 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006134 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006135
Willy Tarreaue7dff022015-04-03 01:14:29 +02006136 if (!(s->flags & SF_ERR_MASK))
6137 s->flags |= SF_ERR_SRVCL;
6138 if (!(s->flags & SF_FINST_MASK))
6139 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006140 return 0;
6141 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006142
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006144 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006145 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006146 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006147 else if (txn->flags & TX_NOT_FIRST)
6148 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006149
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006150 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006151 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006152 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006153
Willy Tarreaue7dff022015-04-03 01:14:29 +02006154 if (!(s->flags & SF_ERR_MASK))
6155 s->flags |= SF_ERR_CLICL;
6156 if (!(s->flags & SF_FINST_MASK))
6157 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006158
Willy Tarreau87b09662015-04-03 00:22:06 +02006159 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006160 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006161 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006162
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006163 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006164 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006165 return 0;
6166 }
6167
6168 /* More interesting part now : we know that we have a complete
6169 * response which at least looks like HTTP. We have an indicator
6170 * of each header's length, so we can parse them quickly.
6171 */
6172
6173 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006174 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006175
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006176 /*
6177 * 1: get the status code
6178 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006179 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006180 if (n < 1 || n > 5)
6181 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006182 /* when the client triggers a 4xx from the server, it's most often due
6183 * to a missing object or permission. These events should be tracked
6184 * because if they happen often, it may indicate a brute force or a
6185 * vulnerability scan.
6186 */
6187 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006188 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006189
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006190 if (objt_server(s->target))
6191 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006192
Willy Tarreau91852eb2015-05-01 13:26:00 +02006193 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6194 * exactly one digit "." one digit. This check may be disabled using
6195 * option accept-invalid-http-response.
6196 */
6197 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6198 if (msg->sl.st.v_l != 8) {
6199 msg->err_pos = 0;
6200 goto hdr_response_bad;
6201 }
6202
6203 if (rep->buf->p[4] != '/' ||
6204 !isdigit((unsigned char)rep->buf->p[5]) ||
6205 rep->buf->p[6] != '.' ||
6206 !isdigit((unsigned char)rep->buf->p[7])) {
6207 msg->err_pos = 4;
6208 goto hdr_response_bad;
6209 }
6210 }
6211
Willy Tarreau5b154472009-12-21 20:11:07 +01006212 /* check if the response is HTTP/1.1 or above */
6213 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006214 ((rep->buf->p[5] > '1') ||
6215 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006216 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006217
6218 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006219 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 +01006220
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006221 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006222 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006223
Willy Tarreau9b28e032012-10-12 23:49:43 +02006224 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006225
Willy Tarreau39650402010-03-15 19:44:39 +01006226 /* Adjust server's health based on status code. Note: status codes 501
6227 * and 505 are triggered on demand by client request, so we must not
6228 * count them as server failures.
6229 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006230 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006231 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006232 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006233 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006234 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006235 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006236
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237 /*
6238 * 2: check for cacheability.
6239 */
6240
6241 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006242 case 100:
6243 /*
6244 * We may be facing a 100-continue response, in which case this
6245 * is not the right response, and we're waiting for the next one.
6246 * Let's allow this response to go to the client and wait for the
6247 * next one.
6248 */
6249 hdr_idx_init(&txn->hdr_idx);
6250 msg->next -= channel_forward(rep, msg->next);
6251 msg->msg_state = HTTP_MSG_RPBEFORE;
6252 txn->status = 0;
6253 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006254 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006255 goto next_one;
6256
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006257 case 200:
6258 case 203:
6259 case 206:
6260 case 300:
6261 case 301:
6262 case 410:
6263 /* RFC2616 @13.4:
6264 * "A response received with a status code of
6265 * 200, 203, 206, 300, 301 or 410 MAY be stored
6266 * by a cache (...) unless a cache-control
6267 * directive prohibits caching."
6268 *
6269 * RFC2616 @9.5: POST method :
6270 * "Responses to this method are not cacheable,
6271 * unless the response includes appropriate
6272 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006273 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006274 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006275 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006276 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6277 break;
6278 default:
6279 break;
6280 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006282 /*
6283 * 3: we may need to capture headers
6284 */
6285 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006286 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006287 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006288 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006289
Willy Tarreau557f1992015-05-01 10:05:17 +02006290 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6291 * by RFC7230#3.3.3 :
6292 *
6293 * The length of a message body is determined by one of the following
6294 * (in order of precedence):
6295 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006296 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6297 * the connection will become a tunnel immediately after the empty
6298 * line that concludes the header fields. A client MUST ignore
6299 * any Content-Length or Transfer-Encoding header fields received
6300 * in such a message. Any 101 response (Switching Protocols) is
6301 * managed in the same manner.
6302 *
6303 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006304 * (Informational), 204 (No Content), or 304 (Not Modified) status
6305 * code is always terminated by the first empty line after the
6306 * header fields, regardless of the header fields present in the
6307 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006309 * 3. If a Transfer-Encoding header field is present and the chunked
6310 * transfer coding (Section 4.1) is the final encoding, the message
6311 * body length is determined by reading and decoding the chunked
6312 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006313 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006314 * If a Transfer-Encoding header field is present in a response and
6315 * the chunked transfer coding is not the final encoding, the
6316 * message body length is determined by reading the connection until
6317 * it is closed by the server. If a Transfer-Encoding header field
6318 * is present in a request and the chunked transfer coding is not
6319 * the final encoding, the message body length cannot be determined
6320 * reliably; the server MUST respond with the 400 (Bad Request)
6321 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006322 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006323 * If a message is received with both a Transfer-Encoding and a
6324 * Content-Length header field, the Transfer-Encoding overrides the
6325 * Content-Length. Such a message might indicate an attempt to
6326 * perform request smuggling (Section 9.5) or response splitting
6327 * (Section 9.4) and ought to be handled as an error. A sender MUST
6328 * remove the received Content-Length field prior to forwarding such
6329 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006330 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006331 * 4. If a message is received without Transfer-Encoding and with
6332 * either multiple Content-Length header fields having differing
6333 * field-values or a single Content-Length header field having an
6334 * invalid value, then the message framing is invalid and the
6335 * recipient MUST treat it as an unrecoverable error. If this is a
6336 * request message, the server MUST respond with a 400 (Bad Request)
6337 * status code and then close the connection. If this is a response
6338 * message received by a proxy, the proxy MUST close the connection
6339 * to the server, discard the received response, and send a 502 (Bad
6340 * Gateway) response to the client. If this is a response message
6341 * received by a user agent, the user agent MUST close the
6342 * connection to the server and discard the received response.
6343 *
6344 * 5. If a valid Content-Length header field is present without
6345 * Transfer-Encoding, its decimal value defines the expected message
6346 * body length in octets. If the sender closes the connection or
6347 * the recipient times out before the indicated number of octets are
6348 * received, the recipient MUST consider the message to be
6349 * incomplete and close the connection.
6350 *
6351 * 6. If this is a request message and none of the above are true, then
6352 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006353 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006354 * 7. Otherwise, this is a response message without a declared message
6355 * body length, so the message body length is determined by the
6356 * number of octets received prior to the server closing the
6357 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006358 */
6359
6360 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006361 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006362 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363 * FIXME: should we parse anyway and return an error on chunked encoding ?
6364 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006365 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6366 txn->status == 101)) {
6367 /* Either we've established an explicit tunnel, or we're
6368 * switching the protocol. In both cases, we're very unlikely
6369 * to understand the next protocols. We have to switch to tunnel
6370 * mode, so that we transfer the request and responses then let
6371 * this protocol pass unmodified. When we later implement specific
6372 * parsers for such protocols, we'll want to check the Upgrade
6373 * header which contains information about that protocol for
6374 * responses with status 101 (eg: see RFC2817 about TLS).
6375 */
6376 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6377 msg->flags |= HTTP_MSGF_XFER_LEN;
6378 goto end;
6379 }
6380
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006381 if (txn->meth == HTTP_METH_HEAD ||
6382 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006383 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006384 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006385 goto skip_content_length;
6386 }
6387
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006388 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006389 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006390 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006391 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006392 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6393 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006394 /* bad transfer-encoding (chunked followed by something else) */
6395 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006396 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 break;
6398 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006399 }
6400
Willy Tarreau1c913912015-04-30 10:57:51 +02006401 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006402 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006403 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006404 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6405 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6406 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006407 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006408 signed long long cl;
6409
Willy Tarreauad14f752011-09-02 20:33:27 +02006410 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006411 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006412 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006413 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006414
Willy Tarreauad14f752011-09-02 20:33:27 +02006415 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006416 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006417 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006418 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006419
Willy Tarreauad14f752011-09-02 20:33:27 +02006420 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006421 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006422 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006423 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006424
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006425 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006426 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006428 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006429
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006430 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006431 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006432 }
6433
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006434 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006435 /* Now we have to check if we need to modify the Connection header.
6436 * This is more difficult on the response than it is on the request,
6437 * because we can have two different HTTP versions and we don't know
6438 * how the client will interprete a response. For instance, let's say
6439 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6440 * HTTP/1.1 response without any header. Maybe it will bound itself to
6441 * HTTP/1.0 because it only knows about it, and will consider the lack
6442 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6443 * the lack of header as a keep-alive. Thus we will use two flags
6444 * indicating how a request MAY be understood by the client. In case
6445 * of multiple possibilities, we'll fix the header to be explicit. If
6446 * ambiguous cases such as both close and keepalive are seen, then we
6447 * will fall back to explicit close. Note that we won't take risks with
6448 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006449 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006450 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006451 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6452 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6453 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6454 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006455 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006456
Willy Tarreau70dffda2014-01-30 03:07:23 +01006457 /* this situation happens when combining pretend-keepalive with httpclose. */
6458 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006459 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006460 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006461 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6462
Willy Tarreau60466522010-01-18 19:08:45 +01006463 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006464 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006465 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6466 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006467
Willy Tarreau60466522010-01-18 19:08:45 +01006468 /* now adjust header transformations depending on current state */
6469 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6470 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6471 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006472 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006473 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006474 }
Willy Tarreau60466522010-01-18 19:08:45 +01006475 else { /* SCL / KAL */
6476 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006477 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006478 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006479 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006480
Willy Tarreau60466522010-01-18 19:08:45 +01006481 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006482 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006483
Willy Tarreau60466522010-01-18 19:08:45 +01006484 /* Some keep-alive responses are converted to Server-close if
6485 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006486 */
Willy Tarreau60466522010-01-18 19:08:45 +01006487 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6488 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006489 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006490 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006491 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006492 }
6493
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006494 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006495 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006496 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006497
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006498 /* end of job, return OK */
6499 rep->analysers &= ~an_bit;
6500 rep->analyse_exp = TICK_ETERNITY;
6501 channel_auto_close(rep);
6502 return 1;
6503
6504 abort_keep_alive:
6505 /* A keep-alive request to the server failed on a network error.
6506 * The client is required to retry. We need to close without returning
6507 * any other information so that the client retries.
6508 */
6509 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006510 rep->analysers &= AN_RES_FLT_END;
6511 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006512 channel_auto_close(rep);
6513 s->logs.logwait = 0;
6514 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006515 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006516 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006517 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006518 return 0;
6519}
6520
6521/* This function performs all the processing enabled for the current response.
6522 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006523 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006524 * other functions. It works like process_request (see indications above).
6525 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006526int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006527{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006528 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006529 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006530 struct http_msg *msg = &txn->rsp;
6531 struct proxy *cur_proxy;
6532 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006533 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006534
Willy Tarreau87b09662015-04-03 00:22:06 +02006535 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 +02006536 now_ms, __FUNCTION__,
6537 s,
6538 rep,
6539 rep->rex, rep->wex,
6540 rep->flags,
6541 rep->buf->i,
6542 rep->analysers);
6543
6544 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6545 return 0;
6546
Willy Tarreau70730dd2014-04-24 18:06:27 +02006547 /* The stats applet needs to adjust the Connection header but we don't
6548 * apply any filter there.
6549 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006550 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6551 rep->analysers &= ~an_bit;
6552 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006553 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006554 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006555
Willy Tarreau58975672014-04-24 21:13:57 +02006556 /*
6557 * We will have to evaluate the filters.
6558 * As opposed to version 1.2, now they will be evaluated in the
6559 * filters order and not in the header order. This means that
6560 * each filter has to be validated among all headers.
6561 *
6562 * Filters are tried with ->be first, then with ->fe if it is
6563 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006564 *
6565 * Maybe we are in resume condiion. In this case I choose the
6566 * "struct proxy" which contains the rule list matching the resume
6567 * pointer. If none of theses "struct proxy" match, I initialise
6568 * the process with the first one.
6569 *
6570 * In fact, I check only correspondance betwwen the current list
6571 * pointer and the ->fe rule list. If it doesn't match, I initialize
6572 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006573 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006574 if (s->current_rule_list == &sess->fe->http_res_rules)
6575 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006576 else
6577 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006578 while (1) {
6579 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006580
Willy Tarreau58975672014-04-24 21:13:57 +02006581 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006582 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006583 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006584
Willy Tarreau51d861a2015-05-22 17:30:48 +02006585 if (ret == HTTP_RULE_RES_BADREQ)
6586 goto return_srv_prx_502;
6587
6588 if (ret == HTTP_RULE_RES_DONE) {
6589 rep->analysers &= ~an_bit;
6590 rep->analyse_exp = TICK_ETERNITY;
6591 return 1;
6592 }
6593 }
6594
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006595 /* we need to be called again. */
6596 if (ret == HTTP_RULE_RES_YIELD) {
6597 channel_dont_close(rep);
6598 return 0;
6599 }
6600
Willy Tarreau58975672014-04-24 21:13:57 +02006601 /* try headers filters */
6602 if (rule_set->rsp_exp != NULL) {
6603 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6604 return_bad_resp:
6605 if (objt_server(s->target)) {
6606 objt_server(s->target)->counters.failed_resp++;
6607 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006608 }
Willy Tarreau58975672014-04-24 21:13:57 +02006609 s->be->be_counters.failed_resp++;
6610 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006611 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006612 txn->status = 502;
6613 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006614 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006615 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02006616 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006617 if (!(s->flags & SF_ERR_MASK))
6618 s->flags |= SF_ERR_PRXCOND;
6619 if (!(s->flags & SF_FINST_MASK))
6620 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006621 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006622 }
Willy Tarreau58975672014-04-24 21:13:57 +02006623 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /* has the response been denied ? */
6626 if (txn->flags & TX_SVDENY) {
6627 if (objt_server(s->target))
6628 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006629
Willy Tarreau58975672014-04-24 21:13:57 +02006630 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006631 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006632 if (sess->listener->counters)
6633 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006634
Willy Tarreau58975672014-04-24 21:13:57 +02006635 goto return_srv_prx_502;
6636 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006637
Willy Tarreau58975672014-04-24 21:13:57 +02006638 /* add response headers from the rule sets in the same order */
6639 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006640 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006641 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006642 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006643 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006644 ret = acl_pass(ret);
6645 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6646 ret = !ret;
6647 if (!ret)
6648 continue;
6649 }
6650 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6651 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006652 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006653
Willy Tarreau58975672014-04-24 21:13:57 +02006654 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006655 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006656 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006657 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006658 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006659
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006660 /* After this point, this anayzer can't return yield, so we can
6661 * remove the bit corresponding to this analyzer from the list.
6662 *
6663 * Note that the intermediate returns and goto found previously
6664 * reset the analyzers.
6665 */
6666 rep->analysers &= ~an_bit;
6667 rep->analyse_exp = TICK_ETERNITY;
6668
Willy Tarreau58975672014-04-24 21:13:57 +02006669 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006670 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006671 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006672
Willy Tarreau58975672014-04-24 21:13:57 +02006673 /*
6674 * Now check for a server cookie.
6675 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006676 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006677 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006678
Willy Tarreau58975672014-04-24 21:13:57 +02006679 /*
6680 * Check for cache-control or pragma headers if required.
6681 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006682 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 +02006683 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006684
Willy Tarreau58975672014-04-24 21:13:57 +02006685 /*
6686 * Add server cookie in the response if needed
6687 */
6688 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6689 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006690 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006691 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6692 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6693 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6694 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6695 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006696 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006697 /* the server is known, it's not the one the client requested, or the
6698 * cookie's last seen date needs to be refreshed. We have to
6699 * insert a set-cookie here, except if we want to insert only on POST
6700 * requests and this one isn't. Note that servers which don't have cookies
6701 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 */
Willy Tarreau58975672014-04-24 21:13:57 +02006703 if (!objt_server(s->target)->cookie) {
6704 chunk_printf(&trash,
6705 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6706 s->be->cookie_name);
6707 }
6708 else {
6709 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6712 /* emit last_date, which is mandatory */
6713 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6714 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6715 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 if (s->be->cookie_maxlife) {
6718 /* emit first_date, which is either the original one or
6719 * the current date.
6720 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006721 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006722 s30tob64(txn->cookie_first_date ?
6723 txn->cookie_first_date >> 2 :
6724 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006725 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006726 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006727 }
Willy Tarreau58975672014-04-24 21:13:57 +02006728 chunk_appendf(&trash, "; path=/");
6729 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006730
Willy Tarreau58975672014-04-24 21:13:57 +02006731 if (s->be->cookie_domain)
6732 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006733
Willy Tarreau58975672014-04-24 21:13:57 +02006734 if (s->be->ck_opts & PR_CK_HTTPONLY)
6735 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006736
Willy Tarreau58975672014-04-24 21:13:57 +02006737 if (s->be->ck_opts & PR_CK_SECURE)
6738 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6741 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006744 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006745 /* the server did not change, only the date was updated */
6746 txn->flags |= TX_SCK_UPDATED;
6747 else
6748 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006749
Willy Tarreau58975672014-04-24 21:13:57 +02006750 /* Here, we will tell an eventual cache on the client side that we don't
6751 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6752 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6753 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006754 */
Willy Tarreau58975672014-04-24 21:13:57 +02006755 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006756
Willy Tarreau58975672014-04-24 21:13:57 +02006757 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006758
Willy Tarreau58975672014-04-24 21:13:57 +02006759 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6760 "Cache-control: private", 22) < 0))
6761 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006762 }
Willy Tarreau58975672014-04-24 21:13:57 +02006763 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 /*
6766 * Check if result will be cacheable with a cookie.
6767 * We'll block the response if security checks have caught
6768 * nasty things such as a cacheable cookie.
6769 */
6770 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6771 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6772 (s->be->options & PR_O_CHK_CACHE)) {
6773 /* we're in presence of a cacheable response containing
6774 * a set-cookie header. We'll block it as requested by
6775 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776 */
Willy Tarreau58975672014-04-24 21:13:57 +02006777 if (objt_server(s->target))
6778 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006779
Willy Tarreau58975672014-04-24 21:13:57 +02006780 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006781 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006782 if (sess->listener->counters)
6783 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784
Willy Tarreau58975672014-04-24 21:13:57 +02006785 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6786 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6787 send_log(s->be, LOG_ALERT,
6788 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6789 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6790 goto return_srv_prx_502;
6791 }
Willy Tarreau03945942009-12-22 16:50:27 +01006792
Willy Tarreau70730dd2014-04-24 18:06:27 +02006793 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006794 /*
6795 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6796 * If an "Upgrade" token is found, the header is left untouched in order
6797 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006798 * if anything but "Upgrade" is present in the Connection header. We don't
6799 * want to touch any 101 response either since it's switching to another
6800 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006801 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006802 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006803 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006804 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006805 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6806 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006807
Willy Tarreau58975672014-04-24 21:13:57 +02006808 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6809 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6810 /* we want a keep-alive response here. Keep-alive header
6811 * required if either side is not 1.1.
6812 */
6813 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6814 want_flags |= TX_CON_KAL_SET;
6815 }
6816 else {
6817 /* we want a close response here. Close header required if
6818 * the server is 1.1, regardless of the client.
6819 */
6820 if (msg->flags & HTTP_MSGF_VER_11)
6821 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006822 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823
Willy Tarreau58975672014-04-24 21:13:57 +02006824 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6825 http_change_connection_header(txn, msg, want_flags);
6826 }
6827
6828 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006829 /* Always enter in the body analyzer */
6830 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6831 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832
Willy Tarreau58975672014-04-24 21:13:57 +02006833 /* if the user wants to log as soon as possible, without counting
6834 * bytes from the server, then this is the right moment. We have
6835 * to temporarily assign bytes_out to log what we currently have.
6836 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006837 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006838 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6839 s->logs.bytes_out = txn->rsp.eoh;
6840 s->do_log(s);
6841 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006842 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006843 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006844}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845
Willy Tarreaud98cf932009-12-27 22:54:55 +01006846/* This function is an analyser which forwards response body (including chunk
6847 * sizes if any). It is called as soon as we must forward, even if we forward
6848 * zero byte. The only situation where it must not be called is when we're in
6849 * tunnel mode and we want to forward till the close. It's used both to forward
6850 * remaining data and to resync after end of body. It expects the msg_state to
6851 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006852 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006853 *
6854 * It is capable of compressing response data both in content-length mode and
6855 * in chunked mode. The state machines follows different flows depending on
6856 * whether content-length and chunked modes are used, since there are no
6857 * trailers in content-length :
6858 *
6859 * chk-mode cl-mode
6860 * ,----- BODY -----.
6861 * / \
6862 * V size > 0 V chk-mode
6863 * .--> SIZE -------------> DATA -------------> CRLF
6864 * | | size == 0 | last byte |
6865 * | v final crlf v inspected |
6866 * | TRAILERS -----------> DONE |
6867 * | |
6868 * `----------------------------------------------'
6869 *
6870 * Compression only happens in the DATA state, and must be flushed in final
6871 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6872 * is performed at once on final states for all bytes parsed, or when leaving
6873 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006874 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006875int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006876{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006877 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006878 struct http_txn *txn = s->txn;
6879 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006880 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006881
Christopher Faulet814d2702017-03-30 11:33:44 +02006882 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6883 now_ms, __FUNCTION__,
6884 s,
6885 res,
6886 res->rex, res->wex,
6887 res->flags,
6888 res->buf->i,
6889 res->analysers);
6890
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006891 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6892 return 0;
6893
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006894 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006895 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006896 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006897 /* Output closed while we were sending data. We must abort and
6898 * wake the other side up.
6899 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006900 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006901 msg->msg_state = HTTP_MSG_ERROR;
6902 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006903 return 1;
6904 }
6905
Willy Tarreau4fe41902010-06-07 22:27:41 +02006906 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006907 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006908
Christopher Fauletd7c91962015-04-30 11:48:27 +02006909 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006910 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6911 ? HTTP_MSG_CHUNK_SIZE
6912 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006913 }
6914
Willy Tarreauefdf0942014-04-24 20:08:57 +02006915 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006916 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006917 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006918 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006919 }
6920
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006921 if (msg->msg_state < HTTP_MSG_DONE) {
6922 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6923 ? http_msg_forward_chunked_body(s, msg)
6924 : http_msg_forward_body(s, msg));
6925 if (!ret)
6926 goto missing_data_or_waiting;
6927 if (ret < 0)
6928 goto return_bad_res;
6929 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006930
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006931 /* other states, DONE...TUNNEL */
6932 /* for keep-alive we don't want to forward closes on DONE */
6933 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6934 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6935 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006936
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006937 if (http_resync_states(s)) {
6938 /* some state changes occurred, maybe the analyser was disabled
6939 * too. */
6940 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6941 if (res->flags & CF_SHUTW) {
6942 /* response errors are most likely due to the
6943 * client aborting the transfer. */
6944 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006945 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006946 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006947 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006948 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006949 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006950 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006951 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006952 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006953
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006954 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006955 if (res->flags & CF_SHUTW)
6956 goto aborted_xfer;
6957
6958 /* stop waiting for data if the input is closed before the end. If the
6959 * client side was already closed, it means that the client has aborted,
6960 * so we don't want to count this as a server abort. Otherwise it's a
6961 * server abort.
6962 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006963 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006964 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006965 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006966 /* If we have some pending data, we continue the processing */
6967 if (!buffer_pending(res->buf)) {
6968 if (!(s->flags & SF_ERR_MASK))
6969 s->flags |= SF_ERR_SRVCL;
6970 s->be->be_counters.srv_aborts++;
6971 if (objt_server(s->target))
6972 objt_server(s->target)->counters.srv_aborts++;
6973 goto return_bad_res_stats_ok;
6974 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006975 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006976
Willy Tarreau40dba092010-03-04 18:14:51 +01006977 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006978 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006979 goto return_bad_res;
6980
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006981 /* When TE: chunked is used, we need to get there again to parse
6982 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006983 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6984 * or if there are filters registered on the stream, we don't want to
6985 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006986 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006987 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006988 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006989 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6990 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006991 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006992
Willy Tarreau5c620922011-05-11 19:56:11 +02006993 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006994 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006995 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006996 * modes are already handled by the stream sock layer. We must not do
6997 * this in content-length mode because it could present the MSG_MORE
6998 * flag with the last block of forwarded data, which would cause an
6999 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007000 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007001 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007002 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007003
Christopher Faulet69744d92017-03-30 10:54:35 +02007004 /* If there is neither content-length, nor transfer-encoding header
7005 * _AND_ there is no data filtering, we can safely forward all data
7006 * indefinitely. */
7007 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_DATA_FILTERS(s, res)) {
7008 buffer_flush(res->buf);
7009 channel_forward_forever(res);
7010 }
7011
Willy Tarreau87b09662015-04-03 00:22:06 +02007012 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007013 return 0;
7014
Willy Tarreau40dba092010-03-04 18:14:51 +01007015 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007016 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007017 if (objt_server(s->target))
7018 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007019
7020 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007021 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007022 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007023 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007024 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007025 res->analysers &= AN_RES_FLT_END;
7026 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 +01007027 if (objt_server(s->target))
7028 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007029
Willy Tarreaue7dff022015-04-03 01:14:29 +02007030 if (!(s->flags & SF_ERR_MASK))
7031 s->flags |= SF_ERR_PRXCOND;
7032 if (!(s->flags & SF_FINST_MASK))
7033 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007034 return 0;
7035
7036 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007037 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007038 txn->rsp.msg_state = HTTP_MSG_ERROR;
7039 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007040 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007041 res->analysers &= AN_RES_FLT_END;
7042 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 +01007043
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007044 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007045 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007046 if (objt_server(s->target))
7047 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007048
Willy Tarreaue7dff022015-04-03 01:14:29 +02007049 if (!(s->flags & SF_ERR_MASK))
7050 s->flags |= SF_ERR_CLICL;
7051 if (!(s->flags & SF_FINST_MASK))
7052 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007053 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007054}
7055
7056
7057static inline int
7058http_msg_forward_body(struct stream *s, struct http_msg *msg)
7059{
7060 struct channel *chn = msg->chn;
7061 int ret;
7062
7063 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7064
7065 if (msg->msg_state == HTTP_MSG_ENDING)
7066 goto ending;
7067
7068 /* Neither content-length, nor transfer-encoding was found, so we must
7069 * read the body until the server connection is closed. In that case, we
7070 * eat data as they come. Of course, this happens for response only. */
7071 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7072 unsigned long long len = (chn->buf->i - msg->next);
7073 msg->chunk_len += len;
7074 msg->body_len += len;
7075 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007076 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7077 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7078 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007079 msg->next += ret;
7080 msg->chunk_len -= ret;
7081 if (msg->chunk_len) {
7082 /* input empty or output full */
7083 if (chn->buf->i > msg->next)
7084 chn->flags |= CF_WAKE_WRITE;
7085 goto missing_data_or_waiting;
7086 }
7087
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007088 /* The server still sending data that should be filtered */
7089 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007090 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007091
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007092 msg->msg_state = HTTP_MSG_ENDING;
7093
7094 ending:
7095 /* we may have some pending data starting at res->buf->p such as a last
7096 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007097 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7098 /* default_ret */ msg->next,
7099 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007100 b_adv(chn->buf, ret);
7101 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007102 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7103 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007104 if (msg->next)
7105 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007106
Christopher Fauletda02e172015-12-04 09:25:05 +01007107 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7108 /* default_ret */ 1,
7109 /* on_error */ goto error,
7110 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007111 msg->msg_state = HTTP_MSG_DONE;
7112 return 1;
7113
7114 missing_data_or_waiting:
7115 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007116 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7117 /* default_ret */ msg->next,
7118 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007119 b_adv(chn->buf, ret);
7120 msg->next -= ret;
7121 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7122 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007123 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007124 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007125 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007126 return 0;
7127 error:
7128 return -1;
7129}
7130
7131static inline int
7132http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7133{
7134 struct channel *chn = msg->chn;
7135 int ret;
7136
7137 /* Here we have the guarantee to be in one of the following state:
7138 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7139 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7140
7141 switch_states:
7142 switch (msg->msg_state) {
7143 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007144 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7145 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7146 /* on_error */ goto error);
7147 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007148 msg->chunk_len -= ret;
7149 if (msg->chunk_len) {
7150 /* input empty or output full */
7151 if (chn->buf->i > msg->next)
7152 chn->flags |= CF_WAKE_WRITE;
7153 goto missing_data_or_waiting;
7154 }
7155
7156 /* nothing left to forward for this chunk*/
7157 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7158 /* fall through for HTTP_MSG_CHUNK_CRLF */
7159
7160 case HTTP_MSG_CHUNK_CRLF:
7161 /* we want the CRLF after the data */
7162 ret = http_skip_chunk_crlf(msg);
7163 if (ret == 0)
7164 goto missing_data_or_waiting;
7165 if (ret < 0)
7166 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007167 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007168 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7169 /* fall through for HTTP_MSG_CHUNK_SIZE */
7170
7171 case HTTP_MSG_CHUNK_SIZE:
7172 /* read the chunk size and assign it to ->chunk_len,
7173 * then set ->next to point to the body and switch to
7174 * DATA or TRAILERS state.
7175 */
7176 ret = http_parse_chunk_size(msg);
7177 if (ret == 0)
7178 goto missing_data_or_waiting;
7179 if (ret < 0)
7180 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007181 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007182 if (msg->chunk_len) {
7183 msg->msg_state = HTTP_MSG_DATA;
7184 goto switch_states;
7185 }
7186 msg->msg_state = HTTP_MSG_TRAILERS;
7187 /* fall through for HTTP_MSG_TRAILERS */
7188
7189 case HTTP_MSG_TRAILERS:
7190 ret = http_forward_trailers(msg);
7191 if (ret < 0)
7192 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007193 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7194 /* default_ret */ 1,
7195 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007196 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007197 if (!ret)
7198 goto missing_data_or_waiting;
7199 break;
7200
7201 case HTTP_MSG_ENDING:
7202 goto ending;
7203
7204 default:
7205 /* This should no happen in this function */
7206 goto error;
7207 }
7208
7209 msg->msg_state = HTTP_MSG_ENDING;
7210 ending:
7211 /* we may have some pending data starting at res->buf->p such as a last
7212 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007213 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007214 /* default_ret */ msg->next,
7215 /* on_error */ goto error);
7216 b_adv(chn->buf, ret);
7217 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007218 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7219 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007220 if (msg->next)
7221 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007222
Christopher Fauletda02e172015-12-04 09:25:05 +01007223 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007224 /* default_ret */ 1,
7225 /* on_error */ goto error,
7226 /* on_wait */ goto waiting);
7227 msg->msg_state = HTTP_MSG_DONE;
7228 return 1;
7229
7230 missing_data_or_waiting:
7231 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007232 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007233 /* default_ret */ msg->next,
7234 /* on_error */ goto error);
7235 b_adv(chn->buf, ret);
7236 msg->next -= ret;
7237 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7238 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007239 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007240 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007241 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007242 return 0;
7243
7244 chunk_parsing_error:
7245 if (msg->err_pos >= 0) {
7246 if (chn->flags & CF_ISRESP)
7247 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7248 msg->msg_state, strm_fe(s));
7249 else
7250 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7251 msg, msg->msg_state, s->be);
7252 }
7253 error:
7254 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007255}
7256
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258/* Iterate the same filter through all request headers.
7259 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 * Since it can manage the switch to another backend, it updates the per-proxy
7261 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007263int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 char *cur_ptr, *cur_end, *cur_next;
7266 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007267 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007269 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 last_hdr = 0;
7272
Willy Tarreau9b28e032012-10-12 23:49:43 +02007273 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 old_idx = 0;
7275
7276 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007277 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007279 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 (exp->action == ACT_ALLOW ||
7281 exp->action == ACT_DENY ||
7282 exp->action == ACT_TARPIT))
7283 return 0;
7284
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007285 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 if (!cur_idx)
7287 break;
7288
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007289 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 cur_ptr = cur_next;
7291 cur_end = cur_ptr + cur_hdr->len;
7292 cur_next = cur_end + cur_hdr->cr + 1;
7293
7294 /* Now we have one header between cur_ptr and cur_end,
7295 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296 */
7297
Willy Tarreau15a53a42015-01-21 13:39:42 +01007298 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007300 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007301 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 last_hdr = 1;
7303 break;
7304
7305 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007306 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007307 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 break;
7309
7310 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007311 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 break;
7314
7315 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007316 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7317 if (trash.len < 0)
7318 return -1;
7319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007320 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 /* FIXME: if the user adds a newline in the replacement, the
7322 * index will not be recalculated for now, and the new line
7323 * will not be counted as a new header.
7324 */
7325
7326 cur_end += delta;
7327 cur_next += delta;
7328 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007329 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007330 break;
7331
7332 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007333 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007334 cur_next += delta;
7335
Willy Tarreaufa355d42009-11-29 18:12:29 +01007336 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007337 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7338 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339 cur_hdr->len = 0;
7340 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007341 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 break;
7343
7344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 }
7346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347 /* keep the link from this header to next one in case of later
7348 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 old_idx = cur_idx;
7351 }
7352 return 0;
7353}
7354
7355
7356/* Apply the filter to the request line.
7357 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7358 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 * Since it can manage the switch to another backend, it updates the per-proxy
7360 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007362int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007364 char *cur_ptr, *cur_end;
7365 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007366 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007367 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368
Willy Tarreau3d300592007-03-18 18:34:41 +01007369 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 (exp->action == ACT_ALLOW ||
7373 exp->action == ACT_DENY ||
7374 exp->action == ACT_TARPIT))
7375 return 0;
7376 else if (exp->action == ACT_REMOVE)
7377 return 0;
7378
7379 done = 0;
7380
Willy Tarreau9b28e032012-10-12 23:49:43 +02007381 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007382 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007383
7384 /* Now we have the request line between cur_ptr and cur_end */
7385
Willy Tarreau15a53a42015-01-21 13:39:42 +01007386 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007389 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 done = 1;
7391 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007394 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 done = 1;
7396 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007399 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 done = 1;
7401 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007404 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7405 if (trash.len < 0)
7406 return -1;
7407
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007408 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409 /* FIXME: if the user adds a newline in the replacement, the
7410 * index will not be recalculated for now, and the new line
7411 * will not be counted as a new header.
7412 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007413
Willy Tarreaufa355d42009-11-29 18:12:29 +01007414 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007415 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007416 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 HTTP_MSG_RQMETH,
7418 cur_ptr, cur_end + 1,
7419 NULL, NULL);
7420 if (unlikely(!cur_end))
7421 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007423 /* we have a full request and we know that we have either a CR
7424 * or an LF at <ptr>.
7425 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007426 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7427 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 /* there is no point trying this regex on headers */
7429 return 1;
7430 }
7431 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432 return done;
7433}
Willy Tarreau97de6242006-12-27 17:18:38 +01007434
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007437/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007438 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007439 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 * unparsable request. Since it can manage the switch to another backend, it
7441 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007442 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007443int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007444{
Willy Tarreau192252e2015-04-04 01:47:55 +02007445 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007446 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447 struct hdr_exp *exp;
7448
7449 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007450 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007452 /*
7453 * The interleaving of transformations and verdicts
7454 * makes it difficult to decide to continue or stop
7455 * the evaluation.
7456 */
7457
Willy Tarreau6c123b12010-01-28 20:22:06 +01007458 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7459 break;
7460
Willy Tarreau3d300592007-03-18 18:34:41 +01007461 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007462 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007463 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007465
7466 /* if this filter had a condition, evaluate it now and skip to
7467 * next filter if the condition does not match.
7468 */
7469 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007470 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007471 ret = acl_pass(ret);
7472 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7473 ret = !ret;
7474
7475 if (!ret)
7476 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007477 }
7478
7479 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007480 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007481 if (unlikely(ret < 0))
7482 return -1;
7483
7484 if (likely(ret == 0)) {
7485 /* The filter did not match the request, it can be
7486 * iterated through all headers.
7487 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007488 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7489 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007492 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493}
7494
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007495
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496/* Find the end of a cookie value contained between <s> and <e>. It works the
7497 * same way as with headers above except that the semi-colon also ends a token.
7498 * See RFC2965 for more information. Note that it requires a valid header to
7499 * return a valid result.
7500 */
7501char *find_cookie_value_end(char *s, const char *e)
7502{
7503 int quoted, qdpair;
7504
7505 quoted = qdpair = 0;
7506 for (; s < e; s++) {
7507 if (qdpair) qdpair = 0;
7508 else if (quoted) {
7509 if (*s == '\\') qdpair = 1;
7510 else if (*s == '"') quoted = 0;
7511 }
7512 else if (*s == '"') quoted = 1;
7513 else if (*s == ',' || *s == ';') return s;
7514 }
7515 return s;
7516}
7517
7518/* Delete a value in a header between delimiters <from> and <next> in buffer
7519 * <buf>. The number of characters displaced is returned, and the pointer to
7520 * the first delimiter is updated if required. The function tries as much as
7521 * possible to respect the following principles :
7522 * - replace <from> delimiter by the <next> one unless <from> points to a
7523 * colon, in which case <next> is simply removed
7524 * - set exactly one space character after the new first delimiter, unless
7525 * there are not enough characters in the block being moved to do so.
7526 * - remove unneeded spaces before the previous delimiter and after the new
7527 * one.
7528 *
7529 * It is the caller's responsibility to ensure that :
7530 * - <from> points to a valid delimiter or the colon ;
7531 * - <next> points to a valid delimiter or the final CR/LF ;
7532 * - there are non-space chars before <from> ;
7533 * - there is a CR/LF at or after <next>.
7534 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007535int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007536{
7537 char *prev = *from;
7538
7539 if (*prev == ':') {
7540 /* We're removing the first value, preserve the colon and add a
7541 * space if possible.
7542 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007543 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 next++;
7545 prev++;
7546 if (prev < next)
7547 *prev++ = ' ';
7548
Willy Tarreau2235b262016-11-05 15:50:20 +01007549 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 next++;
7551 } else {
7552 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007553 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 prev--;
7555 *from = prev;
7556
7557 /* copy the delimiter and if possible a space if we're
7558 * not at the end of the line.
7559 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007560 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007561 *prev++ = *next++;
7562 if (prev + 1 < next)
7563 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007564 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007565 next++;
7566 }
7567 }
7568 return buffer_replace2(buf, prev, next, NULL, 0);
7569}
7570
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007571/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007572 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 * desirable to call it only when needed. This code is quite complex because
7574 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7575 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007577void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007579 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007580 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007582 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7584 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007587 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007588 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007590 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007592 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007593
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007594 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007595 hdr_beg = hdr_next;
7596 hdr_end = hdr_beg + cur_hdr->len;
7597 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 /* We have one full header between hdr_beg and hdr_end, and the
7600 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601 * "Cookie:" headers.
7602 */
7603
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007604 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007605 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007606 old_idx = cur_idx;
7607 continue;
7608 }
7609
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007610 del_from = NULL; /* nothing to be deleted */
7611 preserve_hdr = 0; /* assume we may kill the whole header */
7612
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613 /* Now look for cookies. Conforming to RFC2109, we have to support
7614 * attributes whose name begin with a '$', and associate them with
7615 * the right cookie, if we want to delete this cookie.
7616 * So there are 3 cases for each cookie read :
7617 * 1) it's a special attribute, beginning with a '$' : ignore it.
7618 * 2) it's a server id cookie that we *MAY* want to delete : save
7619 * some pointers on it (last semi-colon, beginning of cookie...)
7620 * 3) it's an application cookie : we *MAY* have to delete a previous
7621 * "special" cookie.
7622 * At the end of loop, if a "special" cookie remains, we may have to
7623 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 * *MUST* delete it.
7625 *
7626 * Note: RFC2965 is unclear about the processing of spaces around
7627 * the equal sign in the ATTR=VALUE form. A careful inspection of
7628 * the RFC explicitly allows spaces before it, and not within the
7629 * tokens (attrs or values). An inspection of RFC2109 allows that
7630 * too but section 10.1.3 lets one think that spaces may be allowed
7631 * after the equal sign too, resulting in some (rare) buggy
7632 * implementations trying to do that. So let's do what servers do.
7633 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7634 * allowed quoted strings in values, with any possible character
7635 * after a backslash, including control chars and delimitors, which
7636 * causes parsing to become ambiguous. Browsers also allow spaces
7637 * within values even without quotes.
7638 *
7639 * We have to keep multiple pointers in order to support cookie
7640 * removal at the beginning, middle or end of header without
7641 * corrupting the header. All of these headers are valid :
7642 *
7643 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7644 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7645 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7646 * | | | | | | | | |
7647 * | | | | | | | | hdr_end <--+
7648 * | | | | | | | +--> next
7649 * | | | | | | +----> val_end
7650 * | | | | | +-----------> val_beg
7651 * | | | | +--------------> equal
7652 * | | | +----------------> att_end
7653 * | | +---------------------> att_beg
7654 * | +--------------------------> prev
7655 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656 */
7657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7659 /* Iterate through all cookies on this line */
7660
7661 /* find att_beg */
7662 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007663 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 att_beg++;
7665
7666 /* find att_end : this is the first character after the last non
7667 * space before the equal. It may be equal to hdr_end.
7668 */
7669 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 while (equal < hdr_end) {
7672 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007674 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007675 continue;
7676 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677 }
7678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7680 * is between <att_beg> and <equal>, both may be identical.
7681 */
7682
7683 /* look for end of cookie if there is an equal sign */
7684 if (equal < hdr_end && *equal == '=') {
7685 /* look for the beginning of the value */
7686 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007687 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007688 val_beg++;
7689
7690 /* find the end of the value, respecting quotes */
7691 next = find_cookie_value_end(val_beg, hdr_end);
7692
7693 /* make val_end point to the first white space or delimitor after the value */
7694 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007695 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 val_end--;
7697 } else {
7698 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 /* We have nothing to do with attributes beginning with '$'. However,
7702 * they will automatically be removed if a header before them is removed,
7703 * since they're supposed to be linked together.
7704 */
7705 if (*att_beg == '$')
7706 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007707
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 /* Ignore cookies with no equal sign */
7709 if (equal == next) {
7710 /* This is not our cookie, so we must preserve it. But if we already
7711 * scheduled another cookie for removal, we cannot remove the
7712 * complete header, but we can remove the previous block itself.
7713 */
7714 preserve_hdr = 1;
7715 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007716 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 val_end += delta;
7718 next += delta;
7719 hdr_end += delta;
7720 hdr_next += delta;
7721 cur_hdr->len += delta;
7722 http_msg_move_end(&txn->req, delta);
7723 prev = del_from;
7724 del_from = NULL;
7725 }
7726 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007727 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 /* if there are spaces around the equal sign, we need to
7730 * strip them otherwise we'll get trouble for cookie captures,
7731 * or even for rewrites. Since this happens extremely rarely,
7732 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7735 int stripped_before = 0;
7736 int stripped_after = 0;
7737
7738 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007739 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007740 equal += stripped_before;
7741 val_beg += stripped_before;
7742 }
7743
7744 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007745 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 val_beg += stripped_after;
7747 stripped_before += stripped_after;
7748 }
7749
7750 val_end += stripped_before;
7751 next += stripped_before;
7752 hdr_end += stripped_before;
7753 hdr_next += stripped_before;
7754 cur_hdr->len += stripped_before;
7755 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007756 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007757 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 /* First, let's see if we want to capture this cookie. We check
7760 * that we don't already have a client side cookie, because we
7761 * can only capture one. Also as an optimisation, we ignore
7762 * cookies shorter than the declared name.
7763 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007764 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7765 (val_end - att_beg >= sess->fe->capture_namelen) &&
7766 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007767 int log_len = val_end - att_beg;
7768
7769 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7770 Alert("HTTP logging : out of memory.\n");
7771 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007772 if (log_len > sess->fe->capture_len)
7773 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 memcpy(txn->cli_cookie, att_beg, log_len);
7775 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007776 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778
Willy Tarreaubca99692010-10-06 19:25:55 +02007779 /* Persistence cookies in passive, rewrite or insert mode have the
7780 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007782 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007783 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007784 * For cookies in prefix mode, the form is :
7785 *
7786 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007788 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7789 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7790 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007791 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7794 * have the server ID between val_beg and delim, and the original cookie between
7795 * delim+1 and val_end. Otherwise, delim==val_end :
7796 *
7797 * Cookie: NAME=SRV; # in all but prefix modes
7798 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7799 * | || || | |+-> next
7800 * | || || | +--> val_end
7801 * | || || +---------> delim
7802 * | || |+------------> val_beg
7803 * | || +-------------> att_end = equal
7804 * | |+-----------------> att_beg
7805 * | +------------------> prev
7806 * +-------------------------> hdr_beg
7807 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007809 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007810 for (delim = val_beg; delim < val_end; delim++)
7811 if (*delim == COOKIE_DELIM)
7812 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 } else {
7814 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007815 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 /* Now check if the cookie contains a date field, which would
7817 * appear after a vertical bar ('|') just after the server name
7818 * and before the delimiter.
7819 */
7820 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7821 if (vbar1) {
7822 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007823 * right is the last seen date. It is a base64 encoded
7824 * 30-bit value representing the UNIX date since the
7825 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007826 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007827 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007828 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007829 if (val_end - vbar1 >= 5) {
7830 val = b64tos30(vbar1);
7831 if (val > 0)
7832 txn->cookie_last_date = val << 2;
7833 }
7834 /* look for a second vertical bar */
7835 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7836 if (vbar1 && (val_end - vbar1 > 5)) {
7837 val = b64tos30(vbar1 + 1);
7838 if (val > 0)
7839 txn->cookie_first_date = val << 2;
7840 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007841 }
7842 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007843
Willy Tarreauf64d1412010-10-07 20:06:11 +02007844 /* if the cookie has an expiration date and the proxy wants to check
7845 * it, then we do that now. We first check if the cookie is too old,
7846 * then only if it has expired. We detect strict overflow because the
7847 * time resolution here is not great (4 seconds). Cookies with dates
7848 * in the future are ignored if their offset is beyond one day. This
7849 * allows an admin to fix timezone issues without expiring everyone
7850 * and at the same time avoids keeping unwanted side effects for too
7851 * long.
7852 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007853 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7854 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007855 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007856 txn->flags &= ~TX_CK_MASK;
7857 txn->flags |= TX_CK_OLD;
7858 delim = val_beg; // let's pretend we have not found the cookie
7859 txn->cookie_first_date = 0;
7860 txn->cookie_last_date = 0;
7861 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007862 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7863 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007864 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007865 txn->flags &= ~TX_CK_MASK;
7866 txn->flags |= TX_CK_EXPIRED;
7867 delim = val_beg; // let's pretend we have not found the cookie
7868 txn->cookie_first_date = 0;
7869 txn->cookie_last_date = 0;
7870 }
7871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 /* Here, we'll look for the first running server which supports the cookie.
7873 * This allows to share a same cookie between several servers, for example
7874 * to dedicate backup servers to specific servers only.
7875 * However, to prevent clients from sticking to cookie-less backup server
7876 * when they have incidentely learned an empty cookie, we simply ignore
7877 * empty cookies and mark them as invalid.
7878 * The same behaviour is applied when persistence must be ignored.
7879 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007880 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007883 while (srv) {
7884 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7885 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007886 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007887 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007888 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007889 /* we found the server and we can use it */
7890 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007891 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007892 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007893 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007894 break;
7895 } else {
7896 /* we found a server, but it's down,
7897 * mark it as such and go on in case
7898 * another one is available.
7899 */
7900 txn->flags &= ~TX_CK_MASK;
7901 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007902 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 srv = srv->next;
7905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906
Willy Tarreauf64d1412010-10-07 20:06:11 +02007907 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007908 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007910 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007911 txn->flags |= TX_CK_UNUSED;
7912 else
7913 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007914 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007916 /* depending on the cookie mode, we may have to either :
7917 * - delete the complete cookie if we're in insert+indirect mode, so that
7918 * the server never sees it ;
7919 * - remove the server id from the cookie value, and tag the cookie as an
7920 * application cookie so that it does not get accidentely removed later,
7921 * if we're in cookie prefix mode
7922 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925
Willy Tarreau9b28e032012-10-12 23:49:43 +02007926 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 val_end += delta;
7928 next += delta;
7929 hdr_end += delta;
7930 hdr_next += delta;
7931 cur_hdr->len += delta;
7932 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007933
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007934 del_from = NULL;
7935 preserve_hdr = 1; /* we want to keep this cookie */
7936 }
7937 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007938 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 del_from = prev;
7940 }
7941 } else {
7942 /* This is not our cookie, so we must preserve it. But if we already
7943 * scheduled another cookie for removal, we cannot remove the
7944 * complete header, but we can remove the previous block itself.
7945 */
7946 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007949 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007950 if (att_beg >= del_from)
7951 att_beg += delta;
7952 if (att_end >= del_from)
7953 att_end += delta;
7954 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 val_end += delta;
7956 next += delta;
7957 hdr_end += delta;
7958 hdr_next += delta;
7959 cur_hdr->len += delta;
7960 http_msg_move_end(&txn->req, delta);
7961 prev = del_from;
7962 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007966 /* continue with next cookie on this header line */
7967 att_beg = next;
7968 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 /* There are no more cookies on this line.
7971 * We may still have one (or several) marked for deletion at the
7972 * end of the line. We must do this now in two ways :
7973 * - if some cookies must be preserved, we only delete from the
7974 * mark to the end of line ;
7975 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007977 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007978 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007979 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007980 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007981 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 cur_hdr->len += delta;
7983 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007984 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985
7986 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007987 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7988 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007989 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007990 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007991 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007992 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007993 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 }
7995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007996 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007997 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007998 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007999}
8000
8001
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002/* Iterate the same filter through all response headers contained in <rtr>.
8003 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8004 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008005int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 char *cur_ptr, *cur_end, *cur_next;
8008 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008009 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008011 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012
8013 last_hdr = 0;
8014
Willy Tarreau9b28e032012-10-12 23:49:43 +02008015 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 old_idx = 0;
8017
8018 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008021 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 (exp->action == ACT_ALLOW ||
8023 exp->action == ACT_DENY))
8024 return 0;
8025
8026 cur_idx = txn->hdr_idx.v[old_idx].next;
8027 if (!cur_idx)
8028 break;
8029
8030 cur_hdr = &txn->hdr_idx.v[cur_idx];
8031 cur_ptr = cur_next;
8032 cur_end = cur_ptr + cur_hdr->len;
8033 cur_next = cur_end + cur_hdr->cr + 1;
8034
8035 /* Now we have one header between cur_ptr and cur_end,
8036 * and the next header starts at cur_next.
8037 */
8038
Willy Tarreau15a53a42015-01-21 13:39:42 +01008039 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 switch (exp->action) {
8041 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008042 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 last_hdr = 1;
8044 break;
8045
8046 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008047 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 last_hdr = 1;
8049 break;
8050
8051 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008052 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8053 if (trash.len < 0)
8054 return -1;
8055
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008056 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008057 /* FIXME: if the user adds a newline in the replacement, the
8058 * index will not be recalculated for now, and the new line
8059 * will not be counted as a new header.
8060 */
8061
8062 cur_end += delta;
8063 cur_next += delta;
8064 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008065 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 break;
8067
8068 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008069 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 cur_next += delta;
8071
Willy Tarreaufa355d42009-11-29 18:12:29 +01008072 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8074 txn->hdr_idx.used--;
8075 cur_hdr->len = 0;
8076 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008077 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 break;
8079
8080 }
8081 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082
8083 /* keep the link from this header to next one in case of later
8084 * removal of next header.
8085 */
8086 old_idx = cur_idx;
8087 }
8088 return 0;
8089}
8090
8091
8092/* Apply the filter to the status line in the response buffer <rtr>.
8093 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8094 * or -1 if a replacement resulted in an invalid status line.
8095 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008096int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008098 char *cur_ptr, *cur_end;
8099 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008100 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008101 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102
8103
Willy Tarreau3d300592007-03-18 18:34:41 +01008104 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008106 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107 (exp->action == ACT_ALLOW ||
8108 exp->action == ACT_DENY))
8109 return 0;
8110 else if (exp->action == ACT_REMOVE)
8111 return 0;
8112
8113 done = 0;
8114
Willy Tarreau9b28e032012-10-12 23:49:43 +02008115 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008116 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117
8118 /* Now we have the status line between cur_ptr and cur_end */
8119
Willy Tarreau15a53a42015-01-21 13:39:42 +01008120 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 switch (exp->action) {
8122 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008123 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 done = 1;
8125 break;
8126
8127 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008128 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 done = 1;
8130 break;
8131
8132 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008133 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8134 if (trash.len < 0)
8135 return -1;
8136
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008137 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138 /* FIXME: if the user adds a newline in the replacement, the
8139 * index will not be recalculated for now, and the new line
8140 * will not be counted as a new header.
8141 */
8142
Willy Tarreaufa355d42009-11-29 18:12:29 +01008143 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008145 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008146 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 cur_ptr, cur_end + 1,
8148 NULL, NULL);
8149 if (unlikely(!cur_end))
8150 return -1;
8151
8152 /* we have a full respnse and we know that we have either a CR
8153 * or an LF at <ptr>.
8154 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008155 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008156 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 /* there is no point trying this regex on headers */
8158 return 1;
8159 }
8160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 return done;
8162}
8163
8164
8165
8166/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008167 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8169 * unparsable response.
8170 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008171int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172{
Willy Tarreau192252e2015-04-04 01:47:55 +02008173 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008174 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008175 struct hdr_exp *exp;
8176
8177 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008178 int ret;
8179
8180 /*
8181 * The interleaving of transformations and verdicts
8182 * makes it difficult to decide to continue or stop
8183 * the evaluation.
8184 */
8185
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008186 if (txn->flags & TX_SVDENY)
8187 break;
8188
Willy Tarreau3d300592007-03-18 18:34:41 +01008189 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8191 exp->action == ACT_PASS)) {
8192 exp = exp->next;
8193 continue;
8194 }
8195
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008196 /* if this filter had a condition, evaluate it now and skip to
8197 * next filter if the condition does not match.
8198 */
8199 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008200 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008201 ret = acl_pass(ret);
8202 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8203 ret = !ret;
8204 if (!ret)
8205 continue;
8206 }
8207
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008209 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 if (unlikely(ret < 0))
8211 return -1;
8212
8213 if (likely(ret == 0)) {
8214 /* The filter did not match the response, it can be
8215 * iterated through all headers.
8216 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008217 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8218 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 }
8221 return 0;
8222}
8223
8224
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008226 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 * desirable to call it only when needed. This function is also used when we
8228 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008230void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008232 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008233 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008234 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 char *hdr_beg, *hdr_end, *hdr_next;
8238 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 /* Iterate through the headers.
8241 * we start with the start line.
8242 */
8243 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008244 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245
8246 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8247 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008248 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
8250 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 hdr_beg = hdr_next;
8252 hdr_end = hdr_beg + cur_hdr->len;
8253 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 /* We have one full header between hdr_beg and hdr_end, and the
8256 * next header starts at hdr_next. We're only interested in
8257 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258 */
8259
Willy Tarreau24581ba2010-08-31 22:39:35 +02008260 is_cookie2 = 0;
8261 prev = hdr_beg + 10;
8262 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008263 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8265 if (!val) {
8266 old_idx = cur_idx;
8267 continue;
8268 }
8269 is_cookie2 = 1;
8270 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008271 }
8272
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8274 * <prev> points to the colon.
8275 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008276 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277
Willy Tarreau24581ba2010-08-31 22:39:35 +02008278 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8279 * check-cache is enabled) and we are not interested in checking
8280 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008281 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008282 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283 return;
8284
Willy Tarreau24581ba2010-08-31 22:39:35 +02008285 /* OK so now we know we have to process this response cookie.
8286 * The format of the Set-Cookie header is slightly different
8287 * from the format of the Cookie header in that it does not
8288 * support the comma as a cookie delimiter (thus the header
8289 * cannot be folded) because the Expires attribute described in
8290 * the original Netscape's spec may contain an unquoted date
8291 * with a comma inside. We have to live with this because
8292 * many browsers don't support Max-Age and some browsers don't
8293 * support quoted strings. However the Set-Cookie2 header is
8294 * clean.
8295 *
8296 * We have to keep multiple pointers in order to support cookie
8297 * removal at the beginning, middle or end of header without
8298 * corrupting the header (in case of set-cookie2). A special
8299 * pointer, <scav> points to the beginning of the set-cookie-av
8300 * fields after the first semi-colon. The <next> pointer points
8301 * either to the end of line (set-cookie) or next unquoted comma
8302 * (set-cookie2). All of these headers are valid :
8303 *
8304 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8305 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8306 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8307 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8308 * | | | | | | | | | |
8309 * | | | | | | | | +-> next hdr_end <--+
8310 * | | | | | | | +------------> scav
8311 * | | | | | | +--------------> val_end
8312 * | | | | | +--------------------> val_beg
8313 * | | | | +----------------------> equal
8314 * | | | +------------------------> att_end
8315 * | | +----------------------------> att_beg
8316 * | +------------------------------> prev
8317 * +-----------------------------------------> hdr_beg
8318 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
Willy Tarreau24581ba2010-08-31 22:39:35 +02008320 for (; prev < hdr_end; prev = next) {
8321 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 /* find att_beg */
8324 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008325 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327
Willy Tarreau24581ba2010-08-31 22:39:35 +02008328 /* find att_end : this is the first character after the last non
8329 * space before the equal. It may be equal to hdr_end.
8330 */
8331 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008332
Willy Tarreau24581ba2010-08-31 22:39:35 +02008333 while (equal < hdr_end) {
8334 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8335 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008336 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008337 continue;
8338 att_end = equal;
8339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008340
Willy Tarreau24581ba2010-08-31 22:39:35 +02008341 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8342 * is between <att_beg> and <equal>, both may be identical.
8343 */
8344
8345 /* look for end of cookie if there is an equal sign */
8346 if (equal < hdr_end && *equal == '=') {
8347 /* look for the beginning of the value */
8348 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008349 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008350 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008351
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 /* find the end of the value, respecting quotes */
8353 next = find_cookie_value_end(val_beg, hdr_end);
8354
8355 /* make val_end point to the first white space or delimitor after the value */
8356 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008357 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008358 val_end--;
8359 } else {
8360 /* <equal> points to next comma, semi-colon or EOL */
8361 val_beg = val_end = next = equal;
8362 }
8363
8364 if (next < hdr_end) {
8365 /* Set-Cookie2 supports multiple cookies, and <next> points to
8366 * a colon or semi-colon before the end. So skip all attr-value
8367 * pairs and look for the next comma. For Set-Cookie, since
8368 * commas are permitted in values, skip to the end.
8369 */
8370 if (is_cookie2)
8371 next = find_hdr_value_end(next, hdr_end);
8372 else
8373 next = hdr_end;
8374 }
8375
8376 /* Now everything is as on the diagram above */
8377
8378 /* Ignore cookies with no equal sign */
8379 if (equal == val_end)
8380 continue;
8381
8382 /* If there are spaces around the equal sign, we need to
8383 * strip them otherwise we'll get trouble for cookie captures,
8384 * or even for rewrites. Since this happens extremely rarely,
8385 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008386 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8388 int stripped_before = 0;
8389 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008392 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 equal += stripped_before;
8394 val_beg += stripped_before;
8395 }
8396
8397 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008398 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008399 val_beg += stripped_after;
8400 stripped_before += stripped_after;
8401 }
8402
8403 val_end += stripped_before;
8404 next += stripped_before;
8405 hdr_end += stripped_before;
8406 hdr_next += stripped_before;
8407 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008408 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008409 }
8410
8411 /* First, let's see if we want to capture this cookie. We check
8412 * that we don't already have a server side cookie, because we
8413 * can only capture one. Also as an optimisation, we ignore
8414 * cookies shorter than the declared name.
8415 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008416 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008417 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008418 (val_end - att_beg >= sess->fe->capture_namelen) &&
8419 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008420 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008421 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422 Alert("HTTP logging : out of memory.\n");
8423 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008424 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008425 if (log_len > sess->fe->capture_len)
8426 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008427 memcpy(txn->srv_cookie, att_beg, log_len);
8428 txn->srv_cookie[log_len] = 0;
8429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 }
8431
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008432 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008433 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008434 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8436 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008437 /* assume passive cookie by default */
8438 txn->flags &= ~TX_SCK_MASK;
8439 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440
8441 /* If the cookie is in insert mode on a known server, we'll delete
8442 * this occurrence because we'll insert another one later.
8443 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008444 * a direct access.
8445 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008446 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008447 /* The "preserve" flag was set, we don't want to touch the
8448 * server's cookie.
8449 */
8450 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008452 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 /* this cookie must be deleted */
8454 if (*prev == ':' && next == hdr_end) {
8455 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008457 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8458 txn->hdr_idx.used--;
8459 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008460 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008461 hdr_next += delta;
8462 http_msg_move_end(&txn->rsp, delta);
8463 /* note: while both invalid now, <next> and <hdr_end>
8464 * are still equal, so the for() will stop as expected.
8465 */
8466 } else {
8467 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008468 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 next = prev;
8470 hdr_end += delta;
8471 hdr_next += delta;
8472 cur_hdr->len += delta;
8473 http_msg_move_end(&txn->rsp, delta);
8474 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008475 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008476 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008477 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008479 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 * with this server since we know it.
8482 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008483 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008484 next += delta;
8485 hdr_end += delta;
8486 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008487 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008488 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489
Willy Tarreauf1348312010-10-07 15:54:11 +02008490 txn->flags &= ~TX_SCK_MASK;
8491 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008494 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008495 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008497 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008498 next += delta;
8499 hdr_end += delta;
8500 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008502 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503
Willy Tarreau827aee92011-03-10 16:55:02 +01008504 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008505 txn->flags &= ~TX_SCK_MASK;
8506 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 }
8508 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008509 /* that's done for this cookie, check the next one on the same
8510 * line when next != hdr_end (only if is_cookie2).
8511 */
8512 }
8513 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008515 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516}
8517
8518
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008520 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008522void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008523{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008524 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008525 char *p1, *p2;
8526
8527 char *cur_ptr, *cur_end, *cur_next;
8528 int cur_idx;
8529
Willy Tarreau5df51872007-11-25 16:20:08 +01008530 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531 return;
8532
8533 /* Iterate through the headers.
8534 * we start with the start line.
8535 */
8536 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008537 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008538
8539 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8540 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008541 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542
8543 cur_hdr = &txn->hdr_idx.v[cur_idx];
8544 cur_ptr = cur_next;
8545 cur_end = cur_ptr + cur_hdr->len;
8546 cur_next = cur_end + cur_hdr->cr + 1;
8547
8548 /* We have one full header between cur_ptr and cur_end, and the
8549 * next header starts at cur_next. We're only interested in
8550 * "Cookie:" headers.
8551 */
8552
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008553 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8554 if (val) {
8555 if ((cur_end - (cur_ptr + val) >= 8) &&
8556 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8557 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8558 return;
8559 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008560 }
8561
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008562 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8563 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 continue;
8565
8566 /* OK, right now we know we have a cache-control header at cur_ptr */
8567
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008568 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008569
8570 if (p1 >= cur_end) /* no more info */
8571 continue;
8572
8573 /* p1 is at the beginning of the value */
8574 p2 = p1;
8575
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008576 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008577 p2++;
8578
8579 /* we have a complete value between p1 and p2 */
8580 if (p2 < cur_end && *p2 == '=') {
8581 /* we have something of the form no-cache="set-cookie" */
8582 if ((cur_end - p1 >= 21) &&
8583 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8584 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008585 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008586 continue;
8587 }
8588
8589 /* OK, so we know that either p2 points to the end of string or to a comma */
8590 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008591 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008592 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8593 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8594 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008595 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008596 return;
8597 }
8598
8599 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008600 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008601 continue;
8602 }
8603 }
8604}
8605
Willy Tarreau58f10d72006-12-04 02:26:12 +01008606
Willy Tarreaub2513902006-12-17 14:52:38 +01008607/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008608 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008609 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008610 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008611 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008612 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008613 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008614 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008615 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008616int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008617{
8618 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008619 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008620 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008621
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008622 if (!uri_auth)
8623 return 0;
8624
Cyril Bonté70be45d2010-10-12 00:14:35 +02008625 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008626 return 0;
8627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008628 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008629 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008630 return 0;
8631
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008632 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008633 return 0;
8634
Willy Tarreaub2513902006-12-17 14:52:38 +01008635 return 1;
8636}
8637
Willy Tarreau4076a152009-04-02 15:18:36 +02008638/*
8639 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 * By default it tries to report the error position as msg->err_pos. However if
8641 * this one is not set, it will then report msg->next, which is the last known
8642 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008643 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008644 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008645void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008646 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008647 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008648{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008649 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008650 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008651 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008652
Willy Tarreauf3764b72016-03-31 13:45:10 +02008653 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008654 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008655 len1 = MIN(len1, es->len);
8656 len2 = es->len - len1; /* remaining data if buffer wraps */
8657
Willy Tarreauf3764b72016-03-31 13:45:10 +02008658 if (!es->buf)
8659 es->buf = malloc(global.tune.bufsize);
8660
8661 if (es->buf) {
8662 memcpy(es->buf, chn->buf->p, len1);
8663 if (len2)
8664 memcpy(es->buf + len1, chn->buf->data, len2);
8665 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008666
Willy Tarreau4076a152009-04-02 15:18:36 +02008667 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008668 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008669 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008670 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008671
Willy Tarreau4076a152009-04-02 15:18:36 +02008672 es->when = date; // user-visible date
8673 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008674 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008675 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008676 if (objt_conn(sess->origin))
8677 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008678 else
8679 memset(&es->src, 0, sizeof(es->src));
8680
Willy Tarreau078272e2010-12-12 12:46:33 +01008681 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008682 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008683 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008684 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008685 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008686 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008687 es->b_out = chn->buf->o;
8688 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008689 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008690 es->m_clen = msg->chunk_len;
8691 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008692}
Willy Tarreaub2513902006-12-17 14:52:38 +01008693
Willy Tarreau294c4732011-12-16 21:35:50 +01008694/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8695 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8696 * performed over the whole headers. Otherwise it must contain a valid header
8697 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8698 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8699 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8700 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008701 * -1. The value fetch stops at commas, so this function is suited for use with
8702 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008703 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008704 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008705unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 struct hdr_idx *idx, int occ,
8707 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008708{
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 struct hdr_ctx local_ctx;
8710 char *ptr_hist[MAX_HDR_HISTORY];
8711 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008712 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008713 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008714
Willy Tarreau294c4732011-12-16 21:35:50 +01008715 if (!ctx) {
8716 local_ctx.idx = 0;
8717 ctx = &local_ctx;
8718 }
8719
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008721 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008722 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 occ--;
8724 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 *vptr = ctx->line + ctx->val;
8726 *vlen = ctx->vlen;
8727 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008728 }
8729 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008730 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 }
8732
8733 /* negative occurrence, we scan all the list then walk back */
8734 if (-occ > MAX_HDR_HISTORY)
8735 return 0;
8736
Willy Tarreau294c4732011-12-16 21:35:50 +01008737 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008738 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008739 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8740 len_hist[hist_ptr] = ctx->vlen;
8741 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008742 hist_ptr = 0;
8743 found++;
8744 }
8745 if (-occ > found)
8746 return 0;
8747 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008748 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8749 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8750 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008751 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008752 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008753 if (hist_ptr >= MAX_HDR_HISTORY)
8754 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008755 *vptr = ptr_hist[hist_ptr];
8756 *vlen = len_hist[hist_ptr];
8757 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008758}
8759
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008760/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8761 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8762 * performed over the whole headers. Otherwise it must contain a valid header
8763 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8764 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8765 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8766 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8767 * -1. This function differs from http_get_hdr() in that it only returns full
8768 * line header values and does not stop at commas.
8769 * The return value is 0 if nothing was found, or non-zero otherwise.
8770 */
8771unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8772 struct hdr_idx *idx, int occ,
8773 struct hdr_ctx *ctx, char **vptr, int *vlen)
8774{
8775 struct hdr_ctx local_ctx;
8776 char *ptr_hist[MAX_HDR_HISTORY];
8777 int len_hist[MAX_HDR_HISTORY];
8778 unsigned int hist_ptr;
8779 int found;
8780
8781 if (!ctx) {
8782 local_ctx.idx = 0;
8783 ctx = &local_ctx;
8784 }
8785
8786 if (occ >= 0) {
8787 /* search from the beginning */
8788 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8789 occ--;
8790 if (occ <= 0) {
8791 *vptr = ctx->line + ctx->val;
8792 *vlen = ctx->vlen;
8793 return 1;
8794 }
8795 }
8796 return 0;
8797 }
8798
8799 /* negative occurrence, we scan all the list then walk back */
8800 if (-occ > MAX_HDR_HISTORY)
8801 return 0;
8802
8803 found = hist_ptr = 0;
8804 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8805 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8806 len_hist[hist_ptr] = ctx->vlen;
8807 if (++hist_ptr >= MAX_HDR_HISTORY)
8808 hist_ptr = 0;
8809 found++;
8810 }
8811 if (-occ > found)
8812 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008813
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008814 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008815 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8816 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8817 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008818 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008819 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008820 if (hist_ptr >= MAX_HDR_HISTORY)
8821 hist_ptr -= MAX_HDR_HISTORY;
8822 *vptr = ptr_hist[hist_ptr];
8823 *vlen = len_hist[hist_ptr];
8824 return 1;
8825}
8826
Willy Tarreaubaaee002006-06-26 02:48:02 +02008827/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008828 * Print a debug line with a header. Always stop at the first CR or LF char,
8829 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8830 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008831 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008832void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008833{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008834 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008835 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008836
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008837 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008838 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008839 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008840 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008841
8842 for (max = 0; start + max < end; max++)
8843 if (start[max] == '\r' || start[max] == '\n')
8844 break;
8845
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008846 UBOUND(max, trash.size - trash.len - 3);
8847 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8848 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008849 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008850}
8851
Willy Tarreaueee5b512015-04-03 23:46:31 +02008852
8853/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8854 * The hdr_idx is allocated as well. In case of allocation failure, everything
8855 * allocated is freed and NULL is returned. Otherwise the new transaction is
8856 * assigned to the stream and returned.
8857 */
8858struct http_txn *http_alloc_txn(struct stream *s)
8859{
8860 struct http_txn *txn = s->txn;
8861
8862 if (txn)
8863 return txn;
8864
8865 txn = pool_alloc2(pool2_http_txn);
8866 if (!txn)
8867 return txn;
8868
8869 txn->hdr_idx.size = global.tune.max_http_hdr;
8870 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8871 if (!txn->hdr_idx.v) {
8872 pool_free2(pool2_http_txn, txn);
8873 return NULL;
8874 }
8875
8876 s->txn = txn;
8877 return txn;
8878}
8879
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008880void http_txn_reset_req(struct http_txn *txn)
8881{
8882 txn->req.flags = 0;
8883 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8884 txn->req.next = 0;
8885 txn->req.chunk_len = 0LL;
8886 txn->req.body_len = 0LL;
8887 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8888}
8889
8890void http_txn_reset_res(struct http_txn *txn)
8891{
8892 txn->rsp.flags = 0;
8893 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8894 txn->rsp.next = 0;
8895 txn->rsp.chunk_len = 0LL;
8896 txn->rsp.body_len = 0LL;
8897 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8898}
8899
Willy Tarreau0937bc42009-12-22 15:03:09 +01008900/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008901 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008902 * the required fields are properly allocated and that we only need to (re)init
8903 * them. This should be used before processing any new request.
8904 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008905void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008907 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008908 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909
8910 txn->flags = 0;
8911 txn->status = -1;
8912
Willy Tarreauf64d1412010-10-07 20:06:11 +02008913 txn->cookie_first_date = 0;
8914 txn->cookie_last_date = 0;
8915
Willy Tarreaueee5b512015-04-03 23:46:31 +02008916 txn->srv_cookie = NULL;
8917 txn->cli_cookie = NULL;
8918 txn->uri = NULL;
8919
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008920 http_txn_reset_req(txn);
8921 http_txn_reset_res(txn);
8922
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008923 txn->req.chn = &s->req;
8924 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008925
8926 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008927
8928 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8929 if (fe->options2 & PR_O2_REQBUG_OK)
8930 txn->req.err_pos = -1; /* let buggy requests pass */
8931
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932 if (txn->hdr_idx.v)
8933 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008934
8935 vars_init(&s->vars_txn, SCOPE_TXN);
8936 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008937}
8938
8939/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008940void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008941{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008942 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008943 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008944
8945 /* these ones will have been dynamically allocated */
8946 pool_free2(pool2_requri, txn->uri);
8947 pool_free2(pool2_capture, txn->cli_cookie);
8948 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008949 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008950
William Lallemanda73203e2012-03-12 12:48:57 +01008951 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008952 txn->uri = NULL;
8953 txn->srv_cookie = NULL;
8954 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008955
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008956 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008957 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008958 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008959 pool_free2(h->pool, s->req_cap[h->index]);
8960 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008961 }
8962
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008963 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008964 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008965 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008966 pool_free2(h->pool, s->res_cap[h->index]);
8967 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008968 }
8969
Willy Tarreau6204cd92016-03-10 16:33:04 +01008970 vars_prune(&s->vars_txn, s->sess, s);
8971 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008972}
8973
8974/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008975void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008976{
8977 http_end_txn(s);
8978 http_init_txn(s);
8979
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008980 /* reinitialise the current rule list pointer to NULL. We are sure that
8981 * any rulelist match the NULL pointer.
8982 */
8983 s->current_rule_list = NULL;
8984
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008985 s->be = strm_fe(s);
8986 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008987 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008988 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008989 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008990 /* re-init store persistence */
8991 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008992 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008993
Willy Tarreau0937bc42009-12-22 15:03:09 +01008994 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008995
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008996 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008997
Willy Tarreau739cfba2010-01-25 23:11:14 +01008998 /* We must trim any excess data from the response buffer, because we
8999 * may have blocked an invalid response from a server that we don't
9000 * want to accidentely forward once we disable the analysers, nor do
9001 * we want those data to come along with next response. A typical
9002 * example of such data would be from a buggy server responding to
9003 * a HEAD with some data, or sending more than the advertised
9004 * content-length.
9005 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009006 if (unlikely(s->res.buf->i))
9007 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009008
Christopher Fauletc0c672a2017-03-28 11:51:33 +02009009 /* Now we can realign the response buffer */
9010 buffer_realign(s->res.buf);
9011
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009012 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009013 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009014
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009015 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009016 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009017
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009018 s->req.rex = TICK_ETERNITY;
9019 s->req.wex = TICK_ETERNITY;
9020 s->req.analyse_exp = TICK_ETERNITY;
9021 s->res.rex = TICK_ETERNITY;
9022 s->res.wex = TICK_ETERNITY;
9023 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01009024 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009025}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009026
Sasha Pachev218f0642014-06-16 12:05:59 -06009027void free_http_res_rules(struct list *r)
9028{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009029 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009030
9031 list_for_each_entry_safe(pr, tr, r, list) {
9032 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009033 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009034 free(pr);
9035 }
9036}
9037
9038void free_http_req_rules(struct list *r)
9039{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009040 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009041
9042 list_for_each_entry_safe(pr, tr, r, list) {
9043 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009044 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009045 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009046
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009047 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009048 free(pr);
9049 }
9050}
9051
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009052/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009053struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009054{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009055 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009056 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009057 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009058 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009059
Vincent Bernat02779b62016-04-03 13:48:43 +02009060 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009061 if (!rule) {
9062 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009063 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009064 }
9065
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009066 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009067 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009068 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009069 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009070 int code;
9071 int hc;
9072
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009073 if (!strcmp(args[0], "tarpit")) {
9074 rule->action = ACT_HTTP_REQ_TARPIT;
9075 rule->deny_status = HTTP_ERR_500;
9076 }
9077 else {
9078 rule->action = ACT_ACTION_DENY;
9079 rule->deny_status = HTTP_ERR_403;
9080 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009081 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009082 if (strcmp(args[cur_arg], "deny_status") == 0) {
9083 cur_arg++;
9084 if (!args[cur_arg]) {
9085 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9086 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9087 goto out_err;
9088 }
9089
9090 code = atol(args[cur_arg]);
9091 cur_arg++;
9092 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9093 if (http_err_codes[hc] == code) {
9094 rule->deny_status = hc;
9095 break;
9096 }
9097 }
9098
9099 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02009100 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
9101 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04009102 }
9103 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009104 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009105 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009106 cur_arg = 1;
9107
9108 while(*args[cur_arg]) {
9109 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009110 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009111 cur_arg+=2;
9112 continue;
9113 } else
9114 break;
9115 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009116 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009117 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009118 cur_arg = 1;
9119
9120 if (!*args[cur_arg] ||
9121 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9122 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9123 file, linenum, args[0]);
9124 goto out_err;
9125 }
9126 rule->arg.nice = atoi(args[cur_arg]);
9127 if (rule->arg.nice < -1024)
9128 rule->arg.nice = -1024;
9129 else if (rule->arg.nice > 1024)
9130 rule->arg.nice = 1024;
9131 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009132 } else if (!strcmp(args[0], "set-tos")) {
9133#ifdef IP_TOS
9134 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009135 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009136 cur_arg = 1;
9137
9138 if (!*args[cur_arg] ||
9139 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9140 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9141 file, linenum, args[0]);
9142 goto out_err;
9143 }
9144
9145 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9146 if (err && *err != '\0') {
9147 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9148 file, linenum, err, args[0]);
9149 goto out_err;
9150 }
9151 cur_arg++;
9152#else
9153 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9154 goto out_err;
9155#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009156 } else if (!strcmp(args[0], "set-mark")) {
9157#ifdef SO_MARK
9158 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009159 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009160 cur_arg = 1;
9161
9162 if (!*args[cur_arg] ||
9163 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9164 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9165 file, linenum, args[0]);
9166 goto out_err;
9167 }
9168
9169 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9170 if (err && *err != '\0') {
9171 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9172 file, linenum, err, args[0]);
9173 goto out_err;
9174 }
9175 cur_arg++;
9176 global.last_checks |= LSTCHK_NETADM;
9177#else
9178 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9179 goto out_err;
9180#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009181 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009182 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009183 cur_arg = 1;
9184
9185 if (!*args[cur_arg] ||
9186 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9187 bad_log_level:
9188 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9189 file, linenum, args[0]);
9190 goto out_err;
9191 }
9192 if (strcmp(args[cur_arg], "silent") == 0)
9193 rule->arg.loglevel = -1;
9194 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9195 goto bad_log_level;
9196 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009197 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009198 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009199 cur_arg = 1;
9200
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009201 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9202 (*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 +01009203 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9204 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009205 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009206 }
9207
9208 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9209 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9210 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009211
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009212 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009213 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009214 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 +01009215 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9216 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9217 file, linenum, args[0], error);
9218 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009219 goto out_err;
9220 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009221 free(proxy->conf.lfs_file);
9222 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9223 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009224 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009225 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009226 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009227 cur_arg = 1;
9228
9229 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009230 (*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 -06009231 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9232 file, linenum, args[0]);
9233 goto out_err;
9234 }
9235
9236 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9237 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9238 LIST_INIT(&rule->arg.hdr_add.fmt);
9239
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009240 error = NULL;
9241 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9242 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9243 args[cur_arg + 1], error);
9244 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009245 goto out_err;
9246 }
9247
9248 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009249 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009250 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 +01009251 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9252 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9253 file, linenum, args[0], error);
9254 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009255 goto out_err;
9256 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009257
9258 free(proxy->conf.lfs_file);
9259 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9260 proxy->conf.lfs_line = proxy->conf.args.line;
9261 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009262 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009263 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009264 cur_arg = 1;
9265
9266 if (!*args[cur_arg] ||
9267 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9268 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9269 file, linenum, args[0]);
9270 goto out_err;
9271 }
9272
9273 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9274 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9275
9276 proxy->conf.args.ctx = ARGC_HRQ;
9277 free(proxy->conf.lfs_file);
9278 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9279 proxy->conf.lfs_line = proxy->conf.args.line;
9280 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009281 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9282 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009283 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009284 struct sample_expr *expr;
9285 unsigned int where;
9286 char *err = NULL;
9287
9288 cur_arg = 1;
9289 proxy->conf.args.ctx = ARGC_TRK;
9290
9291 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9292 if (!expr) {
9293 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9294 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9295 free(err);
9296 goto out_err;
9297 }
9298
9299 where = 0;
9300 if (proxy->cap & PR_CAP_FE)
9301 where |= SMP_VAL_FE_HRQ_HDR;
9302 if (proxy->cap & PR_CAP_BE)
9303 where |= SMP_VAL_BE_HRQ_HDR;
9304
9305 if (!(expr->fetch->val & where)) {
9306 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9307 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9308 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9309 args[cur_arg-1], sample_src_names(expr->fetch->use));
9310 free(expr);
9311 goto out_err;
9312 }
9313
9314 if (strcmp(args[cur_arg], "table") == 0) {
9315 cur_arg++;
9316 if (!args[cur_arg]) {
9317 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9318 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9319 free(expr);
9320 goto out_err;
9321 }
9322 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009323 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009324 cur_arg++;
9325 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009326 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009327 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009328 } else if (strcmp(args[0], "redirect") == 0) {
9329 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009330 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009331
Willy Tarreaube4653b2015-05-28 15:26:58 +02009332 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009333 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9334 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9335 goto out_err;
9336 }
9337
9338 /* this redirect rule might already contain a parsed condition which
9339 * we'll pass to the http-request rule.
9340 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009341 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009342 rule->arg.redir = redir;
9343 rule->cond = redir->cond;
9344 redir->cond = NULL;
9345 cur_arg = 2;
9346 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009347 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9348 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009349 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009350 /*
9351 * '+ 8' for 'add-acl('
9352 * '- 9' for 'add-acl(' + trailing ')'
9353 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009354 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009355
9356 cur_arg = 1;
9357
9358 if (!*args[cur_arg] ||
9359 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9360 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9361 file, linenum, args[0]);
9362 goto out_err;
9363 }
9364
9365 LIST_INIT(&rule->arg.map.key);
9366 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009367 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009368 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009369 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9370 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9371 file, linenum, args[0], error);
9372 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009373 goto out_err;
9374 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009375 free(proxy->conf.lfs_file);
9376 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9377 proxy->conf.lfs_line = proxy->conf.args.line;
9378 cur_arg += 1;
9379 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9380 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009381 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009382 /*
9383 * '+ 8' for 'del-acl('
9384 * '- 9' for 'del-acl(' + trailing ')'
9385 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009386 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387
9388 cur_arg = 1;
9389
9390 if (!*args[cur_arg] ||
9391 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9392 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9393 file, linenum, args[0]);
9394 goto out_err;
9395 }
9396
9397 LIST_INIT(&rule->arg.map.key);
9398 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009399 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009400 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009401 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9402 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9403 file, linenum, args[0], error);
9404 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009405 goto out_err;
9406 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009407 free(proxy->conf.lfs_file);
9408 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9409 proxy->conf.lfs_line = proxy->conf.args.line;
9410 cur_arg += 1;
9411 } else if (strncmp(args[0], "del-map", 7) == 0) {
9412 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009413 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009414 /*
9415 * '+ 8' for 'del-map('
9416 * '- 9' for 'del-map(' + trailing ')'
9417 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009418 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009419
9420 cur_arg = 1;
9421
9422 if (!*args[cur_arg] ||
9423 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9424 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9425 file, linenum, args[0]);
9426 goto out_err;
9427 }
9428
9429 LIST_INIT(&rule->arg.map.key);
9430 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009431 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009432 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009433 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9434 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9435 file, linenum, args[0], error);
9436 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009437 goto out_err;
9438 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009439 free(proxy->conf.lfs_file);
9440 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9441 proxy->conf.lfs_line = proxy->conf.args.line;
9442 cur_arg += 1;
9443 } else if (strncmp(args[0], "set-map", 7) == 0) {
9444 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009445 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009446 /*
9447 * '+ 8' for 'set-map('
9448 * '- 9' for 'set-map(' + trailing ')'
9449 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009450 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009451
9452 cur_arg = 1;
9453
9454 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9455 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9456 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9457 file, linenum, args[0]);
9458 goto out_err;
9459 }
9460
9461 LIST_INIT(&rule->arg.map.key);
9462 LIST_INIT(&rule->arg.map.value);
9463 proxy->conf.args.ctx = ARGC_HRQ;
9464
9465 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009466 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009467 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009468 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9469 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9470 file, linenum, args[0], error);
9471 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009472 goto out_err;
9473 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009474
9475 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009476 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009477 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 +01009478 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9479 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9480 file, linenum, args[0], error);
9481 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009482 goto out_err;
9483 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009484 free(proxy->conf.lfs_file);
9485 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9486 proxy->conf.lfs_line = proxy->conf.args.line;
9487
9488 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009489 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9490 char *errmsg = NULL;
9491 cur_arg = 1;
9492 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009493 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009494 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009495 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009496 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9497 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9498 free(errmsg);
9499 goto out_err;
9500 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009501 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009502 action_build_list(&http_req_keywords.list, &trash);
9503 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9504 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009505 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009506 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009507 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009508 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009509 }
9510
9511 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9512 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009513 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009514
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009515 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9516 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9517 file, linenum, args[0], errmsg);
9518 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009519 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009520 }
9521 rule->cond = cond;
9522 }
9523 else if (*args[cur_arg]) {
9524 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9525 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9526 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009527 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009528 }
9529
9530 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009531 out_err:
9532 free(rule);
9533 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009534}
9535
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009536/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009537struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009538{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009539 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009540 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009541 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009542 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009543
9544 rule = calloc(1, sizeof(*rule));
9545 if (!rule) {
9546 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9547 goto out_err;
9548 }
9549
9550 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009551 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009552 cur_arg = 1;
9553 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009554 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009555 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009556 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009557 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009558 cur_arg = 1;
9559
9560 if (!*args[cur_arg] ||
9561 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9562 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9563 file, linenum, args[0]);
9564 goto out_err;
9565 }
9566 rule->arg.nice = atoi(args[cur_arg]);
9567 if (rule->arg.nice < -1024)
9568 rule->arg.nice = -1024;
9569 else if (rule->arg.nice > 1024)
9570 rule->arg.nice = 1024;
9571 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009572 } else if (!strcmp(args[0], "set-tos")) {
9573#ifdef IP_TOS
9574 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009575 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009576 cur_arg = 1;
9577
9578 if (!*args[cur_arg] ||
9579 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9580 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9581 file, linenum, args[0]);
9582 goto out_err;
9583 }
9584
9585 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9586 if (err && *err != '\0') {
9587 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9588 file, linenum, err, args[0]);
9589 goto out_err;
9590 }
9591 cur_arg++;
9592#else
9593 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9594 goto out_err;
9595#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009596 } else if (!strcmp(args[0], "set-mark")) {
9597#ifdef SO_MARK
9598 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009599 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009600 cur_arg = 1;
9601
9602 if (!*args[cur_arg] ||
9603 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9604 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9605 file, linenum, args[0]);
9606 goto out_err;
9607 }
9608
9609 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9610 if (err && *err != '\0') {
9611 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9612 file, linenum, err, args[0]);
9613 goto out_err;
9614 }
9615 cur_arg++;
9616 global.last_checks |= LSTCHK_NETADM;
9617#else
9618 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9619 goto out_err;
9620#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009621 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009622 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009623 cur_arg = 1;
9624
9625 if (!*args[cur_arg] ||
9626 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9627 bad_log_level:
9628 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9629 file, linenum, args[0]);
9630 goto out_err;
9631 }
9632 if (strcmp(args[cur_arg], "silent") == 0)
9633 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009634 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009635 goto bad_log_level;
9636 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009637 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009638 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009639 cur_arg = 1;
9640
9641 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9642 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9643 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9644 file, linenum, args[0]);
9645 goto out_err;
9646 }
9647
9648 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9649 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9650 LIST_INIT(&rule->arg.hdr_add.fmt);
9651
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009652 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009653 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009654 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 +01009655 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9656 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9657 file, linenum, args[0], error);
9658 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009659 goto out_err;
9660 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009661 free(proxy->conf.lfs_file);
9662 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9663 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009664 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009665 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009666 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009667 cur_arg = 1;
9668
9669 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009670 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9671 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009672 file, linenum, args[0]);
9673 goto out_err;
9674 }
9675
9676 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9677 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9678 LIST_INIT(&rule->arg.hdr_add.fmt);
9679
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009680 error = NULL;
9681 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9682 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9683 args[cur_arg + 1], error);
9684 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009685 goto out_err;
9686 }
9687
9688 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009689 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009690 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 +01009691 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9692 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9693 file, linenum, args[0], error);
9694 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009695 goto out_err;
9696 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009697
9698 free(proxy->conf.lfs_file);
9699 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9700 proxy->conf.lfs_line = proxy->conf.args.line;
9701 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009702 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009703 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009704 cur_arg = 1;
9705
9706 if (!*args[cur_arg] ||
9707 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9708 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9709 file, linenum, args[0]);
9710 goto out_err;
9711 }
9712
9713 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9714 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9715
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009716 proxy->conf.args.ctx = ARGC_HRS;
9717 free(proxy->conf.lfs_file);
9718 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9719 proxy->conf.lfs_line = proxy->conf.args.line;
9720 cur_arg += 1;
9721 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9722 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009723 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009724 /*
9725 * '+ 8' for 'add-acl('
9726 * '- 9' for 'add-acl(' + trailing ')'
9727 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009728 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009729
9730 cur_arg = 1;
9731
9732 if (!*args[cur_arg] ||
9733 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9734 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9735 file, linenum, args[0]);
9736 goto out_err;
9737 }
9738
9739 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009740 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009741 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009742 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009743 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9744 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9745 file, linenum, args[0], error);
9746 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009747 goto out_err;
9748 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009749 free(proxy->conf.lfs_file);
9750 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9751 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009752
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009753 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009754 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9755 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009756 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009757 /*
9758 * '+ 8' for 'del-acl('
9759 * '- 9' for 'del-acl(' + trailing ')'
9760 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009761 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009762
9763 cur_arg = 1;
9764
9765 if (!*args[cur_arg] ||
9766 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9767 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9768 file, linenum, args[0]);
9769 goto out_err;
9770 }
9771
9772 LIST_INIT(&rule->arg.map.key);
9773 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009774 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009775 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009776 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9777 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9778 file, linenum, args[0], error);
9779 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009780 goto out_err;
9781 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009782 free(proxy->conf.lfs_file);
9783 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9784 proxy->conf.lfs_line = proxy->conf.args.line;
9785 cur_arg += 1;
9786 } else if (strncmp(args[0], "del-map", 7) == 0) {
9787 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009788 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009789 /*
9790 * '+ 8' for 'del-map('
9791 * '- 9' for 'del-map(' + trailing ')'
9792 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009793 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009794
9795 cur_arg = 1;
9796
9797 if (!*args[cur_arg] ||
9798 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9799 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9800 file, linenum, args[0]);
9801 goto out_err;
9802 }
9803
9804 LIST_INIT(&rule->arg.map.key);
9805 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009806 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009807 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009808 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9809 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9810 file, linenum, args[0], error);
9811 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009812 goto out_err;
9813 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009814 free(proxy->conf.lfs_file);
9815 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9816 proxy->conf.lfs_line = proxy->conf.args.line;
9817 cur_arg += 1;
9818 } else if (strncmp(args[0], "set-map", 7) == 0) {
9819 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009820 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009821 /*
9822 * '+ 8' for 'set-map('
9823 * '- 9' for 'set-map(' + trailing ')'
9824 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009825 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009826
9827 cur_arg = 1;
9828
9829 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9830 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9831 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9832 file, linenum, args[0]);
9833 goto out_err;
9834 }
9835
9836 LIST_INIT(&rule->arg.map.key);
9837 LIST_INIT(&rule->arg.map.value);
9838
9839 proxy->conf.args.ctx = ARGC_HRS;
9840
9841 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009842 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009843 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009844 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9845 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9846 file, linenum, args[0], error);
9847 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009848 goto out_err;
9849 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009850
9851 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009852 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009853 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 +01009854 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9855 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9856 file, linenum, args[0], error);
9857 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009858 goto out_err;
9859 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009860
9861 free(proxy->conf.lfs_file);
9862 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9863 proxy->conf.lfs_line = proxy->conf.args.line;
9864
9865 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009866 } else if (strcmp(args[0], "redirect") == 0) {
9867 struct redirect_rule *redir;
9868 char *errmsg = NULL;
9869
9870 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9871 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9872 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9873 goto out_err;
9874 }
9875
9876 /* this redirect rule might already contain a parsed condition which
9877 * we'll pass to the http-request rule.
9878 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009879 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009880 rule->arg.redir = redir;
9881 rule->cond = redir->cond;
9882 redir->cond = NULL;
9883 cur_arg = 2;
9884 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009885 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9886 args[0][9] == '\0' && args[0][8] >= '0' &&
9887 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9888 struct sample_expr *expr;
9889 unsigned int where;
9890 char *err = NULL;
9891
9892 cur_arg = 1;
9893 proxy->conf.args.ctx = ARGC_TRK;
9894
9895 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9896 if (!expr) {
9897 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9898 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9899 free(err);
9900 goto out_err;
9901 }
9902
9903 where = 0;
9904 if (proxy->cap & PR_CAP_FE)
9905 where |= SMP_VAL_FE_HRS_HDR;
9906 if (proxy->cap & PR_CAP_BE)
9907 where |= SMP_VAL_BE_HRS_HDR;
9908
9909 if (!(expr->fetch->val & where)) {
9910 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9911 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9912 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9913 args[cur_arg-1], sample_src_names(expr->fetch->use));
9914 free(expr);
9915 goto out_err;
9916 }
9917
9918 if (strcmp(args[cur_arg], "table") == 0) {
9919 cur_arg++;
9920 if (!args[cur_arg]) {
9921 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9922 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9923 free(expr);
9924 goto out_err;
9925 }
9926 /* we copy the table name for now, it will be resolved later */
9927 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9928 cur_arg++;
9929 }
9930 rule->arg.trk_ctr.expr = expr;
9931 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009932 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9933 char *errmsg = NULL;
9934 cur_arg = 1;
9935 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009936 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009937 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009938 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009939 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9940 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9941 free(errmsg);
9942 goto out_err;
9943 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009944 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009945 action_build_list(&http_res_keywords.list, &trash);
9946 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9947 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009948 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009949 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009950 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009951 goto out_err;
9952 }
9953
9954 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9955 struct acl_cond *cond;
9956 char *errmsg = NULL;
9957
9958 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9959 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9960 file, linenum, args[0], errmsg);
9961 free(errmsg);
9962 goto out_err;
9963 }
9964 rule->cond = cond;
9965 }
9966 else if (*args[cur_arg]) {
9967 Alert("parsing [%s:%d]: 'http-response %s' expects"
9968 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9969 file, linenum, args[0], args[cur_arg]);
9970 goto out_err;
9971 }
9972
9973 return rule;
9974 out_err:
9975 free(rule);
9976 return NULL;
9977}
9978
Willy Tarreau4baae242012-12-27 12:00:31 +01009979/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009980 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009981 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9982 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009983 */
9984struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009985 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009986{
9987 struct redirect_rule *rule;
9988 int cur_arg;
9989 int type = REDIRECT_TYPE_NONE;
9990 int code = 302;
9991 const char *destination = NULL;
9992 const char *cookie = NULL;
9993 int cookie_set = 0;
9994 unsigned int flags = REDIRECT_FLAG_NONE;
9995 struct acl_cond *cond = NULL;
9996
9997 cur_arg = 0;
9998 while (*(args[cur_arg])) {
9999 if (strcmp(args[cur_arg], "location") == 0) {
10000 if (!*args[cur_arg + 1])
10001 goto missing_arg;
10002
10003 type = REDIRECT_TYPE_LOCATION;
10004 cur_arg++;
10005 destination = args[cur_arg];
10006 }
10007 else if (strcmp(args[cur_arg], "prefix") == 0) {
10008 if (!*args[cur_arg + 1])
10009 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010010 type = REDIRECT_TYPE_PREFIX;
10011 cur_arg++;
10012 destination = args[cur_arg];
10013 }
10014 else if (strcmp(args[cur_arg], "scheme") == 0) {
10015 if (!*args[cur_arg + 1])
10016 goto missing_arg;
10017
10018 type = REDIRECT_TYPE_SCHEME;
10019 cur_arg++;
10020 destination = args[cur_arg];
10021 }
10022 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10023 if (!*args[cur_arg + 1])
10024 goto missing_arg;
10025
10026 cur_arg++;
10027 cookie = args[cur_arg];
10028 cookie_set = 1;
10029 }
10030 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10031 if (!*args[cur_arg + 1])
10032 goto missing_arg;
10033
10034 cur_arg++;
10035 cookie = args[cur_arg];
10036 cookie_set = 0;
10037 }
10038 else if (strcmp(args[cur_arg], "code") == 0) {
10039 if (!*args[cur_arg + 1])
10040 goto missing_arg;
10041
10042 cur_arg++;
10043 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010044 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010045 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010046 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010047 args[cur_arg - 1], args[cur_arg]);
10048 return NULL;
10049 }
10050 }
10051 else if (!strcmp(args[cur_arg],"drop-query")) {
10052 flags |= REDIRECT_FLAG_DROP_QS;
10053 }
10054 else if (!strcmp(args[cur_arg],"append-slash")) {
10055 flags |= REDIRECT_FLAG_APPEND_SLASH;
10056 }
10057 else if (strcmp(args[cur_arg], "if") == 0 ||
10058 strcmp(args[cur_arg], "unless") == 0) {
10059 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10060 if (!cond) {
10061 memprintf(errmsg, "error in condition: %s", *errmsg);
10062 return NULL;
10063 }
10064 break;
10065 }
10066 else {
10067 memprintf(errmsg,
10068 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10069 args[cur_arg]);
10070 return NULL;
10071 }
10072 cur_arg++;
10073 }
10074
10075 if (type == REDIRECT_TYPE_NONE) {
10076 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10077 return NULL;
10078 }
10079
Willy Tarreaube4653b2015-05-28 15:26:58 +020010080 if (dir && type != REDIRECT_TYPE_LOCATION) {
10081 memprintf(errmsg, "response only supports redirect type 'location'");
10082 return NULL;
10083 }
10084
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010085 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010086 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010087 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010088
10089 if (!use_fmt) {
10090 /* old-style static redirect rule */
10091 rule->rdr_str = strdup(destination);
10092 rule->rdr_len = strlen(destination);
10093 }
10094 else {
10095 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010096
10097 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10098 * if prefix == "/", we don't want to add anything, otherwise it
10099 * makes it hard for the user to configure a self-redirection.
10100 */
Godbachd9722032014-12-18 15:44:58 +080010101 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010102 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010103 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10104 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 +010010105 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10106 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010107 return NULL;
10108 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010109 free(curproxy->conf.lfs_file);
10110 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10111 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010112 }
10113 }
10114
Willy Tarreau4baae242012-12-27 12:00:31 +010010115 if (cookie) {
10116 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10117 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10118 */
10119 rule->cookie_len = strlen(cookie);
10120 if (cookie_set) {
10121 rule->cookie_str = malloc(rule->cookie_len + 10);
10122 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10123 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10124 rule->cookie_len += 9;
10125 } else {
10126 rule->cookie_str = malloc(rule->cookie_len + 21);
10127 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10128 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10129 rule->cookie_len += 20;
10130 }
10131 }
10132 rule->type = type;
10133 rule->code = code;
10134 rule->flags = flags;
10135 LIST_INIT(&rule->list);
10136 return rule;
10137
10138 missing_arg:
10139 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10140 return NULL;
10141}
10142
Willy Tarreau8797c062007-05-07 00:55:35 +020010143/************************************************************************/
10144/* The code below is dedicated to ACL parsing and matching */
10145/************************************************************************/
10146
10147
Willy Tarreau14174bc2012-04-16 14:34:04 +020010148/* This function ensures that the prerequisites for an L7 fetch are ready,
10149 * which means that a request or response is ready. If some data is missing,
10150 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010151 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10152 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010153 *
10154 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010155 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10156 * decide whether or not an HTTP message is present ;
10157 * 0 if the requested data cannot be fetched or if it is certain that
10158 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159 * 1 if an HTTP message is ready
10160 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010161int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010162 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010163{
Willy Tarreau192252e2015-04-04 01:47:55 +020010164 struct http_txn *txn;
10165 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010166
Willy Tarreaube508f12016-03-10 11:47:01 +010010167 /* Note: it is possible that <s> is NULL when called before stream
10168 * initialization (eg: tcp-request connection), so this function is the
10169 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010170 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010171 if (!s)
10172 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010173
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010174 if (!s->txn) {
10175 if (unlikely(!http_alloc_txn(s)))
10176 return 0; /* not enough memory */
10177 http_init_txn(s);
10178 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010179 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010180 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010181
10182 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010183 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010184
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010185 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010186 /* If the buffer does not leave enough free space at the end,
10187 * we must first realign it.
10188 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010189 if (s->req.buf->p > s->req.buf->data &&
10190 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10191 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010192
Willy Tarreau14174bc2012-04-16 14:34:04 +020010193 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010194 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010195 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196
10197 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010198 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199 http_msg_analyzer(msg, &txn->hdr_idx);
10200
10201 /* Still no valid request ? */
10202 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010203 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010204 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010205 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010206 }
10207 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010208 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010209 return 0;
10210 }
10211
10212 /* OK we just got a valid HTTP request. We have some minor
10213 * preparation to perform so that further checks can rely
10214 * on HTTP tests.
10215 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010216
10217 /* If the request was parsed but was too large, we must absolutely
10218 * return an error so that it is not processed. At the moment this
10219 * cannot happen, but if the parsers are to change in the future,
10220 * we want this check to be maintained.
10221 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010222 if (unlikely(s->req.buf->i + s->req.buf->p >
10223 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010224 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010225 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010226 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010227 return 1;
10228 }
10229
Willy Tarreau9b28e032012-10-12 23:49:43 +020010230 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010231 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010232 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010233
Willy Tarreau506d0502013-07-06 13:29:24 +020010234 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10235 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010236 }
10237
Willy Tarreau506d0502013-07-06 13:29:24 +020010238 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010239 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010240 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010241
10242 /* otherwise everything's ready for the request */
10243 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010244 else {
10245 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010246 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10247 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010248 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010249 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010250 }
10251
10252 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010253 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010254 return 1;
10255}
Willy Tarreau8797c062007-05-07 00:55:35 +020010256
Willy Tarreau8797c062007-05-07 00:55:35 +020010257/* 1. Check on METHOD
10258 * We use the pre-parsed method if it is known, and store its number as an
10259 * integer. If it is unknown, we use the pointer and the length.
10260 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010261static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010262{
10263 int len, meth;
10264
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010265 len = strlen(text);
10266 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010267
10268 pattern->val.i = meth;
10269 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010270 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010271 pattern->len = len;
10272 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010273 else {
10274 pattern->ptr.str = NULL;
10275 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010276 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010277 return 1;
10278}
10279
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010280/* This function fetches the method of current HTTP request and stores
10281 * it in the global pattern struct as a chunk. There are two possibilities :
10282 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10283 * in <len> and <ptr> is NULL ;
10284 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10285 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010286 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010287 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010290{
10291 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010292 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010293
Willy Tarreau24e32d82012-04-23 23:55:44 +020010294 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010295
Willy Tarreaube508f12016-03-10 11:47:01 +010010296 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010297 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010298 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010299 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010300 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010301 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10302 /* ensure the indexes are not affected */
10303 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010304 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010305 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10306 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010307 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010308 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010309 return 1;
10310}
10311
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010312/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010313static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010314{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010315 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010316 struct pattern_list *lst;
10317 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010318
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010319 list_for_each_entry(lst, &expr->patterns, list) {
10320 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010321
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010322 /* well-known method */
10323 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010324 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010325 return pattern;
10326 else
10327 continue;
10328 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010329
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010330 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010331 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010332 continue;
10333
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010334 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10336 (!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 +010010337 return pattern;
10338 }
10339 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010340}
10341
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010342static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010343smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010344{
Willy Tarreaube508f12016-03-10 11:47:01 +010010345 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010346 char *ptr;
10347 int len;
10348
Willy Tarreauc0239e02012-04-16 14:42:55 +020010349 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010350
Willy Tarreaube508f12016-03-10 11:47:01 +010010351 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010352 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010353 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010354
10355 while ((len-- > 0) && (*ptr++ != '/'));
10356 if (len <= 0)
10357 return 0;
10358
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010359 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010360 smp->data.u.str.str = ptr;
10361 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010362
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010363 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010364 return 1;
10365}
10366
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010368smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010369{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010370 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010371 char *ptr;
10372 int len;
10373
Willy Tarreauc0239e02012-04-16 14:42:55 +020010374 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010375
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010376 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010377 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10378 return 0;
10379
Willy Tarreau8797c062007-05-07 00:55:35 +020010380 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010381 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010382
10383 while ((len-- > 0) && (*ptr++ != '/'));
10384 if (len <= 0)
10385 return 0;
10386
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010387 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010388 smp->data.u.str.str = ptr;
10389 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010390
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010391 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010392 return 1;
10393}
10394
10395/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010396static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010397smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010398{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010399 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010400 char *ptr;
10401 int len;
10402
Willy Tarreauc0239e02012-04-16 14:42:55 +020010403 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010404
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010405 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010406 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10407 return 0;
10408
Willy Tarreau8797c062007-05-07 00:55:35 +020010409 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010410 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010411
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010412 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010413 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010414 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010415 return 1;
10416}
10417
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010418static int
10419smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10420{
10421 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10422 return 0;
10423
10424 if (!smp->strm->unique_id) {
10425 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10426 return 0;
10427 smp->strm->unique_id[0] = '\0';
10428 }
10429 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10430 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10431
10432 smp->data.type = SMP_T_STR;
10433 smp->data.u.str.str = smp->strm->unique_id;
10434 smp->flags = SMP_F_CONST;
10435 return 1;
10436}
10437
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010438/* returns the longest available part of the body. This requires that the body
10439 * has been waited for using http-buffer-request.
10440 */
10441static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010442smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010443{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010444 struct http_msg *msg;
10445 unsigned long len;
10446 unsigned long block1;
10447 char *body;
10448 struct chunk *temp;
10449
10450 CHECK_HTTP_MESSAGE_FIRST();
10451
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010452 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010453 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010454 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010455 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010456
10457 len = http_body_bytes(msg);
10458 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10459
10460 block1 = len;
10461 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10462 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10463
10464 if (block1 == len) {
10465 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010466 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010467 smp->data.u.str.str = body;
10468 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010469 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10470 }
10471 else {
10472 /* buffer is wrapped, we need to defragment it */
10473 temp = get_trash_chunk();
10474 memcpy(temp->str, body, block1);
10475 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010476 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010477 smp->data.u.str.str = temp->str;
10478 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010479 smp->flags = SMP_F_VOL_TEST;
10480 }
10481 return 1;
10482}
10483
10484
10485/* returns the available length of the body. This requires that the body
10486 * has been waited for using http-buffer-request.
10487 */
10488static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010489smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010490{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010491 struct http_msg *msg;
10492
10493 CHECK_HTTP_MESSAGE_FIRST();
10494
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010495 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010496 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010497 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010498 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010499
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010500 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010501 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010502
10503 smp->flags = SMP_F_VOL_TEST;
10504 return 1;
10505}
10506
10507
10508/* returns the advertised length of the body, or the advertised size of the
10509 * chunks available in the buffer. This requires that the body has been waited
10510 * for using http-buffer-request.
10511 */
10512static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010513smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010514{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010515 struct http_msg *msg;
10516
10517 CHECK_HTTP_MESSAGE_FIRST();
10518
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010519 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010520 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010521 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010522 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010523
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010524 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010525 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010526
10527 smp->flags = SMP_F_VOL_TEST;
10528 return 1;
10529}
10530
10531
Willy Tarreau8797c062007-05-07 00:55:35 +020010532/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010534smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010535{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010536 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010537
Willy Tarreauc0239e02012-04-16 14:42:55 +020010538 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010539
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010540 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010541 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010542 smp->data.u.str.len = txn->req.sl.rq.u_l;
10543 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010544 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010545 return 1;
10546}
10547
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010548static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010549smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010550{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010551 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010552 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010553
Willy Tarreauc0239e02012-04-16 14:42:55 +020010554 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010555
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010556 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010557 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 +020010558 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010559 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010560
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010561 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010562 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010563 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564 return 1;
10565}
10566
10567static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010568smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010569{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010570 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010571 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010572
Willy Tarreauc0239e02012-04-16 14:42:55 +020010573 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010574
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010575 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010576 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 +020010577 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10578 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010579
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010580 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010581 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010582 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010583 return 1;
10584}
10585
Willy Tarreau185b5c42012-04-26 15:11:51 +020010586/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10587 * Accepts an optional argument of type string containing the header field name,
10588 * and an optional argument of type signed or unsigned integer to request an
10589 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010590 * headers are considered from the first one. It does not stop on commas and
10591 * returns full lines instead (useful for User-Agent or Date for example).
10592 */
10593static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010594smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010595{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010596 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010597 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010598 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010599 int occ = 0;
10600 const char *name_str = NULL;
10601 int name_len = 0;
10602
10603 if (!ctx) {
10604 /* first call */
10605 ctx = &static_hdr_ctx;
10606 ctx->idx = 0;
10607 smp->ctx.a[0] = ctx;
10608 }
10609
10610 if (args) {
10611 if (args[0].type != ARGT_STR)
10612 return 0;
10613 name_str = args[0].data.str.str;
10614 name_len = args[0].data.str.len;
10615
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010616 if (args[1].type == ARGT_SINT)
10617 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010618 }
10619
10620 CHECK_HTTP_MESSAGE_FIRST();
10621
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010622 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010623 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 +020010624
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010625 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10626 /* search for header from the beginning */
10627 ctx->idx = 0;
10628
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010629 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010630 /* no explicit occurrence and single fetch => last header by default */
10631 occ = -1;
10632
10633 if (!occ)
10634 /* prepare to report multiple occurrences for ACL fetches */
10635 smp->flags |= SMP_F_NOT_LAST;
10636
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010637 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010638 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010639 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 +020010640 return 1;
10641
10642 smp->flags &= ~SMP_F_NOT_LAST;
10643 return 0;
10644}
10645
10646/* 6. Check on HTTP header count. The number of occurrences is returned.
10647 * Accepts exactly 1 argument of type string. It does not stop on commas and
10648 * returns full lines instead (useful for User-Agent or Date for example).
10649 */
10650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010651smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010652{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010653 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010654 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010655 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010656 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010657 const char *name = NULL;
10658 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010659
Willy Tarreau601a4d12015-04-01 19:16:09 +020010660 if (args && args->type == ARGT_STR) {
10661 name = args->data.str.str;
10662 len = args->data.str.len;
10663 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010664
10665 CHECK_HTTP_MESSAGE_FIRST();
10666
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010667 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010668 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 +020010669
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010670 ctx.idx = 0;
10671 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010672 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010673 cnt++;
10674
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010675 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010676 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010677 smp->flags = SMP_F_VOL_HDR;
10678 return 1;
10679}
10680
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010681static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010682smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010683{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010684 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010685 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010686 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010687 struct chunk *temp;
10688 char del = ',';
10689
10690 if (args && args->type == ARGT_STR)
10691 del = *args[0].data.str.str;
10692
10693 CHECK_HTTP_MESSAGE_FIRST();
10694
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010695 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010696 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 +020010697
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010698 temp = get_trash_chunk();
10699
10700 ctx.idx = 0;
10701 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10702 if (temp->len)
10703 temp->str[temp->len++] = del;
10704 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10705 temp->len += ctx.del;
10706 }
10707
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010708 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010709 smp->data.u.str.str = temp->str;
10710 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010711 smp->flags = SMP_F_VOL_HDR;
10712 return 1;
10713}
10714
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010715/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10716 * Accepts an optional argument of type string containing the header field name,
10717 * and an optional argument of type signed or unsigned integer to request an
10718 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010719 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010720 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010721static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010722smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010723{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010724 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010725 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010726 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010727 int occ = 0;
10728 const char *name_str = NULL;
10729 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010730
Willy Tarreaua890d072013-04-02 12:01:06 +020010731 if (!ctx) {
10732 /* first call */
10733 ctx = &static_hdr_ctx;
10734 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010735 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010736 }
10737
Willy Tarreau185b5c42012-04-26 15:11:51 +020010738 if (args) {
10739 if (args[0].type != ARGT_STR)
10740 return 0;
10741 name_str = args[0].data.str.str;
10742 name_len = args[0].data.str.len;
10743
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010744 if (args[1].type == ARGT_SINT)
10745 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010746 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010747
Willy Tarreaue333ec92012-04-16 16:26:40 +020010748 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010749
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010750 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010751 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 +020010752
Willy Tarreau185b5c42012-04-26 15:11:51 +020010753 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010754 /* search for header from the beginning */
10755 ctx->idx = 0;
10756
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010757 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010758 /* no explicit occurrence and single fetch => last header by default */
10759 occ = -1;
10760
10761 if (!occ)
10762 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010763 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010764
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010765 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010766 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010767 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 +020010768 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010769
Willy Tarreau37406352012-04-23 16:16:37 +020010770 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010771 return 0;
10772}
10773
Willy Tarreauc11416f2007-06-17 16:58:38 +020010774/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010775 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010776 */
10777static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010778smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010779{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010780 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010781 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010782 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010783 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010784 const char *name = NULL;
10785 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010786
Willy Tarreau601a4d12015-04-01 19:16:09 +020010787 if (args && args->type == ARGT_STR) {
10788 name = args->data.str.str;
10789 len = args->data.str.len;
10790 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010791
Willy Tarreaue333ec92012-04-16 16:26:40 +020010792 CHECK_HTTP_MESSAGE_FIRST();
10793
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010794 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010795 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 +020010796
Willy Tarreau33a7e692007-06-10 19:45:56 +020010797 ctx.idx = 0;
10798 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010799 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010800 cnt++;
10801
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010802 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010803 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010804 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010805 return 1;
10806}
10807
Willy Tarreau185b5c42012-04-26 15:11:51 +020010808/* Fetch an HTTP header's integer value. The integer value is returned. It
10809 * takes a mandatory argument of type string and an optional one of type int
10810 * to designate a specific occurrence. It returns an unsigned integer, which
10811 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010812 */
10813static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010814smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010815{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010816 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010817
Willy Tarreauf853c462012-04-23 18:53:56 +020010818 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010819 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010820 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010821 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010822
Willy Tarreaud53e2422012-04-16 17:21:11 +020010823 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010824}
10825
Cyril Bonté69fa9922012-10-25 00:01:06 +020010826/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10827 * and an optional one of type int to designate a specific occurrence.
10828 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010829 */
10830static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010831smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010832{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010833 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010834
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010835 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010836 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010837 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010838 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010839 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010840 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010841 if (smp->data.u.str.len < temp->size - 1) {
10842 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10843 temp->str[smp->data.u.str.len] = '\0';
10844 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010845 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010846 break;
10847 }
10848 }
10849 }
10850
Willy Tarreaud53e2422012-04-16 17:21:11 +020010851 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010852 if (!(smp->flags & SMP_F_NOT_LAST))
10853 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010854 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010855 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010856}
10857
Willy Tarreau737b0c12007-06-10 21:28:46 +020010858/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10859 * the first '/' after the possible hostname, and ends before the possible '?'.
10860 */
10861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010863{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010864 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010865 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010866
Willy Tarreauc0239e02012-04-16 14:42:55 +020010867 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010868
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010869 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010870 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010871 ptr = http_get_path(txn);
10872 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010873 return 0;
10874
10875 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010876 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010877 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010878
10879 while (ptr < end && *ptr != '?')
10880 ptr++;
10881
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010882 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010883 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010884 return 1;
10885}
10886
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010887/* This produces a concatenation of the first occurrence of the Host header
10888 * followed by the path component if it begins with a slash ('/'). This means
10889 * that '*' will not be added, resulting in exactly the first Host entry.
10890 * If no Host header is found, then the path is returned as-is. The returned
10891 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010892 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010893 */
10894static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010895smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010896{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010897 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010898 char *ptr, *end, *beg;
10899 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010900 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010901
10902 CHECK_HTTP_MESSAGE_FIRST();
10903
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010904 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010905 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010906 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010907 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010908
10909 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010910 temp = get_trash_chunk();
10911 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010912 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010913 smp->data.u.str.str = temp->str;
10914 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010915
10916 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010917 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010918 beg = http_get_path(txn);
10919 if (!beg)
10920 beg = end;
10921
10922 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10923
10924 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010925 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10926 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010927 }
10928
10929 smp->flags = SMP_F_VOL_1ST;
10930 return 1;
10931}
10932
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010933/* This produces a 32-bit hash of the concatenation of the first occurrence of
10934 * the Host header followed by the path component if it begins with a slash ('/').
10935 * This means that '*' will not be added, resulting in exactly the first Host
10936 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010937 * is hashed using the path hash followed by a full avalanche hash and provides a
10938 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010939 * high-traffic sites without having to store whole paths.
10940 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010941int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010942smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010943{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010944 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010945 struct hdr_ctx ctx;
10946 unsigned int hash = 0;
10947 char *ptr, *beg, *end;
10948 int len;
10949
10950 CHECK_HTTP_MESSAGE_FIRST();
10951
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010952 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010953 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010954 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010955 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10956 ptr = ctx.line + ctx.val;
10957 len = ctx.vlen;
10958 while (len--)
10959 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10960 }
10961
10962 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010963 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010964 beg = http_get_path(txn);
10965 if (!beg)
10966 beg = end;
10967
10968 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10969
10970 if (beg < ptr && *beg == '/') {
10971 while (beg < ptr)
10972 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10973 }
10974 hash = full_hash(hash);
10975
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010976 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010977 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010978 smp->flags = SMP_F_VOL_1ST;
10979 return 1;
10980}
10981
Willy Tarreau4a550602012-12-09 14:53:32 +010010982/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010983 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10984 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10985 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010986 * that in environments where IPv6 is insignificant, truncating the output to
10987 * 8 bytes would still work.
10988 */
10989static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010990smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010991{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010992 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010993 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010994
10995 if (!cli_conn)
10996 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010997
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010998 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010999 return 0;
11000
Willy Tarreau47ca5452012-12-23 20:22:19 +010011001 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011002 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020011003 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010011004
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011005 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010011006 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011007 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010011008 temp->len += 4;
11009 break;
11010 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011011 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011012 temp->len += 16;
11013 break;
11014 default:
11015 return 0;
11016 }
11017
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011018 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011019 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011020 return 1;
11021}
11022
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011023/* Extracts the query string, which comes after the question mark '?'. If no
11024 * question mark is found, nothing is returned. Otherwise it returns a sample
11025 * of type string carrying the whole query string.
11026 */
11027static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011028smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011029{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011030 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011031 char *ptr, *end;
11032
11033 CHECK_HTTP_MESSAGE_FIRST();
11034
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011035 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011036 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11037 end = ptr + txn->req.sl.rq.u_l;
11038
11039 /* look up the '?' */
11040 do {
11041 if (ptr == end)
11042 return 0;
11043 } while (*ptr++ != '?');
11044
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011045 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011046 smp->data.u.str.str = ptr;
11047 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011048 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11049 return 1;
11050}
11051
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011052static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011053smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011054{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011055 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11056 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11057 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011058
Willy Tarreau24e32d82012-04-23 23:55:44 +020011059 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011060
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011061 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011062 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011063 return 1;
11064}
11065
Willy Tarreau7f18e522010-10-22 20:04:13 +020011066/* return a valid test if the current request is the first one on the connection */
11067static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011068smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011069{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011070 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011071 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011072 return 1;
11073}
11074
Willy Tarreau34db1082012-04-19 17:16:54 +020011075/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011076static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011077smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011078{
11079
Willy Tarreau24e32d82012-04-23 23:55:44 +020011080 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011081 return 0;
11082
Willy Tarreauc0239e02012-04-16 14:42:55 +020011083 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011085 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011086 return 0;
11087
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011088 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011089 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011090 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011091 return 1;
11092}
Willy Tarreau8797c062007-05-07 00:55:35 +020011093
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011094/* Accepts exactly 1 argument of type userlist */
11095static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011096smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011097{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011098 if (!args || args->type != ARGT_USR)
11099 return 0;
11100
11101 CHECK_HTTP_MESSAGE_FIRST();
11102
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011103 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011104 return 0;
11105
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011106 /* if the user does not belong to the userlist or has a wrong password,
11107 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011108 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011109 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011110 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11111 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011112 return 0;
11113
11114 /* pat_match_auth() will need the user list */
11115 smp->ctx.a[0] = args->data.usr;
11116
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011117 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011118 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011119 smp->data.u.str.str = smp->strm->txn->auth.user;
11120 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011121
11122 return 1;
11123}
11124
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011125/* Try to find the next occurrence of a cookie name in a cookie header value.
11126 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11127 * the cookie value is returned into *value and *value_l, and the function
11128 * returns a pointer to the next pointer to search from if the value was found.
11129 * Otherwise if the cookie was not found, NULL is returned and neither value
11130 * nor value_l are touched. The input <hdr> string should first point to the
11131 * header's value, and the <hdr_end> pointer must point to the first character
11132 * not part of the value. <list> must be non-zero if value may represent a list
11133 * of values (cookie headers). This makes it faster to abort parsing when no
11134 * list is expected.
11135 */
David Carlier4686f792015-09-25 14:10:50 +010011136char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011137extract_cookie_value(char *hdr, const char *hdr_end,
11138 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011139 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011140{
11141 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11142 char *next;
11143
11144 /* we search at least a cookie name followed by an equal, and more
11145 * generally something like this :
11146 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11147 */
11148 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11149 /* Iterate through all cookies on this line */
11150
Willy Tarreau2235b262016-11-05 15:50:20 +010011151 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011152 att_beg++;
11153
11154 /* find att_end : this is the first character after the last non
11155 * space before the equal. It may be equal to hdr_end.
11156 */
11157 equal = att_end = att_beg;
11158
11159 while (equal < hdr_end) {
11160 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11161 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011162 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011163 continue;
11164 att_end = equal;
11165 }
11166
11167 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11168 * is between <att_beg> and <equal>, both may be identical.
11169 */
11170
11171 /* look for end of cookie if there is an equal sign */
11172 if (equal < hdr_end && *equal == '=') {
11173 /* look for the beginning of the value */
11174 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011175 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011176 val_beg++;
11177
11178 /* find the end of the value, respecting quotes */
11179 next = find_cookie_value_end(val_beg, hdr_end);
11180
11181 /* make val_end point to the first white space or delimitor after the value */
11182 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011183 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011184 val_end--;
11185 } else {
11186 val_beg = val_end = next = equal;
11187 }
11188
11189 /* We have nothing to do with attributes beginning with '$'. However,
11190 * they will automatically be removed if a header before them is removed,
11191 * since they're supposed to be linked together.
11192 */
11193 if (*att_beg == '$')
11194 continue;
11195
11196 /* Ignore cookies with no equal sign */
11197 if (equal == next)
11198 continue;
11199
11200 /* Now we have the cookie name between att_beg and att_end, and
11201 * its value between val_beg and val_end.
11202 */
11203
11204 if (att_end - att_beg == cookie_name_l &&
11205 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11206 /* let's return this value and indicate where to go on from */
11207 *value = val_beg;
11208 *value_l = val_end - val_beg;
11209 return next + 1;
11210 }
11211
11212 /* Set-Cookie headers only have the name in the first attr=value part */
11213 if (!list)
11214 break;
11215 }
11216
11217 return NULL;
11218}
11219
William Lallemanda43ba4e2014-01-28 18:14:25 +010011220/* Fetch a captured HTTP request header. The index is the position of
11221 * the "capture" option in the configuration file
11222 */
11223static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011224smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011225{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011226 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011227 int idx;
11228
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011229 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011230 return 0;
11231
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011232 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011233
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011234 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 +010011235 return 0;
11236
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011237 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011238 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011239 smp->data.u.str.str = smp->strm->req_cap[idx];
11240 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011241
11242 return 1;
11243}
11244
11245/* Fetch a captured HTTP response header. The index is the position of
11246 * the "capture" option in the configuration file
11247 */
11248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011249smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011250{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011251 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011252 int idx;
11253
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011254 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011255 return 0;
11256
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011257 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011258
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011259 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 +010011260 return 0;
11261
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011262 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011263 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011264 smp->data.u.str.str = smp->strm->res_cap[idx];
11265 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011266
11267 return 1;
11268}
11269
William Lallemand65ad6e12014-01-31 15:08:02 +010011270/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011272smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011273{
11274 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011275 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011276 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011277
Willy Tarreau15e91e12015-04-04 00:52:09 +020011278 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011279 return 0;
11280
William Lallemand96a77852014-02-05 00:30:02 +010011281 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011282
William Lallemand96a77852014-02-05 00:30:02 +010011283 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11284 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011285
William Lallemand96a77852014-02-05 00:30:02 +010011286 temp = get_trash_chunk();
11287 temp->str = txn->uri;
11288 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011289 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011290 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011291 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011292
11293 return 1;
11294
11295}
11296
11297/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11298static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011299smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011300{
11301 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011302 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011303 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011304
Willy Tarreau15e91e12015-04-04 00:52:09 +020011305 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011306 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011307
William Lallemand65ad6e12014-01-31 15:08:02 +010011308 ptr = txn->uri;
11309
11310 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11311 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011312
William Lallemand65ad6e12014-01-31 15:08:02 +010011313 if (!*ptr)
11314 return 0;
11315
11316 ptr++; /* skip the space */
11317
11318 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011319 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011320 if (!ptr)
11321 return 0;
11322 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11323 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011324
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011325 smp->data.u.str = *temp;
11326 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011327 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011328 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011329
11330 return 1;
11331}
11332
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011333/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11334 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11335 */
11336static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011337smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011338{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011339 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011340
Willy Tarreau15e91e12015-04-04 00:52:09 +020011341 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011342 return 0;
11343
11344 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011345 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011346 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011347 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011348
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011349 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011350 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351 smp->flags = SMP_F_CONST;
11352 return 1;
11353
11354}
11355
11356/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11357 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11358 */
11359static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011360smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011361{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011362 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011363
Willy Tarreau15e91e12015-04-04 00:52:09 +020011364 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011365 return 0;
11366
11367 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011368 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011369 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011370 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011371
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011372 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011373 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011374 smp->flags = SMP_F_CONST;
11375 return 1;
11376
11377}
11378
William Lallemand65ad6e12014-01-31 15:08:02 +010011379
Willy Tarreaue333ec92012-04-16 16:26:40 +020011380/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011381 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011382 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011383 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011384 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011385 * Accepts exactly 1 argument of type string. If the input options indicate
11386 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011387 * The returned sample is of type CSTR. Can be used to parse cookies in other
11388 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011389 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011390int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011392 struct http_txn *txn;
11393 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011394 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011395 const struct http_msg *msg;
11396 const char *hdr_name;
11397 int hdr_name_len;
11398 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011399 int occ = 0;
11400 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011401
Willy Tarreau24e32d82012-04-23 23:55:44 +020011402 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011403 return 0;
11404
Willy Tarreaua890d072013-04-02 12:01:06 +020011405 if (!ctx) {
11406 /* first call */
11407 ctx = &static_hdr_ctx;
11408 ctx->idx = 0;
11409 smp->ctx.a[2] = ctx;
11410 }
11411
Willy Tarreaue333ec92012-04-16 16:26:40 +020011412 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011413
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011414 txn = smp->strm->txn;
11415 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011416
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011417 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011418 msg = &txn->req;
11419 hdr_name = "Cookie";
11420 hdr_name_len = 6;
11421 } else {
11422 msg = &txn->rsp;
11423 hdr_name = "Set-Cookie";
11424 hdr_name_len = 10;
11425 }
11426
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011427 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011428 /* no explicit occurrence and single fetch => last cookie by default */
11429 occ = -1;
11430
11431 /* OK so basically here, either we want only one value and it's the
11432 * last one, or we want to iterate over all of them and we fetch the
11433 * next one.
11434 */
11435
Willy Tarreau9b28e032012-10-12 23:49:43 +020011436 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011437 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011438 /* search for the header from the beginning, we must first initialize
11439 * the search parameters.
11440 */
Willy Tarreau37406352012-04-23 16:16:37 +020011441 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011442 ctx->idx = 0;
11443 }
11444
Willy Tarreau28376d62012-04-26 21:26:10 +020011445 smp->flags |= SMP_F_VOL_HDR;
11446
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011447 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011448 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11449 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011450 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11451 goto out;
11452
Willy Tarreau24e32d82012-04-23 23:55:44 +020011453 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011454 continue;
11455
Willy Tarreau37406352012-04-23 16:16:37 +020011456 smp->ctx.a[0] = ctx->line + ctx->val;
11457 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011458 }
11459
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011460 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011461 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011462 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011463 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011464 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011465 &smp->data.u.str.str,
11466 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011467 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011468 found = 1;
11469 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011470 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011471 smp->flags |= SMP_F_NOT_LAST;
11472 return 1;
11473 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011474 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011475 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011476 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011477 /* all cookie headers and values were scanned. If we're looking for the
11478 * last occurrence, we may return it now.
11479 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011480 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011481 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011482 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011483}
11484
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011485/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011486 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011487 * multiple cookies may be parsed on the same line. The returned sample is of
11488 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011489 */
11490static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011491smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011492{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011493 struct http_txn *txn;
11494 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011495 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011496 const struct http_msg *msg;
11497 const char *hdr_name;
11498 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011499 int cnt;
11500 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011501 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011502
Willy Tarreau24e32d82012-04-23 23:55:44 +020011503 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011504 return 0;
11505
Willy Tarreaue333ec92012-04-16 16:26:40 +020011506 CHECK_HTTP_MESSAGE_FIRST();
11507
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011508 txn = smp->strm->txn;
11509 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011510
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011511 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011512 msg = &txn->req;
11513 hdr_name = "Cookie";
11514 hdr_name_len = 6;
11515 } else {
11516 msg = &txn->rsp;
11517 hdr_name = "Set-Cookie";
11518 hdr_name_len = 10;
11519 }
11520
Willy Tarreau9b28e032012-10-12 23:49:43 +020011521 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011522 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011523 ctx.idx = 0;
11524 cnt = 0;
11525
11526 while (1) {
11527 /* Note: val_beg == NULL every time we need to fetch a new header */
11528 if (!val_beg) {
11529 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11530 break;
11531
Willy Tarreau24e32d82012-04-23 23:55:44 +020011532 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011533 continue;
11534
11535 val_beg = ctx.line + ctx.val;
11536 val_end = val_beg + ctx.vlen;
11537 }
11538
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011539 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011540 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011541 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011542 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011543 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011544 &smp->data.u.str.str,
11545 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011546 cnt++;
11547 }
11548 }
11549
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011550 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011551 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011552 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011553 return 1;
11554}
11555
Willy Tarreau51539362012-05-08 12:46:28 +020011556/* Fetch an cookie's integer value. The integer value is returned. It
11557 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11558 */
11559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011560smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011561{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011562 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011563
11564 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011565 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011566 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011567 }
11568
11569 return ret;
11570}
11571
Willy Tarreau8797c062007-05-07 00:55:35 +020011572/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011573/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011574/************************************************************************/
11575
David Cournapeau16023ee2010-12-23 20:55:41 +090011576/*
11577 * Given a path string and its length, find the position of beginning of the
11578 * query string. Returns NULL if no query string is found in the path.
11579 *
11580 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11581 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011582 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011583 */
bedis4c75cca2012-10-05 08:38:24 +020011584static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011585{
11586 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011587
bedis4c75cca2012-10-05 08:38:24 +020011588 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011589 return p ? p + 1 : NULL;
11590}
11591
bedis4c75cca2012-10-05 08:38:24 +020011592static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011593{
bedis4c75cca2012-10-05 08:38:24 +020011594 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011595}
11596
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011597/* after increasing a pointer value, it can exceed the first buffer
11598 * size. This function transform the value of <ptr> according with
11599 * the expected position. <chunks> is an array of the one or two
11600 * avalaible chunks. The first value is the start of the first chunk,
11601 * the second value if the end+1 of the first chunks. The third value
11602 * is NULL or the start of the second chunk and the fourth value is
11603 * the end+1 of the second chunk. The function returns 1 if does a
11604 * wrap, else returns 0.
11605 */
11606static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11607{
11608 if (*ptr < chunks[1])
11609 return 0;
11610 if (!chunks[2])
11611 return 0;
11612 *ptr = chunks[2] + ( *ptr - chunks[1] );
11613 return 1;
11614}
11615
David Cournapeau16023ee2010-12-23 20:55:41 +090011616/*
11617 * Given a url parameter, find the starting position of the first occurence,
11618 * or NULL if the parameter is not found.
11619 *
11620 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11621 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011622 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011623 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011624 * or the second chunk. The caller must be check the position before using the
11625 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011626 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627static const char *
11628find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011629 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011630 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011631{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011632 const char *pos, *last, *equal;
11633 const char **bufs = chunks;
11634 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011635
David Cournapeau16023ee2010-12-23 20:55:41 +090011636
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011637 pos = bufs[0];
11638 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011639 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640 /* Check the equal. */
11641 equal = pos + url_param_name_l;
11642 if (fix_pointer_if_wrap(chunks, &equal)) {
11643 if (equal >= chunks[3])
11644 return NULL;
11645 } else {
11646 if (equal >= chunks[1])
11647 return NULL;
11648 }
11649 if (*equal == '=') {
11650 if (pos + url_param_name_l > last) {
11651 /* process wrap case, we detect a wrap. In this case, the
11652 * comparison is performed in two parts.
11653 */
11654
11655 /* This is the end, we dont have any other chunk. */
11656 if (bufs != chunks || !bufs[2])
11657 return NULL;
11658
11659 /* Compute the length of each part of the comparison. */
11660 l1 = last - pos;
11661 l2 = url_param_name_l - l1;
11662
11663 /* The second buffer is too short to contain the compared string. */
11664 if (bufs[2] + l2 > bufs[3])
11665 return NULL;
11666
11667 if (memcmp(pos, url_param_name, l1) == 0 &&
11668 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11669 return pos;
11670
11671 /* Perform wrapping and jump the string who fail the comparison. */
11672 bufs += 2;
11673 pos = bufs[0] + l2;
11674 last = bufs[1];
11675
11676 } else {
11677 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011678 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11679 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011680 pos += url_param_name_l + 1;
11681 if (fix_pointer_if_wrap(chunks, &pos))
11682 last = bufs[2];
11683 }
11684 }
11685
11686 while (1) {
11687 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011688 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011689 pos++;
11690 if (pos < last)
11691 break;
11692 /* process buffer wrapping. */
11693 if (bufs != chunks || !bufs[2])
11694 return NULL;
11695 bufs += 2;
11696 pos = bufs[0];
11697 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011698 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011699 pos++;
11700 }
11701 return NULL;
11702}
11703
11704/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011705 * Given a url parameter name and a query string, find the next value.
11706 * An empty url_param_name matches the first available parameter.
11707 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11708 * respectively provide a pointer to the value and its end.
11709 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011710 */
11711static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011712find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011713 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011714 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011715{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011716 const char *arg_start, *qs_end;
11717 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011718
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011719 arg_start = chunks[0];
11720 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011721 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011722 /* Looks for an argument name. */
11723 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011724 url_param_name, url_param_name_l,
11725 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011726 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011727 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011728 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011729 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011730 if (!arg_start)
11731 return 0;
11732
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011733 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011734 while (1) {
11735 /* looks for the first argument. */
11736 value_start = memchr(arg_start, '=', qs_end - arg_start);
11737 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011738 /* Check for wrapping. */
11739 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011740 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011741 chunks[2]) {
11742 arg_start = chunks[2];
11743 qs_end = chunks[3];
11744 continue;
11745 }
11746 return 0;
11747 }
11748 break;
11749 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011750 value_start++;
11751 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011752 else {
11753 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011754 value_start = arg_start + url_param_name_l + 1;
11755
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011756 /* Check for pointer wrapping. */
11757 if (fix_pointer_if_wrap(chunks, &value_start)) {
11758 /* Update the end pointer. */
11759 qs_end = chunks[3];
11760
11761 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011762 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011763 return 0;
11764 }
11765 }
11766
David Cournapeau16023ee2010-12-23 20:55:41 +090011767 value_end = value_start;
11768
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011769 while (1) {
11770 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11771 value_end++;
11772 if (value_end < qs_end)
11773 break;
11774 /* process buffer wrapping. */
11775 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011776 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011777 chunks[2]) {
11778 value_end = chunks[2];
11779 qs_end = chunks[3];
11780 continue;
11781 }
11782 break;
11783 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011784
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011785 *vstart = value_start;
11786 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011787 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011788}
11789
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790/* This scans a URL-encoded query string. It takes an optionally wrapping
11791 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11792 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11793 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011794 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011795static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011796smp_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 +090011797{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011798 const char *vstart, *vend;
11799 struct chunk *temp;
11800 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011801
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011802 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011803 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011804 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011805 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011806 return 0;
11807
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011808 /* Create sample. If the value is contiguous, return the pointer as CONST,
11809 * if the value is wrapped, copy-it in a buffer.
11810 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011811 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011812 if (chunks[2] &&
11813 vstart >= chunks[0] && vstart <= chunks[1] &&
11814 vend >= chunks[2] && vend <= chunks[3]) {
11815 /* Wrapped case. */
11816 temp = get_trash_chunk();
11817 memcpy(temp->str, vstart, chunks[1] - vstart);
11818 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011819 smp->data.u.str.str = temp->str;
11820 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011821 } else {
11822 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011823 smp->data.u.str.str = (char *)vstart;
11824 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11826 }
11827
11828 /* Update context, check wrapping. */
11829 chunks[0] = vend;
11830 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11831 chunks[1] = chunks[3];
11832 chunks[2] = NULL;
11833 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011834
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011835 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011836 smp->flags |= SMP_F_NOT_LAST;
11837
David Cournapeau16023ee2010-12-23 20:55:41 +090011838 return 1;
11839}
11840
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011841/* This function iterates over each parameter of the query string. It uses
11842 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011843 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11844 * An optional parameter name is passed in args[0], otherwise any parameter is
11845 * considered. It supports an optional delimiter argument for the beginning of
11846 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011847 */
11848static int
11849smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11850{
11851 struct http_msg *msg;
11852 char delim = '?';
11853 const char *name;
11854 int name_len;
11855
Dragan Dosen26f77e52015-05-25 10:02:11 +020011856 if (!args ||
11857 (args[0].type && args[0].type != ARGT_STR) ||
11858 (args[1].type && args[1].type != ARGT_STR))
11859 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011860
Dragan Dosen26f77e52015-05-25 10:02:11 +020011861 name = "";
11862 name_len = 0;
11863 if (args->type == ARGT_STR) {
11864 name = args->data.str.str;
11865 name_len = args->data.str.len;
11866 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011867
Dragan Dosen26f77e52015-05-25 10:02:11 +020011868 if (args[1].type)
11869 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011870
Dragan Dosen26f77e52015-05-25 10:02:11 +020011871 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011872 CHECK_HTTP_MESSAGE_FIRST();
11873
11874 msg = &smp->strm->txn->req;
11875
11876 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11877 msg->sl.rq.u_l, delim);
11878 if (!smp->ctx.a[0])
11879 return 0;
11880
11881 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011882
11883 /* Assume that the context is filled with NULL pointer
11884 * before the first call.
11885 * smp->ctx.a[2] = NULL;
11886 * smp->ctx.a[3] = NULL;
11887 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011888 }
11889
11890 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11891}
11892
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011893/* This function iterates over each parameter of the body. This requires
11894 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011895 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11896 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11897 * optional second part if the body wraps at the end of the buffer. An optional
11898 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011899 */
11900static int
11901smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11902{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011903 struct http_msg *msg;
11904 unsigned long len;
11905 unsigned long block1;
11906 char *body;
11907 const char *name;
11908 int name_len;
11909
11910 if (!args || (args[0].type && args[0].type != ARGT_STR))
11911 return 0;
11912
11913 name = "";
11914 name_len = 0;
11915 if (args[0].type == ARGT_STR) {
11916 name = args[0].data.str.str;
11917 name_len = args[0].data.str.len;
11918 }
11919
11920 if (!smp->ctx.a[0]) { // first call, find the query string
11921 CHECK_HTTP_MESSAGE_FIRST();
11922
11923 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011924 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011925 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011926 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011927
11928 len = http_body_bytes(msg);
11929 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11930
11931 block1 = len;
11932 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11933 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11934
11935 if (block1 == len) {
11936 /* buffer is not wrapped (or empty) */
11937 smp->ctx.a[0] = body;
11938 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011939
11940 /* Assume that the context is filled with NULL pointer
11941 * before the first call.
11942 * smp->ctx.a[2] = NULL;
11943 * smp->ctx.a[3] = NULL;
11944 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011945 }
11946 else {
11947 /* buffer is wrapped, we need to defragment it */
11948 smp->ctx.a[0] = body;
11949 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011950 smp->ctx.a[2] = msg->chn->buf->data;
11951 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011952 }
11953 }
11954 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11955}
11956
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011957/* Return the signed integer value for the specified url parameter (see url_param
11958 * above).
11959 */
11960static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011961smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011962{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011963 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011964
11965 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011966 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011967 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011968 }
11969
11970 return ret;
11971}
11972
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011973/* This produces a 32-bit hash of the concatenation of the first occurrence of
11974 * the Host header followed by the path component if it begins with a slash ('/').
11975 * This means that '*' will not be added, resulting in exactly the first Host
11976 * entry. If no Host header is found, then the path is used. The resulting value
11977 * is hashed using the url hash followed by a full avalanche hash and provides a
11978 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11979 * high-traffic sites without having to store whole paths.
11980 * this differs from the base32 functions in that it includes the url parameters
11981 * as well as the path
11982 */
11983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011984smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011985{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011986 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011987 struct hdr_ctx ctx;
11988 unsigned int hash = 0;
11989 char *ptr, *beg, *end;
11990 int len;
11991
11992 CHECK_HTTP_MESSAGE_FIRST();
11993
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011994 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011995 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011996 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011997 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11998 ptr = ctx.line + ctx.val;
11999 len = ctx.vlen;
12000 while (len--)
12001 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
12002 }
12003
12004 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020012005 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 +000012006 beg = http_get_path(txn);
12007 if (!beg)
12008 beg = end;
12009
12010 for (ptr = beg; ptr < end ; ptr++);
12011
12012 if (beg < ptr && *beg == '/') {
12013 while (beg < ptr)
12014 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12015 }
12016 hash = full_hash(hash);
12017
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012018 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012019 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012020 smp->flags = SMP_F_VOL_1ST;
12021 return 1;
12022}
12023
12024/* This concatenates the source address with the 32-bit hash of the Host and
12025 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12026 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12027 * on the source address length. The URL hash is stored before the address so
12028 * that in environments where IPv6 is insignificant, truncating the output to
12029 * 8 bytes would still work.
12030 */
12031static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012032smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012033{
12034 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012035 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012036
Dragan Dosendb5af612016-06-16 11:23:01 +020012037 if (!cli_conn)
12038 return 0;
12039
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012040 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012041 return 0;
12042
12043 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012044 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12045 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012046
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012047 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012048 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012049 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012050 temp->len += 4;
12051 break;
12052 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012053 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012054 temp->len += 16;
12055 break;
12056 default:
12057 return 0;
12058 }
12059
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012060 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012061 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012062 return 1;
12063}
12064
Willy Tarreau185b5c42012-04-26 15:11:51 +020012065/* This function is used to validate the arguments passed to any "hdr" fetch
12066 * keyword. These keywords support an optional positive or negative occurrence
12067 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12068 * is assumed that the types are already the correct ones. Returns 0 on error,
12069 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12070 * error message in case of error, that the caller is responsible for freeing.
12071 * The initial location must either be freeable or NULL.
12072 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012073int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012074{
12075 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012076 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012077 return 0;
12078 }
12079 return 1;
12080}
12081
Willy Tarreau276fae92013-07-25 14:36:01 +020012082/* takes an UINT value on input supposed to represent the time since EPOCH,
12083 * adds an optional offset found in args[0] and emits a string representing
12084 * the date in RFC-1123/5322 format.
12085 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012086static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012087{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012088 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012089 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12090 struct chunk *temp;
12091 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012092 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012093 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012094
12095 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012096 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012097 curr_date += args[0].data.sint;
12098
12099 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012100 if (!tm)
12101 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012102
12103 temp = get_trash_chunk();
12104 temp->len = snprintf(temp->str, temp->size - temp->len,
12105 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12106 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12107 tm->tm_hour, tm->tm_min, tm->tm_sec);
12108
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012109 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012110 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012111 return 1;
12112}
12113
Thierry FOURNIERad903512014-04-11 17:51:01 +020012114/* Match language range with language tag. RFC2616 14.4:
12115 *
12116 * A language-range matches a language-tag if it exactly equals
12117 * the tag, or if it exactly equals a prefix of the tag such
12118 * that the first tag character following the prefix is "-".
12119 *
12120 * Return 1 if the strings match, else return 0.
12121 */
12122static inline int language_range_match(const char *range, int range_len,
12123 const char *tag, int tag_len)
12124{
12125 const char *end = range + range_len;
12126 const char *tend = tag + tag_len;
12127 while (range < end) {
12128 if (*range == '-' && tag == tend)
12129 return 1;
12130 if (*range != *tag || tag == tend)
12131 return 0;
12132 range++;
12133 tag++;
12134 }
12135 /* Return true only if the last char of the tag is matched. */
12136 return tag == tend;
12137}
12138
12139/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012140static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012141{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012142 const char *al = smp->data.u.str.str;
12143 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012144 const char *token;
12145 int toklen;
12146 int qvalue;
12147 const char *str;
12148 const char *w;
12149 int best_q = 0;
12150
12151 /* Set the constant to the sample, because the output of the
12152 * function will be peek in the constant configuration string.
12153 */
12154 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012155 smp->data.u.str.size = 0;
12156 smp->data.u.str.str = "";
12157 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012158
12159 /* Parse the accept language */
12160 while (1) {
12161
12162 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012163 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012164 al++;
12165 if (al >= end)
12166 break;
12167
12168 /* Start of the fisrt word. */
12169 token = al;
12170
12171 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012172 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012173 al++;
12174 if (al == token)
12175 goto expect_comma;
12176
12177 /* Length of the token. */
12178 toklen = al - token;
12179 qvalue = 1000;
12180
12181 /* Check if the token exists in the list. If the token not exists,
12182 * jump to the next token.
12183 */
12184 str = args[0].data.str.str;
12185 w = str;
12186 while (1) {
12187 if (*str == ';' || *str == '\0') {
12188 if (language_range_match(token, toklen, w, str-w))
12189 goto look_for_q;
12190 if (*str == '\0')
12191 goto expect_comma;
12192 w = str + 1;
12193 }
12194 str++;
12195 }
12196 goto expect_comma;
12197
12198look_for_q:
12199
12200 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012201 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012202 al++;
12203 if (al >= end)
12204 goto process_value;
12205
12206 /* If ',' is found, process the result */
12207 if (*al == ',')
12208 goto process_value;
12209
12210 /* If the character is different from ';', look
12211 * for the end of the header part in best effort.
12212 */
12213 if (*al != ';')
12214 goto expect_comma;
12215
12216 /* Assumes that the char is ';', now expect "q=". */
12217 al++;
12218
12219 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012220 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012221 al++;
12222 if (al >= end)
12223 goto process_value;
12224
12225 /* Expect 'q'. If no 'q', continue in best effort */
12226 if (*al != 'q')
12227 goto process_value;
12228 al++;
12229
12230 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012231 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012232 al++;
12233 if (al >= end)
12234 goto process_value;
12235
12236 /* Expect '='. If no '=', continue in best effort */
12237 if (*al != '=')
12238 goto process_value;
12239 al++;
12240
12241 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012242 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012243 al++;
12244 if (al >= end)
12245 goto process_value;
12246
12247 /* Parse the q value. */
12248 qvalue = parse_qvalue(al, &al);
12249
12250process_value:
12251
12252 /* If the new q value is the best q value, then store the associated
12253 * language in the response. If qvalue is the biggest value (1000),
12254 * break the process.
12255 */
12256 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012257 smp->data.u.str.str = (char *)w;
12258 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012259 if (qvalue >= 1000)
12260 break;
12261 best_q = qvalue;
12262 }
12263
12264expect_comma:
12265
12266 /* Expect comma or end. If the end is detected, quit the loop. */
12267 while (al < end && *al != ',')
12268 al++;
12269 if (al >= end)
12270 break;
12271
12272 /* Comma is found, jump it and restart the analyzer. */
12273 al++;
12274 }
12275
12276 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012277 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12278 smp->data.u.str.str = args[1].data.str.str;
12279 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012280 }
12281
12282 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012283 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012284}
12285
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012286/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012287static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012288{
12289 /* If the constant flag is set or if not size is avalaible at
12290 * the end of the buffer, copy the string in other buffer
12291 * before decoding.
12292 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012293 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012294 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012295 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12296 smp->data.u.str.str = str->str;
12297 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012298 smp->flags &= ~SMP_F_CONST;
12299 }
12300
12301 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012302 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12303 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012304 return 1;
12305}
12306
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012307static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12308{
12309 struct proxy *fe = strm_fe(smp->strm);
12310 int idx, i;
12311 struct cap_hdr *hdr;
12312 int len;
12313
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012314 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012315 return 0;
12316
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012317 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012318
12319 /* Check the availibity of the capture id. */
12320 if (idx > fe->nb_req_cap - 1)
12321 return 0;
12322
12323 /* Look for the original configuration. */
12324 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12325 hdr != NULL && i != idx ;
12326 i--, hdr = hdr->next);
12327 if (!hdr)
12328 return 0;
12329
12330 /* check for the memory allocation */
12331 if (smp->strm->req_cap[hdr->index] == NULL)
12332 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12333 if (smp->strm->req_cap[hdr->index] == NULL)
12334 return 0;
12335
12336 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012337 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012338 if (len > hdr->len)
12339 len = hdr->len;
12340
12341 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012342 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012343 smp->strm->req_cap[idx][len] = '\0';
12344
12345 return 1;
12346}
12347
12348static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12349{
12350 struct proxy *fe = strm_fe(smp->strm);
12351 int idx, i;
12352 struct cap_hdr *hdr;
12353 int len;
12354
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012355 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012356 return 0;
12357
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012358 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012359
12360 /* Check the availibity of the capture id. */
12361 if (idx > fe->nb_rsp_cap - 1)
12362 return 0;
12363
12364 /* Look for the original configuration. */
12365 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12366 hdr != NULL && i != idx ;
12367 i--, hdr = hdr->next);
12368 if (!hdr)
12369 return 0;
12370
12371 /* check for the memory allocation */
12372 if (smp->strm->res_cap[hdr->index] == NULL)
12373 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12374 if (smp->strm->res_cap[hdr->index] == NULL)
12375 return 0;
12376
12377 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012378 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012379 if (len > hdr->len)
12380 len = hdr->len;
12381
12382 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012383 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012384 smp->strm->res_cap[idx][len] = '\0';
12385
12386 return 1;
12387}
12388
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012389/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012390 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012391 * the relevant part of the request line accordingly. Then it updates various
12392 * pointers to the next elements which were moved, and the total buffer length.
12393 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012394 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12395 * error, though this can be revisited when this code is finally exploited.
12396 *
12397 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12398 * query string and 3 to replace uri.
12399 *
12400 * In query string case, the mark question '?' must be set at the start of the
12401 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012402 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012403int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012404 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012405{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012406 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012407 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012408 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012409 int delta;
12410
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012411 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012412 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012413 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012414 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12415
12416 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012417 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012418 txn->req.sl.rq.m_l += delta;
12419 txn->req.sl.rq.u += delta;
12420 txn->req.sl.rq.v += delta;
12421 break;
12422
12423 case 1: // path
12424 cur_ptr = http_get_path(txn);
12425 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012426 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427
12428 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012429 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 +010012430 cur_end++;
12431
12432 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012433 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012434 txn->req.sl.rq.u_l += delta;
12435 txn->req.sl.rq.v += delta;
12436 break;
12437
12438 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012439 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012440 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012441 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12442 while (cur_ptr < cur_end && *cur_ptr != '?')
12443 cur_ptr++;
12444
12445 /* skip the question mark or indicate that we must insert it
12446 * (but only if the format string is not empty then).
12447 */
12448 if (cur_ptr < cur_end)
12449 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012450 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012451 offset = 0;
12452
12453 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012454 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455 txn->req.sl.rq.u_l += delta;
12456 txn->req.sl.rq.v += delta;
12457 break;
12458
12459 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012460 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012461 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12462
12463 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012464 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012465 txn->req.sl.rq.u_l += delta;
12466 txn->req.sl.rq.v += delta;
12467 break;
12468
12469 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012470 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012471 }
12472
12473 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012474 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012475 txn->req.sl.rq.l += delta;
12476 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012477 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012478 return 0;
12479}
12480
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012481/* This function replace the HTTP status code and the associated message. The
12482 * variable <status> contains the new status code. This function never fails.
12483 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012484void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012485{
12486 struct http_txn *txn = s->txn;
12487 char *cur_ptr, *cur_end;
12488 int delta;
12489 char *res;
12490 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012491 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012492 int msg_len;
12493
12494 chunk_reset(&trash);
12495
12496 res = ultoa_o(status, trash.str, trash.size);
12497 c_l = res - trash.str;
12498
12499 trash.str[c_l] = ' ';
12500 trash.len = c_l + 1;
12501
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012502 /* Do we have a custom reason format string? */
12503 if (msg == NULL)
12504 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012505 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012506 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12507 trash.len += msg_len;
12508
12509 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12510 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12511
12512 /* commit changes and adjust message */
12513 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12514
12515 /* adjust res line offsets and lengths */
12516 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12517 txn->rsp.sl.st.c_l = c_l;
12518 txn->rsp.sl.st.r_l = msg_len;
12519
12520 delta = trash.len - (cur_end - cur_ptr);
12521 txn->rsp.sl.st.l += delta;
12522 txn->hdr_idx.v[0].len += delta;
12523 http_msg_move_end(&txn->rsp, delta);
12524}
12525
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012526/* This function executes one of the set-{method,path,query,uri} actions. It
12527 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012528 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012529 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012530 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12531 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012532 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012533enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012534 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012535{
12536 chunk_reset(&trash);
12537
12538 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012539 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012540 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012541 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012542
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012543 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012544 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012545}
12546
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012547/* This function is just a compliant action wrapper for "set-status". */
12548enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012549 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012550{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012551 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012552 return ACT_RET_CONT;
12553}
12554
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012555/* parse an http-request action among :
12556 * set-method
12557 * set-path
12558 * set-query
12559 * set-uri
12560 *
12561 * All of them accept a single argument of type string representing a log-format.
12562 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12563 * 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 +020012564 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012565 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012566enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12567 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012568{
12569 int cur_arg = *orig_arg;
12570
Thierry FOURNIER42148732015-09-02 17:17:33 +020012571 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012572
12573 switch (args[0][4]) {
12574 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012575 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012576 rule->action_ptr = http_action_set_req_line;
12577 break;
12578 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012579 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012580 rule->action_ptr = http_action_set_req_line;
12581 break;
12582 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012583 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012584 rule->action_ptr = http_action_set_req_line;
12585 break;
12586 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012587 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012588 rule->action_ptr = http_action_set_req_line;
12589 break;
12590 default:
12591 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012592 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012593 }
12594
12595 if (!*args[cur_arg] ||
12596 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12597 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012598 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012599 }
12600
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012601 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012602 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012603 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012604 (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 +010012605 return ACT_RET_PRS_ERR;
12606 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012607
12608 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012609 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012610}
12611
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012612/* parse set-status action:
12613 * This action accepts a single argument of type int representing
12614 * an http status code. It returns ACT_RET_PRS_OK on success,
12615 * ACT_RET_PRS_ERR on error.
12616 */
12617enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12618 struct act_rule *rule, char **err)
12619{
12620 char *error;
12621
Thierry FOURNIER42148732015-09-02 17:17:33 +020012622 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012623 rule->action_ptr = action_http_set_status;
12624
12625 /* Check if an argument is available */
12626 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012627 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012628 return ACT_RET_PRS_ERR;
12629 }
12630
12631 /* convert status code as integer */
12632 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12633 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12634 memprintf(err, "expects an integer status code between 100 and 999");
12635 return ACT_RET_PRS_ERR;
12636 }
12637
12638 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012639
12640 /* set custom reason string */
12641 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12642 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12643 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12644 (*orig_arg)++;
12645 rule->arg.status.reason = strdup(args[*orig_arg]);
12646 (*orig_arg)++;
12647 }
12648
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012649 return ACT_RET_PRS_OK;
12650}
12651
Willy Tarreaua9083d02015-05-08 15:27:59 +020012652/* This function executes the "capture" action. It executes a fetch expression,
12653 * turns the result into a string and puts it in a capture slot. It always
12654 * returns 1. If an error occurs the action is cancelled, but the rule
12655 * processing continues.
12656 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012657enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012658 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012659{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012660 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012661 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012662 char **cap = s->req_cap;
12663 int len;
12664
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012665 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 +020012666 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012667 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012668
12669 if (cap[h->index] == NULL)
12670 cap[h->index] = pool_alloc2(h->pool);
12671
12672 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012673 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012674
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012675 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012676 if (len > h->len)
12677 len = h->len;
12678
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012679 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012680 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012681 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012682}
12683
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012684/* This function executes the "capture" action and store the result in a
12685 * capture slot if exists. It executes a fetch expression, turns the result
12686 * into a string and puts it in a capture slot. It always returns 1. If an
12687 * error occurs the action is cancelled, but the rule processing continues.
12688 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012689enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012690 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012691{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012692 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012694 char **cap = s->req_cap;
12695 struct proxy *fe = strm_fe(s);
12696 int len;
12697 int i;
12698
12699 /* Look for the original configuration. */
12700 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012701 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012702 i--, h = h->next);
12703 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012705
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012706 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 +020012707 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012708 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012709
12710 if (cap[h->index] == NULL)
12711 cap[h->index] = pool_alloc2(h->pool);
12712
12713 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012714 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012715
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012716 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012717 if (len > h->len)
12718 len = h->len;
12719
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012720 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012721 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012722 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012723}
12724
Willy Tarreaua9083d02015-05-08 15:27:59 +020012725/* parse an "http-request capture" action. It takes a single argument which is
12726 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012727 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012728 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012729 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012730enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12731 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012732{
12733 struct sample_expr *expr;
12734 struct cap_hdr *hdr;
12735 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012736 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012737
12738 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12739 if (strcmp(args[cur_arg], "if") == 0 ||
12740 strcmp(args[cur_arg], "unless") == 0)
12741 break;
12742
12743 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012744 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012745 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012746 }
12747
Willy Tarreaua9083d02015-05-08 15:27:59 +020012748 cur_arg = *orig_arg;
12749 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12750 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012751 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012752
12753 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12754 memprintf(err,
12755 "fetch method '%s' extracts information from '%s', none of which is available here",
12756 args[cur_arg-1], sample_src_names(expr->fetch->use));
12757 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012758 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012759 }
12760
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012761 if (!args[cur_arg] || !*args[cur_arg]) {
12762 memprintf(err, "expects 'len or 'id'");
12763 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012764 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012765 }
12766
Willy Tarreaua9083d02015-05-08 15:27:59 +020012767 if (strcmp(args[cur_arg], "len") == 0) {
12768 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012769
12770 if (!(px->cap & PR_CAP_FE)) {
12771 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012772 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012773 }
12774
12775 proxy->conf.args.ctx = ARGC_CAP;
12776
Willy Tarreaua9083d02015-05-08 15:27:59 +020012777 if (!args[cur_arg]) {
12778 memprintf(err, "missing length value");
12779 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012780 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012781 }
12782 /* we copy the table name for now, it will be resolved later */
12783 len = atoi(args[cur_arg]);
12784 if (len <= 0) {
12785 memprintf(err, "length must be > 0");
12786 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012787 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012788 }
12789 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012790
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012791 if (!len) {
12792 memprintf(err, "a positive 'len' argument is mandatory");
12793 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012794 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012795 }
12796
Vincent Bernat02779b62016-04-03 13:48:43 +020012797 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012798 hdr->next = px->req_cap;
12799 hdr->name = NULL; /* not a header capture */
12800 hdr->namelen = 0;
12801 hdr->len = len;
12802 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12803 hdr->index = px->nb_req_cap++;
12804
12805 px->req_cap = hdr;
12806 px->to_log |= LW_REQHDR;
12807
Thierry FOURNIER42148732015-09-02 17:17:33 +020012808 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012809 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012810 rule->arg.cap.expr = expr;
12811 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012812 }
12813
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012814 else if (strcmp(args[cur_arg], "id") == 0) {
12815 int id;
12816 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012817
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012818 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012819
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012820 if (!args[cur_arg]) {
12821 memprintf(err, "missing id value");
12822 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012823 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012824 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012825
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012826 id = strtol(args[cur_arg], &error, 10);
12827 if (*error != '\0') {
12828 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12829 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012830 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012831 }
12832 cur_arg++;
12833
12834 proxy->conf.args.ctx = ARGC_CAP;
12835
Thierry FOURNIER42148732015-09-02 17:17:33 +020012836 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012837 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012838 rule->arg.capid.expr = expr;
12839 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012840 }
12841
12842 else {
12843 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12844 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012845 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012846 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012847
12848 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012849 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012850}
12851
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012852/* This function executes the "capture" action and store the result in a
12853 * capture slot if exists. It executes a fetch expression, turns the result
12854 * into a string and puts it in a capture slot. It always returns 1. If an
12855 * error occurs the action is cancelled, but the rule processing continues.
12856 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012857enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012858 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012859{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012860 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012861 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012862 char **cap = s->res_cap;
12863 struct proxy *fe = strm_fe(s);
12864 int len;
12865 int i;
12866
12867 /* Look for the original configuration. */
12868 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012869 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012870 i--, h = h->next);
12871 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012872 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012873
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012874 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 +020012875 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012876 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012877
12878 if (cap[h->index] == NULL)
12879 cap[h->index] = pool_alloc2(h->pool);
12880
12881 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012882 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012883
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012884 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012885 if (len > h->len)
12886 len = h->len;
12887
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012888 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012889 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012890 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012891}
12892
12893/* parse an "http-response capture" action. It takes a single argument which is
12894 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12895 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012896 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012897 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012898enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12899 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012900{
12901 struct sample_expr *expr;
12902 int cur_arg;
12903 int id;
12904 char *error;
12905
12906 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12907 if (strcmp(args[cur_arg], "if") == 0 ||
12908 strcmp(args[cur_arg], "unless") == 0)
12909 break;
12910
12911 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012912 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012913 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012914 }
12915
12916 cur_arg = *orig_arg;
12917 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12918 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012919 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012920
12921 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12922 memprintf(err,
12923 "fetch method '%s' extracts information from '%s', none of which is available here",
12924 args[cur_arg-1], sample_src_names(expr->fetch->use));
12925 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012926 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012927 }
12928
12929 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012930 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012931 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012932 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012933 }
12934
12935 if (strcmp(args[cur_arg], "id") != 0) {
12936 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12937 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012938 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012939 }
12940
12941 cur_arg++;
12942
12943 if (!args[cur_arg]) {
12944 memprintf(err, "missing id value");
12945 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012946 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012947 }
12948
12949 id = strtol(args[cur_arg], &error, 10);
12950 if (*error != '\0') {
12951 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12952 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012953 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012954 }
12955 cur_arg++;
12956
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012957 proxy->conf.args.ctx = ARGC_CAP;
12958
Thierry FOURNIER42148732015-09-02 17:17:33 +020012959 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012960 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012961 rule->arg.capid.expr = expr;
12962 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012963
12964 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012965 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012966}
12967
William Lallemand73025dd2014-04-24 14:38:37 +020012968/*
12969 * Return the struct http_req_action_kw associated to a keyword.
12970 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012971struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012972{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012973 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012974}
12975
12976/*
12977 * Return the struct http_res_action_kw associated to a keyword.
12978 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012979struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012980{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012981 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012982}
12983
Willy Tarreau12207b32016-11-22 19:48:51 +010012984
12985/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12986 * now.
12987 */
12988static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12989{
12990 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12991 return 1;
12992
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012993 if (*args[2]) {
12994 struct proxy *px;
12995
12996 px = proxy_find_by_name(args[2], 0, 0);
12997 if (px)
12998 appctx->ctx.errors.iid = px->uuid;
12999 else
13000 appctx->ctx.errors.iid = atoi(args[2]);
13001
13002 if (!appctx->ctx.errors.iid) {
13003 appctx->ctx.cli.msg = "No such proxy.\n";
13004 appctx->st0 = CLI_ST_PRINT;
13005 return 1;
13006 }
13007 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013008 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010013009 appctx->ctx.errors.iid = -1; // dump all proxies
13010
Willy Tarreau35069f82016-11-25 09:16:37 +010013011 appctx->ctx.errors.flag = 0;
13012 if (strcmp(args[3], "request") == 0)
13013 appctx->ctx.errors.flag |= 4; // ignore response
13014 else if (strcmp(args[3], "response") == 0)
13015 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013016 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013017 return 0;
13018}
13019
13020/* This function dumps all captured errors onto the stream interface's
13021 * read buffer. It returns 0 if the output buffer is full and it needs
13022 * to be called again, otherwise non-zero.
13023 */
13024static int cli_io_handler_show_errors(struct appctx *appctx)
13025{
13026 struct stream_interface *si = appctx->owner;
13027 extern const char *monthname[12];
13028
13029 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13030 return 1;
13031
13032 chunk_reset(&trash);
13033
13034 if (!appctx->ctx.errors.px) {
13035 /* the function had not been called yet, let's prepare the
13036 * buffer for a response.
13037 */
13038 struct tm tm;
13039
13040 get_localtime(date.tv_sec, &tm);
13041 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13042 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13043 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13044 error_snapshot_id);
13045
13046 if (bi_putchk(si_ic(si), &trash) == -1) {
13047 /* Socket buffer full. Let's try again later from the same point */
13048 si_applet_cant_put(si);
13049 return 0;
13050 }
13051
13052 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013053 appctx->ctx.errors.bol = 0;
13054 appctx->ctx.errors.ptr = -1;
13055 }
13056
13057 /* we have two inner loops here, one for the proxy, the other one for
13058 * the buffer.
13059 */
13060 while (appctx->ctx.errors.px) {
13061 struct error_snapshot *es;
13062
Willy Tarreau35069f82016-11-25 09:16:37 +010013063 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013064 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013065 if (appctx->ctx.errors.flag & 2) // skip req
13066 goto next;
13067 }
13068 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013069 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013070 if (appctx->ctx.errors.flag & 4) // skip resp
13071 goto next;
13072 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013073
13074 if (!es->when.tv_sec)
13075 goto next;
13076
13077 if (appctx->ctx.errors.iid >= 0 &&
13078 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13079 es->oe->uuid != appctx->ctx.errors.iid)
13080 goto next;
13081
13082 if (appctx->ctx.errors.ptr < 0) {
13083 /* just print headers now */
13084
13085 char pn[INET6_ADDRSTRLEN];
13086 struct tm tm;
13087 int port;
13088
13089 get_localtime(es->when.tv_sec, &tm);
13090 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13091 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13092 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13093
13094 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13095 case AF_INET:
13096 case AF_INET6:
13097 port = get_host_port(&es->src);
13098 break;
13099 default:
13100 port = 0;
13101 }
13102
Willy Tarreau35069f82016-11-25 09:16:37 +010013103 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013104 case 0:
13105 chunk_appendf(&trash,
13106 " frontend %s (#%d): invalid request\n"
13107 " backend %s (#%d)",
13108 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13109 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13110 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13111 break;
13112 case 1:
13113 chunk_appendf(&trash,
13114 " backend %s (#%d): invalid response\n"
13115 " frontend %s (#%d)",
13116 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13117 es->oe->id, es->oe->uuid);
13118 break;
13119 }
13120
13121 chunk_appendf(&trash,
13122 ", server %s (#%d), event #%u\n"
13123 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013124 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013125 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13126 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13127 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13128 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13129 es->ev_id,
13130 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013131 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013132 es->m_clen, es->m_blen,
13133 es->b_flags, es->b_out, es->b_tot,
13134 es->len, es->b_wrap, es->pos);
13135
13136 if (bi_putchk(si_ic(si), &trash) == -1) {
13137 /* Socket buffer full. Let's try again later from the same point */
13138 si_applet_cant_put(si);
13139 return 0;
13140 }
13141 appctx->ctx.errors.ptr = 0;
13142 appctx->ctx.errors.sid = es->sid;
13143 }
13144
13145 if (appctx->ctx.errors.sid != es->sid) {
13146 /* the snapshot changed while we were dumping it */
13147 chunk_appendf(&trash,
13148 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13149 if (bi_putchk(si_ic(si), &trash) == -1) {
13150 si_applet_cant_put(si);
13151 return 0;
13152 }
13153 goto next;
13154 }
13155
13156 /* OK, ptr >= 0, so we have to dump the current line */
13157 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13158 int newptr;
13159 int newline;
13160
13161 newline = appctx->ctx.errors.bol;
13162 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13163 if (newptr == appctx->ctx.errors.ptr)
13164 return 0;
13165
13166 if (bi_putchk(si_ic(si), &trash) == -1) {
13167 /* Socket buffer full. Let's try again later from the same point */
13168 si_applet_cant_put(si);
13169 return 0;
13170 }
13171 appctx->ctx.errors.ptr = newptr;
13172 appctx->ctx.errors.bol = newline;
13173 };
13174 next:
13175 appctx->ctx.errors.bol = 0;
13176 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013177 appctx->ctx.errors.flag ^= 1;
13178 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013179 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013180 }
13181
13182 /* dump complete */
13183 return 1;
13184}
13185
13186/* register cli keywords */
13187static struct cli_kw_list cli_kws = {{ },{
13188 { { "show", "errors", NULL },
13189 "show errors : report last request and response errors for each proxy",
13190 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13191 },
13192 {{},}
13193}};
13194
Willy Tarreau4a568972010-05-12 08:08:50 +020013195/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013196/* All supported ACL keywords must be declared here. */
13197/************************************************************************/
13198
13199/* Note: must not be declared <const> as its list will be overwritten.
13200 * Please take care of keeping this list alphabetically sorted.
13201 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013202static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013203 { "base", "base", PAT_MATCH_STR },
13204 { "base_beg", "base", PAT_MATCH_BEG },
13205 { "base_dir", "base", PAT_MATCH_DIR },
13206 { "base_dom", "base", PAT_MATCH_DOM },
13207 { "base_end", "base", PAT_MATCH_END },
13208 { "base_len", "base", PAT_MATCH_LEN },
13209 { "base_reg", "base", PAT_MATCH_REG },
13210 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013211
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013212 { "cook", "req.cook", PAT_MATCH_STR },
13213 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13214 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13215 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13216 { "cook_end", "req.cook", PAT_MATCH_END },
13217 { "cook_len", "req.cook", PAT_MATCH_LEN },
13218 { "cook_reg", "req.cook", PAT_MATCH_REG },
13219 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013220
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013221 { "hdr", "req.hdr", PAT_MATCH_STR },
13222 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13223 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13224 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13225 { "hdr_end", "req.hdr", PAT_MATCH_END },
13226 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13227 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13228 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013229
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013230 /* these two declarations uses strings with list storage (in place
13231 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13232 * and delete functions are relative to the list management. The parse
13233 * and match method are related to the corresponding fetch methods. This
13234 * is very particular ACL declaration mode.
13235 */
13236 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13237 { "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 +020013238
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013239 { "path", "path", PAT_MATCH_STR },
13240 { "path_beg", "path", PAT_MATCH_BEG },
13241 { "path_dir", "path", PAT_MATCH_DIR },
13242 { "path_dom", "path", PAT_MATCH_DOM },
13243 { "path_end", "path", PAT_MATCH_END },
13244 { "path_len", "path", PAT_MATCH_LEN },
13245 { "path_reg", "path", PAT_MATCH_REG },
13246 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013247
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013248 { "req_ver", "req.ver", PAT_MATCH_STR },
13249 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013250
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013251 { "scook", "res.cook", PAT_MATCH_STR },
13252 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13253 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13254 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13255 { "scook_end", "res.cook", PAT_MATCH_END },
13256 { "scook_len", "res.cook", PAT_MATCH_LEN },
13257 { "scook_reg", "res.cook", PAT_MATCH_REG },
13258 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013259
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013260 { "shdr", "res.hdr", PAT_MATCH_STR },
13261 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13262 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13263 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13264 { "shdr_end", "res.hdr", PAT_MATCH_END },
13265 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13266 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13267 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013268
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013269 { "url", "url", PAT_MATCH_STR },
13270 { "url_beg", "url", PAT_MATCH_BEG },
13271 { "url_dir", "url", PAT_MATCH_DIR },
13272 { "url_dom", "url", PAT_MATCH_DOM },
13273 { "url_end", "url", PAT_MATCH_END },
13274 { "url_len", "url", PAT_MATCH_LEN },
13275 { "url_reg", "url", PAT_MATCH_REG },
13276 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013277
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013278 { "urlp", "urlp", PAT_MATCH_STR },
13279 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13280 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13281 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13282 { "urlp_end", "urlp", PAT_MATCH_END },
13283 { "urlp_len", "urlp", PAT_MATCH_LEN },
13284 { "urlp_reg", "urlp", PAT_MATCH_REG },
13285 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013286
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013287 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013288}};
13289
13290/************************************************************************/
13291/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013292/************************************************************************/
13293/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013294static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013295 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013296 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013297 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13298
Willy Tarreau87b09662015-04-03 00:22:06 +020013299 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013300 { "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 +020013301
13302 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013303 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13304 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13305 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013306
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013307 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13308 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013309
Willy Tarreau409bcde2013-01-08 00:31:00 +010013310 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13311 * are only here to match the ACL's name, are request-only and are used
13312 * for ACL compatibility only.
13313 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013314 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13315 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013316 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13317 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013318
13319 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13320 * only here to match the ACL's name, are request-only and are used for
13321 * ACL compatibility only.
13322 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013323 { "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 +020013324 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013325 { "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 +020013326 { "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 +010013327
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013328 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013329 { "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 +010013330 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013331 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013332 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013333 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013334
13335 /* HTTP protocol on the request path */
13336 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013337 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013338
13339 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013340 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13341 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013342
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013343 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013344 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13345 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013346 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013347
Willy Tarreau18ed2562013-01-14 15:56:36 +010013348 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013349 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13350 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013351
Willy Tarreau18ed2562013-01-14 15:56:36 +010013352 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013353 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013354 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13355 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013356
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013357 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013358 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013359 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013360 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013361 { "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 +010013362 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013363 { "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 +010013364
13365 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013366 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013367 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13368 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013369
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013370 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013371 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013372 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013373 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013374 { "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 +010013375 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013376 { "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 +010013377
Willy Tarreau409bcde2013-01-08 00:31:00 +010013378 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013379 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013380 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13381 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013382 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013383
13384 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013385 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013386 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013387 { "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 +020013388 { "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 +010013389
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013390 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013391 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013392 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013393 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013394 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013395 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013396 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013397 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13398 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013399 { "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 +010013400 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013401}};
13402
Willy Tarreau8797c062007-05-07 00:55:35 +020013403
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013404/************************************************************************/
13405/* All supported converter keywords must be declared here. */
13406/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013407/* Note: must not be declared <const> as its list will be overwritten */
13408static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013409 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013410 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013411 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13412 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013413 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013414 { NULL, NULL, 0, 0, 0 },
13415}};
13416
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013417
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013418/************************************************************************/
13419/* All supported http-request action keywords must be declared here. */
13420/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013421struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013422 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013423 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013424 { "set-method", parse_set_req_line },
13425 { "set-path", parse_set_req_line },
13426 { "set-query", parse_set_req_line },
13427 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013428 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013429 }
13430};
13431
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013432struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013433 .kw = {
13434 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013435 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013436 { NULL, NULL }
13437 }
13438};
13439
Willy Tarreau8797c062007-05-07 00:55:35 +020013440__attribute__((constructor))
13441static void __http_protocol_init(void)
13442{
13443 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013444 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013445 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013446 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013447 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013448 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013449}
13450
13451
Willy Tarreau58f10d72006-12-04 02:26:12 +010013452/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013453 * Local variables:
13454 * c-indent-level: 8
13455 * c-basic-offset: 8
13456 * End:
13457 */